From 5a974e38cf2f6b21d9e027fcac2c30b102bb9b43 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Fri, 19 May 2023 18:13:06 +0200 Subject: [PATCH] feat: added a header with title and settings button to the portals (#1467) --- Kit/constants.swift | 2 +- Kit/module/portal.swift | 49 ++++++++++++++++++++++++++++++++++++ Modules/Battery/portal.swift | 11 ++++---- Modules/CPU/portal.swift | 1 + Modules/Disk/portal.swift | 12 +++++---- Modules/GPU/portal.swift | 10 +++++--- Modules/RAM/portal.swift | 12 +++++---- 7 files changed, 77 insertions(+), 20 deletions(-) diff --git a/Kit/constants.swift b/Kit/constants.swift index b819f12c..dc9fc1fa 100644 --- a/Kit/constants.swift +++ b/Kit/constants.swift @@ -18,7 +18,7 @@ public struct Popup_c_s { public let spacing: CGFloat = 2 public let headerHeight: CGFloat = 42 public let separatorHeight: CGFloat = 30 - public let portalHeight: CGFloat = 100 + public let portalHeight: CGFloat = 120 } public struct Settings_c_s { diff --git a/Kit/module/portal.swift b/Kit/module/portal.swift index e8b4687e..cedb9877 100644 --- a/Kit/module/portal.swift +++ b/Kit/module/portal.swift @@ -14,3 +14,52 @@ import Cocoa public protocol Portal_p: NSView { var name: String { get } } + +public class PortalHeader: NSStackView { + private let name: String + + public init(_ name: String) { + self.name = name + + super.init(frame: NSRect.zero) + self.heightAnchor.constraint(equalToConstant: 20).isActive = true + + let title = NSTextField() + title.isEditable = false + title.isSelectable = false + title.isBezeled = false + title.wantsLayer = true + title.textColor = .textColor + title.backgroundColor = .clear + title.canDrawSubviewsIntoLayer = true + title.alignment = .center + title.font = NSFont.systemFont(ofSize: 12, weight: .regular) + title.stringValue = name + + let settings = NSButton() + settings.heightAnchor.constraint(equalToConstant: 18).isActive = true + settings.bezelStyle = .regularSquare + settings.translatesAutoresizingMaskIntoConstraints = false + settings.imageScaling = .scaleProportionallyDown + settings.image = Bundle(for: type(of: self)).image(forResource: "settings")! + settings.contentTintColor = .lightGray + settings.isBordered = false + settings.action = #selector(self.openSettings) + settings.target = self + settings.toolTip = localizedString("Open module settings") + settings.focusRingType = .none + + self.addArrangedSubview(title) + self.addArrangedSubview(NSView()) + self.addArrangedSubview(settings) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + @objc private func openSettings(_ sender: Any) { + self.window?.setIsVisible(false) + NotificationCenter.default.post(name: .toggleSettings, object: nil, userInfo: ["module": self.name]) + } +} diff --git a/Modules/Battery/portal.swift b/Modules/Battery/portal.swift index 894d6ece..3fb89e13 100644 --- a/Modules/Battery/portal.swift +++ b/Modules/Battery/portal.swift @@ -36,13 +36,14 @@ internal class Portal: NSStackView, Portal_p { self.orientation = .vertical self.distribution = .fillEqually + self.spacing = Constants.Popup.spacing*2 self.edgeInsets = NSEdgeInsets( - top: Constants.Popup.margins, - left: Constants.Popup.margins, - bottom: Constants.Popup.margins, - right: Constants.Popup.margins + top: Constants.Popup.spacing*2, + left: Constants.Popup.spacing*2, + bottom: Constants.Popup.spacing*2, + right: Constants.Popup.spacing*2 ) - self.spacing = 0 + self.addArrangedSubview(PortalHeader(name)) let box: NSStackView = NSStackView() box.heightAnchor.constraint(equalToConstant: 13).isActive = true diff --git a/Modules/CPU/portal.swift b/Modules/CPU/portal.swift index ad4a2b79..755b1a8d 100644 --- a/Modules/CPU/portal.swift +++ b/Modules/CPU/portal.swift @@ -63,6 +63,7 @@ public class Portal: NSStackView, Portal_p { bottom: Constants.Popup.spacing*2, right: Constants.Popup.spacing*2 ) + self.addArrangedSubview(PortalHeader(name)) let circle = PieChartView(frame: NSRect.zero, segments: [], drawValue: true) circle.toolTip = localizedString("CPU usage") diff --git a/Modules/Disk/portal.swift b/Modules/Disk/portal.swift index f6bc3dee..1f0ac3ff 100644 --- a/Modules/Disk/portal.swift +++ b/Modules/Disk/portal.swift @@ -26,14 +26,16 @@ internal class Portal: NSStackView, Portal_p { self.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor self.layer?.cornerRadius = 3 - self.orientation = .horizontal + self.orientation = .vertical self.distribution = .fillEqually + self.spacing = Constants.Popup.spacing*2 self.edgeInsets = NSEdgeInsets( - top: Constants.Popup.margins, - left: Constants.Popup.margins, - bottom: Constants.Popup.margins, - right: Constants.Popup.margins + top: Constants.Popup.spacing*2, + left: Constants.Popup.spacing*2, + bottom: Constants.Popup.spacing*2, + right: Constants.Popup.spacing*2 ) + self.addArrangedSubview(PortalHeader(name)) self.circle = PieChartView(frame: NSRect.zero, segments: [], drawValue: true) self.circle!.toolTip = localizedString("Disk usage") diff --git a/Modules/GPU/portal.swift b/Modules/GPU/portal.swift index f12c53fe..91d45dbf 100644 --- a/Modules/GPU/portal.swift +++ b/Modules/GPU/portal.swift @@ -28,14 +28,16 @@ public class Portal: NSStackView, Portal_p { self.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor self.layer?.cornerRadius = 3 - self.orientation = .horizontal + self.orientation = .vertical self.distribution = .fillEqually + self.spacing = Constants.Popup.spacing*2 self.edgeInsets = NSEdgeInsets( - top: Constants.Popup.margins, - left: Constants.Popup.margins, + top: Constants.Popup.spacing*2, + left: Constants.Popup.spacing*2, bottom: 0, - right: Constants.Popup.margins + right: Constants.Popup.spacing*2 ) + self.addArrangedSubview(PortalHeader(name)) self.circle = HalfCircleGraphView() self.circle!.toolTip = localizedString("GPU usage") diff --git a/Modules/RAM/portal.swift b/Modules/RAM/portal.swift index 52436cb0..38833a3b 100644 --- a/Modules/RAM/portal.swift +++ b/Modules/RAM/portal.swift @@ -62,14 +62,16 @@ public class Portal: NSStackView, Portal_p { self.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor self.layer?.cornerRadius = 3 - self.orientation = .horizontal + self.orientation = .vertical self.distribution = .fillEqually + self.spacing = Constants.Popup.spacing*2 self.edgeInsets = NSEdgeInsets( - top: Constants.Popup.margins, - left: Constants.Popup.margins, - bottom: Constants.Popup.margins, - right: Constants.Popup.margins + top: Constants.Popup.spacing*2, + left: Constants.Popup.spacing*2, + bottom: Constants.Popup.spacing*2, + right: Constants.Popup.spacing*2 ) + self.addArrangedSubview(PortalHeader(name)) self.circle = PieChartView(frame: NSRect.zero, segments: [], drawValue: true) self.circle!.toolTip = localizedString("Memory usage")