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

Changed touchbarcontroller to load image in group item

This commit is contained in:
MaandagDev 2020-01-24 01:17:18 +01:00
parent 5f876acc72
commit 13cfab2e82

View File

@ -67,9 +67,9 @@ extension NSTouchBarItem.Identifier {
class TouchBarController: NSObject, NSTouchBarDelegate { class TouchBarController: NSObject, NSTouchBarDelegate {
static let shared = TouchBarController() static let shared = TouchBarController()
var touchBar: NSTouchBar! var touchBar: NSTouchBar!
fileprivate var lastPresetPath = "" fileprivate var lastPresetPath = ""
var jsonItems: [BarItemDefinition] = [] var jsonItems: [BarItemDefinition] = []
var itemDefinitions: [NSTouchBarItem.Identifier: BarItemDefinition] = [:] var itemDefinitions: [NSTouchBarItem.Identifier: BarItemDefinition] = [:]
@ -80,32 +80,32 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
var rightIdentifiers: [NSTouchBarItem.Identifier] = [] var rightIdentifiers: [NSTouchBarItem.Identifier] = []
var scrollArea: ScrollViewItem? var scrollArea: ScrollViewItem?
var centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString)) var centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString))
var blacklistAppIdentifiers: [String] = [] var blacklistAppIdentifiers: [String] = []
var frontmostApplicationIdentifier: String? { var frontmostApplicationIdentifier: String? {
return NSWorkspace.shared.frontmostApplication?.bundleIdentifier return NSWorkspace.shared.frontmostApplication?.bundleIdentifier
} }
private override init() { private override init() {
super.init() super.init()
SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in self?.dismissTouchBar() }), longAction: .none) SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in self?.dismissTouchBar() }), longAction: .none)
SupportedTypesHolder.sharedInstance.register(typename: "close") { _ in SupportedTypesHolder.sharedInstance.register(typename: "close") { _ in
(item: .staticButton(title: ""), action: .custom(closure: { [weak self] in (item: .staticButton(title: ""), action: .custom(closure: { [weak self] in
guard let `self` = self else { return } guard let `self` = self else { return }
self.reloadPreset(path: self.lastPresetPath) self.reloadPreset(path: self.lastPresetPath)
}), longAction: .none, parameters: [.width: .width(30), .image: .image(source: (NSImage(named: NSImage.stopProgressFreestandingTemplateName))!)]) }), longAction: .none, parameters: [.width: .width(30), .image: .image(source: (NSImage(named: NSImage.stopProgressFreestandingTemplateName))!)])
} }
blacklistAppIdentifiers = AppSettings.blacklistedAppIds blacklistAppIdentifiers = AppSettings.blacklistedAppIds
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didLaunchApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didLaunchApplicationNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didTerminateApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didTerminateApplicationNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didActivateApplicationNotification, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(activeApplicationChanged), name: NSWorkspace.didActivateApplicationNotification, object: nil)
reloadStandardConfig() reloadStandardConfig()
} }
func createAndUpdatePreset(newJsonItems: [BarItemDefinition]) { func createAndUpdatePreset(newJsonItems: [BarItemDefinition]) {
if let oldBar = self.touchBar { if let oldBar = self.touchBar {
minimizeSystemModal(oldBar) minimizeSystemModal(oldBar)
@ -117,29 +117,29 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
leftIdentifiers = [] leftIdentifiers = []
centerItems = [] centerItems = []
rightIdentifiers = [] rightIdentifiers = []
loadItemDefinitions(jsonItems: jsonItems) loadItemDefinitions(jsonItems: jsonItems)
createItems() createItems()
centerItems = centerIdentifiers.compactMap({ (identifier) -> NSTouchBarItem? in centerItems = centerIdentifiers.compactMap({ (identifier) -> NSTouchBarItem? in
items[identifier] items[identifier]
}) })
centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString)) centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString))
scrollArea = ScrollViewItem(identifier: centerScrollArea, items: centerItems) scrollArea = ScrollViewItem(identifier: centerScrollArea, items: centerItems)
scrollArea?.gesturesEnabled = AppSettings.multitouchGestures scrollArea?.gesturesEnabled = AppSettings.multitouchGestures
touchBar.delegate = self touchBar.delegate = self
touchBar.defaultItemIdentifiers = [] touchBar.defaultItemIdentifiers = []
touchBar.defaultItemIdentifiers = leftIdentifiers + [centerScrollArea] + rightIdentifiers touchBar.defaultItemIdentifiers = leftIdentifiers + [centerScrollArea] + rightIdentifiers
updateActiveApp() updateActiveApp()
} }
@objc func activeApplicationChanged(_: Notification) { @objc func activeApplicationChanged(_: Notification) {
updateActiveApp() updateActiveApp()
} }
func updateActiveApp() { func updateActiveApp() {
if frontmostApplicationIdentifier != nil && blacklistAppIdentifiers.firstIndex(of: frontmostApplicationIdentifier!) != nil { if frontmostApplicationIdentifier != nil && blacklistAppIdentifiers.firstIndex(of: frontmostApplicationIdentifier!) != nil {
dismissTouchBar() dismissTouchBar()
@ -147,7 +147,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
presentTouchBar() presentTouchBar()
} }
} }
func reloadStandardConfig() { func reloadStandardConfig() {
let presetPath = standardConfigPath let presetPath = standardConfigPath
if !FileManager.default.fileExists(atPath: presetPath), if !FileManager.default.fileExists(atPath: presetPath),
@ -155,16 +155,16 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
try? FileManager.default.createDirectory(atPath: appSupportDirectory, withIntermediateDirectories: true, attributes: nil) try? FileManager.default.createDirectory(atPath: appSupportDirectory, withIntermediateDirectories: true, attributes: nil)
try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath) try? FileManager.default.copyItem(atPath: defaultPreset, toPath: presetPath)
} }
reloadPreset(path: presetPath) reloadPreset(path: presetPath)
} }
func reloadPreset(path: String) { func reloadPreset(path: String) {
lastPresetPath = path lastPresetPath = path
let items = path.fileData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])] let items = path.fileData?.barItemDefinitions() ?? [BarItemDefinition(type: .staticButton(title: "bad preset"), action: .none, longAction: .none, additionalParameters: [:])]
createAndUpdatePreset(newJsonItems: items) createAndUpdatePreset(newJsonItems: items)
} }
func loadItemDefinitions(jsonItems: [BarItemDefinition]) { func loadItemDefinitions(jsonItems: [BarItemDefinition]) {
let dateFormatter = DateFormatter() let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH-mm-ss" dateFormatter.dateFormat = "HH-mm-ss"
@ -184,13 +184,13 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
} }
} }
func createItems() { func createItems() {
for (identifier, definition) in itemDefinitions { for (identifier, definition) in itemDefinitions {
items[identifier] = createItem(forIdentifier: identifier, definition: definition) items[identifier] = createItem(forIdentifier: identifier, definition: definition)
} }
} }
@objc func setupControlStripPresence() { @objc func setupControlStripPresence() {
DFRSystemModalShowsCloseBoxWhenFrontMost(false) DFRSystemModalShowsCloseBoxWhenFrontMost(false)
let item = NSCustomTouchBarItem(identifier: .controlStripItem) let item = NSCustomTouchBarItem(identifier: .controlStripItem)
@ -198,11 +198,11 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
NSTouchBarItem.addSystemTrayItem(item) NSTouchBarItem.addSystemTrayItem(item)
updateControlStripPresence() updateControlStripPresence()
} }
func updateControlStripPresence() { func updateControlStripPresence() {
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true) DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
} }
@objc private func presentTouchBar() { @objc private func presentTouchBar() {
if AppSettings.showControlStripState { if AppSettings.showControlStripState {
updateControlStripPresence() updateControlStripPresence()
@ -211,30 +211,30 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
presentSystemModal(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem) presentSystemModal(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem)
} }
} }
@objc private func dismissTouchBar() { @objc private func dismissTouchBar() {
minimizeSystemModal(touchBar) minimizeSystemModal(touchBar)
updateControlStripPresence() updateControlStripPresence()
} }
@objc func resetControlStrip() { @objc func resetControlStrip() {
dismissTouchBar() dismissTouchBar()
presentTouchBar() presentTouchBar()
} }
func touchBar(_: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? { func touchBar(_: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
if identifier == centerScrollArea { if identifier == centerScrollArea {
return scrollArea return scrollArea
} }
guard let item = self.items[identifier], guard let item = self.items[identifier],
let definition = self.itemDefinitions[identifier], let definition = self.itemDefinitions[identifier],
definition.align != .center else { definition.align != .center else {
return nil return nil
} }
return item return item
} }
func createItem(forIdentifier identifier: NSTouchBarItem.Identifier, definition item: BarItemDefinition) -> NSTouchBarItem? { func createItem(forIdentifier identifier: NSTouchBarItem.Identifier, definition item: BarItemDefinition) -> NSTouchBarItem? {
var barItem: NSTouchBarItem! var barItem: NSTouchBarItem!
switch item.type { switch item.type {
@ -293,7 +293,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
case .darkMode: case .darkMode:
barItem = DarkModeBarItem(identifier: identifier) barItem = DarkModeBarItem(identifier: identifier)
} }
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.tapClosure = action item.tapClosure = action
} }
@ -309,8 +309,12 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
if case let .width(value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth { if case let .width(value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
widthBarItem.setWidth(value: value) widthBarItem.setWidth(value: value)
} }
if case let .image(source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem { if case let .image(source)? = item.additionalParameters[.image] {
item.image = source.image if let item = barItem as? GroupBarItem {
item.collapsedRepresentationImage = source.image
} else if let item = barItem as? CustomButtonTouchBarItem {
item.image = source.image
}
} }
if case let .title(value)? = item.additionalParameters[.title] { if case let .title(value)? = item.additionalParameters[.title] {
if let item = barItem as? GroupBarItem { if let item = barItem as? GroupBarItem {
@ -321,7 +325,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
} }
return barItem return barItem
} }
func action(forItem item: BarItemDefinition) -> (() -> Void)? { func action(forItem item: BarItemDefinition) -> (() -> Void)? {
switch item.action { switch item.action {
case let .hidKey(keycode: keycode): case let .hidKey(keycode: keycode):
@ -353,7 +357,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
return { return {
if let url = URL(string: url), NSWorkspace.shared.open(url) { if let url = URL(string: url), NSWorkspace.shared.open(url) {
#if DEBUG #if DEBUG
print("URL was successfully opened") print("URL was successfully opened")
#endif #endif
} else { } else {
print("error", url) print("error", url)
@ -365,7 +369,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
return nil return nil
} }
} }
func longAction(forItem item: BarItemDefinition) -> (() -> Void)? { func longAction(forItem item: BarItemDefinition) -> (() -> Void)? {
switch item.longAction { switch item.longAction {
case let .hidKey(keycode: keycode): case let .hidKey(keycode: keycode):
@ -395,7 +399,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
return { return {
if let url = URL(string: url), NSWorkspace.shared.open(url) { if let url = URL(string: url), NSWorkspace.shared.open(url) {
#if DEBUG #if DEBUG
print("URL was successfully opened") print("URL was successfully opened")
#endif #endif
} else { } else {
print("error", url) print("error", url)