Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
321 lines
10 KiB
Swift
321 lines
10 KiB
Swift
import Observation
|
|
import OpenClawKit
|
|
import UIKit
|
|
import WebKit
|
|
|
|
@MainActor
|
|
@Observable
|
|
final class ScreenController {
|
|
private weak var activeWebView: WKWebView?
|
|
|
|
var urlString: String = ""
|
|
var errorText: String?
|
|
var isCanvasPresented: Bool = false
|
|
|
|
/// Callback invoked when an openclaw:// deep link is tapped in the canvas
|
|
var onDeepLink: ((URL) -> Void)?
|
|
|
|
/// Callback invoked when the user clicks an A2UI action (e.g. button) inside the canvas web UI.
|
|
var onA2UIAction: (([String: Any]) -> Void)?
|
|
|
|
private var debugStatusEnabled: Bool = false
|
|
private var debugStatusTitle: String?
|
|
private var debugStatusSubtitle: String?
|
|
private var homeCanvasStateJSON: String?
|
|
|
|
init() {
|
|
self.reload()
|
|
}
|
|
|
|
func navigate(to urlString: String, trustA2UIActions _: Bool = false) {
|
|
let trimmed = urlString.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
if trimmed.isEmpty {
|
|
self.urlString = ""
|
|
self.reload()
|
|
return
|
|
}
|
|
if let url = URL(string: trimmed),
|
|
!url.isFileURL,
|
|
let host = url.host,
|
|
LoopbackHost.isLoopback(host)
|
|
{
|
|
// Never try to load loopback URLs from a remote gateway.
|
|
self.showDefaultCanvas()
|
|
return
|
|
}
|
|
self.urlString = (trimmed == "/" ? "" : trimmed)
|
|
self.reload()
|
|
}
|
|
|
|
func reload() {
|
|
self.applyScrollBehavior()
|
|
guard let webView = self.activeWebView else { return }
|
|
|
|
let trimmed = self.urlString.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
if trimmed.isEmpty {
|
|
guard let url = Self.canvasScaffoldURL else { return }
|
|
self.errorText = nil
|
|
webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
|
|
return
|
|
}
|
|
|
|
guard let url = URL(string: trimmed) else {
|
|
self.errorText = "Invalid URL: \(trimmed)"
|
|
return
|
|
}
|
|
self.errorText = nil
|
|
if url.isFileURL {
|
|
webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
|
|
} else {
|
|
webView.load(URLRequest(url: url))
|
|
}
|
|
}
|
|
|
|
func showDefaultCanvas() {
|
|
self.urlString = ""
|
|
self.reload()
|
|
}
|
|
|
|
func presentDefaultCanvas() {
|
|
self.isCanvasPresented = true
|
|
self.showDefaultCanvas()
|
|
}
|
|
|
|
func present(urlString: String) {
|
|
self.isCanvasPresented = true
|
|
self.navigate(to: urlString)
|
|
}
|
|
|
|
func hideCanvas() {
|
|
self.isCanvasPresented = false
|
|
self.showDefaultCanvas()
|
|
}
|
|
|
|
func showLocalA2UI() {
|
|
self.isCanvasPresented = true
|
|
guard let url = Self.localA2UIURL else {
|
|
self.showDefaultCanvas()
|
|
return
|
|
}
|
|
self.urlString = url.absoluteString
|
|
self.reload()
|
|
}
|
|
|
|
func isShowingLocalA2UI() -> Bool {
|
|
guard let url = URL(string: self.urlString),
|
|
url.isFileURL,
|
|
let expected = Self.localA2UIURL
|
|
else { return false }
|
|
return url.standardizedFileURL == expected.standardizedFileURL
|
|
}
|
|
|
|
func setDebugStatusEnabled(_ enabled: Bool) {
|
|
self.debugStatusEnabled = enabled
|
|
self.applyDebugStatusIfNeeded()
|
|
}
|
|
|
|
func updateDebugStatus(title: String?, subtitle: String?) {
|
|
self.debugStatusTitle = title
|
|
self.debugStatusSubtitle = subtitle
|
|
self.applyDebugStatusIfNeeded()
|
|
}
|
|
|
|
func applyDebugStatusIfNeeded() {
|
|
guard let webView = self.activeWebView else { return }
|
|
WebViewJavaScriptSupport.applyDebugStatus(
|
|
webView: webView,
|
|
enabled: self.debugStatusEnabled,
|
|
title: self.debugStatusTitle,
|
|
subtitle: self.debugStatusSubtitle)
|
|
}
|
|
|
|
func updateHomeCanvasState(json: String?) {
|
|
self.homeCanvasStateJSON = json
|
|
self.applyHomeCanvasStateIfNeeded()
|
|
}
|
|
|
|
func applyHomeCanvasStateIfNeeded() {
|
|
guard let webView = self.activeWebView else { return }
|
|
let payload = self.homeCanvasStateJSON ?? "null"
|
|
let js = """
|
|
(() => {
|
|
try {
|
|
const api = globalThis.__openclaw;
|
|
if (!api || typeof api.renderHome !== 'function') return;
|
|
api.renderHome(\(payload));
|
|
} catch (_) {}
|
|
})()
|
|
"""
|
|
webView.evaluateJavaScript(js) { _, _ in }
|
|
}
|
|
|
|
func waitForA2UIReady(timeoutMs: Int) async -> Bool {
|
|
let clock = ContinuousClock()
|
|
let deadline = clock.now.advanced(by: .milliseconds(timeoutMs))
|
|
while clock.now < deadline {
|
|
do {
|
|
let res = try await self.eval(javaScript: """
|
|
(() => {
|
|
try {
|
|
const host = globalThis.openclawA2UI;
|
|
return !!host && typeof host.applyMessages === 'function';
|
|
} catch (_) { return false; }
|
|
})()
|
|
""")
|
|
let trimmed = res.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
|
|
if trimmed == "true" || trimmed == "1" { return true }
|
|
} catch {
|
|
// ignore; page likely still loading
|
|
}
|
|
try? await Task.sleep(nanoseconds: 120_000_000)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func eval(javaScript: String) async throws -> String {
|
|
guard let webView = self.activeWebView else {
|
|
throw NSError(domain: "Screen", code: 3, userInfo: [
|
|
NSLocalizedDescriptionKey: "web view unavailable",
|
|
])
|
|
}
|
|
return try await WebViewJavaScriptSupport.evaluateToString(webView: webView, javaScript: javaScript)
|
|
}
|
|
|
|
func snapshotBase64(
|
|
maxWidth: CGFloat? = nil,
|
|
format: OpenClawCanvasSnapshotFormat,
|
|
quality: Double? = nil) async throws -> String
|
|
{
|
|
let image = try await self.snapshotImage(maxWidth: maxWidth)
|
|
|
|
let data: Data?
|
|
switch format {
|
|
case .png:
|
|
data = image.pngData()
|
|
case .jpeg:
|
|
let q = (quality ?? 0.82).clamped(to: 0.1...1.0)
|
|
data = image.jpegData(compressionQuality: q)
|
|
}
|
|
guard let data else {
|
|
throw NSError(domain: "Screen", code: 1, userInfo: [
|
|
NSLocalizedDescriptionKey: "snapshot encode failed",
|
|
])
|
|
}
|
|
return data.base64EncodedString()
|
|
}
|
|
|
|
private func snapshotImage(maxWidth: CGFloat?) async throws -> UIImage {
|
|
let config = WKSnapshotConfiguration()
|
|
if let maxWidth {
|
|
config.snapshotWidth = NSNumber(value: Double(maxWidth))
|
|
}
|
|
guard let webView = self.activeWebView else {
|
|
throw NSError(domain: "Screen", code: 3, userInfo: [
|
|
NSLocalizedDescriptionKey: "web view unavailable",
|
|
])
|
|
}
|
|
return try await withCheckedThrowingContinuation { cont in
|
|
webView.takeSnapshot(with: config) { image, error in
|
|
if let error {
|
|
cont.resume(throwing: error)
|
|
return
|
|
}
|
|
guard let image else {
|
|
cont.resume(throwing: NSError(domain: "Screen", code: 2, userInfo: [
|
|
NSLocalizedDescriptionKey: "snapshot failed",
|
|
]))
|
|
return
|
|
}
|
|
cont.resume(returning: image)
|
|
}
|
|
}
|
|
}
|
|
|
|
func attachWebView(_ webView: WKWebView) {
|
|
self.activeWebView = webView
|
|
self.reload()
|
|
self.applyDebugStatusIfNeeded()
|
|
self.applyHomeCanvasStateIfNeeded()
|
|
}
|
|
|
|
func detachWebView(_ webView: WKWebView) {
|
|
guard self.activeWebView === webView else { return }
|
|
self.activeWebView = nil
|
|
}
|
|
|
|
private static func bundledResourceURL(
|
|
name: String,
|
|
ext: String,
|
|
subdirectory: String)
|
|
-> URL?
|
|
{
|
|
let bundle = OpenClawKitResources.bundle
|
|
return bundle.url(forResource: name, withExtension: ext, subdirectory: subdirectory)
|
|
?? bundle.url(forResource: name, withExtension: ext)
|
|
}
|
|
|
|
private static let canvasScaffoldURL: URL? = ScreenController.bundledResourceURL(
|
|
name: "scaffold",
|
|
ext: "html",
|
|
subdirectory: "CanvasScaffold")
|
|
|
|
private static let localA2UIURL: URL? = ScreenController.bundledResourceURL(
|
|
name: "index",
|
|
ext: "html",
|
|
subdirectory: "CanvasA2UI")
|
|
|
|
func isTrustedCanvasUIURL(_ url: URL) -> Bool {
|
|
if url.isFileURL {
|
|
let std = url.standardizedFileURL
|
|
if let expected = Self.canvasScaffoldURL,
|
|
std == expected.standardizedFileURL
|
|
{
|
|
return true
|
|
}
|
|
if let expected = Self.localA2UIURL,
|
|
std == expected.standardizedFileURL
|
|
{
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
return false
|
|
}
|
|
|
|
nonisolated static func parseA2UIActionBody(_ body: Any) -> [String: Any]? {
|
|
if let dict = body as? [String: Any] { return dict.isEmpty ? nil : dict }
|
|
if let str = body as? String,
|
|
let data = str.data(using: .utf8),
|
|
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any]
|
|
{
|
|
return json.isEmpty ? nil : json
|
|
}
|
|
if let dict = body as? [AnyHashable: Any] {
|
|
let mapped = dict.reduce(into: [String: Any]()) { acc, pair in
|
|
guard let key = pair.key as? String else { return }
|
|
acc[key] = pair.value
|
|
}
|
|
return mapped.isEmpty ? nil : mapped
|
|
}
|
|
return nil
|
|
}
|
|
|
|
private func applyScrollBehavior() {
|
|
guard let webView = self.activeWebView else { return }
|
|
let trimmed = self.urlString.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
let allowScroll = !trimmed.isEmpty
|
|
let scrollView = webView.scrollView
|
|
// Default canvas needs raw touch events; external pages should scroll.
|
|
scrollView.isScrollEnabled = allowScroll
|
|
scrollView.bounces = allowScroll
|
|
}
|
|
}
|
|
|
|
extension Double {
|
|
fileprivate func clamped(to range: ClosedRange<Double>) -> Double {
|
|
if self < range.lowerBound { return range.lowerBound }
|
|
if self > range.upperBound { return range.upperBound }
|
|
return self
|
|
}
|
|
}
|