From 9a806b81355f46809998d1645cb78020d6c9943e Mon Sep 17 00:00:00 2001 From: Dean Blackketter Date: Mon, 26 Apr 2021 08:13:30 -0700 Subject: [PATCH 1/2] Move color spec into UserDefaults --- ViMac-Swift/BorderView.swift | 4 ++-- ViMac-Swift/HintView.swift | 2 +- ViMac-Swift/UserDefaults/UserDefaultsProperties.swift | 3 +++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ViMac-Swift/BorderView.swift b/ViMac-Swift/BorderView.swift index e4fe9e3..7935467 100644 --- a/ViMac-Swift/BorderView.swift +++ b/ViMac-Swift/BorderView.swift @@ -9,8 +9,8 @@ import Cocoa class BorderView: NSView { - let SIZE = CGFloat(2) - let borderColor = NSColor.red + let SIZE = CGFloat(3) + let borderColor = UserDefaultsProperties.scrollFrameColor.read() override func draw(_ dirtyRect: NSRect) { let rect = cleanRect(dirtyRect: dirtyRect, size: SIZE) diff --git a/ViMac-Swift/HintView.swift b/ViMac-Swift/HintView.swift index 24b1264..2caa183 100644 --- a/ViMac-Swift/HintView.swift +++ b/ViMac-Swift/HintView.swift @@ -11,7 +11,7 @@ import AXSwift class HintView: NSView { static let borderColor = NSColor.darkGray - static let backgroundColor = NSColor(red: 255 / 255, green: 224 / 255, blue: 112 / 255, alpha: 1) + static let backgroundColor = UserDefaultsProperties.hintColor.read() static let untypedHintColor = NSColor.black static let typedHintColor = NSColor(red: 212 / 255, green: 172 / 255, blue: 58 / 255, alpha: 1) diff --git a/ViMac-Swift/UserDefaults/UserDefaultsProperties.swift b/ViMac-Swift/UserDefaults/UserDefaultsProperties.swift index 73c35cf..94f810d 100644 --- a/ViMac-Swift/UserDefaults/UserDefaultsProperties.swift +++ b/ViMac-Swift/UserDefaults/UserDefaultsProperties.swift @@ -14,6 +14,9 @@ struct UserDefaultsProperties { static let keySequenceScrollModeEnabled = UserDefaultsProperty.init("keySequenceScrollModeEnabled", defaultValue: false) static let keySequenceScrollMode = UserDefaultsProperty.init("keySequenceScrollMode", defaultValue: "") static let keySequenceResetDelay = UserDefaultsProperty.init("keySequenceResetDelay", defaultValue: "0.25") + + static let hintColor = UserDefaultsProperty.init("hintColor", defaultValue: NSColor(red: 255 / 255, green: 224 / 255, blue: 112 / 255, alpha: 1)) + static let scrollFrameColor = UserDefaultsProperty.init("hintColor", defaultValue: NSColor(red: 255 / 255, green: 224 / 255, blue: 112 / 255, alpha: 1)) static let AXEnhancedUserInterfaceEnabled = UserDefaultsProperty.init("AXEnhancedUserInterfaceEnabled", defaultValue: false) static let AXManualAccessibilityEnabled = UserDefaultsProperty.init("AXManualAccessibilityEnabled", defaultValue: false) From 41f603642832ddc977273ab25f61f04dcf7aea39 Mon Sep 17 00:00:00 2001 From: Dean Blackketter Date: Tue, 11 May 2021 17:12:56 -0700 Subject: [PATCH 2/2] Add color settings to preference panes --- ViMac-Swift/BorderView.swift | 3 +- ViMac-Swift/HintView.swift | 3 +- .../HintModePreferenceViewController.swift | 20 ++++++- .../ScrollModePreferenceViewController.swift | 19 +++++- .../UserDefaults/UserDefaultsProperties.swift | 2 +- ViMac-Swift/UserPreferences.swift | 58 +++++++++++++++++++ 6 files changed, 96 insertions(+), 9 deletions(-) diff --git a/ViMac-Swift/BorderView.swift b/ViMac-Swift/BorderView.swift index 7935467..4c9cbc9 100644 --- a/ViMac-Swift/BorderView.swift +++ b/ViMac-Swift/BorderView.swift @@ -10,13 +10,12 @@ import Cocoa class BorderView: NSView { let SIZE = CGFloat(3) - let borderColor = UserDefaultsProperties.scrollFrameColor.read() override func draw(_ dirtyRect: NSRect) { let rect = cleanRect(dirtyRect: dirtyRect, size: SIZE) let border = NSBezierPath.init(rect: rect) border.lineWidth = SIZE - self.borderColor.set() + UserPreferences.ScrollMode.ScrollFrameColorProperty.readColor().set() border.stroke() } diff --git a/ViMac-Swift/HintView.swift b/ViMac-Swift/HintView.swift index 2caa183..ec746ee 100644 --- a/ViMac-Swift/HintView.swift +++ b/ViMac-Swift/HintView.swift @@ -11,7 +11,6 @@ import AXSwift class HintView: NSView { static let borderColor = NSColor.darkGray - static let backgroundColor = UserDefaultsProperties.hintColor.read() static let untypedHintColor = NSColor.black static let typedHintColor = NSColor(red: 212 / 255, green: 172 / 255, blue: 58 / 255, alpha: 1) @@ -33,7 +32,7 @@ class HintView: NSView { self.layer?.borderWidth = borderWidth - self.layer?.backgroundColor = HintView.backgroundColor.cgColor + self.layer?.backgroundColor = UserPreferences.HintMode.HintColorProperty.readColor().cgColor self.layer?.borderColor = HintView.borderColor.cgColor self.layer?.cornerRadius = cornerRadius diff --git a/ViMac-Swift/Preferences/HintModePreferenceViewController.swift b/ViMac-Swift/Preferences/HintModePreferenceViewController.swift index c319f8d..5f87514 100644 --- a/ViMac-Swift/Preferences/HintModePreferenceViewController.swift +++ b/ViMac-Swift/Preferences/HintModePreferenceViewController.swift @@ -9,6 +9,7 @@ final class HintModePreferenceViewController: NSViewController, NSTextFieldDeleg private var grid: NSGridView! private var customCharactersField: NSTextField! private var textSizeField: NSTextField! + private var hintColorWell: NSColorWell! init() { if #available(OSX 11.0, *) { @@ -59,7 +60,17 @@ final class HintModePreferenceViewController: NSViewController, NSTextFieldDeleg textSizeField.stringValue = UserPreferences.HintMode.TextSizeProperty.readUnvalidated() ?? "" let textSizeRow: [NSView] = [textSizeLabel, textSizeField] grid.addRow(with: textSizeRow) - + + let hintColorLabel = NSTextField(labelWithString: "Hint Color:") + hintColorWell = NSColorWell() + hintColorWell.action = #selector(onHintColorWellEdit) + hintColorWell.color = UserPreferences.HintMode.HintColorProperty.readColor() + hintColorWell.widthAnchor.constraint(equalToConstant: 25).isActive = true + hintColorWell.heightAnchor.constraint(equalToConstant: 25).isActive = true + + let hintColorRow: [NSView] = [hintColorLabel, hintColorWell] + grid.addRow(with: hintColorRow) + self.view.addSubview(grid) NSLayoutConstraint.activate([ @@ -71,7 +82,12 @@ final class HintModePreferenceViewController: NSViewController, NSTextFieldDeleg grid.centerYAnchor.constraint(equalTo: view.centerYAnchor), ]) } - + + @objc func onHintColorWellEdit() { + let value = hintColorWell.color + UserPreferences.HintMode.HintColorProperty.saveColor(value: value) + } + func onCustomCharactersFieldChange() { let value = customCharactersField.stringValue UserPreferences.HintMode.CustomCharactersProperty.save(value: value) diff --git a/ViMac-Swift/Preferences/ScrollModePreferenceViewController.swift b/ViMac-Swift/Preferences/ScrollModePreferenceViewController.swift index 18bf33d..89b1859 100644 --- a/ViMac-Swift/Preferences/ScrollModePreferenceViewController.swift +++ b/ViMac-Swift/Preferences/ScrollModePreferenceViewController.swift @@ -11,7 +11,8 @@ final class ScrollModePreferenceViewController: NSViewController, NSTextFieldDel private var scrollSensitivityView: NSSlider! private var revHorizontalScrollView: NSButton! private var revVerticalScrollView: NSButton! - + private var scrollFrameColorWell: NSColorWell! + init() { if #available(OSX 11.0, *) { self.toolbarItemIcon = NSImage(systemSymbolName: "dpad", accessibilityDescription: nil)! @@ -68,6 +69,15 @@ final class ScrollModePreferenceViewController: NSViewController, NSTextFieldDel revVerticalScrollView.state = UserPreferences.ScrollMode.ReverseVerticalScrollProperty.read() ? .on : .off grid.addRow(with: [NSGridCell.emptyContentView, revVerticalScrollView]) + let scrollColorLabel = NSTextField(labelWithString: "Scroll Color:") + scrollFrameColorWell = NSColorWell() + scrollFrameColorWell.action = #selector(onScrollFrameColorWellEdit) + scrollFrameColorWell.color = UserPreferences.ScrollMode.ScrollFrameColorProperty.readColor() + scrollFrameColorWell.widthAnchor.constraint(equalToConstant: 25).isActive = true + scrollFrameColorWell.heightAnchor.constraint(equalToConstant: 25).isActive = true + + let scrollColorRow: [NSView] = [scrollColorLabel, scrollFrameColorWell] + grid.addRow(with: scrollColorRow) self.view.addSubview(grid) @@ -81,7 +91,12 @@ final class ScrollModePreferenceViewController: NSViewController, NSTextFieldDel ]) } - func isScrollKeysValid(keys: String) -> Bool { + @objc func onScrollFrameColorWellEdit() { + let value = scrollFrameColorWell.color + UserPreferences.ScrollMode.ScrollFrameColorProperty.saveColor(value: value) + } + + func isScrollKeysValid(keys: String) -> Bool { return UserPreferences.ScrollMode.ScrollKeysProperty.isValid(value: keys) } diff --git a/ViMac-Swift/UserDefaults/UserDefaultsProperties.swift b/ViMac-Swift/UserDefaults/UserDefaultsProperties.swift index b6fb06e..6673b48 100644 --- a/ViMac-Swift/UserDefaults/UserDefaultsProperties.swift +++ b/ViMac-Swift/UserDefaults/UserDefaultsProperties.swift @@ -17,7 +17,7 @@ struct UserDefaultsProperties { static let keySequenceResetDelay = UserDefaultsProperty.init("keySequenceResetDelay", defaultValue: "0.25") static let hintColor = UserDefaultsProperty.init("hintColor", defaultValue: NSColor(red: 255 / 255, green: 224 / 255, blue: 112 / 255, alpha: 1)) - static let scrollFrameColor = UserDefaultsProperty.init("hintColor", defaultValue: NSColor(red: 255 / 255, green: 224 / 255, blue: 112 / 255, alpha: 1)) + static let scrollFrameColor = UserDefaultsProperty.init("scrollFrameColor", defaultValue: NSColor(red: 255 / 255, green: 0 / 255, blue: 0 / 255, alpha: 1)) static let AXEnhancedUserInterfaceEnabled = UserDefaultsProperty.init("AXEnhancedUserInterfaceEnabled", defaultValue: false) static let AXManualAccessibilityEnabled = UserDefaultsProperty.init("AXManualAccessibilityEnabled", defaultValue: false) diff --git a/ViMac-Swift/UserPreferences.swift b/ViMac-Swift/UserPreferences.swift index b84b36c..9fd2123 100644 --- a/ViMac-Swift/UserPreferences.swift +++ b/ViMac-Swift/UserPreferences.swift @@ -32,6 +32,17 @@ extension PreferenceProperty { } } +func stringColor(colorString: String) -> NSColor { + let components = colorString.split(separator: " ") + return NSColor(red: CGFloat((components[0] as NSString).doubleValue), + green: CGFloat((components[1] as NSString).doubleValue), + blue: CGFloat((components[2] as NSString).doubleValue), + alpha: CGFloat((components[3] as NSString).doubleValue) ) +} +func colorString(color: NSColor) -> String { + return String(format: "%f %f %f %f", color.redComponent, color.greenComponent, color.blueComponent, color.alphaComponent) +} + struct UserPreferences { struct HintMode { class CustomCharactersProperty : PreferenceProperty { @@ -68,6 +79,29 @@ struct UserPreferences { return Float(read())! } } + + class HintColorProperty : PreferenceProperty { + + typealias T = String + + static var key = "HintColor" + + static var defaultValue = colorString(color: UserDefaultsProperties.hintColor.read()) + + static func readColor() -> NSColor { + let string = read() + return stringColor(colorString: string) + } + + static func saveColor(value: NSColor) { + self.save(value: colorString(color: value)) + } + + static func isValid(value: String) -> Bool { + return true + } + + } } struct ScrollMode { @@ -168,5 +202,29 @@ struct UserPreferences { return true } } + + class ScrollFrameColorProperty : PreferenceProperty { + + typealias T = String + + static var key = "ScrollColor" + + static var defaultValue = colorString(color: UserDefaultsProperties.scrollFrameColor.read()) + + static func readColor() -> NSColor { + let string = read() + return stringColor(colorString: string) + } + + static func saveColor(value: NSColor) { + self.save(value: colorString(color: value)) + } + + static func isValid(value: String) -> Bool { + return true + } + + } + } }