From 9b09c80d050a060d73bf72d7fafeb3de00cb6f7d Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Sat, 10 Jun 2023 13:53:11 +0200 Subject: [PATCH] feat: removed disabling module when nothing shows in the Speed widget (#1514) --- Kit/Widgets/Speed.swift | 24 +++--------------------- Kit/Widgets/Stack.swift | 24 +----------------------- Kit/module/widget.swift | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/Kit/Widgets/Speed.swift b/Kit/Widgets/Speed.swift index 019a8d23..96559c5b 100644 --- a/Kit/Widgets/Speed.swift +++ b/Kit/Widgets/Speed.swift @@ -109,15 +109,14 @@ public class SpeedWidget: WidgetWrapper { public override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) - var width: CGFloat = 1 - + var width: CGFloat = 0 switch self.modeState { case "oneRow": width = self.drawOneRow(dirtyRect) case "twoRows": width = self.drawTwoRows(dirtyRect) default: - width = 1 + width = 0 } self.setWidth(width) @@ -498,14 +497,6 @@ public class SpeedWidget: WidgetWrapper { findAndToggleEnableNSControlState(self.valueAlignmentView, state: self.valueState) Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_value", value: self.valueState) self.display() - - if !self.valueState && self.icon.isEmpty { - NotificationCenter.default.post(name: .toggleModule, object: nil, userInfo: ["module": self.title, "state": false]) - self.state = false - } else if !self.state { - NotificationCenter.default.post(name: .toggleModule, object: nil, userInfo: ["module": self.title, "state": true]) - self.state = true - } } @objc private func toggleUnits(_ sender: NSControl) { @@ -517,18 +508,9 @@ public class SpeedWidget: WidgetWrapper { @objc private func toggleIcon(_ sender: NSMenuItem) { guard let key = sender.representedObject as? String else { return } self.icon = key + findAndToggleEnableNSControlState(self.transparentIconView, state: self.icon != "none") Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_icon", value: key) self.display() - - if !self.valueState && self.icon == "none" { - NotificationCenter.default.post(name: .toggleModule, object: nil, userInfo: ["module": self.title, "state": false]) - self.state = false - } else if !self.state { - NotificationCenter.default.post(name: .toggleModule, object: nil, userInfo: ["module": self.title, "state": true]) - self.state = true - } - - findAndToggleEnableNSControlState(self.transparentIconView, state: self.icon != "none") } @objc private func toggleBase(_ sender: NSMenuItem) { diff --git a/Kit/Widgets/Stack.swift b/Kit/Widgets/Stack.swift index 046af12d..6f1ca96c 100644 --- a/Kit/Widgets/Stack.swift +++ b/Kit/Widgets/Stack.swift @@ -83,7 +83,7 @@ public class StackWidget: WidgetWrapper { super.draw(dirtyRect) guard !self.values.isEmpty else { - self.drawNoData() + self.setWidth(0) return } @@ -226,28 +226,6 @@ public class StackWidget: WidgetWrapper { }) } - private func drawNoData() { - let size: CGFloat = 15 - let lineWidth = 1 / (NSScreen.main?.backingScaleFactor ?? 1) - let offset = lineWidth / 2 - let totalWidth: CGFloat = (Constants.Widget.margin.x*2) + size + (lineWidth*2) - - NSColor.textColor.set() - - var circle = NSBezierPath() - circle = NSBezierPath(ovalIn: CGRect(x: Constants.Widget.margin.x+offset, y: 1+offset, width: size, height: size)) - circle.stroke() - circle.lineWidth = lineWidth - - let line = NSBezierPath() - line.move(to: NSPoint(x: 3, y: 3.5)) - line.line(to: NSPoint(x: 13.5, y: 14)) - line.lineWidth = lineWidth - line.stroke() - - self.setWidth(totalWidth) - } - // MARK: - Settings public override func settings() -> NSView { diff --git a/Kit/module/widget.swift b/Kit/module/widget.swift index 14b26fa8..51efde13 100644 --- a/Kit/module/widget.swift +++ b/Kit/module/widget.swift @@ -176,15 +176,42 @@ open class WidgetWrapper: NSView, widget_p { } public func setWidth(_ width: CGFloat) { - guard self.shadowSize.width != width else { return } - self.shadowSize.width = width + var newWidth = width + if width == 0 || width == 1 { + newWidth = self.emptyView() + } + + guard self.shadowSize.width != newWidth else { return } + self.shadowSize.width = newWidth DispatchQueue.main.async { - self.setFrameSize(NSSize(width: width, height: self.frame.size.height)) + self.setFrameSize(NSSize(width: newWidth, height: self.frame.size.height)) self.widthHandler?() } } + public func emptyView() -> CGFloat { + let size: CGFloat = 15 + let lineWidth = 1 / (NSScreen.main?.backingScaleFactor ?? 1) + let offset = lineWidth / 2 + let width: CGFloat = (Constants.Widget.margin.x*2) + size + (lineWidth*2) + + NSColor.textColor.set() + + var circle = NSBezierPath() + circle = NSBezierPath(ovalIn: CGRect(x: Constants.Widget.margin.x+offset, y: 1+offset, width: size, height: size)) + circle.stroke() + circle.lineWidth = lineWidth + + let line = NSBezierPath() + line.move(to: NSPoint(x: 3, y: 3.5)) + line.line(to: NSPoint(x: 13.5, y: 14)) + line.lineWidth = lineWidth + line.stroke() + + return width + } + // MARK: - stubs open func settings() -> NSView { return NSView() }