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()
|