From d46d90395c5e0900ca25385d228ffd228698b178 Mon Sep 17 00:00:00 2001 From: allsochen Date: Sat, 12 Oct 2024 15:23:36 +0800 Subject: [PATCH] fix: avoid crashing in some cases --- src/main.swift | 9 +++++++++ src/ui/App.swift | 12 ++++++------ src/ui/preferences-window/LabelAndControl.swift | 10 ++++++---- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main.swift b/src/main.swift index 681e3dc3..1cb89a14 100644 --- a/src/main.swift +++ b/src/main.swift @@ -1,6 +1,13 @@ import AppKit import Darwin +func printStackTrace() { + let stackSymbols = Thread.callStackSymbols + for symbol in stackSymbols { + print(symbol) + } +} + // - 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 @@ -8,6 +15,7 @@ import Darwin signal($0) { s in setNativeCommandTabEnabled(true) debugPrint("Exiting after receiving signal", s) + printStackTrace() exit(0) } } @@ -17,6 +25,7 @@ import Darwin NSSetUncaughtExceptionHandler { (exception) in setNativeCommandTabEnabled(true) debugPrint("Exiting after receiving uncaught NSException", exception) + printStackTrace() exit(0) } diff --git a/src/ui/App.swift b/src/ui/App.swift index 8e6ccdae..88b02e12 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -183,18 +183,16 @@ class App: AppCenterApplication, NSApplicationDelegate { permissionsWindow.show() } - @objc func showFeedbackPanel() { - showSecondaryWindow(feedbackWindow) - } - @objc func supportProject() { NSWorkspace.shared.open(URL(string: App.website + "/support")!) } + @objc func showFeedbackPanel() { + showSecondaryWindow(feedbackWindow) + } + @objc func showPreferencesWindow() { showSecondaryWindow(preferencesWindow) - // Use the center function to continue to center, the `repositionPanel` function cannot center, it may be a system bug - preferencesWindow.center() } func showSecondaryWindow(_ window: NSWindow?) { @@ -202,6 +200,8 @@ class App: AppCenterApplication, NSApplicationDelegate { NSScreen.preferred().repositionPanel(window) App.shared.activate(ignoringOtherApps: true) window.makeKeyAndOrderFront(nil) + // Use the center function to continue to center, the `repositionPanel` function cannot center, it may be a system bug + window.center() } } diff --git a/src/ui/preferences-window/LabelAndControl.swift b/src/ui/preferences-window/LabelAndControl.swift index 4b5eb143..c2a2934c 100644 --- a/src/ui/preferences-window/LabelAndControl.swift +++ b/src/ui/preferences-window/LabelAndControl.swift @@ -260,12 +260,14 @@ class LabelAndControl: NSObject { button.setWidth(segmentWidth, forSegment: i) } if #available(macOS 11.0, *) { - if let preference = preference as? SfSymbolMacroPreference { - button.setImage(NSImage(systemSymbolName: preference.symbolName, accessibilityDescription: nil)!, forSegment: i) + if let preference = preference as? SfSymbolMacroPreference, + let symbolImage = NSImage(systemSymbolName: preference.symbolName, accessibilityDescription: nil) { + button.setImage(symbolImage, forSegment: i) } } - if defaults.int(rawName) == i { - button.selectedSegment = i + let selectedSegment = defaults.int(rawName) + if selectedSegment >= 0 && selectedSegment < macroPreferences.count { + button.selectedSegment = selectedSegment } _ = setupControl(button, rawName, String(i), extraAction: extraAction) }