From 705d0a64b52dbb2e2481246ad4b77cc300ccdcb8 Mon Sep 17 00:00:00 2001 From: bobrosoft Date: Wed, 17 Jul 2019 17:42:03 +0400 Subject: [PATCH 1/2] MusicBarItem: fix for latest macOS --- MTMR/Widgets/MusicBarItem.swift | 73 +++++++++++++++++---------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/MTMR/Widgets/MusicBarItem.swift b/MTMR/Widgets/MusicBarItem.swift index c6c1cab..3c69484 100644 --- a/MTMR/Widgets/MusicBarItem.swift +++ b/MTMR/Widgets/MusicBarItem.swift @@ -10,19 +10,28 @@ import Cocoa import ScriptingBridge class MusicBarItem: CustomButtonTouchBarItem { + private enum Player: String { + case iTunes = "com.apple.iTunes" + case Spotify = "com.spotify.client" + case VOX = "com.coppertino.Vox" + case Chrome = "com.google.Chrome" + case Safari = "com.apple.Safari" + } + + private let playerBundleIdentifiers = [ + Player.iTunes, + Player.Spotify, + Player.VOX, + Player.Chrome, + Player.Safari, + ] + private let interval: TimeInterval private var songTitle: String? private var timer: Timer? - let buttonSize = NSSize(width: 21, height: 21) - - let playerBundleIdentifiers = [ - "com.apple.iTunes", - "com.spotify.client", - "com.coppertino.Vox", - "com.google.Chrome", - "com.apple.Safari", - ] + private let iconSize = NSSize(width: 21, height: 21) + init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval) { self.interval = interval @@ -50,21 +59,21 @@ class MusicBarItem: CustomButtonTouchBarItem { @objc func playPause() { for ident in playerBundleIdentifiers { - if let musicPlayer = SBApplication(bundleIdentifier: ident) { + if let musicPlayer = SBApplication(bundleIdentifier: ident.rawValue) { if musicPlayer.isRunning { - if musicPlayer.className == "SpotifyApplication" { + if ident == .Spotify { let mp = (musicPlayer as SpotifyApplication) mp.playpause!() return - } else if musicPlayer.className == "ITunesApplication" { + } else if ident == .iTunes { let mp = (musicPlayer as iTunesApplication) mp.playpause!() return - } else if musicPlayer.className == "VOXApplication" { + } else if ident == .VOX { let mp = (musicPlayer as VoxApplication) mp.playpause!() return - } else if musicPlayer.className == "SafariApplication" { + } else if ident == .Safari { // You must enable the 'Allow JavaScript from Apple Events' option in Safari's Develop menu to use 'do JavaScript'. let safariApplication = musicPlayer as SafariApplication let safariWindows = safariApplication.windows?().compactMap({ $0 as? SafariWindow }) @@ -84,7 +93,7 @@ class MusicBarItem: CustomButtonTouchBarItem { } } } - // else if (musicPlayer.className == "GoogleChromeApplication") { + // else if (ident == .Chrome) { // let chromeApplication = musicPlayer as GoogleChromeApplication // let chromeWindows = chromeApplication.windows?().compactMap({ $0 as? GoogleChromeWindow }) // for window in chromeWindows! { @@ -111,24 +120,24 @@ class MusicBarItem: CustomButtonTouchBarItem { @objc func nextTrack() { for ident in playerBundleIdentifiers { - if let musicPlayer = SBApplication(bundleIdentifier: ident) { + if let musicPlayer = SBApplication(bundleIdentifier: ident.rawValue) { if musicPlayer.isRunning { - if musicPlayer.className == "SpotifyApplication" { + if ident == .Spotify { let mp = (musicPlayer as SpotifyApplication) mp.nextTrack!() updatePlayer() return - } else if musicPlayer.className == "ITunesApplication" { + } else if ident == .iTunes { let mp = (musicPlayer as iTunesApplication) mp.nextTrack!() updatePlayer() return - } else if musicPlayer.className == "VOXApplication" { + } else if ident == .VOX { let mp = (musicPlayer as VoxApplication) mp.next!() updatePlayer() return - } else if musicPlayer.className == "SafariApplication" { + } else if ident == .Safari { // You must enable the 'Allow JavaScript from Apple Events' option in Safari's Develop menu to use 'do JavaScript'. let safariApplication = musicPlayer as SafariApplication let safariWindows = safariApplication.windows?().compactMap({ $0 as? SafariWindow }) @@ -170,16 +179,16 @@ class MusicBarItem: CustomButtonTouchBarItem { var titleUpdated = false for var ident in playerBundleIdentifiers { - if let musicPlayer = SBApplication(bundleIdentifier: ident) { + if let musicPlayer = SBApplication(bundleIdentifier: ident.rawValue) { if musicPlayer.isRunning { var tempTitle = "" - if musicPlayer.className == "SpotifyApplication" { + if ident == .Spotify { tempTitle = (musicPlayer as SpotifyApplication).title - } else if musicPlayer.className == "ITunesApplication" { + } else if ident == .iTunes { tempTitle = (musicPlayer as iTunesApplication).title - } else if musicPlayer.className == "VOXApplication" { + } else if ident == .VOX { tempTitle = (musicPlayer as VoxApplication).title - } else if musicPlayer.className == "SafariApplication" { + } else if ident == .Safari { let safariApplication = musicPlayer as SafariApplication let safariWindows = safariApplication.windows?().compactMap({ $0 as? SafariWindow }) for window in safariWindows! { @@ -199,10 +208,7 @@ class MusicBarItem: CustomButtonTouchBarItem { } } } - if tempTitle == "" { - ident = "" - } - } else if musicPlayer.className == "GoogleChromeApplication" { + } else if ident == .Chrome { let chromeApplication = musicPlayer as GoogleChromeApplication let chromeWindows = chromeApplication.windows?().compactMap({ $0 as? GoogleChromeWindow }) for window in chromeWindows! { @@ -222,9 +228,6 @@ class MusicBarItem: CustomButtonTouchBarItem { } } } - if tempTitle == "" { - ident = "" - } } if tempTitle == self.songTitle { @@ -240,10 +243,10 @@ class MusicBarItem: CustomButtonTouchBarItem { self.timer = nil self.timer = Timer.scheduledTimer(timeInterval: 0.25, target: self, selector: #selector(self.marquee), userInfo: nil, repeats: true) } - if let ident = ident.ifNotEmpty, - let appPath = NSWorkspace.shared.absolutePathForApplication(withBundleIdentifier: ident) { + if let _ = tempTitle.ifNotEmpty, + let appPath = NSWorkspace.shared.absolutePathForApplication(withBundleIdentifier: ident.rawValue) { let image = NSWorkspace.shared.icon(forFile: appPath) - image.size = self.buttonSize + image.size = self.iconSize self.image = image iconUpdated = true } From 3432e24a55809990ae090f6ada37eb4e2d79ded4 Mon Sep 17 00:00:00 2001 From: bobrosoft Date: Thu, 18 Jul 2019 18:29:08 +0400 Subject: [PATCH 2/2] MusicBarItem: add support for "disableMarquee" param --- MTMR/ItemsParsing.swift | 13 ++++++++----- MTMR/TouchBarController.swift | 4 ++-- MTMR/Widgets/MusicBarItem.swift | 16 ++++++++++++---- README.md | 7 ++++--- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index fb3fd1c..569b357 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -274,11 +274,12 @@ class SupportedTypesHolder { ) }, "music": { decoder in - enum CodingKeys: String, CodingKey { case refreshInterval } + enum CodingKeys: String, CodingKey { case refreshInterval; case disableMarquee } let container = try decoder.container(keyedBy: CodingKeys.self) let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) + let disableMarquee = try container.decodeIfPresent(Bool.self, forKey: .disableMarquee) return ( - item: .music(interval: interval ?? 1800.00), + item: .music(interval: interval ?? 5.0, disableMarquee: disableMarquee ?? false), action: .none, longAction: .none, parameters: [:] @@ -336,7 +337,7 @@ enum ItemType: Decodable { case weather(interval: Double, units: String, api_key: String, icon_type: String) case currency(interval: Double, from: String, to: String, full: Bool) case inputsource() - case music(interval: Double) + case music(interval: Double, disableMarquee: Bool) case groupBar(items: [BarItemDefinition]) case nightShift() case dnd() @@ -365,6 +366,7 @@ enum ItemType: Decodable { case restTime case flip case autoResize + case disableMarquee } enum ItemTypeRaw: String, Decodable { @@ -437,8 +439,9 @@ enum ItemType: Decodable { self = .inputsource() case .music: - let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 1800.0 - self = .music(interval: interval) + let interval = try container.decodeIfPresent(Double.self, forKey: .refreshInterval) ?? 5.0 + let disableMarquee = try container.decodeIfPresent(Bool.self, forKey: .disableMarquee) ?? false + self = .music(interval: interval, disableMarquee: disableMarquee) case .groupBar: let items = try container.decode([BarItemDefinition].self, forKey: .items) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 1389460..7ad5b4c 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -273,8 +273,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to, full: full) case .inputsource(): barItem = InputSourceBarItem(identifier: identifier) - case let .music(interval: interval): - barItem = MusicBarItem(identifier: identifier, interval: interval) + case let .music(interval: interval, disableMarquee: disableMarquee): + barItem = MusicBarItem(identifier: identifier, interval: interval, disableMarquee: disableMarquee) case let .groupBar(items: items): barItem = GroupBarItem(identifier: identifier, items: items) case .nightShift(): diff --git a/MTMR/Widgets/MusicBarItem.swift b/MTMR/Widgets/MusicBarItem.swift index 3c69484..28a82b2 100644 --- a/MTMR/Widgets/MusicBarItem.swift +++ b/MTMR/Widgets/MusicBarItem.swift @@ -27,13 +27,15 @@ class MusicBarItem: CustomButtonTouchBarItem { ] private let interval: TimeInterval + private let disableMarquee: Bool private var songTitle: String? private var timer: Timer? private let iconSize = NSSize(width: 21, height: 21) - init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval) { + init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, disableMarquee: Bool) { self.interval = interval + self.disableMarquee = disableMarquee super.init(identifier: identifier, title: "⏳") isBordered = false @@ -237,11 +239,17 @@ class MusicBarItem: CustomButtonTouchBarItem { } if let songTitle = self.songTitle?.ifNotEmpty { - self.title = " " + songTitle + " " - titleUpdated = true self.timer?.invalidate() self.timer = nil - self.timer = Timer.scheduledTimer(timeInterval: 0.25, target: self, selector: #selector(self.marquee), userInfo: nil, repeats: true) + + if (disableMarquee) { + self.title = " " + songTitle + } else { + self.title = " " + songTitle + " " + self.timer = Timer.scheduledTimer(timeInterval: 0.25, target: self, selector: #selector(self.marquee), userInfo: nil, repeats: true) + } + + titleUpdated = true } if let _ = tempTitle.ifNotEmpty, let appPath = NSWorkspace.shared.absolutePathForApplication(withBundleIdentifier: ident.rawValue) { diff --git a/README.md b/README.md index 92166a6..a950b51 100644 --- a/README.md +++ b/README.md @@ -207,9 +207,10 @@ To close a group, use the button: { "type": "music", "align": "center", - "width": 80, - "bordered": false, - "refreshInterval": 2, // in seconds + "width": 80, // Optional + "bordered": false, // Optional + "refreshInterval": 2, // in seconds. Optional. Default 5 seconds + "disableMarquee": true // to disable marquee effect. Optional. Default false }, ```