From 8031108fc99afa7a0945296178b9185360ee9f50 Mon Sep 17 00:00:00 2001 From: ad Date: Fri, 27 Apr 2018 18:46:29 +0300 Subject: [PATCH] * 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 } }