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

Merge branch 'master' into musicWidget

# Conflicts:
#	MTMR/CustomButtonTouchBarItem.swift
This commit is contained in:
Serg 2018-05-12 13:21:25 +07:00
commit b29c74ccca
14 changed files with 128 additions and 101 deletions

View File

@ -41,6 +41,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
TouchBarController.shared.createAndUpdatePreset()
}
@objc func toggleControlStrip(_ sender: Any?) {
TouchBarController.shared.controlStripState = !TouchBarController.shared.controlStripState
createMenu()
TouchBarController.shared.resetControlStrip()
}
@objc func openPreset(_ sender: Any?) {
let dialog = NSOpenPanel();
@ -71,6 +77,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
menu.addItem(withTitle: "Preferences", action: #selector(openPrefereces(_:)), keyEquivalent: ",")
menu.addItem(withTitle: "Reload Preset", action: #selector(updatePreset(_:)), keyEquivalent: "r")
menu.addItem(withTitle: "Open Preset", action: #selector(openPreset(_:)), keyEquivalent: "O")
menu.addItem(withTitle: TouchBarController.shared.controlStripState ? "Hide Control Strip" : "Show Control Strip" , action: #selector(toggleControlStrip(_:)), keyEquivalent: "T")
menu.addItem(NSMenuItem.separator())
menu.addItem(withTitle: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
statusItem.menu = menu

View File

@ -10,7 +10,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
super.init(identifier: identifier, title: "", onTap: onTap, onLongTap: onLongTap)
self.forceHideConstraint = self.view.widthAnchor.constraint(equalToConstant: 0)
guard let script = source.appleScript else {
button.title = "no script"
self.title = "no script"
return
}
self.script = script
@ -22,7 +22,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
print(error?.description ?? "unknown error")
#endif
DispatchQueue.main.async {
self.button.title = "error"
self.title = "error"
}
return
}
@ -40,7 +40,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
#endif
let scriptResult = self.execute()
DispatchQueue.main.async {
self.button.title = scriptResult
self.title = scriptResult
self.forceHideConstraint.isActive = scriptResult == ""
}
DispatchQueue.main.asyncAfter(deadline: .now() + self.interval) { [weak self] in

View File

@ -10,8 +10,6 @@
@interface MediaKeys : NSObject
+ (void)decreaseVolume;
+ (void)increaseVolume;
+ (void)muteVolume;
+ (void)HIDPostAuxKey:(UInt8)keyCode;
@end

View File

@ -31,7 +31,7 @@ static io_connect_t get_event_driver(void)
}
static void HIDPostAuxKey( const UInt8 auxKeyCode )
static void HIDReleaseAuxKey( const UInt8 auxKeyCode )
{
NXEventData event;
kern_return_t kr;
@ -52,16 +52,8 @@ static void HIDPostAuxKey( const UInt8 auxKeyCode )
kr = IOHIDPostEvent( get_event_driver(), NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
}
+ (void)decreaseVolume {
HIDPostAuxKey(NX_KEYTYPE_SOUND_DOWN);
}
+ (void)increaseVolume {
HIDPostAuxKey(NX_KEYTYPE_SOUND_UP);
}
+ (void)muteVolume {
HIDPostAuxKey(NX_KEYTYPE_MUTE);
+ (void)HIDPostAuxKey: (UInt8)keyCode {
HIDReleaseAuxKey(keyCode);
}
@end

View File

@ -9,9 +9,9 @@
import Cocoa
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
public var tapClosure: (() -> ())?
public var longTapClosure: (() -> ())?
private(set) var button: NSButton!
var tapClosure: (() -> ())?
var longTapClosure: (() -> ())?
private(set) var button: NSButton! //todo hide completely
private var singleClick: NSClickGestureRecognizer!
private var longClick: NSPressGestureRecognizer!
@ -19,9 +19,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> (), onLongTap callbackLong: @escaping () -> (), bezelColor: NSColor? = .clear) {
self.tapClosure = callback
self.longTapClosure = callbackLong
self.attributedTitle = title.defaultTouchbarAttributedString
super.init(identifier: identifier)
installButton(titled: title, bordered: true, backgroundColor: nil)
button = CustomHeightButton(title: title, target: nil, action: nil)
longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong))
longClick.allowedTouchTypes = .direct
@ -31,8 +32,8 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
singleClick.allowedTouchTypes = .direct
singleClick.delegate = self
self.view.addGestureRecognizer(longClick)
self.view.addGestureRecognizer(singleClick)
reinstallButton()
button.attributedTitle = attributedTitle
}
required init?(coder: NSCoder) {
@ -41,30 +42,50 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
var isBordered: Bool = true {
didSet {
installButton(titled: self.button.title, bordered: isBordered, backgroundColor: backgroundColor)
reinstallButton()
}
}
var backgroundColor: NSColor? {
didSet {
installButton(titled: self.button.title, bordered: isBordered, backgroundColor: backgroundColor)
reinstallButton()
}
}
private func installButton(titled title: String, bordered: Bool, backgroundColor: NSColor?) {
button = CustomHeightButton(title: title, target: nil, action: nil)
let cell = CustomButtonCell()
var title: String {
get {
return self.attributedTitle.string
}
set {
self.attributedTitle = newValue.defaultTouchbarAttributedString
}
}
var attributedTitle: NSAttributedString {
didSet {
self.button?.attributedTitle = attributedTitle
}
}
private func reinstallButton() {
let title = button.attributedTitle
let image = button.image
let cell = CustomButtonCell(parentItem: self)
button.cell = cell
if let color = backgroundColor {
cell.isBordered = true
button.bezelColor = color
cell.backgroundColor = color
} else {
button.isBordered = bordered
button.bezelStyle = bordered ? .rounded : .inline
button.isBordered = isBordered
button.bezelStyle = isBordered ? .rounded : .inline
}
button.title = title
button.attributedTitle = title
button.image = image
self.view = button
self.view.addGestureRecognizer(longClick)
self.view.addGestureRecognizer(singleClick)
}
func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: NSGestureRecognizer) -> Bool {
@ -117,15 +138,21 @@ class CustomHeightButton : NSButton {
}
class CustomButtonCell: NSButtonCell {
weak var parentItem: CustomButtonTouchBarItem?
init() {
init(parentItem: CustomButtonTouchBarItem) {
super.init(textCell: "")
self.parentItem = parentItem
}
override func highlight(_ flag: Bool, withFrame cellFrame: NSRect, in controlView: NSView) {
super.highlight(flag, withFrame: cellFrame, in: controlView)
if !self.isBordered {
self.setTitle(self.title, withColor: flag ? .lightGray : .white)
if flag {
self.setAttributedTitle(self.attributedTitle, withColor: .lightGray)
} else if let parentItem = self.parentItem {
self.attributedTitle = parentItem.attributedTitle
}
}
}
@ -133,22 +160,19 @@ class CustomButtonCell: NSButtonCell {
fatalError("init(coder:) has not been implemented")
}
override var title: String! {
get {
return self.attributedTitle.string
}
set (newTitle) {
setTitle(newTitle, withColor: .white)
}
}
func setTitle(_ title: String, withColor color: NSColor) {
let attrTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1])
attrTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count))
func setAttributedTitle(_ title: NSAttributedString, withColor color: NSColor) {
let attrTitle = NSMutableAttributedString(attributedString: title)
attrTitle.addAttributes([.foregroundColor: color], range: NSRange(location: 0, length: attrTitle.length))
self.attributedTitle = attrTitle
}
}
extension String {
var defaultTouchbarAttributedString: NSAttributedString {
let attrTitle = NSMutableAttributedString(string: self, attributes: [.foregroundColor: NSColor.white, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1])
attrTitle.setAlignment(.center, range: NSRange(location: 0, length: self.count))
return attrTitle
}
}

View File

@ -47,11 +47,11 @@ class SupportedTypesHolder {
"delete": { _ in return (item: .staticButton(title: "del"), action: .keyPress(keycode: 117), longAction: .none, parameters: [:])},
"brightnessUp": { _ in
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessUp"))
return (item: .staticButton(title: ""), action: .keyPress(keycode: 113), longAction: .none, parameters: [.image: imageParameter])
return (item: .staticButton(title: ""), action: .keyPress(keycode: 144), longAction: .none, parameters: [.image: imageParameter])
},
"brightnessDown": { _ in
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessDown"))
return (item: .staticButton(title: ""), action: .keyPress(keycode: 107), longAction: .none, parameters: [.image: imageParameter])
return (item: .staticButton(title: ""), action: .keyPress(keycode: 145), longAction: .none, parameters: [.image: imageParameter])
},
"volumeDown": { _ in
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarVolumeDownTemplate)!)

View File

@ -29,24 +29,7 @@ extension KeyPress {
}
}
func doKey(_ key: UInt16, down: Bool) {
let ev = NSEvent.keyEvent(
with: down ? .keyDown : .keyUp,
location: .zero,
modifierFlags: [],
timestamp: TimeInterval(0),
windowNumber: 0,
context: nil,
characters: "",
charactersIgnoringModifiers: "",
isARepeat: false,
keyCode: key)
let cev = ev!.cgEvent!
cev.post(tap: CGEventTapLocation(rawValue: 0)!)
}
func HIDPostAuxKey(_ key: Int32) {
let key = UInt16(key)
doKey(key, down: true)
doKey(key, down: false)
let key = UInt8(key)
MediaKeys.hidPostAuxKey(key)
}

View File

@ -63,6 +63,15 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
var scrollArea: NSCustomTouchBarItem?
var centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString))
var controlStripState: Bool {
get {
return UserDefaults.standard.bool(forKey: "com.toxblh.mtmr.settings.showControlStrip")
}
set {
UserDefaults.standard.set(!controlStripState, forKey: "com.toxblh.mtmr.settings.showControlStrip")
}
}
private override init() {
super.init()
SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in self?.dismissTouchBar()}), longAction: .none)
@ -154,7 +163,16 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
}
@objc private func presentTouchBar() {
NSTouchBar.presentSystemModalFunctionBar(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem)
if self.controlStripState {
NSTouchBar.presentSystemModalFunctionBar(touchBar, systemTrayItemIdentifier: .controlStripItem)
} else {
NSTouchBar.presentSystemModalFunctionBar(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem)
}
}
@objc func resetControlStrip() {
NSTouchBar.minimizeSystemModalFunctionBar(self.touchBar)
presentTouchBar()
}
@objc private func dismissTouchBar() {
@ -212,21 +230,21 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
barItem = MusicBarItem(identifier: identifier, interval: interval, onLongTap: longAction)
}
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
widthBarItem.setWidth(value: value)
}
if case .bordered(let bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem {
item.isBordered = bordered
}
if case .background(let color)? = item.additionalParameters[.background], let item = barItem as? CustomButtonTouchBarItem {
item.backgroundColor = color
}
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
widthBarItem.setWidth(value: value)
}
if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem {
let button = item.button!
button.imageScaling = .scaleProportionallyDown
button.imagePosition = .imageLeading
if (button.title == "") {
if (item.title == "") {
button.imagePosition = .imageOnly
}

View File

@ -10,20 +10,28 @@ import IOKit.ps
import Foundation
class BatteryBarItem: CustomButtonTouchBarItem {
private var timer: Timer!
private let batteryInfo = BatteryInfo()
init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
super.init(identifier: identifier, title: " ", onTap: onTap, onLongTap: onLongTap)
self.view = button
let batteryInfo = BatteryInfo(button: button)
batteryInfo.start()
batteryInfo.updateInfo()
batteryInfo.start { [weak self] in
self?.refresh()
}
self.refresh()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func refresh() {
self.attributedTitle = self.batteryInfo.formattedInfo()
}
deinit {
batteryInfo.stop()
}
}
class BatteryInfo: NSObject {
@ -34,17 +42,11 @@ class BatteryInfo: NSObject {
var isCharging: Bool = false
var ACPower: String = ""
var timeRemaining: String = ""
var button: NSButton
var notifyBlock: ()->() = {}
var loop:CFRunLoopSource?
init(button: NSButton) {
self.button = button
super.init()
self.start()
}
func start() {
func start(notifyBlock: @escaping ()->()) {
self.notifyBlock = notifyBlock
let opaque = Unmanaged.passRetained(self).toOpaque()
let context = UnsafeMutableRawPointer(opaque)
loop = IOPSNotificationCreateRunLoopSource({ (context) in
@ -53,16 +55,17 @@ class BatteryInfo: NSObject {
}
let watcher = Unmanaged<BatteryInfo>.fromOpaque(ctx).takeUnretainedValue()
watcher.updateInfo()
}, context).takeRetainedValue() as CFRunLoopSource
watcher.notifyBlock()
}, context).takeRetainedValue() as CFRunLoopSource
CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode)
}
func stop() {
if !(self.loop != nil) {
self.notifyBlock = {}
guard let loop = self.loop else {
return
}
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), self.loop, CFRunLoopMode.defaultMode)
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode)
self.loop = nil
}
@ -114,7 +117,7 @@ class BatteryInfo: NSObject {
return ""
}
public func updateInfo() {
public func formattedInfo() -> NSAttributedString {
var title = ""
self.getPSInfo()
@ -134,12 +137,11 @@ class BatteryInfo: NSObject {
color = NSColor.red
}
let regularFont = button.attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15)
let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont, .baselineOffset: 1])
let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: NSFont.systemFont(ofSize: 15), .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
return newTitle
}
}

View File

@ -109,9 +109,9 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
let title = String(format: "%@%.2f", self.prefix, value)
let regularFont = button.attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15)
let regularFont = self.attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15)
let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont])
newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count))
button.attributedTitle = newTitle
self.attributedTitle = newTitle
}
}

View File

@ -58,8 +58,9 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
if (iconImage != nil) {
self.button.cell?.image = iconImage
self.button.cell?.image?.size = buttonSize
self.title = ""
} else {
self.button.title = currentSource.name
self.title = currentSource.name
}
}

View File

@ -19,7 +19,7 @@ class TimeTouchBarItem: CustomButtonTouchBarItem {
}
@objc func updateTime() {
button.title = self.dateFormatter.string(from: Date())
self.title = self.dateFormatter.string(from: Date())
}
}

View File

@ -117,7 +117,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
}
func setWeather(text: String) {
button.title = text
self.title = text
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

View File

@ -16,7 +16,9 @@ cd Release
rm -r App.xcarchive
# Prerequisite: npm i -g create-dmg
create-dmg "${NAME}.app"
NAME_DMG="${NAME}.app"
echo $NAME_DMG
create-dmg $NAME_DMG
VERSION=`mdls -raw -name kMDItemVersion ${NAME}.app`