1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00

Refactor setting of highlighted queue song

This commit is contained in:
Daniel Barber 2019-02-07 20:51:45 -05:00
parent ce91a4282c
commit be8b286de8
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
2 changed files with 38 additions and 21 deletions

View File

@ -13,6 +13,9 @@ class QueueViewController: NSViewController, NSOutlineViewDataSource, NSOutlineV
var queue: [MPDClient.Song] = [] var queue: [MPDClient.Song] = []
var queuePos: Int32 = -1 var queuePos: Int32 = -1
let systemFontRegular = NSFont.systemFont(ofSize: 13, weight: .regular)
let systemFontBold = NSFont.systemFont(ofSize: 13, weight: .bold)
struct SongItem { struct SongItem {
var song: MPDClient.Song var song: MPDClient.Song
var queuePos: Int var queuePos: Int
@ -51,29 +54,48 @@ class QueueViewController: NSViewController, NSOutlineViewDataSource, NSOutlineV
guard let queuePos = notification.userInfo?[MPDClient.queuePosKey] as? Int32 guard let queuePos = notification.userInfo?[MPDClient.queuePosKey] as? Int32
else { return } else { return }
if self.queuePos > -1 { let oldSongRowPos = Int(self.queuePos + 1)
let oldSongRow = queueView.rowView(atRow: Int(self.queuePos + 1), makeIfNecessary: true) let newSongRowPos = Int(queuePos + 1)
let oldSongTitleCell = oldSongRow?.view(atColumn: 0) as! NSTableCellView
let oldSongArtistCell = oldSongRow?.view(atColumn: 1) as! NSTableCellView
oldSongTitleCell.textField?.font = NSFont.systemFont(ofSize: 13, weight: .regular)
oldSongArtistCell.textField?.font = NSFont.systemFont(ofSize: 13, weight: .regular)
}
let oldQueuePos = self.queuePos
self.queuePos = queuePos self.queuePos = queuePos
let songRow = queueView.rowView(atRow: Int(self.queuePos + 1), makeIfNecessary: true) setQueuePos(oldSongRowPos: oldSongRowPos, newSongRowPos: newSongRowPos)
let songTitleCell = songRow?.view(atColumn: 0) as! NSTableCellView
let songArtistCell = songRow?.view(atColumn: 1) as! NSTableCellView
songTitleCell.textField?.font = NSFont.systemFont(ofSize: 13, weight: .bold)
songArtistCell.textField?.font = NSFont.systemFont(ofSize: 13, weight: .bold)
queueView.reloadData( queueView.reloadData(
forRowIndexes: [Int(oldQueuePos + 1), Int(queuePos + 1)], forRowIndexes: [oldSongRowPos, newSongRowPos],
columnIndexes: [0, 1] columnIndexes: [0, 1]
) )
} }
func setQueuePos(oldSongRowPos: Int, newSongRowPos: Int) {
if oldSongRowPos > 0 {
guard let oldSongRow = queueView.rowView(atRow: oldSongRowPos, makeIfNecessary: true)
else { return }
guard let oldSongTitleCell = oldSongRow.view(atColumn: 0) as? NSTableCellView
else { return }
setRowFont(rowView: oldSongRow, font: systemFontRegular)
oldSongTitleCell.imageView?.image = nil
}
guard let songRow = queueView.rowView(atRow: newSongRowPos, makeIfNecessary: true)
else { return }
guard let newSongTitleCell = songRow.view(atColumn: 0) as? NSTableCellView
else { return }
setRowFont(rowView: songRow, font: systemFontBold)
newSongTitleCell.imageView?.image = NSImage(named: NSImage.Name("playButton"))
}
func setRowFont(rowView: NSTableRowView, font: NSFont) {
guard let songTitleCell = rowView.view(atColumn: 0) as? NSTableCellView
else { return }
guard let songArtistCell = rowView.view(atColumn: 1) as? NSTableCellView
else { return }
songTitleCell.textField?.font = font
songArtistCell.textField?.font = font
}
func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int { func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
return queue.count + 1 return queue.count + 1
} }
@ -100,11 +122,6 @@ class QueueViewController: NSViewController, NSOutlineViewDataSource, NSOutlineV
) as! NSTableCellView ) as! NSTableCellView
cellView.textField?.stringValue = songItem.song.getTag(MPD_TAG_TITLE) cellView.textField?.stringValue = songItem.song.getTag(MPD_TAG_TITLE)
if songItem.queuePos == self.queuePos {
cellView.imageView?.image = NSImage(named: NSImage.Name("playButton"))
} else {
cellView.imageView?.image = nil
}
return cellView return cellView
case "songArtistColumn": case "songArtistColumn":

View File

@ -815,7 +815,7 @@
<constraints> <constraints>
<constraint firstAttribute="width" constant="17" id="UFf-Fg-9Qg"/> <constraint firstAttribute="width" constant="17" id="UFf-Fg-9Qg"/>
</constraints> </constraints>
<imageCell key="cell" refusesFirstResponder="YES" imageScaling="proportionallyDown" image="playButton" id="ckK-gW-Vhx"/> <imageCell key="cell" refusesFirstResponder="YES" imageScaling="proportionallyDown" id="ckK-gW-Vhx"/>
</imageView> </imageView>
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="i0h-bn-auJ"> <textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="i0h-bn-auJ">
<rect key="frame" x="25" y="0.0" width="197" height="17"/> <rect key="frame" x="25" y="0.0" width="197" height="17"/>