From 3d36cb7d6f979c29b73ea1746e7a8bbdd0646d23 Mon Sep 17 00:00:00 2001 From: Louis Pontoise Date: Thu, 23 Jul 2020 00:35:42 +0900 Subject: [PATCH] fix: occasional crash when updating some preferences --- src/ui/preferences-window/LabelAndControl.swift | 15 ++++++++------- src/ui/preferences-window/tabs/GeneralTab.swift | 13 +++++++------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/ui/preferences-window/LabelAndControl.swift b/src/ui/preferences-window/LabelAndControl.swift index aa26d5c8..f9944895 100644 --- a/src/ui/preferences-window/LabelAndControl.swift +++ b/src/ui/preferences-window/LabelAndControl.swift @@ -10,8 +10,8 @@ enum LabelPosition { class LabelAndControl: NSObject { static func makeLabelWithRecorder(_ labelText: String, _ rawName: String, _ shortcutString: String, _ clearable: Bool = true, labelPosition: LabelPosition = .leftWithSeparator) -> [NSView] { let input = CustomRecorderControl(shortcutString, clearable) - let views = makeLabelWithProvidedControl(labelText, rawName, input, labelPosition: labelPosition, extraAction: { _ in GeneralTab.shortcutChangedCallback(input, rawName) }) - GeneralTab.shortcutChangedCallback(input, rawName) + let views = makeLabelWithProvidedControl(labelText, rawName, input, labelPosition: labelPosition, extraAction: { _ in GeneralTab.shortcutChangedCallback(input) }) + GeneralTab.shortcutChangedCallback(input) return views } @@ -26,7 +26,7 @@ class LabelAndControl: NSObject { static func makeTextArea(_ nCharactersWide: CGFloat, _ nLinesHigh: Int, _ placeholder: String, _ rawName: String, extraAction: ActionClosure? = nil) -> [NSView] { let textArea = TextArea(nCharactersWide, nLinesHigh, placeholder) textArea.callback = { - controlWasChanged(textArea, rawName, nil) + controlWasChanged(textArea, nil) extraAction?(textArea) } textArea.identifier = NSUserInterfaceItemIdentifier(rawName) @@ -92,20 +92,21 @@ class LabelAndControl: NSObject { } static func setupControl(_ control: NSControl, _ rawName: String, _ controlId: String? = nil, extraAction: ActionClosure? = nil) -> NSControl { + control.identifier = NSUserInterfaceItemIdentifier(rawName) control.onAction = { - controlWasChanged($0, rawName, controlId) + controlWasChanged($0, controlId) extraAction?($0) } return control } - static func controlWasChanged(_ senderControl: NSControl, _ rawName: String, _ controlId: String?) { + static func controlWasChanged(_ senderControl: NSControl, _ controlId: String?) { if let newValue = LabelAndControl.getControlValue(senderControl, controlId) { LabelAndControl.updateControlExtras(senderControl, newValue) - Preferences.set(rawName, newValue) + Preferences.set(senderControl.identifier!.rawValue, newValue) } // some preferences require re-creating some components - if ["iconSize", "fontHeight", "theme", "titleTruncation"].contains(where: { (pref: String) -> Bool in pref == rawName }) { + if ["iconSize", "fontHeight", "theme", "titleTruncation"].contains(where: { (pref: String) -> Bool in pref == senderControl.identifier!.rawValue }) { (App.shared as! App).resetPreferencesDependentComponents() } } diff --git a/src/ui/preferences-window/tabs/GeneralTab.swift b/src/ui/preferences-window/tabs/GeneralTab.swift index 7dbb5a32..c3c17cfb 100644 --- a/src/ui/preferences-window/tabs/GeneralTab.swift +++ b/src/ui/preferences-window/tabs/GeneralTab.swift @@ -130,24 +130,25 @@ class GeneralTab: NSViewController, PreferencePane { App.shortcutMonitor.addAction(shortcutActions[controlId]!, forKeyEvent: type) } - @objc static func shortcutChangedCallback(_ sender: NSControl, _ rawName: String) { - if rawName == "holdShortcut" { - addShortcut(.up, Shortcut(keyEquivalent: Preferences.holdShortcut)!, rawName) + @objc static func shortcutChangedCallback(_ sender: NSControl) { + let controlId = sender.identifier!.rawValue + if controlId == "holdShortcut" { + addShortcut(.up, Shortcut(keyEquivalent: Preferences.holdShortcut)!, controlId) shortcutsDependentOnHoldShortcut.forEach { if $0.identifier!.rawValue == "arrowKeysEnabled" { GeneralTab.arrowKeysEnabledCallback($0) } else { - GeneralTab.shortcutChangedCallback($0, rawName) + GeneralTab.shortcutChangedCallback($0) } } } else { // remove the holdShortcut character in case they also use it in the other shortcuts let newValue = Preferences.holdShortcut.reduce((sender as! RecorderControl).stringValue, { $0.replacingOccurrences(of: String($1), with: "") }) if newValue.isEmpty { - removeShortcutIfExists(rawName, .down) + removeShortcutIfExists(controlId, .down) return } - addShortcut(.down, Shortcut(keyEquivalent: Preferences.holdShortcut + newValue)!, rawName) + addShortcut(.down, Shortcut(keyEquivalent: Preferences.holdShortcut + newValue)!, controlId) } }