From 5d0fff227630c689e10d5723b96a233e004f971b Mon Sep 17 00:00:00 2001 From: Louis Pontoise Date: Wed, 1 Jun 2022 18:53:44 +0900 Subject: [PATCH] feat: smoother rounded corners for the main window --- src/ui/App.swift | 2 +- src/ui/main-window/ThumbnailsPanel.swift | 2 +- src/ui/main-window/ThumbnailsView.swift | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/ui/App.swift b/src/ui/App.swift index 55c5b1c76..1fc1c1dd3 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -97,7 +97,7 @@ class App: AppCenterApplication, NSApplicationDelegate { // we put application code here which should be executed on init() and Preferences change func resetPreferencesDependentComponents() { ThumbnailsView.recycledViews = ThumbnailsView.recycledViews.map { _ in ThumbnailView() } - thumbnailsPanel.thumbnailsView.layer!.cornerRadius = Preferences.windowCornerRadius + thumbnailsPanel.thumbnailsView.updateRoundedCorners(Preferences.windowCornerRadius) } func restart() { diff --git a/src/ui/main-window/ThumbnailsPanel.swift b/src/ui/main-window/ThumbnailsPanel.swift index 7ce9b7644..a0ec4bede 100644 --- a/src/ui/main-window/ThumbnailsPanel.swift +++ b/src/ui/main-window/ThumbnailsPanel.swift @@ -13,7 +13,7 @@ class ThumbnailsPanel: NSPanel, NSWindowDelegate { hasShadow = false titleVisibility = .hidden backgroundColor = .clear - contentView!.addSubview(thumbnailsView) + contentView! = thumbnailsView // triggering AltTab before or during Space transition animation brings the window on the Space post-transition collectionBehavior = .canJoinAllSpaces // 2nd highest level possible; this allows the app to go on top of context menus diff --git a/src/ui/main-window/ThumbnailsView.swift b/src/ui/main-window/ThumbnailsView.swift index 3730f6741..e8df9d476 100644 --- a/src/ui/main-window/ThumbnailsView.swift +++ b/src/ui/main-window/ThumbnailsView.swift @@ -10,12 +10,26 @@ class ThumbnailsView: NSVisualEffectView { material = Preferences.windowMaterial state = .active wantsLayer = true - layer!.cornerRadius = Preferences.windowCornerRadius + updateRoundedCorners(Preferences.windowCornerRadius) addSubview(scrollView) // TODO: think about this optimization more (1...100).forEach { _ in ThumbnailsView.recycledViews.append(ThumbnailView()) } } + /// using layer!.cornerRadius works but the corners are aliased; this custom approach gives smooth rounded corners + /// see https://stackoverflow.com/a/29386935/2249756 + func updateRoundedCorners(_ cornerRadius: CGFloat) { + let edgeLength = 2.0 * cornerRadius + 1.0 + maskImage = NSImage(size: NSSize(width: edgeLength, height: edgeLength), flipped: false) { rect in + let bezierPath = NSBezierPath(roundedRect: rect, xRadius: cornerRadius, yRadius: cornerRadius) + NSColor.black.set() + bezierPath.fill() + return true + } + maskImage!.capInsets = NSEdgeInsets(top: cornerRadius, left: cornerRadius, bottom: cornerRadius, right: cornerRadius) + maskImage!.resizingMode = .stretch + } + func nextRow(_ direction: Direction) -> [ThumbnailView]? { let step = direction == .down ? 1 : -1 if let currentRow = Windows.focusedWindow()?.row {