141 lines
5.1 KiB
Swift
Executable File
141 lines
5.1 KiB
Swift
Executable File
//
|
|
// TrackTableCellView.swift
|
|
// Harmony
|
|
//
|
|
// Created by Kim Wittenburg on 21.01.15.
|
|
// Copyright (c) 2015 Das Code Kollektiv. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
import AppKitPlus
|
|
|
|
/// A table cell view to display information for a `SongItem`.
|
|
public class SongTableCellView: AdvancedTableCellView {
|
|
|
|
// MARK: Types
|
|
|
|
private struct Images {
|
|
|
|
/// Caches the tick image for track cells so that it does not need to be
|
|
/// reloaded every time a cell is configured.
|
|
static let tickImage = NSImage(named: "Tick")?.imageByMaskingWithColor(NSColor.clearColor())
|
|
|
|
/// Caches the gray tick image for track cells so that it does not need to be
|
|
/// reloaded every time a cell is configured.
|
|
static let grayTickImage = NSImage(named: "Tick")?.imageByMaskingWithColor(NSColor.lightGrayColor())
|
|
|
|
}
|
|
|
|
// MARK: Properties
|
|
|
|
/// An outlet do display the track number. This acts as a secondary label.
|
|
/// The text color is automatically adjusted based on the `backgroundStyle`
|
|
/// of the receiver.
|
|
///
|
|
/// Intended to be used as accessory view
|
|
@IBOutlet public lazy var trackNumberTextField: NSTextField? = {
|
|
let trackNumberTextField = NSTextField()
|
|
trackNumberTextField.bordered = false
|
|
trackNumberTextField.drawsBackground = false
|
|
trackNumberTextField.selectable = false
|
|
trackNumberTextField.lineBreakMode = .ByTruncatingTail
|
|
trackNumberTextField.font = NSFont.systemFontOfSize(0)
|
|
trackNumberTextField.textColor = NSColor.secondaryLabelColor()
|
|
trackNumberTextField.translatesAutoresizingMaskIntoConstraints = false
|
|
return trackNumberTextField
|
|
}()
|
|
|
|
/// Intended to be used as accessory view.
|
|
///
|
|
/// This property replaces `imageView`.
|
|
@IBOutlet public lazy var savedImageView: NSImageView! = {
|
|
let secondaryImageView = NSImageView()
|
|
secondaryImageView.imageScaling = .ScaleProportionallyDown
|
|
secondaryImageView.translatesAutoresizingMaskIntoConstraints = false
|
|
return secondaryImageView
|
|
}()
|
|
|
|
// MARK: Intitializers
|
|
|
|
override public func setupView() {
|
|
leftAccessoryView = trackNumberTextField
|
|
super.setupView()
|
|
}
|
|
|
|
// MARK: Overrides
|
|
|
|
override public var backgroundStyle: NSBackgroundStyle {
|
|
get {
|
|
return super.backgroundStyle
|
|
}
|
|
set {
|
|
super.backgroundStyle = newValue
|
|
let trackNumberCell = self.trackNumberTextField?.cell as? NSTextFieldCell
|
|
trackNumberCell?.backgroundStyle = newValue
|
|
|
|
switch newValue {
|
|
case .Light:
|
|
trackNumberTextField?.textColor = NSColor.secondaryLabelColor()
|
|
case .Dark:
|
|
trackNumberTextField?.textColor = NSColor.secondarySelectedControlColor()
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
override public var imageView: NSImageView? {
|
|
set {
|
|
savedImageView = newValue
|
|
}
|
|
get {
|
|
return savedImageView
|
|
}
|
|
}
|
|
|
|
// MARK: Methods
|
|
|
|
/// Sets up the receiver to display the specified `songItem`.
|
|
public func setupForSongItem(songItem: SongItem) {
|
|
style = (songItem.parentItem != nil && songItem.parentItem!.hasCommonArtist) ? .Simple : .CompactSubtitle
|
|
|
|
textField?.stringValue = Preferences.sharedPreferences.useCensoredNames ? songItem.song.censoredName : songItem.song.name
|
|
if songItem.associatedTracks.isEmpty {
|
|
textField?.textColor = NSColor.disabledControlTextColor()
|
|
} else if songItem.associatedTracks.count > 1 || songItem.associatedTracks.first!.name?.compare(songItem.song.name, options: Preferences.sharedPreferences.caseSensitive ? [] : .CaseInsensitiveSearch, range: nil, locale: nil) != .OrderedSame {
|
|
textField?.textColor = NSColor.redColor()
|
|
} else {
|
|
textField?.textColor = NSColor.controlTextColor()
|
|
}
|
|
secondaryTextField?.stringValue = songItem.song.artist.name
|
|
trackNumberTextField?.stringValue = "\(songItem.song.trackNumber)"
|
|
if songItem.associatedTracks.isEmpty {
|
|
imageView?.image = SongTableCellView.Images.grayTickImage
|
|
} else {
|
|
imageView?.image = SongTableCellView.Images.tickImage
|
|
}
|
|
if songItem.saved {
|
|
let aspectRatioConstraint = NSLayoutConstraint(
|
|
item: savedImageView,
|
|
attribute: .Width,
|
|
relatedBy: .Equal,
|
|
toItem: savedImageView,
|
|
attribute: .Height,
|
|
multiplier: 1,
|
|
constant: 0)
|
|
let widthConstraint = NSLayoutConstraint(
|
|
item: savedImageView,
|
|
attribute: .Width,
|
|
relatedBy: .Equal,
|
|
toItem: nil,
|
|
attribute: .Width,
|
|
multiplier: 1,
|
|
constant: 17)
|
|
setRightAccessoryView(savedImageView, withConstraints: [aspectRatioConstraint, widthConstraint])
|
|
} else {
|
|
rightAccessoryView = nil
|
|
}
|
|
}
|
|
|
|
}
|