Skip to content

Commit b7156ce

Browse files
authored
fix(macos): drag the window from split-view pane headers (#104048)
* fix(macos): drag the window from split-view pane headers In the macOS app, split-screen pane header rows (session title strip) now start a native window drag instead of selecting the title text. The Control UI posts an openclawWindowDrag script message on mousedown over passive header chrome; the dashboard window validates the trusted main-frame source and the in-flight left-mouse press before calling NSWindow.performDrag. Pane headers are chrome, so they are also no longer text-selectable. * refactor(macos): move the dashboard failure page out of the window controller SwiftLint type_body_length: the drag-message handler pushed DashboardWindowController past 800 lines; the failure-page HTML is presentation-only and now lives in DashboardFailurePage.
1 parent b29fe95 commit b7156ce

7 files changed

Lines changed: 316 additions & 111 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import Foundation
2+
3+
/// Renders the static error page the dashboard window shows when the Control
4+
/// UI cannot load. Presentation-only; kept out of `DashboardWindowController`
5+
/// so the controller stays focused on window/navigation behavior.
6+
enum DashboardFailurePage {
7+
static func html(title: String, message: String, detail: String?, url: URL?) -> String {
8+
let detailHTML = detail.map { "<p class=\"detail\">\(self.htmlEscape($0))</p>" } ?? ""
9+
let urlHTML = url.map { "<code>\(self.htmlEscape($0.absoluteString))</code>" } ?? ""
10+
return """
11+
<!doctype html>
12+
<html>
13+
<head>
14+
<meta charset="utf-8">
15+
<style>
16+
:root { color-scheme: light dark; }
17+
* { box-sizing: border-box; }
18+
body {
19+
margin: 0;
20+
min-height: 100vh;
21+
display: grid;
22+
place-items: center;
23+
background: #101114;
24+
color: rgba(255,255,255,.92);
25+
font: 15px -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
26+
}
27+
main {
28+
width: min(540px, calc(100vw - 72px));
29+
padding: 34px;
30+
border: 1px solid rgba(255,255,255,.12);
31+
border-radius: 22px;
32+
background: rgba(255,255,255,.035);
33+
box-shadow: 0 28px 90px rgba(0,0,0,.36);
34+
line-height: 1.45;
35+
}
36+
.badge {
37+
width: 44px;
38+
height: 44px;
39+
display: grid;
40+
place-items: center;
41+
margin-bottom: 20px;
42+
border-radius: 14px;
43+
background: rgba(255,255,255,.07);
44+
color: #ff746b;
45+
font-size: 24px;
46+
}
47+
h1 {
48+
margin: 0 0 12px;
49+
font-size: 24px;
50+
line-height: 1.16;
51+
font-weight: 700;
52+
letter-spacing: 0;
53+
}
54+
p {
55+
margin: 0;
56+
color: rgba(255,255,255,.76);
57+
font-size: 16px;
58+
}
59+
.detail {
60+
margin-top: 14px;
61+
color: rgba(255,255,255,.56);
62+
font-size: 13px;
63+
}
64+
code {
65+
display: block;
66+
margin-top: 18px;
67+
padding: 12px;
68+
border: 1px solid rgba(255,255,255,.08);
69+
border-radius: 10px;
70+
background: rgba(0,0,0,.26);
71+
color: rgba(255,255,255,.76);
72+
overflow-wrap: anywhere;
73+
font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
74+
}
75+
@media (prefers-color-scheme: light) {
76+
body { background: #f5f6f8; color: rgba(0,0,0,.86); }
77+
main {
78+
background: rgba(255,255,255,.84);
79+
border-color: rgba(0,0,0,.1);
80+
box-shadow: 0 28px 90px rgba(0,0,0,.12);
81+
}
82+
.badge { background: rgba(0,0,0,.06); }
83+
p { color: rgba(0,0,0,.68); }
84+
.detail { color: rgba(0,0,0,.54); }
85+
code {
86+
background: rgba(0,0,0,.05);
87+
border-color: rgba(0,0,0,.08);
88+
color: rgba(0,0,0,.68);
89+
}
90+
}
91+
</style>
92+
</head>
93+
<body>
94+
<main>
95+
<div class="badge">!</div>
96+
<h1>\(self.htmlEscape(title))</h1>
97+
<p>\(self.htmlEscape(message))</p>
98+
\(detailHTML)
99+
\(urlHTML)
100+
</main>
101+
</body>
102+
</html>
103+
"""
104+
}
105+
106+
private static func htmlEscape(_ value: String) -> String {
107+
value
108+
.replacingOccurrences(of: "&", with: "&amp;")
109+
.replacingOccurrences(of: "<", with: "&lt;")
110+
.replacingOccurrences(of: ">", with: "&gt;")
111+
.replacingOccurrences(of: "\"", with: "&quot;")
112+
.replacingOccurrences(of: "'", with: "&#39;")
113+
}
114+
}

‎apps/macos/Sources/OpenClaw/DashboardWindowController.swift‎

Lines changed: 45 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ private final class DashboardLinkMessageHandler: NSObject, WKScriptMessageHandle
2727
}
2828
}
2929

