Skip to content

Commit 2278ca6

Browse files
authored
feat(ui): click the desktop to take control (#123096)
* feat(ui): click the desktop to take control View-only desktop connections now escalate to control by clicking anywhere on the desktop stage (transparent overlay button, keyboard accessible, same 'Take control' accessible name). The toolbar button is removed; the connecting status overlay becomes click-through so control can still be requested mid-connect. * fix(ui): use cursor-action token for desktop take-control overlay * test(ui): prove take-control click above a real noVNC surface In-page RFB 3.8 fake server (security None) lets the production DesktopClient drive the real noVNC client; the overlay click must hit-test above the mounted canvas and reconnect with control. * fix(ui): narrow fake RFB socket payload to ArrayBuffer-backed bytes
1 parent 62ecb31 commit 2278ca6

5 files changed

Lines changed: 183 additions & 14 deletions

File tree

‎docs/gateway/cloud-workers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ Set `"desktop": true` in a crabbox profile's `settings` to lease worker boxes wi
204204

205205
Operators with `operator.admin` access watch and control the desktop from the Control UI **Desktop** panel (also in the command palette). The panel lists desktop-capable environments from `environments.list`, shows only apps the provider advertised, and launches those apps through `worker.desktop.launch`. It connects through the Gateway, which forwards the box's loopback VNC over the same pinned SSH transport used for worker traffic — the desktop is never exposed on the box's network, and the VNC password is delivered only inside the authenticated `worker.desktop.observe` RPC result, never stored by the Gateway.
206206

207-
Connections start view-only. **Take control** requests an input-capable connection; only one controller is active at a time, and taking control disconnects the previous controller (they are downgraded to view-only). Up to 8 observers can watch one environment. The desktop forward starts on first observe and shuts down about a minute after the last observer disconnects; stopping or reclaiming the environment tears it down immediately.
207+
Connections start view-only. Clicking the desktop takes control (an input-capable connection); only one controller is active at a time, and taking control disconnects the previous controller (they are downgraded to view-only). Up to 8 observers can watch one environment. The desktop forward starts on first observe and shuts down about a minute after the last observer disconnects; stopping or reclaiming the environment tears it down immediately.
208208

209209
The Browser launcher starts one visible Chrome or Chromium process on the worker display with raw CDP bound to `127.0.0.1` and a fresh lease-scoped user-data directory. It does not import cookies, attach the Chrome extension relay, or use Chrome MCP. The operator toolbar and cloud-worker agent share this process. A worker turn receives the normal `browser` tool only when the lease advertises Browser, the bundled Browser plugin is active, and normal tool policy allows `browser`; workers without that capability keep the existing coding-tool catalog. Generic desktop `computer` control is not part of this Labs surface.
210210

‎ui/src/components/desktop/desktop-panel-launcher-styles.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export const desktopPanelLauncherStyles = css`
5555
.desktop-connecting {
5656
position: absolute;
5757
inset: 0;
58+
/* Status overlay only; clicks must reach the take-control surface below. */
59+
pointer-events: none;
5860
display: flex;
5961
align-items: center;
6062
justify-content: center;

‎ui/src/components/desktop/desktop-panel-styles.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,17 @@ export const desktopPanelStyles = css`
161161
inset: 0;
162162
background: var(--bg);
163163
}
164+
/* View-only affordance: clicking anywhere on the desktop takes control. */
165+
.desktop-stage__take-control {
166+
position: absolute;
167+
inset: 0;
168+
border: 0;
169+
padding: 0;
170+
background: transparent;
171+
cursor: var(--cursor-action, pointer);
172+
}
173+
.desktop-stage__take-control:focus-visible {
174+
outline: 2px solid var(--accent, #ff5c5c);
175+
outline-offset: -2px;
176+
}
164177
`;

‎ui/src/components/desktop/desktop-panel.ts‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -596,18 +596,6 @@ class OpenClawDesktopPanel extends OpenClawLitElement {
596596
</div>`
597597
: nothing}
598598
<span class="desktop-toolbar__spacer"></span>
599-
${!this.controlling
600-
? html`<button
601-
class="desktop-toolbar-action"
602-
type="button"
603-
title=${t("desktop.takeControl")}
604-
aria-label=${t("desktop.takeControl")}
605-
@click=${() =>
606-
this.environmentId && void this.connectEnvironment(this.environmentId, true)}
607-
>
608-
${t("desktop.takeControl")}
609-
</button>`
610-
: nothing}
611599
<button
612600
class="desktop-toolbar-action"
613601
type="button"
@@ -620,6 +608,16 @@ class OpenClawDesktopPanel extends OpenClawLitElement {
620608
</div>
621609
<div class="desktop-stage">
622610
<div class="desktop-surface"></div>
611+
${!this.controlling
612+
? html`<button
613+
class="desktop-stage__take-control"
614+
type="button"
615+
title=${t("desktop.takeControl")}
616+
aria-label=${t("desktop.takeControl")}
617+
@click=${() =>
618+
this.environmentId && void this.connectEnvironment(this.environmentId, true)}
619+
></button>`
620+
: nothing}
623621
${this.state === "connecting"
624622
? html`<div class="desktop-connecting" role="status" aria-live="polite">
625623
<span class="desktop-connecting__monitor" aria-hidden="true">${icons.monitor}</span>

‎ui/src/e2e/desktop-panel.e2e.test.ts‎

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,24 @@ suite.define(() => {
668668
await panel.getByRole("button", { name: "Connect", exact: true }).click();
669669
await expect.poll(async () => (await gateway.getRequests("desktop.observe")).length).toBe(2);
670670

671-
await panel.getByRole("button", { name: "Take control", exact: true }).click();
671+
const takeControl = panel.getByRole("button", { name: "Take control", exact: true });
672+
const overlayCoversStage = await panel.evaluate((element) => {
673+
const stage = element.shadowRoot?.querySelector<HTMLElement>(".desktop-stage");
674+
const overlay = element.shadowRoot?.querySelector<HTMLElement>(
675+
".desktop-stage__take-control",
676+
);
677+
if (!stage || !overlay) {
678+
return false;
679+
}
680+
const stageRect = stage.getBoundingClientRect();
681+
const overlayRect = overlay.getBoundingClientRect();
682+
return (
683+
Math.abs(stageRect.width - overlayRect.width) < 1 &&
684+
Math.abs(stageRect.height - overlayRect.height) < 1
685+
);
686+
});
687+
expect(overlayCoversStage).toBe(true);
688+
await takeControl.click();
672689
await expect.poll(async () => (await gateway.getRequests("desktop.observe")).length).toBe(3);
673690
const observeRequests = await gateway.getRequests("desktop.observe");
674691
expect(observeRequests[2]?.params).toEqual({
@@ -685,6 +702,145 @@ suite.define(() => {
685702
});
686703
});
687704

705+
it("takes control by clicking a real noVNC-mounted desktop", async () => {
706+
await suite.withPage({ serviceWorkers: "block" }, async ({ page }) => {
707+
const gateway = await installMockGateway(page, {
708+
featureMethods: ["desktop.observe", "environments.list"],
709+
methodResponses: {
710+
"sessions.list": sessionsList("active"),
711+
"environments.list": { environments: [workerDesktopEnvironment] },
712+
"desktop.observe": {
713+
cases: [
714+
{
715+
match: {
716+
source: { kind: "environment", environmentId: "worker-desktop-1" },
717+
control: false,
718+
},
719+
response: {
720+
transport: "rfb",
721+
wsPath: "/desktop/observe?token=view",
722+
expiresAtMs: 60_000,
723+
control: false,
724+
},
725+
},
726+
{
727+
match: {
728+
source: { kind: "environment", environmentId: "worker-desktop-1" },
729+
control: true,
730+
},
731+
response: {
732+
transport: "rfb",
733+
wsPath: "/desktop/observe?token=control",
734+
expiresAtMs: 60_000,
735+
control: true,
736+
},
737+
},
738+
],
739+
},
740+
},
741+
});
742+
await page.goto(`${suite.server.baseUrl}chat`);
743+
// Route desktop observe sockets to an in-page RFB 3.8 server (security
744+
// None) so the production DesktopClient drives the real noVNC client.
745+
await page.evaluate(() => {
746+
const GatewaySocket = window.WebSocket;
747+
class FakeRfbSocket extends EventTarget {
748+
binaryType = "arraybuffer";
749+
protocol = "";
750+
readyState = 0;
751+
onerror: ((event: Event) => void) | null = null;
752+
onmessage: ((event: MessageEvent<ArrayBuffer>) => void) | null = null;
753+
onopen: ((event: Event) => void) | null = null;
754+
onclose: ((event: CloseEvent) => void) | null = null;
755+
private handshake = 0;
756+
constructor() {
757+
super();
758+
setTimeout(() => {
759+
if (this.readyState !== 0) {
760+
return;
761+
}
762+
this.readyState = 1;
763+
this.onopen?.(new Event("open"));
764+
this.deliver(new TextEncoder().encode("RFB 003.008\n"));
765+
}, 0);
766+
}
767+
private deliver(bytes: Uint8Array<ArrayBuffer>): void {
768+
setTimeout(() => {
769+
if (this.readyState === 1) {
770+
this.onmessage?.(new MessageEvent("message", { data: bytes.buffer }));
771+
}
772+
}, 0);
773+
}
774+
send(): void {
775+
// Handshake replies are fixed-size, so respond by stage instead of
776+
// parsing: version -> security types, choice -> ok, init -> ServerInit.
777+
this.handshake += 1;
778+
if (this.handshake === 1) {
779+
this.deliver(new Uint8Array([1, 1]));
780+
} else if (this.handshake === 2) {
781+
this.deliver(new Uint8Array([0, 0, 0, 0]));
782+
} else if (this.handshake === 3) {
783+
const name = new TextEncoder().encode("fake-desktop");
784+
const init = new Uint8Array(24 + name.length);
785+
const view = new DataView(init.buffer);
786+
view.setUint16(0, 800);
787+
view.setUint16(2, 600);
788+
init.set([32, 24, 0, 1], 4);
789+
view.setUint16(8, 255);
790+
view.setUint16(10, 255);
791+
view.setUint16(12, 255);
792+
init.set([16, 8, 0], 14);
793+
view.setUint32(20, name.length);
794+
init.set(name, 24);
795+
this.deliver(init);
796+
}
797+
}
798+
close(code = 1000, reason = ""): void {
799+
if (this.readyState === 3) {
800+
return;
801+
}
802+
this.readyState = 3;
803+
const event = new CloseEvent("close", { code, reason });
804+
this.onclose?.(event);
805+
this.dispatchEvent(new CloseEvent("close", { code, reason }));
806+
}
807+
}
808+
const RoutedSocket = function (url: string, protocols?: string | string[]) {
809+
return url.includes("/desktop/observe")
810+
? new FakeRfbSocket()
811+
: new GatewaySocket(url, protocols);
812+
};
813+
RoutedSocket.prototype = GatewaySocket.prototype;
814+
Object.assign(RoutedSocket, {
815+
CONNECTING: 0,
816+
OPEN: 1,
817+
CLOSING: 2,
818+
CLOSED: 3,
819+
});
820+
window.WebSocket = RoutedSocket as unknown as typeof WebSocket;
821+
});
822+
823+
await openDirectDesktop(page, "worker-desktop-1");
824+
const panel = page.locator("openclaw-desktop-panel");
825+
await panel.locator("section[aria-label='Desktop']").waitFor();
826+
await panel.locator(".desktop-surface canvas").waitFor();
827+
828+
const takeControl = panel.getByRole("button", { name: "Take control", exact: true });
829+
await takeControl.waitFor();
830+
// Playwright refuses the click if the noVNC canvas intercepted it, so a
831+
// successful click proves the overlay hit-tests above the real surface.
832+
await takeControl.click();
833+
await expect.poll(async () => (await gateway.getRequests("desktop.observe")).length).toBe(2);
834+
const observeRequests = await gateway.getRequests("desktop.observe");
835+
expect(observeRequests[1]?.params).toEqual({
836+
source: { kind: "environment", environmentId: "worker-desktop-1" },
837+
control: true,
838+
});
839+
await panel.locator(".desktop-surface canvas").waitFor();
840+
expect(await takeControl.count()).toBe(0);
841+
});
842+
});
843+
688844
it("shows only apps advertised by the selected environment", async () => {
689845
await suite.withPage({ serviceWorkers: "block" }, async ({ page }) => {
690846
const gateway = await installMockGateway(page, {

0 commit comments

Comments
 (0)