Improve the artwork selector collection view.

This commit is contained in:
Damiano Galassi committed 2018-06-28 13:20:51 +02:00
1 parent 37356b8cc5
commit 8560fcdc38
5 files changed
+143 -95

No files matched your search

+3 -3
View File
@@ -41,11 +41,11 @@ DQ
</connections>
</button>
<slider verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="8">
<rect key="frame" x="364" y="49" width="96" height="18"/>
<rect key="frame" x="360" y="49" width="100" height="18"/>
<constraints>
<constraint firstAttribute="width" constant="96" id="0Mf-Un-HXH"/>
<constraint firstAttribute="width" constant="100" id="0Mf-Un-HXH"/>
</constraints>
<sliderCell key="cell" controlSize="small" continuous="YES" alignment="left" minValue="100" maxValue="500" doubleValue="250" tickMarkPosition="above" numberOfTickMarks="5" sliderType="linear" id="15"/>
<sliderCell key="cell" controlSize="small" continuous="YES" alignment="left" maxValue="100" doubleValue="50" tickMarkPosition="above" numberOfTickMarks="5" sliderType="linear" id="15"/>
<accessibility description="Zoom"/>
<connections>
<action selector="zoomSliderDidChange:" target="-2" id="44"/>
+19 -5
View File
@@ -267,10 +267,14 @@ class CollectionView : NSCollectionView {
nextResponder?.keyDown(with: event)
} else if selectionIndexPaths.isEmpty {
if key == NSRightArrowFunctionKey || key == NSDownArrowFunctionKey {
animator().selectItems(at: Set([IndexPath(item: 0, section: 0)]), scrollPosition: .bottom)
let indexPathSet = Set([IndexPath(item: 0, section: 0)])
animator().selectItems(at: indexPathSet, scrollPosition: .bottom)
delegate?.collectionView?(self, didSelectItemsAt: indexPathSet)
} else if key == NSLeftArrowFunctionKey || key == NSUpArrowFunctionKey {
let numberOfItems = dataSource?.collectionView(self, numberOfItemsInSection: 0) ?? 1
animator().selectItems(at: Set([IndexPath(item: numberOfItems - 1, section: 0)]), scrollPosition: .bottom)
let indexPathSet = Set([IndexPath(item: numberOfItems - 1, section: 0)])
animator().selectItems(at: indexPathSet, scrollPosition: .bottom)
delegate?.collectionView?(self, didSelectItemsAt: indexPathSet)
} else {
super.keyDown(with: event)
}
@@ -352,8 +356,20 @@ class ArtworkSelectorController: NSWindowController, NSCollectionViewDataSource,
// MARK: - User Interface
@IBAction func zoomSliderDidChange(_ sender: Any) {
let standardSize = NSSize(width: 154, height: 194)
if let layout = imageBrowser.collectionViewLayout as? NSCollectionViewFlowLayout {
layout.itemSize = NSSize(width: Int(slider.floatValue), height: Int(slider.floatValue * 1.25))
if slider.floatValue == 50 {
layout.itemSize = standardSize
} else if slider.floatValue < 50 {
let zoomValue = (CGFloat(slider.floatValue) + 50) / 100
layout.itemSize = NSSize(width: Int(standardSize.width * zoomValue),
height: Int(standardSize.height * zoomValue))
} else {
let zoomValue = pow((CGFloat(slider.floatValue) + 50) / 100, 2.4)
layout.itemSize = NSSize(width: Int(standardSize.width * zoomValue),
height: Int(standardSize.height * zoomValue))
}
}
}
@@ -365,9 +381,7 @@ class ArtworkSelectorController: NSWindowController, NSCollectionViewDataSource,
imageBrowser.reloadItems(at: [indexPath])
}
if imageBrowser.selectionIndexPaths.isEmpty {
imageBrowser.selectionIndexPaths = selectionIndexPaths
}
}
private func selectArtwork(at index: Int) {
+111 -79
View File
@@ -13,15 +13,47 @@ class ArtworkSelectorViewItemLabel : NSTextField {
@IBInspectable var highlightTextColor: NSColor = .alternateSelectedControlTextColor
@IBInspectable var cornerRadius: CGFloat = 3
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addObservers()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
addObservers()
}
deinit {
removeObservers()
}
private func addObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(windowDidBecomeKey), name: NSWindow.didBecomeKeyNotification, object: window)
NotificationCenter.default.addObserver(self, selector: #selector(windowDidBecomeKey), name: NSWindow.didResignKeyNotification, object: window)
}
private func removeObservers() {
NotificationCenter.default.removeObserver(self, name: NSWindow.didBecomeKeyNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: NSWindow.didResignKeyNotification, object: nil)
}
@objc func windowDidBecomeKey() {
needsDisplay = true
}
override func draw(_ dirtyRect: NSRect) {
let windowIsKeyWindow = window?.isKeyWindow ?? false
var attributedString = attributedStringValue
if isHighlighted {
if isHighlighted && windowIsKeyWindow == true {
let mutableAttributedString = attributedString.mutableCopy() as! NSMutableAttributedString
let range = NSRange(location: 0, length: mutableAttributedString.length)
mutableAttributedString.removeAttribute(NSAttributedString.Key.foregroundColor, range: range)
mutableAttributedString.addAttributes([NSAttributedString.Key.foregroundColor : highlightTextColor], range: range)
attributedString = mutableAttributedString
}
let framesetter = CTFramesetterCreateWithAttributedString(attributedString)
let path = CGMutablePath()
@@ -42,20 +74,19 @@ class ArtworkSelectorViewItemLabel : NSTextField {
var origins = [CGPoint](repeating: CGPoint.zero, count: lineCount)
CTFrameGetLineOrigins(totalFrame, CFRange(), &origins)
if isHighlighted {
for index in 0 ..< lineCount {
let line = lines[index]
let glyphRuns = CTLineGetGlyphRuns(line) as! [CTRun]
let glyphCount = glyphRuns.count
for index in 0 ..< lineCount {
let line = lines[index]
let glyphRuns = CTLineGetGlyphRuns(line) as! [CTRun]
let glyphCount = glyphRuns.count
let origin = origins[index]
// context.textPosition = pos
let origin = origins[index]
context.textPosition = origin
for i in 0 ..< glyphCount {
let run = glyphRuns[i]
// let attributes = CTRunGetAttributes(run)
// if CFDictionaryGetValue(attributes, "HighlightText") {
for i in 0 ..< glyphCount {
let run = glyphRuns[i]
// let attributes = CTRunGetAttributes(run)
// if CFDictionaryGetValue(attributes, "HighlightText") {
if isHighlighted {
var runBounds = CGRect.zero
var ascent: CGFloat = 0, descent: CGFloat = 0, leading: CGFloat = 0
@@ -68,47 +99,95 @@ class ArtworkSelectorViewItemLabel : NSTextField {
let highlightCGColor = highlightColor.cgColor
let roundeRect = runBounds.insetBy(dx: -4, dy: 0)
let roundedPath = NSBezierPath(roundedRect: roundeRect.integral, xRadius: cornerRadius, yRadius: cornerRadius)
context.setFillColor(highlightCGColor)
roundedPath.fill()
// CTRunDraw(run, context, CFRange())
// }
context.setFillColor(windowIsKeyWindow ? highlightCGColor : NSColor.gridColor.cgColor)
roundedPath.fill()
}
CTRunDraw(run, context, CFRange())
}
}
CTFrameDraw(totalFrame, context)
// CTFrameDraw(totalFrame, context)
context.restoreGState()
}
}
class ArtworkSelectorViewItemView: NSView {
let imageLayer: CALayer = CALayer()
let backgroundLayer: CALayer = CALayer()
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
setUp()
}
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
setUp()
}
private func setUp() {
wantsLayer = true
imageLayer.anchorPoint = CGPoint.zero
imageLayer.position = CGPoint(x: 4, y: 40)
imageLayer.contentsGravity = .resizeAspect
imageLayer.shadowRadius = 1
imageLayer.shadowColor = NSColor.labelColor.cgColor
imageLayer.shadowOffset = CGSize.zero
imageLayer.isOpaque = true
backgroundLayer.anchorPoint = CGPoint.zero
backgroundLayer.position = CGPoint(x: 0, y: 36)
backgroundLayer.cornerRadius = 8
backgroundLayer.isHidden = true
backgroundLayer.isOpaque = true
let actions: [String : CAAction] = ["contents": NSNull(),
"hidden": NSNull(),
"bounds": NSNull()]
imageLayer.actions = actions
backgroundLayer.actions = actions
layer?.addSublayer(backgroundLayer)
layer?.addSublayer(imageLayer)
}
override func hitTest(_ point: NSPoint) -> NSView? {
guard let superview = superview else { return nil }
if super.hitTest(point) != nil {
let convertedPoint = superview.convert(point, to: self)
if let layer = layer?.sublayers?.last {
if layer.hitTest(convertedPoint) != nil {
return self
}
if imageLayer.hitTest(convertedPoint) != nil {
return self
}
return superview
} else {
return nil
}
}
override func layout() {
super.layout()
imageLayer.bounds = CGRect(x: 0, y: 0, width: bounds.width - 8, height: bounds.height - 44)
backgroundLayer.bounds = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height - 36)
if #available(OSX 10.14, *) {
imageLayer.shadowColor = NSColor.labelColor.cgColor
backgroundLayer.backgroundColor = NSColor.unemphasizedSelectedContentBackgroundColor.cgColor
} else {
backgroundLayer.backgroundColor = NSColor.controlHighlightColor.cgColor
}
}
}
@available(OSX 10.11, *)
class ArtworkSelectorViewItem: NSCollectionViewItem {
@IBOutlet var subTextField: NSTextField!
private let imageLayer: CALayer = CALayer()
private let backgroundLayer: CALayer = CALayer()
// MARK: Properties
var itemView : ArtworkSelectorViewItemView { get { return (view as? ArtworkSelectorViewItemView)! } }
@IBOutlet var subTextField: NSTextField!
var doubleAction: Selector?
weak var target: AnyObject?
@@ -131,10 +210,10 @@ class ArtworkSelectorViewItem: NSCollectionViewItem {
var image: NSImage? {
didSet {
if image != nil {
imageLayer.contents = image
imageLayer.shadowOpacity = 0.8
itemView.imageLayer.contents = image
itemView.imageLayer.shadowOpacity = 0.8
} else {
imageLayer.contents = NSImage(imageLiteralResourceName: "Placeholder")
itemView.imageLayer.contents = NSImage(imageLiteralResourceName: "Placeholder")
}
}
}
@@ -159,59 +238,12 @@ class ArtworkSelectorViewItem: NSCollectionViewItem {
}
}
// MARK: View Controller life cycle
override func viewDidLoad() {
super.viewDidLoad()
// self.view.layer?.shouldRasterize = true
self.view.canDrawSubviewsIntoLayer = true
imageLayer.anchorPoint = CGPoint.zero
imageLayer.position = CGPoint(x: 4, y: 40)
imageLayer.contentsGravity = .resizeAspect
imageLayer.shadowRadius = 1
imageLayer.shadowColor = NSColor.labelColor.cgColor
imageLayer.shadowOffset = CGSize.zero
imageLayer.isOpaque = true
backgroundLayer.anchorPoint = CGPoint.zero
backgroundLayer.position = CGPoint(x: 0, y: 36)
backgroundLayer.backgroundColor = NSColor.controlHighlightColor.cgColor
backgroundLayer.cornerRadius = 8
backgroundLayer.isHidden = true
backgroundLayer.isOpaque = true
let actions: [String : CAAction] = ["contents": NSNull(),
"hidden": NSNull(),
"bounds": NSNull()]
imageLayer.actions = actions
backgroundLayer.actions = actions
view.layer?.addSublayer(backgroundLayer)
view.layer?.addSublayer(imageLayer)
}
override func viewDidLayout() {
let bounds = self.view.bounds
imageLayer.bounds = CGRect(x: 0, y: 0, width: bounds.width - 8, height: bounds.height - 44)
backgroundLayer.bounds = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height - 36)
super.viewDidLayout()
}
static private let paragraph: NSParagraphStyle = {
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
return paragraph
}()
private func updateSelectionHighlight() {
if highlightState == .forSelection || (isSelected && highlightState != .forDeselection) {
backgroundLayer.isHidden = false
itemView.backgroundLayer.isHidden = false
textField?.isHighlighted = true
} else {
backgroundLayer.isHidden = true
itemView.backgroundLayer.isHidden = true
textField?.isHighlighted = false
}
}
+7 -7
View File
@@ -19,26 +19,26 @@
<rect key="frame" x="0.0" y="0.0" width="128" height="171"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" misplaced="YES" id="Wkh-6c-0tU" customClass="ArtworkSelectorViewItemLabel" customModule="Subler" customModuleProvider="target">
<rect key="frame" x="0.0" y="17" width="128" height="17"/>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" misplaced="YES" textCompletion="NO" id="Wkh-6c-0tU" customClass="ArtworkSelectorViewItemLabel" customModule="Subler" customModuleProvider="target">
<rect key="frame" x="-2" y="17" width="132" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="center" title="Label" id="TsL-Ak-xnf">
<textFieldCell key="cell" allowsUndo="NO" alignment="center" title="Label" drawsBackground="YES" usesSingleLineMode="YES" id="TsL-Ak-xnf">
<font key="font" metaFont="cellTitle"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" misplaced="YES" id="fGU-Kh-WjD">
<rect key="frame" x="2" y="4" width="124" height="14"/>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" misplaced="YES" textCompletion="NO" id="fGU-Kh-WjD">
<rect key="frame" x="-2" y="4" width="132" height="14"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="center" title="Label" id="ggX-Q2-CZ0">
<textFieldCell key="cell" allowsUndo="NO" alignment="center" title="Label" drawsBackground="YES" usesSingleLineMode="YES" id="ggX-Q2-CZ0">
<font key="font" metaFont="system" size="10"/>
<color key="textColor" name="disabledControlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<point key="canvasLocation" x="-39" y="112.5"/>
<point key="canvasLocation" x="-425" y="83"/>
</customView>
</objects>
</document>
+3 -1
View File
@@ -32,7 +32,9 @@ class PresetEditorViewController: SBMovieViewController {
super.viewDidLoad()
view.appearance = NSAppearance(named: .aqua)
if #available(OSX 10.14, *) {} else {
view.appearance = NSAppearance(named: .aqua)
}
presetTitle.stringValue = preset.title
preset.changed = true