30+
@MainActor
31+
private final class DashboardWindowDragMessageHandler: NSObject, WKScriptMessageHandler {
32+
weak var owner: DashboardWindowController?
33+
34+
func userContentController(_: WKUserContentController, didReceive message: WKScriptMessage) {
35+
self.owner?.receiveWindowDragMessage(message)
36+
}
37+
}
38+
3039
@MainActor
3140
final class DashboardWindowController: NSWindowController, WKNavigationDelegate, WKUIDelegate, NSWindowDelegate {
3241
private static let linkMessageHandlerName = "openclawLink"
42+
private static let windowDragMessageHandlerName = "openclawWindowDrag"
3343

3444
private let webView: WKWebView
3545
private let linkBrowser: DashboardLinkBrowserView
@@ -55,6 +65,8 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
5565
config.userContentController = WKUserContentController()
5666
let linkMessageHandler = DashboardLinkMessageHandler()
5767
config.userContentController.add(linkMessageHandler, name: Self.linkMessageHandlerName)
68+
let windowDragMessageHandler = DashboardWindowDragMessageHandler()
69+
config.userContentController.add(windowDragMessageHandler, name: Self.windowDragMessageHandlerName)
5870
Self.installNativeChromeScript(into: config.userContentController)
5971
Self.installNativeAuthScript(into: config.userContentController, url: url, auth: auth)
6072

@@ -104,6 +116,7 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
104116
// optional browser collapsed until a link explicitly opens it.
105117
self.linkBrowserItem.isCollapsed = true
106118
linkMessageHandler.owner = self
119+
windowDragMessageHandler.owner = self
107120
self.webView.navigationDelegate = self
108121
self.webView.uiDelegate = self
109122
self.linkBrowser.webViewNavigationDelegate = self
@@ -229,7 +242,7 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
229242
self.refreshNativeAuthScript(url: self.currentURL, auth: self.auth)
230243
self.webView.stopLoading()
231244
self.webView.loadHTMLString(
232-
Self.failureHTML(title: title, message: message, detail: detail, url: nil),
245+
DashboardFailurePage.html(title: title, message: message, detail: detail, url: nil),
233246
baseURL: nil)
234247
self.show()
235248
}
@@ -278,6 +291,36 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
278291
}
279292
}
280293

