Skip to content

Commit

Permalink
fix: better float rounding = sharper cell contents
Browse files Browse the repository at this point in the history
  • Loading branch information
louis.pontoise authored and lwouis committed Mar 10, 2020
1 parent 1faa74c commit 9a96e49
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
2 changes: 1 addition & 1 deletion alt-tab-macos/logic/Spaces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)] {
Expand Down
18 changes: 7 additions & 11 deletions alt-tab-macos/ui/Cell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion alt-tab-macos/ui/CollectionViewCenterFlowLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 4 additions & 8 deletions alt-tab-macos/ui/ThumbnailsPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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!)) }
Expand All @@ -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
}
}

0 comments on commit 9a96e49

Please sign in to comment.