Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color settings to preference pane #435

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions ViMac-Swift/BorderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
import Cocoa

class BorderView: NSView {
let SIZE = CGFloat(2)
let borderColor = NSColor.red
let SIZE = CGFloat(3)

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

Expand Down
3 changes: 1 addition & 2 deletions ViMac-Swift/HintView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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 untypedHintColor = NSColor.black
static let typedHintColor = NSColor(red: 212 / 255, green: 172 / 255, blue: 58 / 255, alpha: 1)

Expand All @@ -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

Expand Down
20 changes: 18 additions & 2 deletions ViMac-Swift/Preferences/HintModePreferenceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, *) {
Expand Down Expand Up @@ -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([
Expand All @@ -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)
Expand Down
19 changes: 17 additions & 2 deletions ViMac-Swift/Preferences/ScrollModePreferenceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)!
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}

Expand Down
3 changes: 3 additions & 0 deletions ViMac-Swift/UserDefaults/UserDefaultsProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct UserDefaultsProperties {
static let keySequenceScrollModeEnabled = UserDefaultsProperty<Bool>.init("keySequenceScrollModeEnabled", defaultValue: false)
static let keySequenceScrollMode = UserDefaultsProperty<String>.init("keySequenceScrollMode", defaultValue: "")
static let keySequenceResetDelay = UserDefaultsProperty<String>.init("keySequenceResetDelay", defaultValue: "0.25")

static let hintColor = UserDefaultsProperty<NSColor>.init("hintColor", defaultValue: NSColor(red: 255 / 255, green: 224 / 255, blue: 112 / 255, alpha: 1))
static let scrollFrameColor = UserDefaultsProperty<NSColor>.init("scrollFrameColor", defaultValue: NSColor(red: 255 / 255, green: 0 / 255, blue: 0 / 255, alpha: 1))

static let AXEnhancedUserInterfaceEnabled = UserDefaultsProperty<Bool>.init("AXEnhancedUserInterfaceEnabled", defaultValue: false)
static let AXManualAccessibilityEnabled = UserDefaultsProperty<Bool>.init("AXManualAccessibilityEnabled", defaultValue: false)
Expand Down
58 changes: 58 additions & 0 deletions ViMac-Swift/UserPreferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

}

}
}