From 2c47b4e585b0a938ba508c3ad7d5106d44ebf401 Mon Sep 17 00:00:00 2001 From: lwouis Date: Sat, 1 Jun 2024 14:00:57 +0200 Subject: [PATCH] fix: restore default cmd+tab shortcut when alt-tab crashes --- src/main.swift | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main.swift b/src/main.swift index a6e2c736..681e3dc3 100644 --- a/src/main.swift +++ b/src/main.swift @@ -1,11 +1,22 @@ import AppKit import Darwin -// if the app is quit/force-quit from Activity Monitor, it will receive SIGTERM and applicationWillTerminate won't be called -// we intercept SIGTERM so we can reset the native command-tab shortcut -signal(SIGTERM) { _ in - // symbolic hotkeys state persist after the app is quit; we restore this shortcut before quitting +// - if the app is quit/force-quit from Activity Monitor, it will receive SIGTERM and applicationWillTerminate won't be called +// - if the app crashes in swift code (e.g. unexpected nil object), SIGTRAP is sent +// we intercept these signals, and reset the native command-tab shortcut +[SIGTERM, SIGTRAP].forEach { + signal($0) { s in + setNativeCommandTabEnabled(true) + debugPrint("Exiting after receiving signal", s) + exit(0) + } +} + +// - if the app crashes in objective-c code, an NSException may be sent +// we intercept the exception, and reset the native command-tab shortcut +NSSetUncaughtExceptionHandler { (exception) in setNativeCommandTabEnabled(true) + debugPrint("Exiting after receiving uncaught NSException", exception) exit(0) }