feat: added monochrome accent for Speed widget pictogram (#633)

This commit is contained in:
Serhiy Mytrovtsiy
2021-09-27 19:16:32 +02:00
parent 00503b8027
commit 71164398e8
2 changed files with 46 additions and 23 deletions

View File

@@ -17,6 +17,7 @@ public class SpeedWidget: WidgetWrapper {
private var valueState: Bool = true
private var baseValue: String = "byte"
private var unitsState: Bool = true
private var monochromeState: Bool = false
private var symbols: [String] = ["U", "D"]
@@ -28,6 +29,17 @@ public class SpeedWidget: WidgetWrapper {
private var width: CGFloat = 58
private var downloadColor: NSColor {
get {
return self.monochromeState ? MonochromeColor.blue : NSColor.systemBlue
}
}
private var uploadColor: NSColor {
get {
return self.monochromeState ? MonochromeColor.red : NSColor.red
}
}
public init(title: String, config: NSDictionary?, preview: Bool = false) {
let widgetTitle: String = title
if config != nil {
@@ -53,6 +65,7 @@ public class SpeedWidget: WidgetWrapper {
self.icon = Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_icon", defaultValue: self.baseValue)
self.baseValue = Store.shared.string(key: "\(self.title)_base", defaultValue: self.baseValue)
self.unitsState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_units", defaultValue: self.unitsState)
self.monochromeState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_monochrome", defaultValue: self.monochromeState)
}
if self.valueState && self.icon != "none" {
@@ -129,20 +142,12 @@ public class SpeedWidget: WidgetWrapper {
var downloadCircle = NSBezierPath()
downloadCircle = NSBezierPath(ovalIn: CGRect(x: Constants.Widget.margin.x, y: y-0.2, width: size, height: size))
if self.downloadValue >= 1_024 {
NSColor.systemBlue.set()
} else {
NSColor.textColor.setFill()
}
(self.downloadValue >= 1_024 ? self.downloadColor : NSColor.textColor).set()
downloadCircle.fill()
var uploadCircle = NSBezierPath()
uploadCircle = NSBezierPath(ovalIn: CGRect(x: Constants.Widget.margin.x, y: 10.5, width: size, height: size))
if self.uploadValue >= 1_024 {
NSColor.red.setFill()
} else {
NSColor.textColor.setFill()
}
(self.uploadValue >= 1_024 ? self.uploadColor : NSColor.textColor).set()
uploadCircle.fill()
}
@@ -162,11 +167,7 @@ public class SpeedWidget: WidgetWrapper {
arrowAngle: arrowAngle
)
if self.downloadValue >= 1_024 {
NSColor.systemBlue.set()
} else {
NSColor.textColor.set()
}
(self.downloadValue >= 1_024 ? self.downloadColor : NSColor.textColor).set()
downloadArrow.lineWidth = lineWidth
downloadArrow.stroke()
downloadArrow.close()
@@ -178,12 +179,8 @@ public class SpeedWidget: WidgetWrapper {
pointerLineLength: arrowSize,
arrowAngle: arrowAngle
)
if self.uploadValue >= 1_024 {
NSColor.red.set()
} else {
NSColor.textColor.set()
}
(self.uploadValue >= 1_024 ? self.uploadColor : NSColor.textColor).set()
uploadArrow.lineWidth = lineWidth
uploadArrow.stroke()
uploadArrow.close()
@@ -195,7 +192,7 @@ public class SpeedWidget: WidgetWrapper {
if self.symbols.count > 1 {
let downloadAttributes = [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 9, weight: .regular),
NSAttributedString.Key.foregroundColor: downloadValue >= 1_024 ? NSColor(red: (26/255.0), green: (126/255.0), blue: (252/255.0), alpha: 0.8) : NSColor.textColor,
NSAttributedString.Key.foregroundColor: self.downloadValue >= 1_024 ? self.downloadColor : NSColor.textColor,
NSAttributedString.Key.paragraphStyle: NSMutableParagraphStyle()
]
let rect = CGRect(x: Constants.Widget.margin.x, y: 1, width: 8, height: rowHeight)
@@ -206,7 +203,7 @@ public class SpeedWidget: WidgetWrapper {
if !self.symbols.isEmpty {
let uploadAttributes = [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 9, weight: .regular),
NSAttributedString.Key.foregroundColor: uploadValue >= 1_024 ? NSColor.red : NSColor.textColor,
NSAttributedString.Key.foregroundColor: self.uploadValue >= 1_024 ? self.uploadColor : NSColor.textColor,
NSAttributedString.Key.paragraphStyle: NSMutableParagraphStyle()
]
let rect = CGRect(x: Constants.Widget.margin.x, y: rowHeight+1, width: 8, height: rowHeight)
@@ -248,6 +245,13 @@ public class SpeedWidget: WidgetWrapper {
state: self.unitsState
))
view.addArrangedSubview(toggleTitleRow(
frame: NSRect(x: 0, y: 0, width: view.frame.width, height: Constants.Settings.row),
title: localizedString("Monochrome accent"),
action: #selector(toggleMonochrome),
state: self.monochromeState
))
return view
}
@@ -309,6 +313,19 @@ public class SpeedWidget: WidgetWrapper {
Store.shared.set(key: "\(self.title)_base", value: self.baseValue)
}
@objc private func toggleMonochrome(_ sender: NSControl) {
var state: NSControl.StateValue? = nil
if #available(OSX 10.15, *) {
state = sender is NSSwitch ? (sender as! NSSwitch).state: nil
} else {
state = sender is NSButton ? (sender as! NSButton).state: nil
}
self.monochromeState = state! == .on ? true : false
Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_monochrome", value: self.monochromeState)
self.display()
}
public func setValue(upload: Int64, download: Int64) {
var updated: Bool = false

View File

@@ -182,6 +182,12 @@ public var controlAccentColor: NSColor {
}
}
public class MonochromeColor {
static public let base: NSColor = NSColor.textColor
static public let red: NSColor = NSColor(red: (145), green: (145), blue: (145), alpha: 1)
static public let blue: NSColor = NSColor(red: (113), green: (113), blue: (113), alpha: 1)
}
public typealias colorZones = (orange: Double, red: Double)
public extension Notification.Name {