294+
/// The Control UI posts this from mousedown on passive pane-header chrome
295+
/// (split-view session titles). WKWebView swallows titlebar-style drags, so
296+
/// the web side asks the window to take over the in-flight mouse gesture.
297+
fileprivate func receiveWindowDragMessage(_ message: WKScriptMessage) {
298+
guard message.name == Self.windowDragMessageHandlerName,
299+
message.webView === self.webView,
300+
message.frameInfo.isMainFrame,
301+
Self.isTrustedLinkSource(message.frameInfo.request.url, dashboardURL: self.currentURL),
302+
Self.isWindowDragRequest(message.body),
303+
let window
304+
else {
305+
return
306+
}
307+
// The script message arrives async; during a press the app's current
308+
// event is still the initiating left-mouse-down (or a later drag). A
309+
// finished click leaves left-mouse-up here and starts no drag.
310+
guard let event = NSApp.currentEvent,
311+
event.type == .leftMouseDown || event.type == .leftMouseDragged,
312+
event.window === window
313+
else {
314+
return
315+
}
316+
window.performDrag(with: event)
317+
}
318+
319+
static func isWindowDragRequest(_ body: Any) -> Bool {
320+
guard let payload = body as? [String: Any] else { return false }
321+
return payload["type"] as? String == "window-drag"
322+
}
323+
281324
static func linkRequest(from body: Any) -> DashboardLinkRequest? {
282325
guard let payload = body as? [String: Any],
283326
payload["type"] as? String == "open-link",
@@ -803,121 +846,13 @@ final class DashboardWindowController: NSWindowController, WKNavigationDelegate,
803846
dashboard load failed url=\(dashboardLogString(for: self.currentURL), privacy: .public) \
804847
error=\(error.localizedDescription, privacy: .public)
805848
""")
806-
let html = Self.failureHTML(
849+
let html = DashboardFailurePage.html(
807850
title: "Dashboard unavailable",
808851
message: error.localizedDescription,
809852
detail: "The dashboard window is open, but the web UI could not load from this endpoint.",
810853
url: self.currentURL)
811854
self.webView.loadHTMLString(html, baseURL: nil)
812855
}
813-
814-
private static func failureHTML(title: String, message: String, detail: String?, url: URL?) -> String {
815-
let detailHTML = detail.map { "<p class=\"detail\">\(self.htmlEscape($0))</p>" } ?? ""
816-
let urlHTML = url.map { "<code>\(self.htmlEscape($0.absoluteString))</code>" } ?? ""
817-
return """
818-
<!doctype html>
819-
<html>
820-
<head>
821-
<meta charset="utf-8">
822-
<style>
823-
:root { color-scheme: light dark; }
824-
* { box-sizing: border-box; }
825-
body {
826-
margin: 0;
827-
min-height: 100vh;
828-
display: grid;
829-
place-items: center;
830-
background: #101114;
831-
color: rgba(255,255,255,.92);
832-
font: 15px -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
833-
}
834-
main {
835-
width: min(540px, calc(100vw - 72px));
836-
padding: 34px;
837-
border: 1px solid rgba(255,255,255,.12);
838-
border-radius: 22px;
839-
background: rgba(255,255,255,.035);
840-
box-shadow: 0 28px 90px rgba(0,0,0,.36);
841-
line-height: 1.45;
842-
}
843-
.badge {
844-
width: 44px;
845-
height: 44px;
846-
display: grid;
847-
place-items: center;
848-
margin-bottom: 20px;
849-
border-radius: 14px;
850-
background: rgba(255,255,255,.07);
851-
color: #ff746b;
852-
font-size: 24px;
853-
}
854-
h1 {
855-
margin: 0 0 12px;
856-
font-size: 24px;
857-
line-height: 1.16;
858-
font-weight: 700;
859-
letter-spacing: 0;
860-
}
861-
p {
862-
margin: 0;
863-
color: rgba(255,255,255,.76);
864-
font-size: 16px;
865-
}
866-
.detail {
867-
margin-top: 14px;
868-
color: rgba(255,255,255,.56);
869-
font-size: 13px;
870-
}
871-
code {
872-
display: block;
873-
margin-top: 18px;
874-
padding: 12px;
875-
border: 1px solid rgba(255,255,255,.08);
876-
border-radius: 10px;
877-
background: rgba(0,0,0,.26);
878-
color: rgba(255,255,255,.76);
879-
overflow-wrap: anywhere;
880-
font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
881-
}
882-
@media (prefers-color-scheme: light) {
883-
body { background: #f5f6f8; color: rgba(0,0,0,.86); }
884-
main {
885-
background: rgba(255,255,255,.84);
886-
border-color: rgba(0,0,0,.1);
887-
box-shadow: 0 28px 90px rgba(0,0,0,.12);
888-
}
889-
.badge { background: rgba(0,0,0,.06); }
890-
p { color: rgba(0,0,0,.68); }
891-
.detail { color: rgba(0,0,0,.54); }
892-
code {
893-
background: rgba(0,0,0,.05);
894-
border-color: rgba(0,0,0,.08);
895-
color: rgba(0,0,0,.68);
896-
}
897-
}
898-
</style>
899-
</head>
900-
<body>
901-
<main>
902-
<div class="badge">!</div>
903-
<h1>\(self.htmlEscape(title))</h1>
904-
<p>\(self.htmlEscape(message))</p>
905-
\(detailHTML)
906-
\(urlHTML)
907-
</main>
908-
</body>
909-
</html>
910-
"""
911-
}
912-
913-
private static func htmlEscape(_ value: String) -> String {
914-
value
915-
.replacingOccurrences(of: "&", with: "&amp;")
916-
.replacingOccurrences(of: "<", with: "&lt;")
917-
.replacingOccurrences(of: ">", with: "&gt;")
918-
.replacingOccurrences(of: "\"", with: "&quot;")
919-
.replacingOccurrences(of: "'", with: "&#39;")
920-
}
921856
}
922857

923858
#if DEBUG

‎apps/macos/Tests/OpenClawIPCTests/DashboardWindowSmokeTests.swift‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ struct DashboardWindowSmokeTests {
8686
]) == nil)
8787
}
8888

89+
@Test func `dashboard accepts only typed window drag requests`() {
90+
#expect(DashboardWindowController.isWindowDragRequest(["type": "window-drag"]))
91+
#expect(!DashboardWindowController.isWindowDragRequest(["type": "open-link"]))
92+
#expect(!DashboardWindowController.isWindowDragRequest(["type": 1]))
93+
#expect(!DashboardWindowController.isWindowDragRequest("window-drag"))
94+
}
95+
8996
@Test func `dashboard trusts only its main control path for link messages`() throws {
9097
let dashboard = try #require(URL(string: "http://127.0.0.1:18789/control/"))
9198
let trusted = try #require(URL(string: "http://127.0.0.1:18789/control/chat"))

0 commit comments

Comments
 (0)