diff --git a/Kit/Widgets/State.swift b/Kit/Widgets/State.swift index fedb0533..2af15653 100644 --- a/Kit/Widgets/State.swift +++ b/Kit/Widgets/State.swift @@ -12,8 +12,13 @@ import Cocoa public class StateWidget: WidgetWrapper { + private var activeColorState: Color = .secondGreen + private var nonactiveColorState: Color = .secondRed + private var value: Bool = false + private var colors: [Color] = Color.allColors + public init(title: String, config: NSDictionary?, preview: Bool = false) { if config != nil { var configuration = config! @@ -36,6 +41,11 @@ public class StateWidget: WidgetWrapper { )) self.canDrawConcurrently = true + + if !preview { + self.activeColorState = Color.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_activeColor", defaultValue: self.activeColorState.key)) + self.nonactiveColorState = Color.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_nonactiveColor", defaultValue: self.nonactiveColorState.key)) + } } required init?(coder: NSCoder) { @@ -46,7 +56,8 @@ public class StateWidget: WidgetWrapper { super.draw(dirtyRect) let circle = NSBezierPath(ovalIn: CGRect(x: Constants.Widget.margin.x, y: (self.frame.height - 8)/2, width: 8, height: 8)) - (self.value ? NSColor.systemGreen : NSColor.systemRed).set() + let color = self.value ? self.activeColorState : self.nonactiveColorState + (color.additional as? NSColor)?.set() circle.fill() } @@ -57,4 +68,50 @@ public class StateWidget: WidgetWrapper { self.display() }) } + + // MARK: - Settings + + public override func settings() -> NSView { + let view = SettingsContainerView() + + view.addArrangedSubview(selectSettingsRow( + title: localizedString("Active state color"), + action: #selector(self.toggleActiveColor), + items: self.colors, + selected: self.activeColorState.key + )) + + view.addArrangedSubview(selectSettingsRow( + title: localizedString("Nonactive state color"), + action: #selector(self.toggleNonactiveColor), + items: self.colors, + selected: self.nonactiveColorState.key + )) + + return view + } + + @objc private func toggleActiveColor(_ sender: NSMenuItem) { + guard let key = sender.representedObject as? String else { + return + } + if let newColor = Color.allCases.first(where: { $0.key == key }) { + self.activeColorState = newColor + } + + Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_activeColor", value: key) + self.display() + } + + @objc private func toggleNonactiveColor(_ sender: NSMenuItem) { + guard let key = sender.representedObject as? String else { + return + } + if let newColor = Color.allCases.first(where: { $0.key == key }) { + self.nonactiveColorState = newColor + } + + Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_nonactiveColor", value: key) + self.display() + } } diff --git a/Kit/types.swift b/Kit/types.swift index f1ef2fab..2643964f 100644 --- a/Kit/types.swift +++ b/Kit/types.swift @@ -172,6 +172,14 @@ extension Color: CaseIterable { ] } + public static var allColors: [Color] { + return [.clear, .white, .black, .gray, .secondGray, .darkGray, .lightGray, + .red, .secondRed, .green, .secondGreen, .blue, .secondBlue, .yellow, .secondYellow, + .orange, .secondOrange, .purple, .secondPurple, .brown, .secondBrown, + .cyan, .magenta, .pink, .teal, .indigo + ] + } + public static func fromString(_ key: String, defaultValue: Color = .systemAccent) -> Color { return Color.allCases.first{ $0.key == key } ?? defaultValue }