vibetunnel/VibeTunnel/Core/Utilities/WindowCenteringHelper.swift
Peter Steinberger 3d68c64fb2 Center dialogs
2025-06-16 20:56:23 +02:00

33 lines
No EOL
1.3 KiB
Swift

import AppKit
/// Helper class for consistent window centering across the application
enum WindowCenteringHelper {
/// Centers a window on the active screen (where the mouse cursor is located)
/// - Parameter window: The NSWindow to center
static func centerOnActiveScreen(_ window: NSWindow) {
if let screen = NSScreen.main ?? NSScreen.screens.first {
let screenFrame = screen.visibleFrame
let windowFrame = window.frame
let newX = screenFrame.midX - windowFrame.width / 2
let newY = screenFrame.midY - windowFrame.height / 2
window.setFrameOrigin(NSPoint(x: newX, y: newY))
}
}
/// Positions a window off-screen (useful for hidden windows)
/// - Parameter window: The NSWindow to position off-screen
static func positionOffScreen(_ window: NSWindow) {
if let screen = NSScreen.main {
let screenFrame = screen.frame
window.setFrame(NSRect(x: screenFrame.midX, y: screenFrame.minY - 1000, width: 1, height: 1), display: false)
}
}
/// Centers a window using the built-in NSWindow center method
/// - Parameter window: The NSWindow to center
static func centerDefault(_ window: NSWindow) {
window.center()
}
}