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/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 671d20f..636d3ce 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -22,8 +22,12 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat super.init(identifier: identifier) button = NSButton(title: title, target: self, action: nil) - button.bezelColor = bezelColor + + button.cell = CustomButtonCell(backgroundColor: bezelColor!) + button.cell?.title = title button.title = title + + button.bezelColor = bezelColor self.view = button longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong)) @@ -68,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 @@ -82,3 +86,35 @@ 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 + } + } + + required init(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } +} + +extension NSButton { + var title: String { + get { + return ""// (self.cell?.title)! + } + + set (newTitle) { + 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/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/ScrollViewItem.swift b/MTMR/ScrollViewItem.swift index 7fe49ea..850039b 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: -2.5) self.view = scrollView } diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 873c2a7..c9a890b 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -206,9 +206,9 @@ 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.imageScaling = .scaleProportionallyDown button.imagePosition = .imageLeading - button.imageHugsTitle = true + button.cell?.image = source.image button.bezelColor = .clear } return barItem 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 } } diff --git a/MTMR/Widgets/CurrencyBarItem.swift b/MTMR/Widgets/CurrencyBarItem.swift index 7322499..2a4b512 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: button.attributedTitle.attribute(NSAttributedStringKey.font, at: 0, effectiveRange: nil)]) + newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count)) button.attributedTitle = newTitle } } diff --git a/MTMR/Widgets/InputSourceBarItem.swift b/MTMR/Widgets/InputSourceBarItem.swift index d131111..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(); @@ -19,12 +20,21 @@ class InputSourceBarItem: CustomButtonTouchBarItem { observeIputSourceChangedNotification(); textInputSourceDidChange() + 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) { 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() @@ -42,7 +52,8 @@ class InputSourceBarItem: CustomButtonTouchBarItem { } if (iconImage != nil) { - self.button.image = iconImage + self.button.cell?.image = iconImage + self.button.cell?.image?.size = buttonSize } else { self.button.title = currentSource.name }