fix: better float rounding = sharper cell contents

This commit is contained in:
louis.pontoise authored and lwouis committed 2020-03-10 14:00:37 +09:00
1 parent 1faa74c7c5
commit 9a96e4988a
4 files changed
+13 -21

No files matched your search

+1 -1
View File
@@ -26,7 +26,7 @@ class Spaces {
static func updateCurrentSpace() {
currentSpaceId = CGSManagedDisplayGetCurrentSpace(cgsMainConnectionId, Screen.mainUuid())
currentSpaceIndex = allIdsAndIndexes().first { $0.0 == currentSpaceId }!.1
debugPrint("current space", currentSpaceId)
debugPrint("Current space", currentSpaceId)
}
static func allIdsAndIndexes() -> [(CGSSpaceID, SpaceIndex)] {
+7 -11
View File
@@ -42,7 +42,8 @@ class Cell: NSCollectionViewItem {
func updateRecycledCellWithNewContent(_ element: Window, _ mouseDownCallback: @escaping MouseDownCallback, _ mouseMovedCallback: @escaping MouseMovedCallback, _ screen: NSScreen) {
window = element
thumbnail.image = element.thumbnail
let thumbnailSize = Cell.thumbnailSize(element.thumbnail, screen)
let (thumbnailWidth, thumbnailHeight) = Cell.thumbnailSize(element.thumbnail, screen)
let thumbnailSize = NSSize(width: thumbnailWidth.rounded(), height: thumbnailHeight.rounded())
thumbnail.image?.size = thumbnailSize
thumbnail.frame.size = thumbnailSize
appIcon.image = element.icon
@@ -110,27 +111,22 @@ class Cell: NSCollectionViewItem {
}
static func widthMax(_ screen: NSScreen) -> CGFloat {
return floor((ThumbnailsPanel.widthMax(screen) / Preferences.minCellsPerRow - Preferences.interCellPadding) * Cell.downscaleFactor())
return (ThumbnailsPanel.widthMax(screen) / Preferences.minCellsPerRow - Preferences.interCellPadding) * Cell.downscaleFactor()
}
static func widthMin(_ screen: NSScreen) -> CGFloat {
return floor((ThumbnailsPanel.widthMax(screen) / Preferences.maxCellsPerRow - Preferences.interCellPadding) * Cell.downscaleFactor())
return (ThumbnailsPanel.widthMax(screen) / Preferences.maxCellsPerRow - Preferences.interCellPadding) * Cell.downscaleFactor()
}
static func height(_ screen: NSScreen) -> CGFloat {
return floor((ThumbnailsPanel.heightMax(screen) / Preferences.nCellsRows - Preferences.interCellPadding) * Cell.downscaleFactor())
return (ThumbnailsPanel.heightMax(screen) / Preferences.nCellsRows - Preferences.interCellPadding) * Cell.downscaleFactor()
}
static func width(_ image: NSImage?, _ screen: NSScreen) -> CGFloat {
return floor(max(thumbnailSize(image, screen).width + Preferences.intraCellPadding * 2, Cell.widthMin(screen)))
return max(thumbnailSize(image, screen).0 + Preferences.intraCellPadding * 2, Cell.widthMin(screen))
}
static func thumbnailSize(_ image: NSImage?, _ screen: NSScreen) -> NSSize {
let (width, height) = thumbnailSize_(image, screen)
return NSSize(width: floor(width), height: floor(height))
}
static func thumbnailSize_(_ image: NSImage?, _ screen: NSScreen) -> (CGFloat, CGFloat) {
static func thumbnailSize(_ image: NSImage?, _ screen: NSScreen) -> (CGFloat, CGFloat) {
let thumbnailWidthMin = Cell.widthMin(screen) - Preferences.intraCellPadding * 2
let thumbnailHeightMax = Cell.height(screen) - Preferences.intraCellPadding * 3 - Preferences.iconSize
let thumbnailWidthMax = Cell.widthMax(screen) - Preferences.intraCellPadding * 2
@@ -45,7 +45,7 @@ class CollectionViewCenterFlowLayout: NSCollectionViewFlowLayout {
}
private func shiftCenteredElementToTheLeft(_ attributes: [NSCollectionViewLayoutAttributes], _ widestRow: CGFloat, _ totalHeight: CGFloat) {
let horizontalMargin = floor((collectionView!.frame.size.width - widestRow) / 2)
let horizontalMargin = ((collectionView!.frame.size.width - widestRow) / 2).rounded()
for attribute in attributes {
attribute.frame.origin.x -= horizontalMargin
}
+4 -8
View File
@@ -75,7 +75,7 @@ class ThumbnailsPanel: NSPanel, NSCollectionViewDataSource, NSCollectionViewDele
func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
guard indexPath.item < Windows.list.count else { return .zero }
return NSSize(width: Cell.width(Windows.list[indexPath.item].thumbnail, currentScreen!), height: Cell.height(currentScreen!))
return NSSize(width: Cell.width(Windows.list[indexPath.item].thumbnail, currentScreen!).rounded(), height: Cell.height(currentScreen!).rounded())
}
func highlightCell() {
@@ -97,7 +97,7 @@ class ThumbnailsPanel: NSPanel, NSCollectionViewDataSource, NSCollectionViewDele
let layout = collectionView.collectionViewLayout as! CollectionViewCenterFlowLayout
if uiWorkShouldBeDone { layout.currentScreen = screen }
if uiWorkShouldBeDone { layout.invalidateLayout() }
if uiWorkShouldBeDone { collectionView.setFrameSize(NSSize(width: ThumbnailsPanel.widthMax(screen), height: ThumbnailsPanel.heightMax(screen))) }
if uiWorkShouldBeDone { collectionView.setFrameSize(NSSize(width: ThumbnailsPanel.widthMax(screen).rounded(), height: ThumbnailsPanel.heightMax(screen).rounded())) }
if uiWorkShouldBeDone { collectionView.reloadData() }
if uiWorkShouldBeDone { collectionView.layoutSubtreeIfNeeded() }
if uiWorkShouldBeDone { collectionView.setFrameSize(NSSize(width: layout.widestRow!, height: layout.totalHeight!)) }
@@ -108,14 +108,10 @@ class ThumbnailsPanel: NSPanel, NSCollectionViewDataSource, NSCollectionViewDele
}
static func widthMax(_ screen: NSScreen) -> CGFloat {
return floor(screen.frame.width * Preferences.maxScreenUsage - Preferences.windowPadding * 2)
return screen.frame.width * Preferences.maxScreenUsage - Preferences.windowPadding * 2
}
static func heightMax(_ screen: NSScreen) -> CGFloat {
return floor(screen.frame.height * Preferences.maxScreenUsage - Preferences.windowPadding * 2)
}
static func heightMin(_ screen: NSScreen) -> CGFloat {
return floor(Cell.height(screen) - Preferences.windowPadding * 2)
return screen.frame.height * Preferences.maxScreenUsage - Preferences.windowPadding * 2
}
}