1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 01:18:39 +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
}
self.script = script
button.bezelColor = .clear
DispatchQueue.main.async {
var error: NSDictionary?
guard script.compileAndReturnError(&error) else {

View File

@ -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
}
}
}

View File

@ -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()
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}