Skip to content
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

Sort json keys for custom layout ids #1592

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions Amethyst/Layout/Layouts/CustomLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class CustomLayout<Window: WindowType>: StatefulLayout<Window> {
let window = JSWindow<Window>(id: id, window: layoutWindow)
return partialResult.merging([layoutWindow.id: window]) { current, _ in return current }
}
let jsWindowsArg = windows.map { window -> [String: Any] in
let jsWindowsArg = windows.map { window -> [String: Any?] in
let jsWindow = jsWindows[window.id]!
return [
"id": jsWindow.id,
Expand All @@ -162,7 +162,7 @@ class CustomLayout<Window: WindowType>: StatefulLayout<Window> {
]
}

let extendedFrames: [[String: Any]]? = extendedFrameAssignments(windowSet, on: screen)?.compactMap { frameAssignmentOperation in
let extendedFrames: [[String: Any?]]? = extendedFrameAssignments(windowSet, on: screen)?.compactMap { frameAssignmentOperation in
let frameAssignment = frameAssignmentOperation.frameAssignment
guard let jsWindow = jsWindows[frameAssignment.window.id] else {
return nil
Expand Down Expand Up @@ -273,15 +273,20 @@ class CustomLayout<Window: WindowType>: StatefulLayout<Window> {
}

private func idHash(forWindowID windowID: WindowID) -> String? {
guard let encodedID = try? JSONEncoder().encode(windowID) else {
return nil
}
do {
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys

var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
encodedID.withUnsafeBytes {
_ = CC_SHA256($0.baseAddress, CC_LONG(encodedID.count), &hash)
let encodedID = try encoder.encode(windowID)
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
encodedID.withUnsafeBytes {
_ = CC_SHA256($0.baseAddress, CC_LONG(encodedID.count), &hash)
}
return hash.map { String(format: "%02hhx", $0) }.joined()
} catch {
log.warning("Failed to hash window id: \(error)")
return nil
}
return hash.map { String(format: "%02hhx", $0) }.joined()
}

private func jsChange(forChange change: Change<Window>) -> [String: String] {
Expand Down
Loading