diff --git a/src/apps/Menu/src/MenuController.swift b/src/apps/Menu/src/MenuController.swift index 19dd1e463..bfa04a9cb 100644 --- a/src/apps/Menu/src/MenuController.swift +++ b/src/apps/Menu/src/MenuController.swift @@ -236,6 +236,7 @@ public class MenuController: NSObject, NSMenuDelegate { @objc func quitKarabiner(_: Any) { KarabinerAppHelper.shared.quitKarabiner( - askForConfirmation: LibKrbn.Settings.shared.askForConfirmationBeforeQuitting) + askForConfirmation: LibKrbn.Settings.shared.askForConfirmationBeforeQuitting, + quitFrom: .menu) } } diff --git a/src/apps/SettingsWindow/src/View/ActionView.swift b/src/apps/SettingsWindow/src/View/ActionView.swift index 40608d773..20819d837 100644 --- a/src/apps/SettingsWindow/src/View/ActionView.swift +++ b/src/apps/SettingsWindow/src/View/ActionView.swift @@ -24,7 +24,8 @@ struct ActionView: View { Button( action: { KarabinerAppHelper.shared.quitKarabiner( - askForConfirmation: settings.askForConfirmationBeforeQuitting) + askForConfirmation: settings.askForConfirmationBeforeQuitting, + quitFrom: .settings) }, label: { Label("Quit Karabiner-Elements", systemImage: "xmark.circle.fill") diff --git a/src/apps/share/swift/KarabinerAppHelper.swift b/src/apps/share/swift/KarabinerAppHelper.swift index eb4816971..526131cf8 100644 --- a/src/apps/share/swift/KarabinerAppHelper.swift +++ b/src/apps/share/swift/KarabinerAppHelper.swift @@ -20,7 +20,12 @@ final class KarabinerAppHelper { } } - func quitKarabiner(askForConfirmation: Bool) { + enum QuitFrom { + case menu + case settings + } + + func quitKarabiner(askForConfirmation: Bool, quitFrom: QuitFrom) { if askForConfirmation { let alert = NSAlert() alert.messageText = "Are you sure you want to quit Karabiner-Elements?" @@ -32,15 +37,19 @@ final class KarabinerAppHelper { let result = alert.runModal() if result == .OK { - quitKarabiner(askForConfirmation: false) + quitKarabiner( + askForConfirmation: false, + quitFrom: quitFrom) } } else { - ProcessInfo.processInfo.disableSuddenTermination() - - libkrbn_services_unregister_all_agents() - libkrbn_killall_settings() - - ProcessInfo.processInfo.enableSuddenTermination() + switch quitFrom { + case .menu: + libkrbn_killall_settings() + libkrbn_services_unregister_all_agents() + case .settings: + libkrbn_services_unregister_all_agents() + libkrbn_killall_settings() + } } } } diff --git a/src/share/services_utility.hpp b/src/share/services_utility.hpp index 7284773ce..114d22134 100644 --- a/src/share/services_utility.hpp +++ b/src/share/services_utility.hpp @@ -122,9 +122,13 @@ inline void bootout_old_agents(void) { inline void unregister_all_agents(void) { unregister_core_agents(); - unregister_menu_agent(); unregister_multitouch_extension_agent(); unregister_notification_window_agent(); + + // `unregister_all_agents` might be called within Karabiner-Menu.app. + // In that case, `unregister_menu_agent` will terminate itself, + // so it needs to be called after unregistering other services. + unregister_menu_agent(); } inline bool core_daemons_enabled(void) {