Fix a crash on 10.12 and earlier.

This commit is contained in:
Damiano Galassi committed 2019-09-15 15:25:28 +02:00
1 parent 879bf33c2b
commit 8de2581285
8 files changed
+125 -91

No files matched your search

+6 -1
View File
@@ -260,7 +260,12 @@ final class ArtworkSelectorController: NSViewController, NSCollectionViewDataSou
let range = (artworks.count - endIndex ..< artworks.count)
let indexes = range.map { IndexPath(item: $0, section: 0)}
imageBrowser.insertItems(at: Set(indexes))
if #available(OSX 10.13, *) {
imageBrowser.insertItems(at: Set(indexes))
} else {
// Call this on 10.12 and 10.11 to avoid a crash
imageBrowser.reloadData()
}
}
private func selectArtwork(type: ArtworkType, service: String) {
+38 -22
View File
@@ -115,9 +115,9 @@ final class ArtworkSelectorViewItemLabel : NSTextField {
final class ArtworkSelectorViewItemView: NSView {
let imageLayer: CALayer = CALayer()
let backgroundLayer: CALayer = CALayer()
let emptyLayer: CAShapeLayer = CAShapeLayer()
private let imageLayer: CALayer = CALayer()
private let backgroundLayer: CALayer = CALayer()
private let emptyLayer: CAShapeLayer = CAShapeLayer()
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
@@ -133,6 +133,13 @@ final class ArtworkSelectorViewItemView: NSView {
layer = CALayer()
wantsLayer = true
let actions: [String : CAAction] = ["contents": NSNull(),
"hidden": NSNull(),
"bounds": NSNull()]
imageLayer.actions = actions
backgroundLayer.actions = actions
emptyLayer.actions = actions
imageLayer.anchorPoint = CGPoint.zero
imageLayer.position = CGPoint(x: 4, y: 40)
imageLayer.contentsGravity = .resizeAspect
@@ -157,13 +164,6 @@ final class ArtworkSelectorViewItemView: NSView {
emptyLayer.fillColor = NSColor.windowBackgroundColor.cgColor
emptyLayer.isOpaque = true
let actions: [String : CAAction] = ["contents": NSNull(),
"hidden": NSNull(),
"bounds": NSNull()]
imageLayer.actions = actions
backgroundLayer.actions = actions
emptyLayer.actions = actions
updateBackgroundColor()
layer?.addSublayer(backgroundLayer)
@@ -211,28 +211,37 @@ final class ArtworkSelectorViewItemView: NSView {
backgroundLayer.bounds = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height - 36)
emptyLayer.bounds = CGRect(x: 0, y: 0, width: bounds.width - 16, height: bounds.height - 56)
let path = CGMutablePath()
path.addRoundedRect(in: emptyLayer.bounds, cornerWidth: 14, cornerHeight: 14)
emptyLayer.path = path
emptyLayer.path = CGPath(roundedRect: emptyLayer.bounds, cornerWidth: 14, cornerHeight: 14, transform: nil)
if let image = imageLayer.contents as? NSImage {
let rect = AVMakeRect(aspectRatio: image.size, insideRect: imageLayer.bounds)
let path = CGMutablePath()
path.addRect(rect)
imageLayer.shadowPath = path
imageLayer.shadowPath = CGPath(rect: rect, transform: nil)
} else {
imageLayer.shadowPath = nil
}
}
var highlighted: Bool = false {
didSet {
backgroundLayer.isHidden = !highlighted
}
}
var image: NSImage? {
didSet {
imageLayer.contents = image
imageLayer.shadowPath = nil
emptyLayer.isHidden = image != nil
}
}
}
final class ArtworkSelectorViewItem: NSCollectionViewItem {
// MARK: Properties
var itemView : ArtworkSelectorViewItemView { get { return (view as? ArtworkSelectorViewItemView)! } }
var itemView : ArtworkSelectorViewItemView? { get { return (view as? ArtworkSelectorViewItemView) } }
@IBOutlet var subTextField: NSTextField!
var doubleAction: Selector?
@@ -261,8 +270,7 @@ final class ArtworkSelectorViewItem: NSCollectionViewItem {
var image: NSImage? {
didSet {
itemView.imageLayer.contents = image
itemView.emptyLayer.isHidden = image != nil
itemView?.image = image
}
}
@@ -288,10 +296,10 @@ final class ArtworkSelectorViewItem: NSCollectionViewItem {
private func updateSelectionHighlight() {
if highlightState == .forSelection || (isSelected && highlightState != .forDeselection) {
itemView.backgroundLayer.isHidden = false
itemView?.highlighted = true
textField?.isHighlighted = true
} else {
itemView.backgroundLayer.isHidden = true
itemView?.highlighted = false
textField?.isHighlighted = false
}
}
@@ -306,4 +314,12 @@ final class ArtworkSelectorViewItem: NSCollectionViewItem {
}
}
override func prepareForReuse() {
title = nil
subtitle = nil
image = nil
itemView?.highlighted = false
textField?.isHighlighted = false
}
}
+2 -1
View File
@@ -40,11 +40,12 @@
<constraint firstItem="fGU-Kh-WjD" firstAttribute="leading" secondItem="Hz6-mo-xeY" secondAttribute="leading" constant="-2" id="0Wf-6t-6Lg"/>
<constraint firstAttribute="trailing" secondItem="Wkh-6c-0tU" secondAttribute="trailing" constant="-2" id="9df-FA-2lX"/>
<constraint firstAttribute="bottom" secondItem="Wkh-6c-0tU" secondAttribute="bottom" constant="17" id="EOh-x3-ntT"/>
<constraint firstItem="Wkh-6c-0tU" firstAttribute="top" relation="greaterThanOrEqual" secondItem="Hz6-mo-xeY" secondAttribute="top" constant="20" id="HaR-VX-NRg"/>
<constraint firstAttribute="bottom" secondItem="fGU-Kh-WjD" secondAttribute="bottom" constant="4" id="Xx6-ak-lcw"/>
<constraint firstItem="Wkh-6c-0tU" firstAttribute="leading" secondItem="Hz6-mo-xeY" secondAttribute="leading" constant="-2" id="cPi-y8-nFz"/>
<constraint firstAttribute="trailing" secondItem="fGU-Kh-WjD" secondAttribute="trailing" constant="-2" id="fLm-DU-mGT"/>
</constraints>
<point key="canvasLocation" x="-575" y="163.5"/>
<point key="canvasLocation" x="-781" y="102.5"/>
</customView>
</objects>
</document>
+3 -4
View File
@@ -45,10 +45,8 @@ class MetadataSearchViewController: ViewController, MetadataSearchControllerDele
}
override func transition(from fromViewController: NSViewController, to toViewController: NSViewController, options: NSViewController.TransitionOptions = [], completionHandler completion: (() -> Void)? = nil) {
CATransaction.begin()
addChild(toViewController)
super.transition(from: fromViewController, to: toViewController, options: options, completionHandler: completion)
CATransaction.commit()
}
func didSelect(metadata: MetadataResult?) {
@@ -57,8 +55,9 @@ class MetadataSearchViewController: ViewController, MetadataSearchControllerDele
delegate?.didSelect(metadata: result)
presentingViewController?.dismiss(self)
} else {
artworkViewController = ArtworkSelectorController(metadata: result, delegate: self)
transition(from: metadataViewController, to: artworkViewController!, options: [.slideForward, .allowUserInteraction], completionHandler: {
let controller = ArtworkSelectorController(metadata: result, delegate: self)
artworkViewController = controller
transition(from: metadataViewController, to: controller, options: [.slideForward], completionHandler: {
self.removeChild(at: 0)
self.view.window?.makeFirstResponder(self.artworkViewController?.imageBrowser)
})