Skip to content

Commit

Permalink
Add QuitFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Aug 6, 2024
1 parent acac302 commit 9bec28e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/apps/Menu/src/MenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 2 additions & 1 deletion src/apps/SettingsWindow/src/View/ActionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
25 changes: 17 additions & 8 deletions src/apps/share/swift/KarabinerAppHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand All @@ -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()
}
}
}
}
6 changes: 5 additions & 1 deletion src/share/services_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 9bec28e

Please sign in to comment.