Skip to content

Commit

Permalink
refactor: better mimic apple centering
Browse files Browse the repository at this point in the history
Also use enum instead of bool to self-document the interface
  • Loading branch information
Louis Pontoise committed Nov 1, 2019
1 parent c2e76ff commit 4c44100
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
24 changes: 11 additions & 13 deletions alt-tab-macos/logic/Screen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,18 @@ class Screen {
return NSScreen.screens.first { NSMouseInRect(NSEvent.mouseLocation, $0.frame, false) }
}

/*
usage notes:
- useAppleVerticalOffset: applies a vertical offset (higher positioning) attempting to approximate the NSView.center() results but only if we are not dealing with screen filling frames (height)
*/
static func showCenteredFrontPanel(_ panel: NSPanel, _ screen: NSScreen, _ useAppleVerticalOffset: Bool = true) {
var verticalOffset: CGFloat = 0

if useAppleVerticalOffset && panel.frame.height < screen.visibleFrame.height / 1.5 {
verticalOffset = CGFloat(screen.visibleFrame.height * 0.175)
}

let centerPosition = NSPoint(x: screen.visibleFrame.midX - panel.frame.width / 2, y: screen.visibleFrame.midY - panel.frame.height / 2 + verticalOffset)
panel.setFrameOrigin(centerPosition)
static func showPanel(_ panel: NSPanel, _ screen: NSScreen, _ alignment: VerticalAlignment) {
let float = screen.visibleFrame.height - panel.frame.height
debugPrint(float, alignment.rawValue, float * alignment.rawValue)
let y = max(screen.visibleFrame.height - panel.frame.height, 0) * alignment.rawValue
panel.setFrameOrigin(NSPoint(x: screen.visibleFrame.midX - panel.frame.width * 0.5, y: y))
panel.makeKeyAndOrderFront(nil)
Application.shared.arrangeInFront(nil)
}
}

enum VerticalAlignment: CGFloat {
case centered = 0.5
// vertically centered but with an upward offset, similar to a book title; mimics NSView.center()
case appleCentered = 0.75
}
6 changes: 3 additions & 3 deletions alt-tab-macos/ui/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class Application: NSApplication, NSApplicationDelegate, NSWindowDelegate {
}
}

@objc func showCenteredPreferencesPanel() {
Screen.showCenteredFrontPanel(preferencesPanel!, Screen.getPreferredScreen())
@objc func showPreferencesPanel() {
Screen.showPanel(preferencesPanel!, Screen.getPreferredScreen(), .appleCentered)
}

func computeOpenWindows() {
Expand Down Expand Up @@ -106,7 +106,7 @@ class Application: NSApplication, NSApplicationDelegate, NSWindowDelegate {
workItem = DispatchWorkItem {
if !workItem.isCancelled { self.thumbnailsPanel!.computeThumbnails(currentScreen) }
if !workItem.isCancelled { self.thumbnailsPanel!.highlightCellAt(step) }
if !workItem.isCancelled { Screen.showCenteredFrontPanel(self.thumbnailsPanel!, currentScreen, true) }
if !workItem.isCancelled { Screen.showPanel(self.thumbnailsPanel!, currentScreen, .appleCentered) }
}
workItems.append(workItem)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Preferences.windowDisplayDelay!, execute: workItem)
Expand Down
2 changes: 1 addition & 1 deletion alt-tab-macos/ui/StatusItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class StatusItem {
keyEquivalent: "")
item.menu!.addItem(
withTitle: "Preferences…",
action: #selector(application.showCenteredPreferencesPanel),
action: #selector(application.showPreferencesPanel),
keyEquivalent: ",")
item.menu!.addItem(
withTitle: "Quit \(ProcessInfo.processInfo.processName)",
Expand Down

0 comments on commit 4c44100

Please sign in to comment.