From e1268d0d65f32a0a17aa0ab0a7bcf234b2ed036c Mon Sep 17 00:00:00 2001 From: ad Date: Thu, 26 Apr 2018 22:36:12 +0300 Subject: [PATCH 01/13] * NSButtonCell --- MTMR/CustomButtonTouchBarItem.swift | 25 ++++++++++++++++++++++++- MTMR/CustomSlider.swift | 16 ++++++++-------- MTMR/Widgets/InputSourceBarItem.swift | 3 ++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 671d20f..b068ef1 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -22,8 +22,11 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat super.init(identifier: identifier) button = NSButton(title: title, target: self, action: nil) + + button.cell = CustomButtonCell(backgroundColor: bezelColor!) + button.cell?.title = title + button.bezelColor = bezelColor - button.title = title self.view = button longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong)) @@ -82,3 +85,23 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat } } +class CustomButtonCell: NSButtonCell { + init(backgroundColor: NSColor) { + super.init(textCell: "") + if backgroundColor != .clear { + self.isBordered = true + self.bezelStyle = .rounded + self.backgroundColor = backgroundColor + } else { + self.isBordered = false + } + + self.font = NSFont.systemFont(ofSize: 15, weight: .regular ) + + } + + required init(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} + diff --git a/MTMR/CustomSlider.swift b/MTMR/CustomSlider.swift index 81aea36..621b6cb 100644 --- a/MTMR/CustomSlider.swift +++ b/MTMR/CustomSlider.swift @@ -49,19 +49,19 @@ class CustomSliderCell: NSSliderCell { override func drawBar(inside aRect: NSRect, flipped: Bool) { _barRect = aRect - var rect = aRect - rect.size.height = CGFloat(4) let barRadius = CGFloat(2) - let value = CGFloat((self.doubleValue - self.minValue) / (self.maxValue - self.minValue)) - let finalWidth = CGFloat(value * (self.controlView!.frame.size.width - 12)) + + var bgRect = aRect + bgRect.size.height = CGFloat(4) - var leftRect = rect - leftRect.size.width = finalWidth - let bg = NSBezierPath(roundedRect: rect, xRadius: barRadius, yRadius: barRadius) + let bg = NSBezierPath(roundedRect: bgRect, xRadius: barRadius, yRadius: barRadius) NSColor.lightGray.setFill() bg.fill() - let active = NSBezierPath(roundedRect: leftRect, xRadius: barRadius, yRadius: barRadius) + var activeRect = bgRect + + activeRect.size.width = CGFloat((Double(bgRect.size.width) / (self.maxValue - self.minValue)) * self.doubleValue) + let active = NSBezierPath(roundedRect: activeRect, xRadius: barRadius, yRadius: barRadius) NSColor.darkGray.setFill() active.fill() } diff --git a/MTMR/Widgets/InputSourceBarItem.swift b/MTMR/Widgets/InputSourceBarItem.swift index d131111..e71e41b 100644 --- a/MTMR/Widgets/InputSourceBarItem.swift +++ b/MTMR/Widgets/InputSourceBarItem.swift @@ -42,7 +42,8 @@ class InputSourceBarItem: CustomButtonTouchBarItem { } if (iconImage != nil) { - self.button.image = iconImage + self.button.imageScaling = .scaleProportionallyUpOrDown + self.button.cell?.image = iconImage } else { self.button.title = currentSource.name } From 76bdf127452eaff598850f45ba8727589334b4b2 Mon Sep 17 00:00:00 2001 From: ad Date: Thu, 26 Apr 2018 22:50:38 +0300 Subject: [PATCH 02/13] * fixed switching input source --- MTMR/Widgets/InputSourceBarItem.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/MTMR/Widgets/InputSourceBarItem.swift b/MTMR/Widgets/InputSourceBarItem.swift index e71e41b..b621ad0 100644 --- a/MTMR/Widgets/InputSourceBarItem.swift +++ b/MTMR/Widgets/InputSourceBarItem.swift @@ -19,12 +19,18 @@ class InputSourceBarItem: CustomButtonTouchBarItem { observeIputSourceChangedNotification(); textInputSourceDidChange() + self.button.cell?.action = #selector(switchInputSource) self.button.action = #selector(switchInputSource) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } + + @objc override func handleGestureSingle(gr: NSClickGestureRecognizer) { + super.handleGestureSingle(gr: gr) + switchInputSource() + } @objc public func textInputSourceDidChange() { let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue() From a0dbfca828a813dd797a486d5902eff02ff36199 Mon Sep 17 00:00:00 2001 From: ad Date: Fri, 27 Apr 2018 12:55:54 +0300 Subject: [PATCH 03/13] * input source widget image size --- MTMR/Widgets/InputSourceBarItem.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/MTMR/Widgets/InputSourceBarItem.swift b/MTMR/Widgets/InputSourceBarItem.swift index b621ad0..c56e26e 100644 --- a/MTMR/Widgets/InputSourceBarItem.swift +++ b/MTMR/Widgets/InputSourceBarItem.swift @@ -11,6 +11,7 @@ import Cocoa class InputSourceBarItem: CustomButtonTouchBarItem { fileprivate var notificationCenter: CFNotificationCenter + let buttonSize = NSSize(width: 21, height: 21) init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) { notificationCenter = CFNotificationCenterGetDistributedCenter(); @@ -21,6 +22,9 @@ class InputSourceBarItem: CustomButtonTouchBarItem { self.button.cell?.action = #selector(switchInputSource) self.button.action = #selector(switchInputSource) + + self.button.frame.size = buttonSize + self.button.bounds.size = buttonSize } required init?(coder: NSCoder) { @@ -48,8 +52,8 @@ class InputSourceBarItem: CustomButtonTouchBarItem { } if (iconImage != nil) { - self.button.imageScaling = .scaleProportionallyUpOrDown self.button.cell?.image = iconImage + self.button.cell?.image?.size = buttonSize } else { self.button.title = currentSource.name } From cc1332dd02ca26e4fa189b1756576e8b8d1f51e0 Mon Sep 17 00:00:00 2001 From: ad Date: Fri, 27 Apr 2018 14:05:13 +0300 Subject: [PATCH 04/13] * button font color --- MTMR/CustomButtonTouchBarItem.swift | 18 +++++++++++++++--- MTMR/Widgets/CurrencyBarItem.swift | 10 +++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index b068ef1..b89f130 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -95,9 +95,6 @@ class CustomButtonCell: NSButtonCell { } else { self.isBordered = false } - - self.font = NSFont.systemFont(ofSize: 15, weight: .regular ) - } required init(coder: NSCoder) { @@ -105,3 +102,18 @@ class CustomButtonCell: NSButtonCell { } } +extension NSButton { + var title: String { + get { + return "" + } + + set (newTitle) { + let attrTitle = NSMutableAttributedString(string: newTitle as String, attributes: [NSAttributedStringKey.foregroundColor: NSColor.white, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 15, weight: .regular)]) + attrTitle.setAlignment(.center, range: NSRange(location: 0, length: newTitle.count)) + + self.attributedTitle = attrTitle + } + } +} + diff --git a/MTMR/Widgets/CurrencyBarItem.swift b/MTMR/Widgets/CurrencyBarItem.swift index 7322499..f648db3 100644 --- a/MTMR/Widgets/CurrencyBarItem.swift +++ b/MTMR/Widgets/CurrencyBarItem.swift @@ -107,14 +107,10 @@ class CurrencyBarItem: CustomButtonTouchBarItem { } self.oldValue = value - button.title = String(format: "%@%.2f", self.prefix, value) - - let textRange = NSRange(location: 0, length: button.title.count) - let newTitle = NSMutableAttributedString(string: button.title) - newTitle.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: textRange) - newTitle.addAttribute(NSAttributedStringKey.font, value: button.font!, range: textRange) - newTitle.setAlignment(.center, range: textRange) + let title = String(format: "%@%.2f", self.prefix, value) + let newTitle = NSMutableAttributedString(string: title as String, attributes: [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 15, weight: .regular)]) + newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count)) button.attributedTitle = newTitle } } From 5f02bb2962d8988f8033321bbb422e6995d90359 Mon Sep 17 00:00:00 2001 From: ad Date: Fri, 27 Apr 2018 14:32:07 +0300 Subject: [PATCH 05/13] * font style --- MTMR/CustomButtonTouchBarItem.swift | 3 ++- MTMR/Widgets/CurrencyBarItem.swift | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index b89f130..6a14353 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -25,6 +25,7 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat button.cell = CustomButtonCell(backgroundColor: bezelColor!) button.cell?.title = title + button.title = title button.bezelColor = bezelColor self.view = button @@ -105,7 +106,7 @@ class CustomButtonCell: NSButtonCell { extension NSButton { var title: String { get { - return "" + return (self.cell?.title)! } set (newTitle) { diff --git a/MTMR/Widgets/CurrencyBarItem.swift b/MTMR/Widgets/CurrencyBarItem.swift index f648db3..2a4b512 100644 --- a/MTMR/Widgets/CurrencyBarItem.swift +++ b/MTMR/Widgets/CurrencyBarItem.swift @@ -109,7 +109,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem { let title = String(format: "%@%.2f", self.prefix, value) - let newTitle = NSMutableAttributedString(string: title as String, attributes: [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 15, weight: .regular)]) + let newTitle = NSMutableAttributedString(string: title as String, attributes: [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: button.attributedTitle.attribute(NSAttributedStringKey.font, at: 0, effectiveRange: nil)]) newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count)) button.attributedTitle = newTitle } From d1a577868ba612aeca893f51a6de388bcea72dd8 Mon Sep 17 00:00:00 2001 From: ad Date: Fri, 27 Apr 2018 16:17:40 +0300 Subject: [PATCH 06/13] * button background --- MTMR/CustomButtonTouchBarItem.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 6a14353..88f52ad 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -106,7 +106,7 @@ class CustomButtonCell: NSButtonCell { extension NSButton { var title: String { get { - return (self.cell?.title)! + return ""// (self.cell?.title)! } set (newTitle) { From 8031108fc99afa7a0945296178b9185360ee9f50 Mon Sep 17 00:00:00 2001 From: ad Date: Fri, 27 Apr 2018 18:46:29 +0300 Subject: [PATCH 07/13] * font baseline like in original touchbar, styled battery widget (time showing as superscript) --- MTMR/CustomButtonTouchBarItem.swift | 2 +- MTMR/Widgets/BatteryBarItem.swift | 33 +++++++++++++---------------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 88f52ad..fcf5c80 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -110,7 +110,7 @@ extension NSButton { } set (newTitle) { - let attrTitle = NSMutableAttributedString(string: newTitle as String, attributes: [NSAttributedStringKey.foregroundColor: NSColor.white, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 15, weight: .regular)]) + let attrTitle = NSMutableAttributedString(string: newTitle as String, attributes: [NSAttributedStringKey.foregroundColor: NSColor.white, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 15, weight: .regular), NSAttributedStringKey.baselineOffset: 1]) attrTitle.setAlignment(.center, range: NSRange(location: 0, length: newTitle.count)) self.attributedTitle = attrTitle diff --git a/MTMR/Widgets/BatteryBarItem.swift b/MTMR/Widgets/BatteryBarItem.swift index 1517266..ef9462d 100644 --- a/MTMR/Widgets/BatteryBarItem.swift +++ b/MTMR/Widgets/BatteryBarItem.swift @@ -108,13 +108,11 @@ class BatteryInfo: NSObject { func getFormattedTime(time: Int) -> String { if (time > 0) { - let timeFormatted = NSString(format: " (%d:%02d)", time / 60, time % 60) as String + let timeFormatted = NSString(format: " %d:%02d", time / 60, time % 60) as String return timeFormatted - } else if (time == 0) { - return "" } - return " (?)" + return "" } public func updateInfo() { @@ -122,27 +120,26 @@ class BatteryInfo: NSObject { self.getPSInfo() if ACPower == "AC Power" { - title += "⚡️" + if current < 100 { + title += "⚡️" + } timeRemaining = getFormattedTime(time: timeToFull) } else { timeRemaining = getFormattedTime(time: timeToEmpty) } - title += String(current) + "%" + timeRemaining - button?.title = title + title += String(current) + "%" - if current < 10 && ACPower != "AC Power" { - let pstyle = NSMutableParagraphStyle() - pstyle.alignment = .center - - button?.attributedTitle = NSMutableAttributedString( - string: title, - attributes: [ - NSAttributedStringKey.foregroundColor: NSColor.red, - NSAttributedStringKey.paragraphStyle: pstyle, - NSAttributedStringKey.font: NSFont.systemFont(ofSize: 16) - ]) + var color = NSColor.white + if current <= 10 && ACPower != "AC Power" { + color = NSColor.red } + + let newTitle = NSMutableAttributedString(string: title as String, attributes: [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: button?.attributedTitle.attribute(NSAttributedStringKey.font, at: 0, effectiveRange: nil), NSAttributedStringKey.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 } } From cf5d8bf546c34d841607583a505f5e3bac71e490 Mon Sep 17 00:00:00 2001 From: ad Date: Fri, 27 Apr 2018 19:03:58 +0300 Subject: [PATCH 08/13] * staticButton image size --- MTMR/TouchBarController.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 873c2a7..e35a3b1 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -206,9 +206,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem { let button = item.button! - button.image = source.image - button.imagePosition = .imageLeading - button.imageHugsTitle = true + button.cell?.image = source.image + button.cell?.image?.size = NSSize(width: 36, height: 36) button.bezelColor = .clear } return barItem From 55dc551516bda41d809003683c70c22a0d1d7695 Mon Sep 17 00:00:00 2001 From: ad Date: Fri, 27 Apr 2018 19:17:19 +0300 Subject: [PATCH 09/13] * fix for longAction --- MTMR/CustomButtonTouchBarItem.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index fcf5c80..636d3ce 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -72,10 +72,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat case .began: if self.longTapClosure != nil { hf.tap(strong: 2) - self.tapClosure() + self.longTapClosure() } else { hf.tap(strong: 6) - self.longTapClosure() + self.tapClosure() print("long click") } break From c76027ca9d78a63d4b38e56ac8a51065ec9a75a2 Mon Sep 17 00:00:00 2001 From: ad Date: Fri, 27 Apr 2018 20:18:22 +0300 Subject: [PATCH 10/13] * fix for button.image size --- MTMR/TouchBarController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index e35a3b1..2a3a927 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -206,8 +206,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem { let button = item.button! + button.imageScaling = .scaleProportionallyDown button.cell?.image = source.image - button.cell?.image?.size = NSSize(width: 36, height: 36) button.bezelColor = .clear } return barItem From b95f5af95f3b3991f7929d0357d113d8ad6ae3b5 Mon Sep 17 00:00:00 2001 From: Toxblh Date: Fri, 27 Apr 2018 19:13:55 +0100 Subject: [PATCH 11/13] lead position for image in image+title button --- MTMR/TouchBarController.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 2a3a927..c9a890b 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -207,6 +207,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem { let button = item.button! button.imageScaling = .scaleProportionallyDown + button.imagePosition = .imageLeading button.cell?.image = source.image button.bezelColor = .clear } From 5ed17906017499d6a40c74283f400f6e042665d6 Mon Sep 17 00:00:00 2001 From: ad Date: Fri, 27 Apr 2018 21:53:27 +0300 Subject: [PATCH 12/13] * scrollView baseline fix --- MTMR/AppleScriptTouchBarItem.swift | 1 - MTMR/ScrollViewItem.swift | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/MTMR/AppleScriptTouchBarItem.swift b/MTMR/AppleScriptTouchBarItem.swift index 1f3af9b..183a3e2 100644 --- a/MTMR/AppleScriptTouchBarItem.swift +++ b/MTMR/AppleScriptTouchBarItem.swift @@ -14,7 +14,6 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem { return } self.script = script - button.bezelColor = .clear DispatchQueue.main.async { var error: NSDictionary? guard script.compileAndReturnError(&error) else { diff --git a/MTMR/ScrollViewItem.swift b/MTMR/ScrollViewItem.swift index 7fe49ea..8a8c961 100644 --- a/MTMR/ScrollViewItem.swift +++ b/MTMR/ScrollViewItem.swift @@ -10,6 +10,7 @@ class ScrollViewItem: NSCustomTouchBarItem { stackView.orientation = .horizontal let scrollView = NSScrollView(frame: CGRect(origin: .zero, size: stackView.fittingSize)) scrollView.documentView = stackView + scrollView.documentView?.bounds.origin = CGPoint(x: 0.0, y: -5.5) self.view = scrollView } From 479190d12b3a36c69246cdf56ca942bf6928d747 Mon Sep 17 00:00:00 2001 From: Toxblh Date: Fri, 27 Apr 2018 23:38:13 +0100 Subject: [PATCH 13/13] fix center line for apple script --- MTMR/ScrollViewItem.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MTMR/ScrollViewItem.swift b/MTMR/ScrollViewItem.swift index 8a8c961..850039b 100644 --- a/MTMR/ScrollViewItem.swift +++ b/MTMR/ScrollViewItem.swift @@ -10,7 +10,7 @@ class ScrollViewItem: NSCustomTouchBarItem { stackView.orientation = .horizontal let scrollView = NSScrollView(frame: CGRect(origin: .zero, size: stackView.fittingSize)) scrollView.documentView = stackView - scrollView.documentView?.bounds.origin = CGPoint(x: 0.0, y: -5.5) + scrollView.documentView?.bounds.origin = CGPoint(x: 0.0, y: -2.5) self.view = scrollView }