diff --git a/Base.lproj/ArtworkSelector.xib b/Base.lproj/ArtworkSelector.xib index ad79169..d400eea 100755 --- a/Base.lproj/ArtworkSelector.xib +++ b/Base.lproj/ArtworkSelector.xib @@ -41,11 +41,11 @@ DQ - + - + - + diff --git a/Classes/ArtworkSelector.swift b/Classes/ArtworkSelector.swift index 2c5e9b1..8cb697d 100644 --- a/Classes/ArtworkSelector.swift +++ b/Classes/ArtworkSelector.swift @@ -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) { diff --git a/Classes/ArtworkSelectorViewItem.swift b/Classes/ArtworkSelectorViewItem.swift index dffaec3..07af5c0 100644 --- a/Classes/ArtworkSelectorViewItem.swift +++ b/Classes/ArtworkSelectorViewItem.swift @@ -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 } } diff --git a/Classes/ArtworkSelectorViewItem.xib b/Classes/ArtworkSelectorViewItem.xib index b0eda17..1a940df 100644 --- a/Classes/ArtworkSelectorViewItem.xib +++ b/Classes/ArtworkSelectorViewItem.xib @@ -19,26 +19,26 @@ - - + + - + - - + + - + - + diff --git a/Classes/PresetEditorViewController.swift b/Classes/PresetEditorViewController.swift index 17ade5b..46d1471 100644 --- a/Classes/PresetEditorViewController.swift +++ b/Classes/PresetEditorViewController.swift @@ -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