From 2c06d24276e0b6b3b41c515309337756c0a66bdf Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Thu, 7 Feb 2019 21:27:24 -0500 Subject: [PATCH] Change the queue icon depending on state --- .../Controllers/QueueViewController.swift | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Persephone/Controllers/QueueViewController.swift b/Persephone/Controllers/QueueViewController.swift index 4479085..2d09bb6 100644 --- a/Persephone/Controllers/QueueViewController.swift +++ b/Persephone/Controllers/QueueViewController.swift @@ -13,6 +13,8 @@ class QueueViewController: NSViewController, NSOutlineViewDataSource, NSOutlineV var queue: [MPDClient.Song] = [] var queuePos: Int32 = -1 + var queueIcon: NSImage? = nil + let systemFontRegular = NSFont.systemFont(ofSize: 13, weight: .regular) let systemFontBold = NSFont.systemFont(ofSize: 13, weight: .bold) @@ -26,6 +28,13 @@ class QueueViewController: NSViewController, NSOutlineViewDataSource, NSOutlineV queueView.columnAutoresizingStyle = .uniformColumnAutoresizingStyle + NotificationCenter.default.addObserver( + self, + selector: #selector(stateChanged(_:)), + name: MPDClient.stateChanged, + object: AppDelegate.mpdClient + ) + NotificationCenter.default.addObserver( self, selector: #selector(queueChanged(_:)), @@ -41,6 +50,13 @@ class QueueViewController: NSViewController, NSOutlineViewDataSource, NSOutlineV ) } + @objc func stateChanged(_ notification: Notification) { + guard let state = notification.userInfo?[MPDClient.stateKey] as? mpd_state + else { return } + + setQueueIcon(state) + } + @objc func queueChanged(_ notification: Notification) { guard let queue = notification.userInfo?[MPDClient.queueKey] as? [MPDClient.Song] else { return } @@ -66,6 +82,17 @@ class QueueViewController: NSViewController, NSOutlineViewDataSource, NSOutlineV ) } + func setQueueIcon(_ state: mpd_state) { + switch state { + case MPD_STATE_PLAY: + self.queueIcon = NSImage(named: NSImage.Name("playButton")) + case MPD_STATE_PAUSE: + self.queueIcon = NSImage(named: NSImage.Name("pauseButton")) + default: + self.queueIcon = nil + } + } + func setQueuePos(oldSongRowPos: Int, newSongRowPos: Int) { if oldSongRowPos > 0 { guard let oldSongRow = queueView.rowView(atRow: oldSongRowPos, makeIfNecessary: true) @@ -83,7 +110,7 @@ class QueueViewController: NSViewController, NSOutlineViewDataSource, NSOutlineV else { return } setRowFont(rowView: songRow, font: systemFontBold) - newSongTitleCell.imageView?.image = NSImage(named: NSImage.Name("playButton")) + newSongTitleCell.imageView?.image = self.queueIcon } func setRowFont(rowView: NSTableRowView, font: NSFont) {