@@ -22,14 +22,13 @@ class ViewController: NSViewController, NSUserInterfaceValidations, AudioControl
2222 private var lastTitle : String ?
2323 private var togglePlaybackMenuItemTitle : String ?
2424 private var togglePlaybackMenuItemIsEnabled : Bool ?
25- private var observationContext = 0
2625
2726 override func viewDidLoad( ) {
2827 super. viewDidLoad ( )
2928
3029 observeUserDefaultsController ( )
31-
3230 updateStatusBarItem ( )
31+ updateDockIcon ( )
3332
3433 indicateUnavailability ( )
3534
@@ -54,8 +53,6 @@ class ViewController: NSViewController, NSUserInterfaceValidations, AudioControl
5453 return AudioController ( url: url, delegate: self , delegateQueue: DispatchQueue . main, makeSession: makeSession)
5554 }
5655
57- // MARK: -
58-
5956 @IBAction func play( _ sender: NSButton ) {
6057 log. info ( " User pressed play " )
6158 prepareForPlaybackStart ( )
@@ -78,20 +75,11 @@ class ViewController: NSViewController, NSUserInterfaceValidations, AudioControl
7875 toggle ( )
7976 }
8077
81- @objc func toggleStatus( _ sender: NSStatusBarButton ) {
82- log. info ( " User clicked status bar toggle " )
83- if togglePlaybackMenuItemIsEnabled == true {
84- toggle ( )
85- }
86- }
87-
8878 private func toggle( ) {
8979 isPlaying ? prepareForPlaybackStop ( ) : prepareForPlaybackStart ( )
9080 audioController? . playPause ( )
9181 }
9282
93- // MARK: -
94-
9583 private func indicateUnavailability( ) {
9684 isPlaying = false
9785
@@ -223,8 +211,6 @@ class ViewController: NSViewController, NSUserInterfaceValidations, AudioControl
223211 fadeIn ( titleStackView)
224212 }
225213
226- // MARK: -
227-
228214 private func fadeOut( _ view: NSView , duration: TimeInterval = 2 , then: ( ( ) -> Void ) ? = nil ) {
229215 NSAnimationContext . runAnimationGroup ( { context in
230216 context. duration = duration
@@ -239,8 +225,6 @@ class ViewController: NSViewController, NSUserInterfaceValidations, AudioControl
239225 } , completionHandler: then)
240226 }
241227
242- // MARK: Music Menu
243-
244228 private func menuTitlePlay( ) -> String {
245229 return NSLocalizedString ( " Play " , comment: " Menu Item - Music > Play " )
246230 }
@@ -260,20 +244,29 @@ class ViewController: NSViewController, NSUserInterfaceValidations, AudioControl
260244 return true
261245 }
262246
263- // MARK: Status Bar Button
264-
265247 private func makeStatusButtonItem( ) -> NSStatusItem {
266- let statusBar = NSStatusBar . system
267-
268- let item = statusBar. statusItem ( withLength: NSStatusItem . squareLength)
269-
248+ let item = NSStatusBar . system. statusItem ( withLength: NSStatusItem . squareLength)
270249 item. image = #imageLiteral( resourceName: " StatusBarButton " )
271- item. action = #selector( toggleStatus ( _ : ) )
250+ item. action = #selector( statusBarEvent )
272251 item. target = self
273-
252+ item . button ? . sendAction ( on : [ . leftMouseUp , . rightMouseUp ] )
274253 return item
275254 }
276255
256+ @objc private func statusBarEvent( _ sender: NSStatusBarButton ) {
257+ if let event = NSApp . currentEvent {
258+ if event. modifierFlags. contains ( . control) || event. type == . rightMouseUp {
259+ configuration. hideDockIcon = false
260+ view. window? . makeKeyAndOrderFront ( self )
261+ view. window? . makeMain ( )
262+ } else {
263+ if togglePlaybackMenuItemIsEnabled == true {
264+ toggle ( )
265+ }
266+ }
267+ }
268+ }
269+
277270 private func removeStatusBarItem( ) {
278271 if let item = statusItem {
279272 item. statusBar? . removeStatusItem ( item)
@@ -299,15 +292,15 @@ class ViewController: NSViewController, NSUserInterfaceValidations, AudioControl
299292 }
300293
301294 private func updateStatusBarItem( ) {
302- if showStatusBarIcon {
295+ if configuration. hideStatusBarIcon {
296+ if statusItem != nil {
297+ removeStatusBarItem ( )
298+ }
299+ } else {
303300 if statusItem == nil {
304301 statusItem = makeStatusButtonItem ( )
305302 gleanStatusItemState ( )
306303 }
307- } else {
308- if statusItem != nil {
309- removeStatusBarItem ( )
310- }
311304 }
312305 }
313306
@@ -320,43 +313,52 @@ class ViewController: NSViewController, NSUserInterfaceValidations, AudioControl
320313 statusItem? . toolTip = lastTitle
321314 }
322315
323- // MARK: View Menu
316+ private func updateDockIcon( ) {
317+ if configuration. hideDockIcon {
318+ NSApp ? . setActivationPolicy ( . accessory)
319+ } else {
320+ NSApp ? . setActivationPolicy ( . regular)
321+ }
322+ }
324323
325- let showStatusBarIconKVOPath = " values.showStatusBarIcon "
324+ private var userDefaultsController = NSUserDefaultsController . shared
325+ // Apparently, cannot use Swift 4 KVO with NSUserDefaultsController,
326+ // so do it the old way.
327+ private var observationContext = 0
328+ private let hideStatusBarIconKVOPath = " values.hideStatusBarIcon "
329+ private let hideDockIconKVOPath = " values.hideDockIcon "
326330
327331 private func observeUserDefaultsController( ) {
328- NSUserDefaultsController . shared. addObserver ( self , forKeyPath: showStatusBarIconKVOPath, options: [ ] , context: & observationContext)
332+ userDefaultsController. addObserver ( self , forKeyPath: hideStatusBarIconKVOPath, options: [ ] , context: & observationContext)
333+ userDefaultsController. addObserver ( self , forKeyPath: hideDockIconKVOPath, options: [ ] , context: & observationContext)
329334 }
330335
331336 private func unobserveUserDefaultsController( ) {
332- NSUserDefaultsController . shared. removeObserver ( self , forKeyPath: showStatusBarIconKVOPath, context: & observationContext)
337+ userDefaultsController. removeObserver ( self , forKeyPath: hideStatusBarIconKVOPath, context: & observationContext)
338+ userDefaultsController. removeObserver ( self , forKeyPath: hideDockIconKVOPath, context: & observationContext)
333339 }
334340
335341 override func observeValue( forKeyPath keyPath: String ? , of object: Any ? , change: [ NSKeyValueChangeKey : Any ] ? , context: UnsafeMutableRawPointer ? ) {
336342 if context == & observationContext {
337- if keyPath == showStatusBarIconKVOPath {
343+ if keyPath == hideStatusBarIconKVOPath {
338344 updateStatusBarItem ( )
345+ } else if keyPath == hideDockIconKVOPath {
346+ updateDockIcon ( )
339347 }
340348 } else {
341349 super. observeValue ( forKeyPath: keyPath, of: object, change: change, context: context)
342350 }
343351 }
344352
345- var showNotifications : Bool {
346- return NSUserDefaultsController . shared. defaults. bool ( forKey: " showNotifications " )
347- }
348-
349- var showStatusBarIcon : Bool {
350- return NSUserDefaultsController . shared. defaults. bool ( forKey: " showStatusBarIcon " )
351- }
352-
353353 private func maybeShowNotification( _ titleComponents: TitleComponents ) {
354- if showNotifications {
355- let notification = NSUserNotification ( )
356- notification. title = titleComponents. song
357- notification. subtitle = titleComponents. artist
358- NSUserNotificationCenter . default. deliver ( notification)
354+ if configuration. hideNotifications {
355+ return
359356 }
357+
358+ let notification = NSUserNotification ( )
359+ notification. title = titleComponents. song
360+ notification. subtitle = titleComponents. artist
361+ NSUserNotificationCenter . default. deliver ( notification)
360362 }
361363
362364 @objc func paste( _ sender: Any ) {
0 commit comments