Repository navigation
fix: preserve window layout identities across restarts and screen lock #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3aa96a3
fix: restore browser windows after tab changes
ai-eks ff5e927
fix: capture window layouts on screen lock
ai-eks 99b9c23
fix: persist verified window identities for saved layouts
ai-eks 66725fd
fix: preserve original window bounds during screen lock
ai-eks a2fad94
refactor: require snapshot IDs for window captures
ai-eks 7b34caa
fix: complete lock captures and optimize window assignment
ai-eks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import AppKit | ||
| import ApplicationServices | ||
| import Darwin | ||
|
|
||
| enum WindowIdentityResolver { | ||
| private typealias GetWindowID = @convention(c) (AXUIElement, UnsafeMutablePointer<CGWindowID>) -> AXError | ||
|
|
||
| // 私有接口:用于精确关联 AX 对象与 WindowServer 窗口,不能用标题/尺寸推测。 | ||
| // 动态加载失败时禁用这一层身份核验,继续原有匹配流程。 | ||
| private static let getWindowID: GetWindowID? = { | ||
| guard let handle = dlopen( | ||
| "/System/Library/Frameworks/ApplicationServices.framework/Frameworks/HIServices.framework/HIServices", | ||
| RTLD_LAZY | RTLD_LOCAL | ||
| ) else { return nil } | ||
| guard let symbol = dlsym(handle, "_AXUIElementGetWindow") else { | ||
| dlclose(handle) | ||
| return nil | ||
| } | ||
| // 保留库句柄,确保函数指针在进程存续期间有效。 | ||
| return unsafeBitCast(symbol, to: GetWindowID.self) | ||
| }() | ||
|
|
||
| static func windowID(for element: AXUIElement) -> CGWindowID? { | ||
| guard let getWindowID else { return nil } | ||
| var windowID = kCGNullWindowID | ||
| guard getWindowID(element, &windowID) == .success, | ||
| windowID != kCGNullWindowID else { return nil } | ||
| return windowID | ||
| } | ||
|
|
||
| static func currentSessionIdentifier() -> String? { | ||
| var length = 0 | ||
| guard sysctlbyname("kern.bootsessionuuid", nil, &length, nil, 0) == 0, | ||
| length > 0 else { return nil } | ||
| var bootSession = [CChar](repeating: 0, count: length) | ||
| guard sysctlbyname("kern.bootsessionuuid", &bootSession, &length, nil, 0) == 0 else { return nil } | ||
| var auditInfo = auditinfo_addr_t() | ||
| guard getaudit_addr(&auditInfo, Int32(MemoryLayout<auditinfo_addr_t>.size)) == 0 else { return nil } | ||
| return "\(String(cString: bootSession)):\(geteuid()):\(auditInfo.ai_asid)" | ||
| } | ||
|
|
||
| static func identity( | ||
| for application: NSRunningApplication, | ||
| windowID: CGWindowID?, | ||
| sessionIdentifier: String? | ||
| ) -> WindowRuntimeIdentity? { | ||
| guard let windowID, windowID != kCGNullWindowID, | ||
| let sessionIdentifier, !sessionIdentifier.isEmpty, | ||
| let bundleIdentifier = application.bundleIdentifier, | ||
| let launchDate = application.launchDate, | ||
| application.processIdentifier > 0, | ||
| !application.isTerminated else { return nil } | ||
| return WindowRuntimeIdentity( | ||
| bundleIdentifier: bundleIdentifier, | ||
| processIdentifier: application.processIdentifier, | ||
| processLaunchDate: launchDate, | ||
| sessionIdentifier: sessionIdentifier, | ||
| windowID: windowID | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.