mirror of
https://github.com/danbee/persephone
synced 2025-03-04 08:39:11 +00:00
Importing Cocoa pulls in CoreData, which we're not using. https://github.com/brentsimmons/NetNewsWire/blob/master/Technotes/CodingGuidelines.md
67 lines
1.6 KiB
Swift
67 lines
1.6 KiB
Swift
//
|
|
// AlbumViewLayout.swift
|
|
// Persephone
|
|
//
|
|
// Created by Daniel Barber on 2019/2/18.
|
|
// Copyright © 2019 Dan Barber. All rights reserved.
|
|
//
|
|
|
|
import AppKit
|
|
|
|
class AlbumViewLayout: NSCollectionViewFlowLayout {
|
|
let maxItemWidth: CGFloat = 180
|
|
let albumInfoHeight: CGFloat = 39
|
|
var scrollPosition: CGFloat = 0
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
super.init()
|
|
|
|
minimumLineSpacing = 20
|
|
minimumInteritemSpacing = 20
|
|
sectionInset = NSEdgeInsets(
|
|
top: 20,
|
|
left: 40,
|
|
bottom: 60,
|
|
right: 40
|
|
)
|
|
}
|
|
|
|
override func prepare() {
|
|
super.prepare()
|
|
|
|
guard let collectionView = collectionView
|
|
else { return }
|
|
|
|
let width = collectionView.bounds.size.width
|
|
var divider: CGFloat = 1
|
|
var itemWidth: CGFloat = 0
|
|
|
|
repeat {
|
|
let totalPaddingWidth = sectionInset.left + sectionInset.right
|
|
let totalGutterWidth = (divider - 1) * (minimumInteritemSpacing)
|
|
itemWidth = (width - totalPaddingWidth - totalGutterWidth - 1) / divider
|
|
divider = divider + 1
|
|
} while itemWidth > maxItemWidth
|
|
|
|
let itemHeight = itemWidth + albumInfoHeight
|
|
|
|
itemSize = NSSize(width: itemWidth, height: itemHeight)
|
|
}
|
|
|
|
func saveScrollPosition() {
|
|
guard let collectionView = collectionView
|
|
else { return }
|
|
|
|
if let scrollView = collectionView.enclosingScrollView {
|
|
scrollPosition = scrollView.documentVisibleRect.minY / collectionView.bounds.height
|
|
}
|
|
}
|
|
|
|
func setScrollPosition() {
|
|
guard let collectionView = collectionView
|
|
else { return }
|
|
|
|
collectionView.scroll(NSPoint(x: 0, y: scrollPosition * collectionView.bounds.height))
|
|
}
|
|
}
|