From a40a3bfe3c6f351ce147c6d3c75c38245fdc4c2f Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Mon, 1 Jul 2024 17:49:47 +0200 Subject: [PATCH] Hide the collection view items labels when they are not needed --- Classes/ArtworkSelectorViewItem.swift | 59 +++++++++++++------ Classes/ArtworkSelectorViewItem.xib | 5 +- Classes/PropertyView/Base.lproj/MovieView.xib | 24 ++++---- .../PropertyView/MovieViewController.swift | 10 ++-- 4 files changed, 61 insertions(+), 37 deletions(-) diff --git a/Classes/ArtworkSelectorViewItem.swift b/Classes/ArtworkSelectorViewItem.swift index c8eecd4..f9a8d74 100644 --- a/Classes/ArtworkSelectorViewItem.swift +++ b/Classes/ArtworkSelectorViewItem.swift @@ -125,19 +125,34 @@ final class ArtworkSelectorViewItemView: NSView { setUp() } + private let padding: CGFloat = 8 + private var paddingBottom: CGFloat = 36 { + didSet { + imageLayer.position = CGPoint(x: padding / 2, y: padding / 2 + paddingBottom) + backgroundLayer.position = CGPoint(x: 0, y: paddingBottom) + emptyLayer.position = CGPoint(x: padding, y: padding + paddingBottom) + } + } + private let corderRadius: CGFloat = 14 + private func setUp() { layer = CALayer() + + layerContentsRedrawPolicy = .onSetNeedsDisplay wantsLayer = true + guard let layer else { return } + let actions: [String : CAAction] = ["contents": NSNull(), "hidden": NSNull(), "bounds": NSNull()] imageLayer.actions = actions backgroundLayer.actions = actions emptyLayer.actions = actions + layer.actions = actions imageLayer.anchorPoint = CGPoint.zero - imageLayer.position = CGPoint(x: 4, y: 40) + imageLayer.position = CGPoint(x: padding / 2, y: padding / 2 + paddingBottom) imageLayer.contentsGravity = .resizeAspect imageLayer.shadowRadius = 1 imageLayer.shadowColor = NSColor.labelColor.cgColor @@ -146,14 +161,14 @@ final class ArtworkSelectorViewItemView: NSView { imageLayer.isOpaque = true backgroundLayer.anchorPoint = CGPoint.zero - backgroundLayer.position = CGPoint(x: 0, y: 36) + backgroundLayer.position = CGPoint(x: 0, y: paddingBottom) backgroundLayer.backgroundColor = NSColor.controlHighlightColor.cgColor backgroundLayer.cornerRadius = 8 backgroundLayer.isHidden = true backgroundLayer.isOpaque = true emptyLayer.anchorPoint = CGPoint.zero - emptyLayer.position = CGPoint(x: 8, y: 46) + emptyLayer.position = CGPoint(x: padding, y: padding + paddingBottom) emptyLayer.lineWidth = 3.0 emptyLayer.lineDashPattern = [12,5] emptyLayer.strokeColor = NSColor.secondarySelectedControlColor.cgColor @@ -162,11 +177,9 @@ final class ArtworkSelectorViewItemView: NSView { updateBackgroundColor() - layer?.addSublayer(backgroundLayer) - layer?.addSublayer(emptyLayer) - layer?.addSublayer(imageLayer) - - layerContentsRedrawPolicy = .onSetNeedsDisplay + layer.addSublayer(backgroundLayer) + layer.addSublayer(emptyLayer) + layer.addSublayer(imageLayer) } override func hitTest(_ point: NSPoint) -> NSView? { @@ -203,11 +216,12 @@ final class ArtworkSelectorViewItemView: NSView { 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) - emptyLayer.bounds = CGRect(x: 0, y: 0, width: bounds.width - 16, height: bounds.height - 56) - emptyLayer.path = CGPath(roundedRect: emptyLayer.bounds, cornerWidth: 14, cornerHeight: 14, transform: nil) + imageLayer.bounds = CGRect(x: 0, y: 0, width: bounds.width - padding, height: bounds.height - padding - paddingBottom) + backgroundLayer.bounds = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height - paddingBottom) + + emptyLayer.bounds = CGRect(x: 0, y: 0, width: bounds.width - padding * 2, height: bounds.height - padding * 2 - paddingBottom - 4) + emptyLayer.path = CGPath(roundedRect: emptyLayer.bounds, cornerWidth: corderRadius, cornerHeight: corderRadius, transform: nil) if let image = imageLayer.contents as? NSImage { let rect = AVMakeRect(aspectRatio: image.size, insideRect: imageLayer.bounds) @@ -223,6 +237,12 @@ final class ArtworkSelectorViewItemView: NSView { } } + var areLabelsHidden: Bool = false { + didSet { + paddingBottom = areLabelsHidden ? 0 : 36 + } + } + var image: NSImage? { didSet { imageLayer.contents = image @@ -258,19 +278,24 @@ final class ArtworkSelectorViewItem: NSCollectionViewItem { textField?.layer?.isOpaque = true } + private func updateLabels() { + let state = title?.isEmpty == false && subtitle?.isEmpty == false + itemView?.areLabelsHidden = !state + textField?.isHidden = !state + subTextField?.isHidden = !state + } + override var title: String? { - set (title) { + didSet { textField?.stringValue = title ?? "" - super.title = title - } - get { - return super.title + updateLabels() } } var subtitle: String? { didSet { subTextField?.stringValue = subtitle ?? "" + updateLabels() } } diff --git a/Classes/ArtworkSelectorViewItem.xib b/Classes/ArtworkSelectorViewItem.xib index e7d4bdc..9b35d53 100644 --- a/Classes/ArtworkSelectorViewItem.xib +++ b/Classes/ArtworkSelectorViewItem.xib @@ -1,9 +1,8 @@ - + - - + diff --git a/Classes/PropertyView/Base.lproj/MovieView.xib b/Classes/PropertyView/Base.lproj/MovieView.xib index 92fd4a4..dafb0dc 100755 --- a/Classes/PropertyView/Base.lproj/MovieView.xib +++ b/Classes/PropertyView/Base.lproj/MovieView.xib @@ -25,15 +25,15 @@ - + - + - + @@ -90,13 +90,13 @@ - + - + - + @@ -325,7 +325,7 @@ - +