vibetunnel/mac/VibeTunnel/Core/Utilities/WindowCenteringHelper.swift
Peter Steinberger a9fd66c291 refactor: major project restructuring - move macOS app to mac/ directory
- Move all macOS-specific code from root to mac/ directory
- Move app icons and assets to dedicated assets/ directory
- Update GitHub workflows for new structure
- Consolidate documentation files
- Clean up root directory for better multi-platform organization
2025-06-20 13:20:01 +02:00

49 lines
1.8 KiB
Swift

import AppKit
/// Helper class for consistent window centering across the application.
///
/// Provides utility methods for positioning windows on screen, including
/// centering on the active display and moving windows off-screen when needed.
/// Used throughout VibeTunnel to ensure consistent window placement behavior.
enum WindowCenteringHelper {
/// Centers a window on the active screen (where the mouse cursor is located)
/// - Parameter window: The NSWindow to center
@MainActor
static func centerOnActiveScreen(_ window: NSWindow) {
// Get the screen that contains the mouse cursor
let mouseLocation = NSEvent.mouseLocation
let activeScreen = NSScreen.screens.first { screen in
screen.frame.contains(mouseLocation)
} ?? NSScreen.main ?? NSScreen.screens.first
if let screen = activeScreen {
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
@MainActor
static func positionOffScreen(_ window: NSWindow) {
if let screen = NSScreen.main {
let screenFrame = screen.frame
window.setFrame(
NSRect(x: screenFrame.midX, y: screenFrame.minY - 1_000, width: 1, height: 1),
display: false
)
}
}
/// Centers a window using the built-in NSWindow center method
/// - Parameter window: The NSWindow to center
@MainActor
static func centerDefault(_ window: NSWindow) {
window.center()
}
}