fix: dropdown preferences crashed in non-english (closes #217)

This commit is contained in:
Louis Pontoise authored and lwouis committed 2020-04-08 19:28:53 +09:00
1 parent 4eb9db049b
commit 5447d5fc21
5 files changed
+36 -28

No files matched your search

+22 -14
View File
@@ -21,12 +21,12 @@ class Preferences {
"showMinimizedWindows": true,
"showHiddenWindows": true,
"windowDisplayDelay": 0,
"theme": ThemePreference.macOs.rawValue,
"showOnScreen": ShowOnScreenPreference.active.rawValue,
"alignThumbnails": AlignThumbnailsPreference.left.rawValue,
"appsToShow": AppsToShowPreference.all.rawValue,
"spacesToShow": SpacesToShowPreference.all.rawValue,
"screensToShow": ScreensToShowPreference.all.rawValue,
"theme": "0",
"showOnScreen": "0",
"alignThumbnails": "0",
"appsToShow": "0",
"spacesToShow": "0",
"screensToShow": "0",
"hideSpaceNumberLabels": false,
"startAtLogin": true,
]
@@ -60,12 +60,12 @@ class Preferences {
static var startAtLogin: Bool { defaults.bool(forKey: "startAtLogin") }
// macro values
static var theme: ThemePreference { ThemePreference(rawValue: defaults.string(forKey: "theme")!)! }
static var showOnScreen: ShowOnScreenPreference { ShowOnScreenPreference(rawValue: defaults.string(forKey: "showOnScreen")!)! }
static var alignThumbnails: AlignThumbnailsPreference { AlignThumbnailsPreference(rawValue: defaults.string(forKey: "alignThumbnails")!)! }
static var appsToShow: AppsToShowPreference { AppsToShowPreference(rawValue: defaults.string(forKey: "appsToShow")!)! }
static var spacesToShow: SpacesToShowPreference { SpacesToShowPreference(rawValue: defaults.string(forKey: "spacesToShow")!)! }
static var screensToShow: ScreensToShowPreference { ScreensToShowPreference(rawValue: defaults.string(forKey: "screensToShow")!)! }
static var theme: ThemePreference { ThemePreference.allCases[Int(defaults.string(forKey: "theme")!)!] }
static var showOnScreen: ShowOnScreenPreference { ShowOnScreenPreference.allCases[Int(defaults.string(forKey: "showOnScreen")!)!] }
static var alignThumbnails: AlignThumbnailsPreference { AlignThumbnailsPreference.allCases[Int(defaults.string(forKey: "alignThumbnails")!)!] }
static var appsToShow: AppsToShowPreference { AppsToShowPreference.allCases[Int(defaults.string(forKey: "appsToShow")!)!] }
static var spacesToShow: SpacesToShowPreference { SpacesToShowPreference.allCases[Int(defaults.string(forKey: "spacesToShow")!)!] }
static var screensToShow: ScreensToShowPreference { ScreensToShowPreference.allCases[Int(defaults.string(forKey: "screensToShow")!)!] }
// derived values
static var cellBorderWidth: CGFloat { theme.themeParameters.cellBorderWidth }
@@ -108,6 +108,7 @@ class Preferences {
// MacroPreference are collection of values derived from a single key
// we don't want to store every value in UserDefaults as the user could change them and contradict the macro
protocol MacroPreference {
var localizedString: LocalizedString { get }
}
struct ThemeParameters {
@@ -185,10 +186,17 @@ enum ThemePreference: String, CaseIterable, MacroPreference {
case macOs = " macOS"
case windows10 = "❖ Windows 10"
var localizedString: LocalizedString {
switch self {
case .macOs: return NSLocalizedString(" macOS", comment: "")
case .windows10: return NSLocalizedString("❖ Windows 10", comment: "")
}
}
var themeParameters: ThemeParameters {
switch self {
case .macOs: return ThemeParameters(label: NSLocalizedString(" macOS", comment: ""), cellBorderWidth: 0, cellCornerRadius: 5, windowCornerRadius: 20, highlightBorderColor: .clear, highlightBackgroundColor: NSColor(red: 0, green: 0, blue: 0, alpha: 0.4))
case .windows10: return ThemeParameters(label: NSLocalizedString("❖ Windows 10", comment: ""), cellBorderWidth: 2, cellCornerRadius: 0, windowCornerRadius: 0, highlightBorderColor: .white, highlightBackgroundColor: .clear)
case .macOs: return ThemeParameters(label: self.localizedString, cellBorderWidth: 0, cellCornerRadius: 5, windowCornerRadius: 20, highlightBorderColor: .clear, highlightBackgroundColor: NSColor(red: 0, green: 0, blue: 0, alpha: 0.4))
case .windows10: return ThemeParameters(label: self.localizedString, cellBorderWidth: 2, cellCornerRadius: 0, windowCornerRadius: 0, highlightBorderColor: .white, highlightBackgroundColor: .clear)
}
}
}
@@ -22,14 +22,14 @@ class LabelAndControl: NSObject {
return views
}
static func makeLabelWithDropdown(_ labelText: String, _ rawName: String, _ values: [String], _ suffixText: String? = nil) -> [NSView] {
static func makeLabelWithDropdown(_ labelText: String, _ rawName: String, _ values: [MacroPreference], _ suffixText: String? = nil) -> [NSView] {
return makeLabelWithProvidedControl(labelText, rawName, makeDropDown(rawName, values), suffixText)
}
static func makeDropDown(_ rawName: String, _ values: [String]) -> NSPopUpButton {
static func makeDropDown(_ rawName: String, _ macroPreferences: [MacroPreference]) -> NSPopUpButton {
let popUp = NSPopUpButton()
popUp.addItems(withTitles: values)
popUp.selectItem(withTitle: Preferences.getString(rawName)!)
popUp.addItems(withTitles: macroPreferences.map { $0.localizedString })
popUp.selectItem(at: Int(Preferences.getString(rawName)!)!)
return popUp
}
@@ -99,7 +99,7 @@ class LabelAndControl: NSObject {
static func getControlValue(_ control: NSControl) -> String {
if control is NSPopUpButton {
return (control as! NSPopUpButton).titleOfSelectedItem!
return String((control as! NSPopUpButton).indexOfSelectedItem)
} else if control is NSSlider {
return String(format: "%.0f", control.doubleValue) // we are only interested in decimals of the provided double
} else if control is NSButton {
@@ -28,7 +28,7 @@ class PreferencesWindow: NSWindow {
makeKeyAndOrderFront(nil)
}
@objc static func controlWasChanged(_ senderControl: NSControl) {
static func controlWasChanged(_ senderControl: NSControl) {
let newValue = LabelAndControl.getControlValue(senderControl)
LabelAndControl.updateControlExtras(senderControl, newValue)
Preferences.set(senderControl.identifier!.rawValue, newValue)
@@ -9,15 +9,15 @@ class AppearanceTab {
private static func makeView() -> NSGridView {
let grid = GridView.make([
LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Theme", comment: ""), "theme", ThemePreference.allCases.map { $0.themeParameters.label }),
LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Align windows", comment: ""), "alignThumbnails", AlignThumbnailsPreference.allCases.map { $0.localizedString }),
LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Theme", comment: ""), "theme", ThemePreference.allCases),
LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Align windows", comment: ""), "alignThumbnails", AlignThumbnailsPreference.allCases),
LabelAndControl.makeLabelWithSlider(NSLocalizedString("Max size on screen", comment: ""), "maxScreenUsage", 10, 100, 10, true, "%"),
LabelAndControl.makeLabelWithSlider(NSLocalizedString("Min windows per row", comment: ""), "minCellsPerRow", 1, 20, 20, true),
LabelAndControl.makeLabelWithSlider(NSLocalizedString("Max windows per row", comment: ""), "maxCellsPerRow", 1, 40, 20, true),
LabelAndControl.makeLabelWithSlider(NSLocalizedString("Rows of windows", comment: ""), "rowsCount", 1, 20, 20, true),
LabelAndControl.makeLabelWithSlider(NSLocalizedString("Window app icon size", comment: ""), "iconSize", 0, 64, 11, false, "px"),
LabelAndControl.makeLabelWithSlider(NSLocalizedString("Window title font size", comment: ""), "fontHeight", 0, 64, 11, false, "px"),
LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Show on", comment: ""), "showOnScreen", ShowOnScreenPreference.allCases.map { $0.localizedString }),
LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Show on", comment: ""), "showOnScreen", ShowOnScreenPreference.allCases),
LabelAndControl.makeLabelWithSlider(NSLocalizedString("Apparition delay", comment: ""), "windowDisplayDelay", 0, 2000, 11, false, "ms"),
LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Hide space number labels", comment: ""), "hideSpaceNumberLabels"),
])
@@ -22,9 +22,9 @@ class GeneralTab {
let holdAndPress = StackView(holdShortcut)
let checkboxesExplanations = LabelAndControl.makeLabel(NSLocalizedString("Select windows using:", comment: ""))
let checkboxes = StackView([StackView(enableArrows), StackView(enableMouse)], .vertical)
let appsToShow = dropdown("appsToShow", AppsToShowPreference.allCases.map { $0.localizedString })
let spacesToShow = dropdown("spacesToShow", SpacesToShowPreference.allCases.map { $0.localizedString })
let screensToShow = dropdown("screensToShow", ScreensToShowPreference.allCases.map { $0.localizedString })
let appsToShow = dropdown("appsToShow", AppsToShowPreference.allCases)
let spacesToShow = dropdown("spacesToShow", SpacesToShowPreference.allCases)
let screensToShow = dropdown("screensToShow", ScreensToShowPreference.allCases)
let showMinimizedWindows = StackView(LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Minimized", comment: ""), "showMinimizedWindows", labelPosition: .right))
let showHiddenWindows = StackView(LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Hidden", comment: ""), "showHiddenWindows", labelPosition: .right))
let shortcuts = StackView([nextWindowShortcut, previousWindowShortcut, cancelShortcut].map { StackView($0) }, .vertical)
@@ -48,8 +48,8 @@ class GeneralTab {
return grid
}
private static func dropdown(_ rawName: String, _ values: [String]) -> NSControl {
let dropdown = LabelAndControl.makeDropDown(rawName, values)
private static func dropdown(_ rawName: String, _ macroPreferences: [MacroPreference]) -> NSControl {
let dropdown = LabelAndControl.makeDropDown(rawName, macroPreferences)
return LabelAndControl.setupControl(dropdown, rawName)
}