Skip to content

Commit

Permalink
feat: smoother rounded corners for the main window
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Jun 1, 2022
1 parent d7b6b7c commit 5d0fff2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/main-window/ThumbnailsPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/ui/main-window/ThumbnailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

2 comments on commit 5d0fff2

@KastanDay
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So cool to see this attention to detail (anti-aliased corners)! Thanks for your efforts 🫡

@lwouis
Copy link
Owner Author

@lwouis lwouis commented on 5d0fff2 Jun 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KastanDay I thought no-one would notice. I'm glad to hear you appreciate it~

Please sign in to comment.