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

Merge pull request #193 from bobrosoft:master

MusicBarItem: fix for latest macOS + add "disableMarquee" param
This commit is contained in:
Anton Palgunov 2019-07-23 13:36:28 +01:00 committed by GitHub
commit 000b825ec9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 49 deletions

View File

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

View File

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

View File

@ -10,21 +10,32 @@ 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 let disableMarquee: Bool
private var songTitle: String?
private var timer: Timer?
let buttonSize = NSSize(width: 21, height: 21)
private let iconSize = NSSize(width: 21, height: 21)
let playerBundleIdentifiers = [
"com.apple.iTunes",
"com.spotify.client",
"com.coppertino.Vox",
"com.google.Chrome",
"com.apple.Safari",
]
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
@ -50,21 +61,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 +95,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 +122,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 +181,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 +210,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 +230,6 @@ class MusicBarItem: CustomButtonTouchBarItem {
}
}
}
if tempTitle == "" {
ident = ""
}
}
if tempTitle == self.songTitle {
@ -234,16 +239,22 @@ 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 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
}

View File

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