fix(companion): bind listener by port and source screen size from snapshot

This commit is contained in:
pj committed 2026-06-07 15:45:35 +05:30
1 parent ce6b99dd7d
commit a5d6c40825
1 file changed
+14 -9
+14 -9
View File
@@ -20,7 +20,7 @@ final class Server {
.flatMap { UInt16($0) } ?? 27753 .flatMap { UInt16($0) } ?? 27753
self.port = NWEndpoint.Port(rawValue: resolvedPort)! self.port = NWEndpoint.Port(rawValue: resolvedPort)!
let parameters = NWParameters.tcp let parameters = NWParameters.tcp
parameters.requiredLocalEndpoint = NWEndpoint.hostPort(host: "127.0.0.1", port: self.port) parameters.allowLocalEndpointReuse = true
self.listener = try NWListener(using: parameters, on: self.port) self.listener = try NWListener(using: parameters, on: self.port)
} }
@@ -109,21 +109,26 @@ final class Server {
} }
private func describeScreen() -> [String: Any] { private func describeScreen() -> [String: Any] {
var bounds = CGRect.zero // The runner process has no foreground scene, so UIScreen.main.bounds
// returns a fallback size. The springboard application snapshot frame is
// the device size in points; the screen scale comes from UIScreen.
var scale: CGFloat = 1 var scale: CGFloat = 1
let collect = { let collect = { scale = UIScreen.main.scale }
let screen = UIScreen.main
bounds = screen.bounds
scale = screen.scale
}
if Thread.isMainThread { if Thread.isMainThread {
collect() collect()
} else { } else {
DispatchQueue.main.sync(execute: collect) DispatchQueue.main.sync(execute: collect)
} }
var width = 0
var height = 0
if let root = Snapshot.elements(bundleIdentifier: "com.apple.springboard").first,
let frame = root["frame"] as? [String: Any] {
width = Int(frame["width"] as? Double ?? 0)
height = Int(frame["height"] as? Double ?? 0)
}
return [ return [
"widthPoints": Int(bounds.size.width), "widthPoints": width,
"heightPoints": Int(bounds.size.height), "heightPoints": height,
"scale": Double(scale), "scale": Double(scale),
] ]
} }