1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00
persephone/Persephone/Controllers/QueueViewController.swift
Dan Barber 52b0a3b067
Add track progress bar
Adds a slider control that shows the track playback progress. The slider
can be used to seek through the track.
2019-02-22 20:59:01 -05:00

161 lines
4.2 KiB
Swift

//
// QueueController.swift
// Persephone
//
// Created by Daniel Barber on 2019/2/01.
// Copyright © 2019 Dan Barber. All rights reserved.
//
import Cocoa
class QueueViewController: NSViewController,
NSOutlineViewDelegate {
var dataSource = QueueDataSource()
@IBOutlet var queueView: NSOutlineView!
override func viewDidLoad() {
super.viewDidLoad()
setupNotificationObservers()
queueView.dataSource = dataSource
queueView.columnAutoresizingStyle = .sequentialColumnAutoresizingStyle
}
override func keyDown(with event: NSEvent) {
switch event.keyCode {
case NSEvent.keyCodeSpace:
nextResponder?.keyDown(with: event)
default:
super.keyDown(with: event)
}
}
@IBAction func playTrack(_ sender: Any) {
if dataSource.queuePos >= 0 {
AppDelegate.mpdClient.playTrack(queuePos: dataSource.queuePos)
}
}
@objc func stateChanged(_ notification: Notification) {
guard let state = notification.userInfo?[Notification.stateKey] as? MPDClient.Status.State
else { return }
dataSource.setQueueIcon(state)
}
@objc func queueChanged(_ notification: Notification) {
guard let queue = notification.userInfo?[Notification.queueKey] as? [MPDClient.Song]
else { return }
dataSource.updateQueue(queue)
queueView.reloadData()
}
@objc func queuePosChanged(_ notification: Notification) {
guard let queuePos = notification.userInfo?[Notification.queuePosKey] as? Int
else { return }
dataSource.setQueuePos(queuePos)
queueView.reloadData()
}
func outlineView(
_ outlineView: NSOutlineView,
selectionIndexesForProposedSelection proposedSelectionIndexes: IndexSet
) -> IndexSet {
if proposedSelectionIndexes.contains(0) {
return IndexSet()
} else {
return proposedSelectionIndexes
}
}
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
if let songItem = item as? SongItem {
switch tableColumn?.identifier.rawValue {
case "songTitleColumn":
return cellForSongTitle(outlineView, with: songItem)
case "songArtistColumn":
return cellForSongArtist(outlineView, with: songItem)
default:
return nil
}
} else if tableColumn?.identifier.rawValue == "songTitleColumn" {
return cellForQueueHeading(outlineView)
} else {
return nil
}
}
func cellForSongTitle(_ outlineView: NSOutlineView, with songItem: SongItem) -> NSView {
let cellView = outlineView.makeView(
withIdentifier: .queueSongTitle,
owner: self
) as! NSTableCellView
cellView.textField?.stringValue = songItem.song.getTag(.title)
if songItem.isPlaying {
cellView.textField?.font = .systemFontBold
cellView.imageView?.image = dataSource.queueIcon
} else {
cellView.textField?.font = .systemFontRegular
cellView.imageView?.image = nil
}
return cellView
}
func cellForSongArtist(_ outlineView: NSOutlineView, with songItem: SongItem) -> NSView {
let cellView = outlineView.makeView(
withIdentifier: .queueSongArtist,
owner: self
) as! NSTableCellView
cellView.textField?.stringValue = songItem.song.getTag(.artist)
if songItem.isPlaying {
cellView.textField?.font = .systemFontBold
} else {
cellView.textField?.font = .systemFontRegular
}
return cellView
}
func cellForQueueHeading(_ outlineView: NSOutlineView) -> NSView {
let cellView = outlineView.makeView(
withIdentifier: .queueHeading,
owner: self
) as! NSTableCellView
cellView.textField?.stringValue = "QUEUE"
return cellView
}
func setupNotificationObservers() {
NotificationCenter.default.addObserver(
self,
selector: #selector(stateChanged(_:)),
name: Notification.stateChanged,
object: AppDelegate.mpdClient
)
NotificationCenter.default.addObserver(
self,
selector: #selector(queueChanged(_:)),
name: Notification.queueChanged,
object: AppDelegate.mpdClient
)
NotificationCenter.default.addObserver(
self,
selector: #selector(queuePosChanged(_:)),
name: Notification.queuePosChanged,
object: AppDelegate.mpdClient
)
}
}