diff --git a/MTMR/AppleScriptTouchBarItem.swift b/MTMR/AppleScriptTouchBarItem.swift index ac66db7..9659a9b 100644 --- a/MTMR/AppleScriptTouchBarItem.swift +++ b/MTMR/AppleScriptTouchBarItem.swift @@ -10,7 +10,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem { super.init(identifier: identifier, title: "⏳", onTap: onTap, onLongTap: onLongTap) self.forceHideConstraint = self.view.widthAnchor.constraint(equalToConstant: 0) guard let script = source.appleScript else { - button.title = "no script" + self.title = "no script" return } self.script = script @@ -22,7 +22,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem { print(error?.description ?? "unknown error") #endif DispatchQueue.main.async { - self.button.title = "error" + self.title = "error" } return } @@ -40,7 +40,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem { #endif let scriptResult = self.execute() DispatchQueue.main.async { - self.button.title = scriptResult + self.title = scriptResult self.forceHideConstraint.isActive = scriptResult == "" } DispatchQueue.main.asyncAfter(deadline: .now() + self.interval) { [weak self] in diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 5848892..5804842 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -11,7 +11,7 @@ import Cocoa class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate { private let tapClosure: (() -> ())? private let longTapClosure: (() -> ())? - private(set) var button: NSButton! + private(set) var button: NSButton! //todo hide completely private var singleClick: NSClickGestureRecognizer! private var longClick: NSPressGestureRecognizer! @@ -19,8 +19,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> (), onLongTap callbackLong: @escaping () -> (), bezelColor: NSColor? = .clear) { self.tapClosure = callback self.longTapClosure = callbackLong + self.attributedTitle = title.defaultTouchbarAttributedString super.init(identifier: identifier) + button = CustomHeightButton(title: title, target: nil, action: nil) longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong)) longClick.allowedTouchTypes = .direct @@ -30,7 +32,8 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat singleClick.allowedTouchTypes = .direct singleClick.delegate = self - installButton(titled: title, bordered: true, backgroundColor: nil) + reinstallButton() + button.attributedTitle = attributedTitle } required init?(coder: NSCoder) { @@ -39,29 +42,46 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat var isBordered: Bool = true { didSet { - installButton(titled: self.button.title, bordered: isBordered, backgroundColor: backgroundColor) + reinstallButton() } } var backgroundColor: NSColor? { didSet { - installButton(titled: self.button.title, bordered: isBordered, backgroundColor: backgroundColor) + reinstallButton() } } - private func installButton(titled title: String, bordered: Bool, backgroundColor: NSColor?) { - button = CustomHeightButton(title: title, target: nil, action: nil) - let cell = CustomButtonCell() + var title: String { + get { + return self.attributedTitle.string + } + set { + self.attributedTitle = newValue.defaultTouchbarAttributedString + } + } + + var attributedTitle: NSAttributedString { + didSet { + self.button?.attributedTitle = attributedTitle + } + } + + private func reinstallButton() { + let title = button.attributedTitle + let image = button.image + let cell = CustomButtonCell(parentItem: self) button.cell = cell if let color = backgroundColor { cell.isBordered = true button.bezelColor = color cell.backgroundColor = color } else { - button.isBordered = bordered - button.bezelStyle = bordered ? .rounded : .inline + button.isBordered = isBordered + button.bezelStyle = isBordered ? .rounded : .inline } - button.title = title + button.attributedTitle = title + button.image = image self.view = button self.view.addGestureRecognizer(longClick) @@ -118,15 +138,21 @@ class CustomHeightButton : NSButton { } class CustomButtonCell: NSButtonCell { + weak var parentItem: CustomButtonTouchBarItem? - init() { + init(parentItem: CustomButtonTouchBarItem) { super.init(textCell: "") + self.parentItem = parentItem } override func highlight(_ flag: Bool, withFrame cellFrame: NSRect, in controlView: NSView) { super.highlight(flag, withFrame: cellFrame, in: controlView) if !self.isBordered { - self.setTitle(self.title, withColor: flag ? .lightGray : .white) + if flag { + self.setAttributedTitle(self.attributedTitle, withColor: .lightGray) + } else if let parentItem = self.parentItem { + self.attributedTitle = parentItem.attributedTitle + } } } @@ -134,22 +160,19 @@ class CustomButtonCell: NSButtonCell { fatalError("init(coder:) has not been implemented") } - override var title: String! { - get { - return self.attributedTitle.string - } - - set (newTitle) { - setTitle(newTitle, withColor: .white) - } - } - - func setTitle(_ title: String, withColor color: NSColor) { - let attrTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1]) - attrTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count)) - + func setAttributedTitle(_ title: NSAttributedString, withColor color: NSColor) { + let attrTitle = NSMutableAttributedString(attributedString: title) + attrTitle.addAttributes([.foregroundColor: color], range: NSRange(location: 0, length: attrTitle.length)) self.attributedTitle = attrTitle } } +extension String { + var defaultTouchbarAttributedString: NSAttributedString { + let attrTitle = NSMutableAttributedString(string: self, attributes: [.foregroundColor: NSColor.white, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1]) + attrTitle.setAlignment(.center, range: NSRange(location: 0, length: self.count)) + return attrTitle + } +} + diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 4a2e3e8..b981a29 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -222,7 +222,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { button.imageScaling = .scaleProportionallyDown button.imagePosition = .imageLeading - if (button.title == "") { + if (item.title == "") { button.imagePosition = .imageOnly } diff --git a/MTMR/Widgets/BatteryBarItem.swift b/MTMR/Widgets/BatteryBarItem.swift index 6d07354..02cd2c7 100644 --- a/MTMR/Widgets/BatteryBarItem.swift +++ b/MTMR/Widgets/BatteryBarItem.swift @@ -10,20 +10,28 @@ import IOKit.ps import Foundation class BatteryBarItem: CustomButtonTouchBarItem { - private var timer: Timer! + private let batteryInfo = BatteryInfo() init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) { super.init(identifier: identifier, title: " ", onTap: onTap, onLongTap: onLongTap) - self.view = button - let batteryInfo = BatteryInfo(button: button) - batteryInfo.start() - batteryInfo.updateInfo() + batteryInfo.start { [weak self] in + self?.refresh() + } + self.refresh() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } + + func refresh() { + self.attributedTitle = self.batteryInfo.formattedInfo() + } + + deinit { + batteryInfo.stop() + } } class BatteryInfo: NSObject { @@ -34,17 +42,11 @@ class BatteryInfo: NSObject { var isCharging: Bool = false var ACPower: String = "" var timeRemaining: String = "" - - var button: NSButton + var notifyBlock: ()->() = {} var loop:CFRunLoopSource? - init(button: NSButton) { - self.button = button - super.init() - self.start() - } - - func start() { + func start(notifyBlock: @escaping ()->()) { + self.notifyBlock = notifyBlock let opaque = Unmanaged.passRetained(self).toOpaque() let context = UnsafeMutableRawPointer(opaque) loop = IOPSNotificationCreateRunLoopSource({ (context) in @@ -53,16 +55,17 @@ class BatteryInfo: NSObject { } let watcher = Unmanaged.fromOpaque(ctx).takeUnretainedValue() - watcher.updateInfo() - }, context).takeRetainedValue() as CFRunLoopSource + watcher.notifyBlock() + }, context).takeRetainedValue() as CFRunLoopSource CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode) } func stop() { - if !(self.loop != nil) { + self.notifyBlock = {} + guard let loop = self.loop else { return } - CFRunLoopRemoveSource(CFRunLoopGetCurrent(), self.loop, CFRunLoopMode.defaultMode) + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode) self.loop = nil } @@ -114,7 +117,7 @@ class BatteryInfo: NSObject { return "" } - public func updateInfo() { + public func formattedInfo() -> NSAttributedString { var title = "" self.getPSInfo() @@ -134,12 +137,11 @@ class BatteryInfo: NSObject { color = NSColor.red } - let regularFont = button.attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15) - let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont, .baselineOffset: 1]) + let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: NSFont.systemFont(ofSize: 15), .baselineOffset: 1]) let newTitleSecond = NSMutableAttributedString(string: timeRemaining as String, attributes: [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 8, weight: .regular), NSAttributedStringKey.baselineOffset: 7]) newTitle.append(newTitleSecond) newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count)) - button.attributedTitle = newTitle + return newTitle } } diff --git a/MTMR/Widgets/CurrencyBarItem.swift b/MTMR/Widgets/CurrencyBarItem.swift index 9e9137d..eddff4f 100644 --- a/MTMR/Widgets/CurrencyBarItem.swift +++ b/MTMR/Widgets/CurrencyBarItem.swift @@ -109,9 +109,9 @@ class CurrencyBarItem: CustomButtonTouchBarItem { let title = String(format: "%@%.2f", self.prefix, value) - let regularFont = button.attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15) + let regularFont = self.attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15) let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont]) newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count)) - button.attributedTitle = newTitle + self.attributedTitle = newTitle } } diff --git a/MTMR/Widgets/InputSourceBarItem.swift b/MTMR/Widgets/InputSourceBarItem.swift index f32abd0..1d9f49d 100644 --- a/MTMR/Widgets/InputSourceBarItem.swift +++ b/MTMR/Widgets/InputSourceBarItem.swift @@ -58,8 +58,9 @@ class InputSourceBarItem: CustomButtonTouchBarItem { if (iconImage != nil) { self.button.cell?.image = iconImage self.button.cell?.image?.size = buttonSize + self.title = "" } else { - self.button.title = currentSource.name + self.title = currentSource.name } } diff --git a/MTMR/Widgets/TimeTouchBarItem.swift b/MTMR/Widgets/TimeTouchBarItem.swift index 21d98d3..5785c52 100644 --- a/MTMR/Widgets/TimeTouchBarItem.swift +++ b/MTMR/Widgets/TimeTouchBarItem.swift @@ -19,7 +19,7 @@ class TimeTouchBarItem: CustomButtonTouchBarItem { } @objc func updateTime() { - button.title = self.dateFormatter.string(from: Date()) + self.title = self.dateFormatter.string(from: Date()) } } diff --git a/MTMR/Widgets/WeatherBarItem.swift b/MTMR/Widgets/WeatherBarItem.swift index ef088a8..257febc 100644 --- a/MTMR/Widgets/WeatherBarItem.swift +++ b/MTMR/Widgets/WeatherBarItem.swift @@ -117,7 +117,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate { } func setWeather(text: String) { - button.title = text + self.title = text } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {