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 {
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()
}
}
@@ -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)
}
}