Files
oai-swift/oAITests/WindowPositionTests.swift
rune f5fd09ac33 Self-heal off-screen main window instead of requiring quit/relaunch
macOS's built-in window-restore (Resume) can place the window at
coordinates for a display that's no longer connected, leaving it
visible-but-invisible with no way to recover short of quitting. Now
re-centers automatically on app activate/Dock reopen, plus a manual
"Reset Window Position" menu command as a guaranteed fallback.
2026-08-18 14:20:17 +02:00

50 lines
1.9 KiB
Swift

//
// WindowPositionTests.swift
// oAITests
//
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
// Copyright (C) 2026 Rune Olsen
import Testing
import Foundation
import CoreGraphics
@testable import Confab
@Suite("Off-screen window detection")
struct WindowPositionTests {
private let mainScreen = CGRect(x: 0, y: 0, width: 2560, height: 1664)
@Test("Window inside the only connected screen is not off-screen")
func onscreenWindow() {
let frame = CGRect(x: 100, y: 100, width: 1024, height: 800)
#expect(AppDelegate.isFrameOffscreen(frame, screenFrames: [mainScreen]) == false)
}
@Test("Window at coordinates from a now-disconnected external display is off-screen")
func offscreenWindow() {
// e.g. previously positioned on an external monitor to the right of the built-in display
let frame = CGRect(x: 3000, y: 200, width: 1024, height: 800)
#expect(AppDelegate.isFrameOffscreen(frame, screenFrames: [mainScreen]) == true)
}
@Test("Window merely partially overlapping a screen counts as on-screen")
func partiallyOverlappingWindow() {
let frame = CGRect(x: 2500, y: 100, width: 1024, height: 800)
#expect(AppDelegate.isFrameOffscreen(frame, screenFrames: [mainScreen]) == false)
}
@Test("Off-screen relative to one display but onscreen on a second connected display")
func multiMonitorOnscreen() {
let secondScreen = CGRect(x: 2560, y: 0, width: 1920, height: 1080)
let frame = CGRect(x: 3000, y: 200, width: 1024, height: 800)
#expect(AppDelegate.isFrameOffscreen(frame, screenFrames: [mainScreen, secondScreen]) == false)
}
@Test("No connected screens means every window is off-screen")
func noScreens() {
let frame = CGRect(x: 100, y: 100, width: 1024, height: 800)
#expect(AppDelegate.isFrameOffscreen(frame, screenFrames: []) == true)
}
}