Ariver
2026-06-18 1fcf6730652d509e78de4b80888772083f5b124a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import AppKit
import Foundation
 
final class FullscreenHostDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
    private let readyFile: String
    private let screenIndex: Int
    private var window: NSWindow?
    private var requestedScreenFrame: CGRect = .zero
 
    init(readyFile: String, screenIndex: Int) {
        self.readyFile = readyFile
        self.screenIndex = screenIndex
    }
 
    func applicationDidFinishLaunching(_ notification: Notification) {
        NSApp.setActivationPolicy(.regular)
 
        let screens = NSScreen.screens
        let screen = screens.indices.contains(screenIndex) ? screens[screenIndex] : NSScreen.main
        let frame = screen?.frame ?? CGRect(x: 0, y: 0, width: 1280, height: 800)
        requestedScreenFrame = frame
        let window = NSWindow(
            contentRect: CGRect(
                x: frame.midX - 480,
                y: frame.midY - 300,
                width: 960,
                height: 600
            ),
            styleMask: [.titled, .closable, .resizable, .miniaturizable],
            backing: .buffered,
            defer: false
        )
        window.title = "Aligner QA Fullscreen Host"
        window.collectionBehavior = [.fullScreenPrimary]
        window.delegate = self
 
        let label = NSTextField(labelWithString: "Aligner Fullscreen QA Host")
        label.font = .systemFont(ofSize: 36, weight: .semibold)
        label.textColor = .white
        label.alignment = .center
        label.frame = CGRect(x: 80, y: 250, width: 800, height: 80)
 
        let content = NSView(frame: window.contentView?.bounds ?? .zero)
        content.wantsLayer = true
        content.layer?.backgroundColor = NSColor(calibratedRed: 0.08, green: 0.10, blue: 0.14, alpha: 1).cgColor
        content.addSubview(label)
        window.contentView = content
 
        self.window = window
        window.makeKeyAndOrderFront(nil)
        NSApp.activate(ignoringOtherApps: true)
 
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            window.toggleFullScreen(nil)
        }
    }
 
    func windowDidEnterFullScreen(_ notification: Notification) {
        writeReady(reason: "windowDidEnterFullScreen")
    }
 
    private func writeReady(reason: String) {
        let screenDescription = NSScreen.screens.enumerated().map { index, screen in
            "screen=\(index) frame=x:\(Int(screen.frame.minX)),y:\(Int(screen.frame.minY)),w:\(Int(screen.frame.width)),h:\(Int(screen.frame.height))"
        }.joined(separator: "\n")
        let targetScreen = window?.screen
        let targetFrame = targetScreen?.frame ?? .zero
        let text = "readyReason=\(reason)\n" +
            "requestedScreenIndex=\(screenIndex)\n" +
            "requestedScreenFrame=x:\(Int(requestedScreenFrame.minX)),y:\(Int(requestedScreenFrame.minY)),w:\(Int(requestedScreenFrame.width)),h:\(Int(requestedScreenFrame.height))\n" +
            "targetScreenFrame=x:\(Int(targetFrame.minX)),y:\(Int(targetFrame.minY)),w:\(Int(targetFrame.width)),h:\(Int(targetFrame.height))\n" +
            "mouse=x:\(Int(NSEvent.mouseLocation.x)),y:\(Int(NSEvent.mouseLocation.y))\n" +
            "\(screenDescription)\n"
 
        try? text.write(toFile: readyFile, atomically: true, encoding: .utf8)
    }
}
 
func argumentValue(prefix: String) -> String? {
    CommandLine.arguments.first { $0.hasPrefix(prefix) }.map { String($0.dropFirst(prefix.count)) }
}
 
guard let readyFile = argumentValue(prefix: "--ready-file=") else {
    fputs("usage: round1-fullscreen-host.swift --ready-file=<path> [--screen-index=<index>]\n", stderr)
    exit(2)
}
 
let screenIndex = argumentValue(prefix: "--screen-index=").flatMap(Int.init) ?? 0
let delegate = FullscreenHostDelegate(readyFile: readyFile, screenIndex: screenIndex)
NSApplication.shared.delegate = delegate
NSApplication.shared.run()