node-mac-recorder
Version:
Native macOS screen recording package for Node.js applications
159 lines (133 loc) ⢠5.92 kB
JavaScript
const WindowSelector = require('./window-selector');
async function testBringToFront() {
console.log('š Bring To Front Test');
console.log('======================\n');
const selector = new WindowSelector();
try {
// Test 1: Manual bring to front
console.log('š Test 1: Manual window bring-to-front');
console.log('Move cursor over a window and press SPACE to bring it to front\n');
let currentWindow = null;
selector.on('windowEntered', (window) => {
currentWindow = window;
console.log(`\nš Window: ${window.appName} - "${window.title}" (ID: ${window.id})`);
console.log(' š Position:', `(${window.x}, ${window.y})`);
console.log(' š Size:', `${window.width} Ć ${window.height}`);
console.log(' š” Press SPACE to bring this window to front');
console.log(' š” Press A to enable AUTO bring-to-front');
console.log(' š” Press D to disable AUTO bring-to-front');
});
selector.on('windowLeft', (window) => {
console.log(`šŖ Left: ${window.appName} - "${window.title}"`);
currentWindow = null;
});
// Keyboard controls
const readline = require('readline');
readline.emitKeypressEvents(process.stdin);
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}
process.stdin.on('keypress', async (str, key) => {
if (key.name === 'space' && currentWindow) {
console.log(`\nš Bringing window to front: ${currentWindow.appName} - "${currentWindow.title}"`);
try {
const success = await selector.bringWindowToFront(currentWindow.id);
if (success) {
console.log(' ā
Window brought to front successfully!');
} else {
console.log(' ā Failed to bring window to front');
}
} catch (error) {
console.log(' ā Error:', error.message);
}
} else if (key.name === 'a') {
console.log('\nš Enabling AUTO bring-to-front mode...');
selector.setBringToFrontEnabled(true);
console.log(' ā
Auto mode ON - Windows will come to front automatically');
} else if (key.name === 'd') {
console.log('\nš Disabling AUTO bring-to-front mode...');
selector.setBringToFrontEnabled(false);
console.log(' ā
Auto mode OFF - Manual control only');
} else if (key.ctrl && key.name === 'c') {
process.exit(0);
}
});
console.log('š Starting window selection...\n');
console.log('š Controls:');
console.log(' SPACE - Bring current window to front');
console.log(' A - Enable AUTO bring-to-front');
console.log(' D - Disable AUTO bring-to-front');
console.log(' Ctrl+C - Exit\n');
await selector.startSelection();
// Keep running
process.on('SIGINT', async () => {
console.log('\nš Stopping...');
await selector.cleanup();
process.exit(0);
});
// Prevent exit
setInterval(() => {}, 1000);
} catch (error) {
console.error('ā Error:', error.message);
console.error(error.stack);
} finally {
// Cleanup
await selector.cleanup();
}
}
async function testAutoBringToFront() {
console.log('š¤ Auto Bring To Front Test');
console.log('============================\n');
const selector = new WindowSelector();
try {
// Enable auto bring-to-front
console.log('š Enabling auto bring-to-front...');
selector.setBringToFrontEnabled(true);
let windowCount = 0;
selector.on('windowEntered', (window) => {
windowCount++;
console.log(`\n[${windowCount}] š AUTO FRONT: ${window.appName} - "${window.title}"`);
console.log(` š Position: (${window.x}, ${window.y})`);
console.log(` š Size: ${window.width} Ć ${window.height}`);
console.log(' š Window should automatically come to front!');
});
console.log('ā
Auto bring-to-front enabled');
console.log('š±ļø Move cursor over different windows');
console.log('š Each window should automatically come to front');
console.log('ā±ļø Test will run for 30 seconds\n');
await selector.startSelection();
// Auto-stop after 30 seconds
setTimeout(async () => {
console.log('\nā° Test completed!');
console.log(`š Total windows auto-focused: ${windowCount}`);
selector.setBringToFrontEnabled(false);
await selector.cleanup();
process.exit(0);
}, 30000);
} catch (error) {
console.error('ā Error:', error.message);
await selector.cleanup();
}
}
// Main function
async function main() {
const args = process.argv.slice(2);
if (args.includes('--auto')) {
await testAutoBringToFront();
} else if (args.includes('--help')) {
console.log('Bring To Front Tests:');
console.log('====================');
console.log('node bring-to-front-test.js [option]');
console.log('');
console.log('Options:');
console.log(' --manual Manual bring-to-front test (default)');
console.log(' --auto Auto bring-to-front test');
console.log(' --help Show this help');
} else {
await testBringToFront();
}
}
if (require.main === module) {
main().catch(console.error);
}