Hide the collection view items labels when they are not needed

This commit is contained in:
Damiano Galassi committed 2024-07-01 17:49:47 +02:00
1 parent a751ccb9ee
commit a40a3bfe3c
4 files changed
+61 -37

No files matched your search

+42 -17
View File
@@ -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()
}
}
+2 -3
View File
@@ -1,9 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="17132" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="22505" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<development version="11000" identifier="xcode"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17132"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22505"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
+12 -12
View File
@@ -25,15 +25,15 @@
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="1">
<rect key="frame" x="0.0" y="0.0" width="769" height="306"/>
<rect key="frame" x="0.0" y="0.0" width="769" height="279"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<tabView controlSize="small" translatesAutoresizingMaskIntoConstraints="NO" id="10">
<rect key="frame" x="-11" y="-12" width="791" height="313"/>
<rect key="frame" x="-11" y="-12" width="791" height="286"/>
<tabViewItems>
<tabViewItem label="Metadata" identifier="1" id="11">
<view key="view" id="12">
<rect key="frame" x="10" y="25" width="771" height="86"/>
<rect key="frame" x="10" y="25" width="771" height="252"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<popUpButton toolTip="Add a new metadata item" translatesAutoresizingMaskIntoConstraints="NO" id="30">
@@ -90,13 +90,13 @@
<accessibility description="Sets"/>
</popUpButton>
<scrollView autohidesScrollers="YES" horizontalLineScroll="16" horizontalPageScroll="10" verticalLineScroll="16" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="r4i-wT-qDC">
<rect key="frame" x="17" y="36" width="737" height="44"/>
<rect key="frame" x="17" y="36" width="737" height="206"/>
<clipView key="contentView" id="cQT-bX-0YB">
<rect key="frame" x="1" y="1" width="735" height="42"/>
<rect key="frame" x="1" y="1" width="735" height="204"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" tableStyle="plain" columnReordering="NO" autosaveColumns="NO" typeSelect="NO" rowHeight="14" rowSizeStyle="automatic" headerView="qXU-xB-xRF" viewBased="YES" floatsGroupRows="NO" id="qg5-r0-E4V" customClass="ExpandedTableView" customModule="Subler" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="735" height="88"/>
<rect key="frame" x="0.0" y="0.0" width="735" height="181"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
@@ -325,7 +325,7 @@
</tabViewItem>
<tabViewItem label="Artwork" identifier="Item 1" id="15">
<view key="view" id="16">
<rect key="frame" x="10" y="25" width="771" height="275"/>
<rect key="frame" x="10" y="25" width="771" height="248"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="223">
@@ -366,16 +366,16 @@
</connections>
</slider>
<scrollView wantsLayer="YES" autohidesScrollers="YES" horizontalLineScroll="10" horizontalPageScroll="10" verticalLineScroll="10" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="WUe-C3-hRA">
<rect key="frame" x="17" y="36" width="737" height="233"/>
<rect key="frame" x="17" y="36" width="737" height="206"/>
<clipView key="contentView" id="jau-dn-6LI">
<rect key="frame" x="1" y="1" width="735" height="231"/>
<rect key="frame" x="1" y="1" width="735" height="204"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<collectionView selectable="YES" allowsMultipleSelection="YES" id="HHe-zs-flI" customClass="CollectionView" customModule="Subler" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="735" height="231"/>
<rect key="frame" x="0.0" y="0.0" width="735" height="204"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES"/>
<collectionViewFlowLayout key="collectionViewLayout" minimumInteritemSpacing="10" minimumLineSpacing="10" id="ofy-8i-tTx">
<size key="itemSize" width="280" height="260"/>
<size key="itemSize" width="280" height="224"/>
<edgeInsets key="sectionInset" left="16" right="16" top="16" bottom="16"/>
</collectionViewFlowLayout>
<color key="primaryBackgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
@@ -391,7 +391,7 @@
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" doubleValue="1" horizontal="NO" id="bD2-5M-Iiz">
<rect key="frame" x="720" y="1" width="16" height="231"/>
<rect key="frame" x="720" y="1" width="16" height="204"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
@@ -59,7 +59,7 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie
// Artwork tab
private var artworks: [MP42MetadataItem]
private let standardSize = NSSize(width: 154, height: 192)
private let standardSize = NSSize(width: 280, height: 224)
@IBOutlet var removeArtworkButton: NSButton!
@IBOutlet var artworksView: CollectionView!
@@ -949,9 +949,9 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie
let artwork = artworks[index]
collectionViewItem.image = artwork.imageValue?.image
collectionViewItem.title = nil
collectionViewItem.subtitle = nil
collectionViewItem.target = self
collectionViewItem.title = ""
collectionViewItem.subtitle = ""
return collectionViewItem
}
@@ -1235,12 +1235,12 @@ class MovieViewController: PropertyView, NSTableViewDataSource, ExpandedTableVie
} else if floatValue < 50 {
let zoomValue = (floatValue + 50) / 100
layout.itemSize = NSSize(width: Int(standardSize.width * zoomValue),
height: Int((standardSize.height - 32) * zoomValue + 32))
height: Int(standardSize.height * zoomValue))
} else {
let zoomValue = pow((floatValue + 50) / 100, 2.4)
layout.itemSize = NSSize(width: Int(standardSize.width * zoomValue),
height: Int((standardSize.height - 32) * zoomValue + 32))
height: Int(standardSize.height * zoomValue))
}
}
}