Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : SP Term9// File : Packages/SPTermKit/Sources/WindowManager/ScreenLayout.swift10// Module : WindowManager11// Repository : https://git.spboucher.ai/sp-term12// Created : 2026-11-1713// Last Modified : 2026-11-1714//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : None specified (no license file in this repository)80// Maintainer : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import CoreGraphics88import Foundation89import WorkspaceKit9091/// A display as the window manager sees it. Built from `NSScreen` in the app, or by hand in tests,92/// so placement logic never touches AppKit and can be exercised with simulated monitors.93public struct ScreenDescriptor: Sendable, Hashable, Identifiable {94 public var id: UInt3295 public var name: String96 /// Full frame in global coordinates (origin bottom-left of the main display).97 public var frame: CGRect98 /// Frame without menu bar and Dock.99 public var visibleFrame: CGRect100 public var scale: CGFloat101 public var isMain: Bool102103 public init(id: UInt32, name: String, frame: CGRect, visibleFrame: CGRect? = nil, scale: CGFloat = 2, isMain: Bool = false) {104 self.id = id105 self.name = name106 self.frame = frame107 self.visibleFrame = visibleFrame ?? frame108 self.scale = scale109 self.isMain = isMain110 }111112 public var reference: ScreenReference {113 ScreenReference(displayID: id, name: name, frame: frame, isMain: isMain)114 }115}116117/// Why a window ended up where it did; shown in logs and asserted in tests.118public enum PlacementReason: String, Sendable, Equatable {119 /// The saved display is still there with the same geometry.120 case sameScreen121 /// The saved display is there but its resolution/arrangement changed; position was rescaled.122 case screenGeometryChanged123 /// The saved display was matched by name and size after its id changed.124 case matchedByName125 /// The saved display is gone; the window moved to another display.126 case screenMissing127 /// No saved position; default placement.128 case defaultPlacement129}130131public struct Placement: Sendable, Equatable {132 public var frame: CGRect133 public var screenID: UInt32134 public var reason: PlacementReason135}136137/// Change between two display configurations.138public struct ScreenChange: Sendable, Equatable {139 public var added: [UInt32]140 public var removed: [UInt32]141 /// Displays still present whose frame, visible frame or scale changed.142 public var changed: [UInt32]143 public var isEmpty: Bool { added.isEmpty && removed.isEmpty && changed.isEmpty }144145 public static func between(_ old: [ScreenDescriptor], _ new: [ScreenDescriptor]) -> ScreenChange {146 let oldByID = Dictionary(old.map { ($0.id, $0) }, uniquingKeysWith: { a, _ in a })147 let newByID = Dictionary(new.map { ($0.id, $0) }, uniquingKeysWith: { a, _ in a })148 return ScreenChange(149 added: new.map(\.id).filter { oldByID[$0] == nil },150 removed: old.map(\.id).filter { newByID[$0] == nil },151 changed: new.filter { screen in152 guard let before = oldByID[screen.id] else { return false }153 return before.frame != screen.frame || before.visibleFrame != screen.visibleFrame || before.scale != screen.scale154 }.map(\.id))155 }156}157