fix: occasional crash when updating some preferences

This commit is contained in:
Louis Pontoise authored and lwouis committed 2020-07-23 00:48:52 +09:00
1 parent c5ab19c677
commit 3d36cb7d6f
2 files changed
+15 -13

No files matched your search

@@ -10,8 +10,8 @@ enum LabelPosition {
class LabelAndControl: NSObject { class LabelAndControl: NSObject {
static func makeLabelWithRecorder(_ labelText: String, _ rawName: String, _ shortcutString: String, _ clearable: Bool = true, labelPosition: LabelPosition = .leftWithSeparator) -> [NSView] { static func makeLabelWithRecorder(_ labelText: String, _ rawName: String, _ shortcutString: String, _ clearable: Bool = true, labelPosition: LabelPosition = .leftWithSeparator) -> [NSView] {
let input = CustomRecorderControl(shortcutString, clearable) let input = CustomRecorderControl(shortcutString, clearable)
let views = makeLabelWithProvidedControl(labelText, rawName, input, labelPosition: labelPosition, extraAction: { _ in GeneralTab.shortcutChangedCallback(input, rawName) }) let views = makeLabelWithProvidedControl(labelText, rawName, input, labelPosition: labelPosition, extraAction: { _ in GeneralTab.shortcutChangedCallback(input) })
GeneralTab.shortcutChangedCallback(input, rawName) GeneralTab.shortcutChangedCallback(input)
return views return views
} }
@@ -26,7 +26,7 @@ class LabelAndControl: NSObject {
static func makeTextArea(_ nCharactersWide: CGFloat, _ nLinesHigh: Int, _ placeholder: String, _ rawName: String, extraAction: ActionClosure? = nil) -> [NSView] { static func makeTextArea(_ nCharactersWide: CGFloat, _ nLinesHigh: Int, _ placeholder: String, _ rawName: String, extraAction: ActionClosure? = nil) -> [NSView] {
let textArea = TextArea(nCharactersWide, nLinesHigh, placeholder) let textArea = TextArea(nCharactersWide, nLinesHigh, placeholder)
textArea.callback = { textArea.callback = {
controlWasChanged(textArea, rawName, nil) controlWasChanged(textArea, nil)
extraAction?(textArea) extraAction?(textArea)
} }
textArea.identifier = NSUserInterfaceItemIdentifier(rawName) 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 { static func setupControl(_ control: NSControl, _ rawName: String, _ controlId: String? = nil, extraAction: ActionClosure? = nil) -> NSControl {
control.identifier = NSUserInterfaceItemIdentifier(rawName)
control.onAction = { control.onAction = {
controlWasChanged($0, rawName, controlId) controlWasChanged($0, controlId)
extraAction?($0) extraAction?($0)
} }
return control 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) { if let newValue = LabelAndControl.getControlValue(senderControl, controlId) {
LabelAndControl.updateControlExtras(senderControl, newValue) LabelAndControl.updateControlExtras(senderControl, newValue)
Preferences.set(rawName, newValue) Preferences.set(senderControl.identifier!.rawValue, newValue)
} }
// some preferences require re-creating some components // 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() (App.shared as! App).resetPreferencesDependentComponents()
} }
} }
@@ -130,24 +130,25 @@ class GeneralTab: NSViewController, PreferencePane {
App.shortcutMonitor.addAction(shortcutActions[controlId]!, forKeyEvent: type) App.shortcutMonitor.addAction(shortcutActions[controlId]!, forKeyEvent: type)
} }
@objc static func shortcutChangedCallback(_ sender: NSControl, _ rawName: String) { @objc static func shortcutChangedCallback(_ sender: NSControl) {
if rawName == "holdShortcut" { let controlId = sender.identifier!.rawValue
addShortcut(.up, Shortcut(keyEquivalent: Preferences.holdShortcut)!, rawName) if controlId == "holdShortcut" {
addShortcut(.up, Shortcut(keyEquivalent: Preferences.holdShortcut)!, controlId)
shortcutsDependentOnHoldShortcut.forEach { shortcutsDependentOnHoldShortcut.forEach {
if $0.identifier!.rawValue == "arrowKeysEnabled" { if $0.identifier!.rawValue == "arrowKeysEnabled" {
GeneralTab.arrowKeysEnabledCallback($0) GeneralTab.arrowKeysEnabledCallback($0)
} else { } else {
GeneralTab.shortcutChangedCallback($0, rawName) GeneralTab.shortcutChangedCallback($0)
} }
} }
} else { } else {
// remove the holdShortcut character in case they also use it in the other shortcuts // 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: "") }) let newValue = Preferences.holdShortcut.reduce((sender as! RecorderControl).stringValue, { $0.replacingOccurrences(of: String($1), with: "") })
if newValue.isEmpty { if newValue.isEmpty {
removeShortcutIfExists(rawName, .down) removeShortcutIfExists(controlId, .down)
return return
} }
addShortcut(.down, Shortcut(keyEquivalent: Preferences.holdShortcut + newValue)!, rawName) addShortcut(.down, Shortcut(keyEquivalent: Preferences.holdShortcut + newValue)!, controlId)
} }
} }