1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 17:38:38 +00:00

Merge pull request #49 from ad/master

* NSButtonCell
This commit is contained in:
Anton Palgunov 2018-04-27 23:39:02 +01:00 committed by GitHub
commit 6609a7c806
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 80 additions and 40 deletions

View File

@ -14,7 +14,6 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
return return
} }
self.script = script self.script = script
button.bezelColor = .clear
DispatchQueue.main.async { DispatchQueue.main.async {
var error: NSDictionary? var error: NSDictionary?
guard script.compileAndReturnError(&error) else { guard script.compileAndReturnError(&error) else {

View File

@ -22,8 +22,12 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
super.init(identifier: identifier) super.init(identifier: identifier)
button = NSButton(title: title, target: self, action: nil) button = NSButton(title: title, target: self, action: nil)
button.bezelColor = bezelColor
button.cell = CustomButtonCell(backgroundColor: bezelColor!)
button.cell?.title = title
button.title = title button.title = title
button.bezelColor = bezelColor
self.view = button self.view = button
longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong)) longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong))
@ -68,10 +72,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
case .began: case .began:
if self.longTapClosure != nil { if self.longTapClosure != nil {
hf.tap(strong: 2) hf.tap(strong: 2)
self.tapClosure() self.longTapClosure()
} else { } else {
hf.tap(strong: 6) hf.tap(strong: 6)
self.longTapClosure() self.tapClosure()
print("long click") print("long click")
} }
break 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
}
}
}

View File

@ -49,19 +49,19 @@ class CustomSliderCell: NSSliderCell {
override func drawBar(inside aRect: NSRect, flipped: Bool) { override func drawBar(inside aRect: NSRect, flipped: Bool) {
_barRect = aRect _barRect = aRect
var rect = aRect
rect.size.height = CGFloat(4)
let barRadius = CGFloat(2) 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 leftRect = rect var bgRect = aRect
leftRect.size.width = finalWidth bgRect.size.height = CGFloat(4)
let bg = NSBezierPath(roundedRect: rect, xRadius: barRadius, yRadius: barRadius)
let bg = NSBezierPath(roundedRect: bgRect, xRadius: barRadius, yRadius: barRadius)
NSColor.lightGray.setFill() NSColor.lightGray.setFill()
bg.fill() 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() NSColor.darkGray.setFill()
active.fill() active.fill()
} }

View File

@ -10,6 +10,7 @@ class ScrollViewItem: NSCustomTouchBarItem {
stackView.orientation = .horizontal stackView.orientation = .horizontal
let scrollView = NSScrollView(frame: CGRect(origin: .zero, size: stackView.fittingSize)) let scrollView = NSScrollView(frame: CGRect(origin: .zero, size: stackView.fittingSize))
scrollView.documentView = stackView scrollView.documentView = stackView
scrollView.documentView?.bounds.origin = CGPoint(x: 0.0, y: -2.5)
self.view = scrollView self.view = scrollView
} }

View File

@ -206,9 +206,9 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem { if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem {
let button = item.button! let button = item.button!
button.image = source.image button.imageScaling = .scaleProportionallyDown
button.imagePosition = .imageLeading button.imagePosition = .imageLeading
button.imageHugsTitle = true button.cell?.image = source.image
button.bezelColor = .clear button.bezelColor = .clear
} }
return barItem return barItem

View File

@ -108,13 +108,11 @@ class BatteryInfo: NSObject {
func getFormattedTime(time: Int) -> String { func getFormattedTime(time: Int) -> String {
if (time > 0) { 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 return timeFormatted
} else if (time == 0) {
return ""
} }
return " (?)" return ""
} }
public func updateInfo() { public func updateInfo() {
@ -122,27 +120,26 @@ class BatteryInfo: NSObject {
self.getPSInfo() self.getPSInfo()
if ACPower == "AC Power" { if ACPower == "AC Power" {
if current < 100 {
title += "⚡️" title += "⚡️"
}
timeRemaining = getFormattedTime(time: timeToFull) timeRemaining = getFormattedTime(time: timeToFull)
} else { } else {
timeRemaining = getFormattedTime(time: timeToEmpty) timeRemaining = getFormattedTime(time: timeToEmpty)
} }
title += String(current) + "%" + timeRemaining title += String(current) + "%"
button?.title = title
if current < 10 && ACPower != "AC Power" { var color = NSColor.white
let pstyle = NSMutableParagraphStyle() if current <= 10 && ACPower != "AC Power" {
pstyle.alignment = .center color = NSColor.red
button?.attributedTitle = NSMutableAttributedString(
string: title,
attributes: [
NSAttributedStringKey.foregroundColor: NSColor.red,
NSAttributedStringKey.paragraphStyle: pstyle,
NSAttributedStringKey.font: NSFont.systemFont(ofSize: 16)
])
} }
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
} }
} }

View File

@ -107,14 +107,10 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
} }
self.oldValue = value self.oldValue = value
button.title = String(format: "%@%.2f", self.prefix, value) let 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 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 button.attributedTitle = newTitle
} }
} }

View File

@ -11,6 +11,7 @@ import Cocoa
class InputSourceBarItem: CustomButtonTouchBarItem { class InputSourceBarItem: CustomButtonTouchBarItem {
fileprivate var notificationCenter: CFNotificationCenter fileprivate var notificationCenter: CFNotificationCenter
let buttonSize = NSSize(width: 21, height: 21)
init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) { init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
notificationCenter = CFNotificationCenterGetDistributedCenter(); notificationCenter = CFNotificationCenterGetDistributedCenter();
@ -19,13 +20,22 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
observeIputSourceChangedNotification(); observeIputSourceChangedNotification();
textInputSourceDidChange() textInputSourceDidChange()
self.button.cell?.action = #selector(switchInputSource)
self.button.action = #selector(switchInputSource) self.button.action = #selector(switchInputSource)
self.button.frame.size = buttonSize
self.button.bounds.size = buttonSize
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
@objc override func handleGestureSingle(gr: NSClickGestureRecognizer) {
super.handleGestureSingle(gr: gr)
switchInputSource()
}
@objc public func textInputSourceDidChange() { @objc public func textInputSourceDidChange() {
let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue() let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue()
@ -42,7 +52,8 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
} }
if (iconImage != nil) { if (iconImage != nil) {
self.button.image = iconImage self.button.cell?.image = iconImage
self.button.cell?.image?.size = buttonSize
} else { } else {
self.button.title = currentSource.name self.button.title = currentSource.name
} }