@@ -500,14 +500,18 @@ function createMainWindow() {
500500
501501 // Prevent window from closing, just hide it instead
502502 mainWindow . on ( "close" , ( event ) => {
503+ console . log ( "[MAIN] Window close event, isQuitting:" , app . isQuitting ) ;
503504 if ( ! app . isQuitting ) {
504505 event . preventDefault ( ) ;
505506 mainWindow . hide ( ) ;
506507 console . log ( "[MAIN] Window hidden instead of closed" ) ;
508+ } else {
509+ console . log ( "[MAIN] Allowing window to close because app is quitting" ) ;
507510 }
508511 } ) ;
509512
510513 mainWindow . on ( "closed" , ( ) => {
514+ console . log ( "[MAIN] Window closed event" ) ;
511515 mainWindow = null ;
512516 } ) ;
513517 } catch ( error ) {
@@ -765,17 +769,13 @@ function startKeyboardListener() {
765769function stopKeyboardListener ( ) {
766770 if ( keyboardListener ) {
767771 console . log ( "[KEYBOARD] Stopping keyboard listener..." ) ;
768- // First try SIGTERM
769- keyboardListener . kill ( "SIGTERM" ) ;
770-
771- // Give it a moment to terminate gracefully
772- setTimeout ( ( ) => {
773- if ( keyboardListener && ! keyboardListener . killed ) {
774- console . log ( "[KEYBOARD] Force killing keyboard listener..." ) ;
775- keyboardListener . kill ( "SIGKILL" ) ;
776- }
777- } , 100 ) ;
778-
772+ try {
773+ // Force kill immediately for clean quit
774+ keyboardListener . kill ( "SIGKILL" ) ;
775+ console . log ( "[KEYBOARD] Keyboard listener killed" ) ;
776+ } catch ( error ) {
777+ console . error ( "[KEYBOARD] Error killing listener:" , error ) ;
778+ }
779779 keyboardListener = null ;
780780 }
781781}
@@ -1520,16 +1520,21 @@ app.whenReady().then(async () => {
15201520} ) ;
15211521
15221522app . on ( "window-all-closed" , ( ) => {
1523+ console . log ( "[APP] window-all-closed event fired" ) ;
15231524 stopKeyboardListener ( ) ;
1524- if ( process . platform !== "darwin" ) {
1525+ // On macOS, only quit if explicitly quitting (not just closing window)
1526+ if ( process . platform !== "darwin" || app . isQuitting ) {
15251527 stopPermissionMonitoring ( ) ;
15261528 app . quit ( ) ;
15271529 }
15281530} ) ;
15291531
1530- app . on ( "before-quit" , ( ) => {
1531- console . log ( "[APP] before-quit event fired" ) ;
1532+ // Handle Cmd+Q and system-initiated quits properly
1533+ app . on ( "before-quit" , ( event ) => {
1534+ console . log ( "[APP] before-quit event fired, isQuitting:" , app . isQuitting ) ;
15321535 app . isQuitting = true ;
1536+
1537+ // Stop all background processes
15331538 stopPermissionMonitoring ( ) ;
15341539 stopKeyboardListener ( ) ;
15351540
@@ -1538,6 +1543,20 @@ app.on("before-quit", () => {
15381543 logStream . end ( ) ;
15391544 logStream = null ;
15401545 }
1546+
1547+ // Destroy windows to ensure clean quit
1548+ if ( pillWindow && ! pillWindow . isDestroyed ( ) ) {
1549+ pillWindow . destroy ( ) ;
1550+ }
1551+ if ( mainWindow && ! mainWindow . isDestroyed ( ) ) {
1552+ mainWindow . close ( ) ;
1553+ }
1554+ } ) ;
1555+
1556+ // Handle system-initiated quits (like from System Preferences "Quit and Reopen")
1557+ app . on ( "will-quit" , ( event ) => {
1558+ console . log ( "[APP] will-quit event fired, isQuitting:" , app . isQuitting ) ;
1559+ app . isQuitting = true ;
15411560} ) ;
15421561
15431562app . on ( "activate" , ( ) => {
0 commit comments