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

Add support for multiple actions and same trigger

This commit is contained in:
Matteo Piccina 2020-08-02 15:12:57 +02:00
parent f25fe57c7c
commit 3f0d9da721
11 changed files with 62 additions and 39 deletions

View File

@ -8,13 +8,25 @@
import Cocoa import Cocoa
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate { struct ItemAction {
typealias TriggerClosure = (() -> Void)? typealias TriggerClosure = (() -> Void)?
var actions: [Action.Trigger: TriggerClosure] = [:] {
let trigger: Action.Trigger
let closure: TriggerClosure
init(trigger: Action.Trigger, _ closure: TriggerClosure) {
self.trigger = trigger
self.closure = closure
}
}
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
var actions: [ItemAction] = [] {
didSet { didSet {
multiClick.isDoubleClickEnabled = actions[.doubleTap] != nil multiClick.isDoubleClickEnabled = actions.filter({ $0.trigger == .doubleTap }).count > 0
multiClick.isTripleClickEnabled = actions[.tripleTap] != nil multiClick.isTripleClickEnabled = actions.filter({ $0.trigger == .tripleTap }).count > 0
longClick.isEnabled = actions[.longTap] != nil longClick.isEnabled = actions.filter({ $0.trigger == .longTap }).count > 0
} }
} }
var finishViewConfiguration: ()->() = {} var finishViewConfiguration: ()->() = {}
@ -123,26 +135,29 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
return true return true
} }
func callActions(for trigger: Action.Trigger) {
let itemActions = self.actions.filter { $0.trigger == trigger }
for itemAction in itemActions {
itemAction.closure?()
}
}
@objc func handleGestureSingleTap() { @objc func handleGestureSingleTap() {
guard let singleTap = self.actions[.singleTap] else { return } callActions(for: .singleTap)
singleTap?()
} }
@objc func handleGestureDoubleTap() { @objc func handleGestureDoubleTap() {
guard let doubleTap = self.actions[.doubleTap] else { return } callActions(for: .doubleTap)
doubleTap?()
} }
@objc func handleGestureTripleTap() { @objc func handleGestureTripleTap() {
guard let tripleTap = self.actions[.tripleTap] else { return } callActions(for: .tripleTap)
tripleTap?()
} }
@objc func handleGestureLong(gr: NSPressGestureRecognizer) { @objc func handleGestureLong(gr: NSPressGestureRecognizer) {
switch gr.state { switch gr.state {
case .possible: // tiny hack because we're calling action manually case .possible: // tiny hack because we're calling action manually
guard let longTap = self.actions[.longTap] else { return } callActions(for: .longTap)
longTap?()
break break
default: default:
break break

View File

@ -323,15 +323,15 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
if let action = self.action(forItem: item), let item = barItem as? CustomButtonTouchBarItem { if let action = self.action(forItem: item), let item = barItem as? CustomButtonTouchBarItem {
item.actions[.singleTap] = action item.actions.append(ItemAction(trigger: .singleTap, action))
} }
if let longAction = self.longAction(forItem: item), let item = barItem as? CustomButtonTouchBarItem { if let longAction = self.longAction(forItem: item), let item = barItem as? CustomButtonTouchBarItem {
item.actions[.longTap] = longAction item.actions.append(ItemAction(trigger: .longTap, longAction))
} }
if let touchBarItem = barItem as? CustomButtonTouchBarItem { if let touchBarItem = barItem as? CustomButtonTouchBarItem {
for action in item.actions { for action in item.actions {
touchBarItem.actions[action.trigger] = self.closure(for: action) touchBarItem.actions.append(ItemAction(trigger: action.trigger, self.closure(for: action)))
} }
} }
if case let .bordered(bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem { if case let .bordered(bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem {

View File

@ -82,12 +82,14 @@ class AppScrubberTouchBarItem: NSCustomTouchBarItem {
public func createAppButton(for app: DockItem) -> DockBarItem { public func createAppButton(for app: DockItem) -> DockBarItem {
let item = DockBarItem(app) let item = DockBarItem(app)
item.isBordered = false item.isBordered = false
item.actions[.singleTap] = { [weak self] in item.actions.append(contentsOf: [
self?.switchToApp(app: app) ItemAction(trigger: .singleTap) { [weak self] in
} self?.switchToApp(app: app)
item.actions[.longTap] = { [weak self] in },
self?.handleHalfLongPress(item: app) ItemAction(trigger: .longTap) { [weak self] in
} self?.handleHalfLongPress(item: app)
}
])
item.killAppClosure = {[weak self] in item.killAppClosure = {[weak self] in
self?.handleLongPress(item: app) self?.handleLongPress(item: app)
} }

View File

@ -11,7 +11,7 @@ class DarkModeBarItem: CustomButtonTouchBarItem, Widget {
isBordered = false isBordered = false
setWidth(value: 24) setWidth(value: 24)
actions[.singleTap] = { [weak self] in self?.DarkModeToggle() } actions.append(ItemAction(trigger: .singleTap) { [weak self] in self?.DarkModeToggle() })
timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(refresh), userInfo: nil, repeats: true) timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(refresh), userInfo: nil, repeats: true)

View File

@ -16,7 +16,7 @@ class DnDBarItem: CustomButtonTouchBarItem {
isBordered = false isBordered = false
setWidth(value: 32) setWidth(value: 32)
actions[.singleTap] = { [weak self] in self?.DnDToggle() } actions.append(ItemAction(trigger: .singleTap) { [weak self] in self?.DnDToggle() })
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(refresh), userInfo: nil, repeats: true) timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(refresh), userInfo: nil, repeats: true)

View File

@ -18,9 +18,10 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
observeIputSourceChangedNotification() observeIputSourceChangedNotification()
textInputSourceDidChange() textInputSourceDidChange()
actions[.singleTap] = { [weak self] in
actions.append(ItemAction(trigger: .singleTap) { [weak self] in
self?.switchInputSource() self?.switchInputSource()
} })
} }
required init?(coder _: NSCoder) { required init?(coder _: NSCoder) {

View File

@ -43,9 +43,9 @@ class MusicBarItem: CustomButtonTouchBarItem {
isBordered = false isBordered = false
actions = [ actions = [
.singleTap: { [weak self] in self?.playPause() }, ItemAction(trigger: .singleTap) { [weak self] in self?.playPause() },
.doubleTap: { [weak self] in self?.previousTrack() }, ItemAction(trigger: .doubleTap) { [weak self] in self?.previousTrack() },
.longTap: { [weak self] in self?.nextTrack() } ItemAction(trigger: .longTap) { [weak self] in self?.nextTrack() }
] ]
refreshAndSchedule() refreshAndSchedule()

View File

@ -31,7 +31,7 @@ class NightShiftBarItem: CustomButtonTouchBarItem {
isBordered = false isBordered = false
setWidth(value: 28) setWidth(value: 28)
actions[.singleTap] = { [weak self] in self?.nightShiftAction() } actions.append(ItemAction(trigger: .singleTap) { [weak self] in self?.nightShiftAction() })
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(refresh), userInfo: nil, repeats: true) timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(refresh), userInfo: nil, repeats: true)

View File

@ -51,10 +51,10 @@ class PomodoroBarItem: CustomButtonTouchBarItem, Widget {
self.workTime = workTime self.workTime = workTime
self.restTime = restTime self.restTime = restTime
super.init(identifier: identifier, title: defaultTitle) super.init(identifier: identifier, title: defaultTitle)
actions = [ actions.append(contentsOf: [
.singleTap: { [weak self] in self?.startStopWork() }, ItemAction(trigger: .singleTap) { [weak self] in self?.startStopWork() },
.longTap: { [weak self] in self?.startStopRest() } ItemAction(trigger: .longTap) { [weak self] in self?.startStopRest() }
] ])
} }
required init?(coder _: NSCoder) { required init?(coder _: NSCoder) {

View File

@ -75,9 +75,9 @@ class UpNextScrubberTouchBarItem: NSCustomTouchBarItem {
let item = UpNextItem(event: event) let item = UpNextItem(event: event)
item.backgroundColor = self.getBackgroundColor(startDate: event.startDate) item.backgroundColor = self.getBackgroundColor(startDate: event.startDate)
// Bind tap event // Bind tap event
item.actions[.singleTap] = { [weak self] in item.actions.append(ItemAction(trigger: .singleTap) { [weak self] in
self?.switchToApp(event: event) self?.switchToApp(event: event)
} })
// Add to view // Add to view
self.items.append(item) self.items.append(item)
// Check if should display any more // Check if should display any more

View File

@ -51,7 +51,12 @@ class YandexWeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate
manager.desiredAccuracy = kCLLocationAccuracyHundredMeters manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
manager.startUpdatingLocation() manager.startUpdatingLocation()
actions[.singleTap] = actions[.singleTap] ?? defaultTapAction if actions.filter({ $0.trigger == .singleTap }).isEmpty {
actions.append(ItemAction(
trigger: .singleTap,
defaultTapAction
))
}
} }
required init?(coder _: NSCoder) { required init?(coder _: NSCoder) {