Skip to content

Commit

Permalink
feat: add colors translation
Browse files Browse the repository at this point in the history
  • Loading branch information
exelban committed Apr 16, 2021
1 parent f4289c1 commit 05091a9
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 239 deletions.
30 changes: 16 additions & 14 deletions ModuleKit/Widgets/BarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class BarChart: WidgetWrapper {
private var labelState: Bool = false
private var boxState: Bool = true
private var frameState: Bool = false
private var colorState: widget_c = .systemAccent
private var colorState: Color = .systemAccent

private var colors: [widget_c] = widget_c.allCases
private var colors: [Color] = Color.allCases
private var value: [Double] = []
private var pressureLevel: Int = 0
private var colorZones: colorZones = (0.6, 0.8)
Expand Down Expand Up @@ -50,10 +50,8 @@ public class BarChart: WidgetWrapper {
if let box = configuration["Box"] as? Bool {
self.boxState = box
}
if let colorsToDisable = configuration["Unsupported colors"] as? [String] {
self.colors = self.colors.filter { (color: widget_c) -> Bool in
return !colorsToDisable.contains("\(color.self)")
}
if let unsupportedColors = configuration["Unsupported colors"] as? [String] {
self.colors = self.colors.filter{ !unsupportedColors.contains($0.key) }
}
if let color = configuration["Color"] as? String {
if let defaultColor = colors.first(where: { "\($0.self)" == color }) {
Expand All @@ -75,7 +73,7 @@ public class BarChart: WidgetWrapper {
self.boxState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_box", defaultValue: self.boxState)
self.frameState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_frame", defaultValue: self.frameState)
self.labelState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_label", defaultValue: self.labelState)
self.colorState = widget_c(rawValue: Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_color", defaultValue: self.colorState.rawValue)) ?? self.colorState
self.colorState = Color.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_color", defaultValue: self.colorState.key))
}

if preview {
Expand Down Expand Up @@ -185,7 +183,7 @@ public class BarChart: WidgetWrapper {
} else {
(isDarkMode ? NSColor.white : NSColor.black).set()
}
default: colorFromString("\(self.colorState.self)").set()
default: (self.colorState.additional as? NSColor ?? NSColor.controlAccentColor).set()
}

partition.fill()
Expand Down Expand Up @@ -273,12 +271,12 @@ public class BarChart: WidgetWrapper {
)
view.addSubview(self.frameSettingsView!)

view.addSubview(SelectColorRow(
view.addSubview(SelectRow(
frame: NSRect(x: 0, y: (rowHeight + Constants.Settings.margin) * 0, width: view.frame.width, height: rowHeight),
title: LocalizedString("Color"),
action: #selector(toggleColor),
items: self.colors.map{ $0.rawValue },
selected: self.colorState.rawValue
items: self.colors,
selected: self.colorState.key
))

return view
Expand Down Expand Up @@ -335,10 +333,14 @@ public class BarChart: WidgetWrapper {
}

@objc private func toggleColor(_ sender: NSMenuItem) {
if let newColor = widget_c.allCases.first(where: { $0.rawValue == sender.title }) {
guard let key = sender.representedObject as? String else {
return
}
if let newColor = Color.allCases.first(where: { $0.key == key }) {
self.colorState = newColor
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_color", value: self.colorState.rawValue)
self.display()
}

Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_color", value: key)
self.display()
}
}
30 changes: 16 additions & 14 deletions ModuleKit/Widgets/LineChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class LineChart: WidgetWrapper {
private var frameState: Bool = false
private var valueState: Bool = false
private var valueColorState: Bool = false
private var colorState: widget_c = .systemAccent
private var colorState: Color = .systemAccent

private let width: CGFloat = 34

Expand All @@ -28,7 +28,7 @@ public class LineChart: WidgetWrapper {
width: 34,
height: Constants.Widget.height - (2*Constants.Widget.margin.y)
), num: 60)
private var colors: [widget_c] = widget_c.allCases
private var colors: [Color] = Color.allCases
private var value: Double = 0
private var pressureLevel: Int = 0

Expand All @@ -50,10 +50,8 @@ public class LineChart: WidgetWrapper {
if let value = config!["Value"] as? Bool {
self.valueState = value
}
if let colorsToDisable = config!["Unsupported colors"] as? [String] {
self.colors = self.colors.filter { (color: widget_c) -> Bool in
return !colorsToDisable.contains("\(color.self)")
}
if let unsupportedColors = config!["Unsupported colors"] as? [String] {
self.colors = self.colors.filter{ !unsupportedColors.contains($0.key) }
}
if let color = config!["Color"] as? String {
if let defaultColor = colors.first(where: { "\($0.self)" == color }) {
Expand All @@ -77,7 +75,7 @@ public class LineChart: WidgetWrapper {
self.valueState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_value", defaultValue: self.valueState)
self.labelState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_label", defaultValue: self.labelState)
self.valueColorState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_valueColor", defaultValue: self.valueColorState)
self.colorState = widget_c(rawValue: Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_color", defaultValue: self.colorState.rawValue)) ?? self.colorState
self.colorState = Color.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_color", defaultValue: self.colorState.key))
}

if self.labelState {
Expand Down Expand Up @@ -120,7 +118,7 @@ public class LineChart: WidgetWrapper {
} else {
color = (isDarkMode ? NSColor.white : NSColor.black)
}
default: color = colorFromString("\(self.colorState.self)")
default: color = self.colorState.additional as? NSColor ?? NSColor.controlAccentColor
}

if self.labelState {
Expand Down Expand Up @@ -287,12 +285,12 @@ public class LineChart: WidgetWrapper {
)
view.addSubview(self.frameSettingsView!)

view.addSubview(SelectColorRow(
view.addSubview(SelectRow(
frame: NSRect(x: 0, y: (rowHeight + Constants.Settings.margin) * 1, width: view.frame.width, height: rowHeight),
title: LocalizedString("Color"),
action: #selector(toggleColor),
items: self.colors.map{ $0.rawValue },
selected: self.colorState.rawValue
items: self.colors,
selected: self.colorState.key
))

view.addSubview(ToggleTitleRow(
Expand Down Expand Up @@ -368,11 +366,15 @@ public class LineChart: WidgetWrapper {
}

@objc private func toggleColor(_ sender: NSMenuItem) {
if let newColor = widget_c.allCases.first(where: { $0.rawValue == sender.title }) {
guard let key = sender.representedObject as? String else {
return
}
if let newColor = Color.allCases.first(where: { $0.key == key }) {
self.colorState = newColor
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_color", value: self.colorState.rawValue)
self.display()
}

Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_color", value: key)
self.display()
}

@objc private func toggleValueColor(_ sender: NSControl) {
Expand Down
30 changes: 16 additions & 14 deletions ModuleKit/Widgets/Mini.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public class Mini: WidgetWrapper {
private let defaultTitle: String

private var labelState: Bool = true
private var colorState: widget_c = .monochrome
private var colorState: Color = .monochrome

private var labelLayer: CATextLayer? = nil
private var valueLayer: CATextLayer? = nil

private let onlyValueWidth: CGFloat = 40
private var colors: [widget_c] = widget_c.allCases
private var colors: [Color] = Color.allCases

private var value: Double = 0
private var pressureLevel: Int = 0
Expand Down Expand Up @@ -53,10 +53,8 @@ public class Mini: WidgetWrapper {
if let label = configuration["Label"] as? Bool {
self.labelState = label
}
if let colorsToDisable = configuration["Unsupported colors"] as? [String] {
self.colors = self.colors.filter { (color: widget_c) -> Bool in
return !colorsToDisable.contains("\(color.self)")
}
if let unsupportedColors = configuration["Unsupported colors"] as? [String] {
self.colors = self.colors.filter{ !unsupportedColors.contains($0.key) }
}
if let color = configuration["Color"] as? String {
if let defaultColor = colors.first(where: { "\($0.self)" == color }) {
Expand All @@ -76,7 +74,7 @@ public class Mini: WidgetWrapper {
self.canDrawConcurrently = true

if !preview {
self.colorState = widget_c(rawValue: Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_color", defaultValue: self.colorState.rawValue)) ?? self.colorState
self.colorState = Color.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_color", defaultValue: self.colorState.key))
self.labelState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_label", defaultValue: self.labelState)
}
}
Expand Down Expand Up @@ -112,7 +110,7 @@ public class Mini: WidgetWrapper {
case .utilization: color = value.usageColor()
case .pressure: color = self.pressureLevel.pressureColor()
case .monochrome: color = (isDarkMode ? NSColor.white : NSColor.black)
default: color = colorFromString("\(self.colorState.self)")
default: color = self.colorState.additional as? NSColor ?? NSColor.controlAccentColor
}

let stringAttributes = [
Expand Down Expand Up @@ -185,23 +183,27 @@ public class Mini: WidgetWrapper {
state: self.labelState
))

view.addSubview(SelectColorRow(
view.addSubview(SelectRow(
frame: NSRect(x: 0, y: (rowHeight + Constants.Settings.margin) * 0, width: view.frame.width, height: rowHeight),
title: LocalizedString("Color"),
action: #selector(toggleColor),
items: self.colors.map{ $0.rawValue },
selected: self.colorState.rawValue
items: self.colors,
selected: self.colorState.key
))

return view
}

@objc private func toggleColor(_ sender: NSMenuItem) {
if let newColor = widget_c.allCases.first(where: { $0.rawValue == sender.title }) {
guard let key = sender.representedObject as? String else {
return
}
if let newColor = Color.allCases.first(where: { $0.key == key }) {
self.colorState = newColor
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_color", value: self.colorState.rawValue)
self.display()
}

Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_color", value: key)
self.display()
}

@objc private func toggleLabel(_ sender: NSControl) {
Expand Down
4 changes: 4 additions & 0 deletions Stats.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
9A58DEA424B3647600716A9F /* settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A58DEA324B3647600716A9F /* settings.swift */; };
9A599701261121F00043560F /* Label.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A599700261121F00043560F /* Label.swift */; };
9A5AF11B2469CE9B00684737 /* popup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A5AF11A2469CE9B00684737 /* popup.swift */; };
9A5F191626220D510085C3CC /* types.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A5F191526220D510085C3CC /* types.swift */; };
9A65295825B78056005E2DE4 /* NetworkChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A65295725B78056005E2DE4 /* NetworkChart.swift */; };
9A65654A253F20EF0096B607 /* settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A656549253F20EF0096B607 /* settings.swift */; };
9A656562253F788A0096B607 /* popup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A656561253F788A0096B607 /* popup.swift */; };
Expand Down Expand Up @@ -409,6 +410,7 @@
9A599700261121F00043560F /* Label.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = "<group>"; };
9A5AF11A2469CE9B00684737 /* popup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = popup.swift; sourceTree = "<group>"; };
9A5F0503256A9135002FF75F /* nb */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nb; path = nb.lproj/Localizable.strings; sourceTree = "<group>"; };
9A5F191526220D510085C3CC /* types.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = types.swift; sourceTree = "<group>"; };
9A65295725B78056005E2DE4 /* NetworkChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkChart.swift; sourceTree = "<group>"; };
9A654920244074B500E30B74 /* extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = extensions.swift; sourceTree = "<group>"; };
9A65492224407EA600E30B74 /* store.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = store.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -621,6 +623,7 @@
9A0C82D324460E4400FAE3D4 /* launchAtLogin.swift */,
9A654920244074B500E30B74 /* extensions.swift */,
9A1D5E4A25235C8100B82BFC /* helpers.swift */,
9A5F191526220D510085C3CC /* types.swift */,
9A0C82DD24460F7200FAE3D4 /* Info.plist */,
9A9D728924471FAE005CF997 /* SMC.swift */,
9A81C76F2449B8D500825D92 /* Charts.swift */,
Expand Down Expand Up @@ -1439,6 +1442,7 @@
9A1D5E4B25235C8100B82BFC /* helpers.swift in Sources */,
9A0C82EE2446124800FAE3D4 /* SystemKit.swift in Sources */,
9A9D728A24471FAE005CF997 /* SMC.swift in Sources */,
9A5F191626220D510085C3CC /* types.swift in Sources */,
9A0C82E824460F9E00FAE3D4 /* launchAtLogin.swift in Sources */,
9A0C82E924460F9F00FAE3D4 /* store.swift in Sources */,
);
Expand Down
18 changes: 2 additions & 16 deletions StatsKit/extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public extension NSView {
return row
}

func SelectRow(frame: NSRect, title: String, action: Selector, items: [KeyValue_t], selected: String) -> NSView {
func SelectRow(frame: NSRect, title: String, action: Selector, items: [KeyValue_p], selected: String) -> NSView {
let row: NSView = NSView(frame: frame)

let rowTitle: NSTextField = LabelField(frame: NSRect(x: 0, y: (row.frame.height - 16)/2, width: row.frame.width - 52, height: 17), title)
Expand All @@ -360,7 +360,7 @@ public extension NSView {
return row
}

func SelectView(action: Selector, items: [KeyValue_t], selected: String) -> NSPopUpButton {
func SelectView(action: Selector, items: [KeyValue_p], selected: String) -> NSPopUpButton {
let select: NSPopUpButton = NSPopUpButton(frame: NSRect(x: 0, y: 0, width: 50, height: 26))
select.target = self
select.action = action
Expand All @@ -384,20 +384,6 @@ public extension NSView {
}
}

public extension Notification.Name {
static let toggleSettings = Notification.Name("toggleSettings")
static let toggleModule = Notification.Name("toggleModule")
static let togglePopup = Notification.Name("togglePopup")
static let toggleWidget = Notification.Name("toggleWidget")
static let openModuleSettings = Notification.Name("openModuleSettings")
static let settingsAppear = Notification.Name("settingsAppear")
static let switchWidget = Notification.Name("switchWidget")
static let checkForUpdates = Notification.Name("checkForUpdates")
static let changeCronInterval = Notification.Name("changeCronInterval")
static let clickInSettings = Notification.Name("clickInSettings")
static let refreshPublicIP = Notification.Name("refreshPublicIP")
}

public class NSButtonWithPadding: NSButton {
public var horizontalPadding: CGFloat = 0
public var verticalPadding: CGFloat = 0
Expand Down

0 comments on commit 05091a9

Please sign in to comment.