|
| 1 | +package ai.openclaw.app.ui |
| 2 | + |
| 3 | +import ai.openclaw.app.MainViewModel |
| 4 | +import ai.openclaw.app.NodeRuntime |
| 5 | +import ai.openclaw.app.ui.design.ClawPlainIconButton |
| 6 | +import ai.openclaw.app.ui.design.ClawScaffold |
| 7 | +import ai.openclaw.app.ui.design.ClawTheme |
| 8 | +import android.annotation.SuppressLint |
| 9 | +import android.view.View |
| 10 | +import android.webkit.WebSettings |
| 11 | +import android.webkit.WebView |
| 12 | +import android.webkit.WebViewClient |
| 13 | +import androidx.compose.foundation.layout.Arrangement |
| 14 | +import androidx.compose.foundation.layout.Box |
| 15 | +import androidx.compose.foundation.layout.Column |
| 16 | +import androidx.compose.foundation.layout.PaddingValues |
| 17 | +import androidx.compose.foundation.layout.Row |
| 18 | +import androidx.compose.foundation.layout.fillMaxSize |
| 19 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 20 | +import androidx.compose.foundation.layout.imePadding |
| 21 | +import androidx.compose.foundation.layout.padding |
| 22 | +import androidx.compose.material.icons.Icons |
| 23 | +import androidx.compose.material.icons.automirrored.filled.ArrowBack |
| 24 | +import androidx.compose.material.icons.outlined.Terminal |
| 25 | +import androidx.compose.material3.Icon |
| 26 | +import androidx.compose.material3.Text |
| 27 | +import androidx.compose.runtime.Composable |
| 28 | +import androidx.compose.runtime.DisposableEffect |
| 29 | +import androidx.compose.runtime.collectAsState |
| 30 | +import androidx.compose.runtime.getValue |
| 31 | +import androidx.compose.runtime.key |
| 32 | +import androidx.compose.runtime.remember |
| 33 | +import androidx.compose.ui.Alignment |
| 34 | +import androidx.compose.ui.Modifier |
| 35 | +import androidx.compose.ui.platform.LocalContext |
| 36 | +import androidx.compose.ui.text.style.TextOverflow |
| 37 | +import androidx.compose.ui.unit.dp |
| 38 | +import androidx.compose.ui.viewinterop.AndroidView |
| 39 | +import androidx.webkit.WebSettingsCompat |
| 40 | +import androidx.webkit.WebViewCompat |
| 41 | +import androidx.webkit.WebViewFeature |
| 42 | +import kotlinx.serialization.json.buildJsonObject |
| 43 | +import kotlinx.serialization.json.put |
| 44 | + |
| 45 | +/** |
| 46 | + * Full-height terminal surface: embeds the gateway-served terminal-only |
| 47 | + * Control UI document (`/?view=terminal`, the same ghostty-web surface the |
| 48 | + * desktop Control UI uses) for the currently connected gateway. |
| 49 | + */ |
| 50 | +@Composable |
| 51 | +internal fun TerminalSettingsScreen( |
| 52 | + viewModel: MainViewModel, |
| 53 | + onBack: () -> Unit, |
| 54 | +) { |
| 55 | + val isConnected by viewModel.isConnected.collectAsState() |
| 56 | + val controlPage by viewModel.gatewayControlPage.collectAsState() |
| 57 | + ClawScaffold( |
| 58 | + contentPadding = PaddingValues(start = ClawTheme.spacing.lg, top = 14.dp, end = ClawTheme.spacing.lg, bottom = 6.dp), |
| 59 | + ) { |
| 60 | + Column(modifier = Modifier.fillMaxSize().imePadding(), verticalArrangement = Arrangement.spacedBy(10.dp)) { |
| 61 | + Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(9.dp)) { |
| 62 | + ClawPlainIconButton( |
| 63 | + icon = Icons.AutoMirrored.Filled.ArrowBack, |
| 64 | + contentDescription = "Back", |
| 65 | + onClick = onBack, |
| 66 | + ) |
| 67 | + Text(text = "Terminal", style = ClawTheme.type.title, color = ClawTheme.colors.text, modifier = Modifier.weight(1f), maxLines = 1, overflow = TextOverflow.Ellipsis) |
| 68 | + Icon(imageVector = Icons.Outlined.Terminal, contentDescription = null, tint = ClawTheme.colors.textMuted) |
| 69 | + } |
| 70 | + Box(modifier = Modifier.fillMaxWidth().weight(1f)) { |
| 71 | + val page = controlPage |
| 72 | + if (isConnected && page != null) { |
| 73 | + // Recreate the WebView only when the gateway page or credentials |
| 74 | + // change; recompositions must not restart live shell sessions. |
| 75 | + key(page) { |
| 76 | + TerminalWebView(page = page, modifier = Modifier.fillMaxSize()) |
| 77 | + } |
| 78 | + } else { |
| 79 | + Column(modifier = Modifier.fillMaxWidth().padding(top = 48.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(6.dp)) { |
| 80 | + Text(text = "Terminal needs a connected gateway", style = ClawTheme.type.section, color = ClawTheme.colors.text) |
| 81 | + Text(text = "Connect to your gateway to open a shell in the agent workspace.", style = ClawTheme.type.body, color = ClawTheme.colors.textMuted) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** Minimal WebView host for the terminal page; no script bridges needed. */ |
| 90 | +@SuppressLint("SetJavaScriptEnabled") |
| 91 | +// Deprecated file-URL settings are still force-disabled defensively, like the canvas host. |
| 92 | +@Suppress("DEPRECATION") |
| 93 | +@Composable |
| 94 | +private fun TerminalWebView( |
| 95 | + page: NodeRuntime.GatewayControlPage, |
| 96 | + modifier: Modifier = Modifier, |
| 97 | +) { |
| 98 | + val context = LocalContext.current |
| 99 | + val webViewRef = remember { arrayOfNulls<WebView>(1) } |
| 100 | + |
| 101 | + DisposableEffect(Unit) { |
| 102 | + onDispose { |
| 103 | + val webView = webViewRef[0] ?: return@onDispose |
| 104 | + webView.stopLoading() |
| 105 | + webView.destroy() |
| 106 | + webViewRef[0] = null |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + AndroidView( |
| 111 | + modifier = modifier, |
| 112 | + factory = { |
| 113 | + val webView = WebView(context) |
| 114 | + val webSettings = webView.settings |
| 115 | + webSettings.setAllowContentAccess(false) |
| 116 | + webSettings.setAllowFileAccess(false) |
| 117 | + webSettings.setAllowFileAccessFromFileURLs(false) |
| 118 | + webSettings.setAllowUniversalAccessFromFileURLs(false) |
| 119 | + webSettings.setSafeBrowsingEnabled(true) |
| 120 | + webSettings.javaScriptEnabled = true |
| 121 | + webSettings.domStorageEnabled = true |
| 122 | + webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE |
| 123 | + webSettings.builtInZoomControls = false |
| 124 | + webSettings.displayZoomControls = false |
| 125 | + webSettings.setSupportZoom(false) |
| 126 | + // targetSdk 33+ ignores Force Dark APIs; the terminal page owns its own |
| 127 | + // dark palette, so opt out of algorithmic darkening like the canvas host. |
| 128 | + if (WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) { |
| 129 | + WebSettingsCompat.setAlgorithmicDarkeningAllowed(webSettings, false) |
| 130 | + } |
| 131 | + webView.overScrollMode = View.OVER_SCROLL_NEVER |
| 132 | + webView.webViewClient = WebViewClient() |
| 133 | + installTerminalAuthScript(webView, page) |
| 134 | + webView.loadUrl("${page.baseUrl}/?view=terminal") |
| 135 | + webViewRef[0] = webView |
| 136 | + webView |
| 137 | + }, |
| 138 | + ) |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Hands the gateway credentials to the Control UI via its |
| 143 | + * `__OPENCLAW_NATIVE_CONTROL_AUTH__` startup contract (the same mechanism the |
| 144 | + * macOS Dashboard and iOS Terminal hub use), origin-locked by the platform's |
| 145 | + * allowed-origin rules, so the token never appears in the page URL. Without |
| 146 | + * document-start script support the page simply shows its own login gate. |
| 147 | + */ |
| 148 | +private fun installTerminalAuthScript( |
| 149 | + webView: WebView, |
| 150 | + page: NodeRuntime.GatewayControlPage, |
| 151 | +) { |
| 152 | + if (page.token == null && page.password == null) return |
| 153 | + if (!WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) return |
| 154 | + val gatewayUrl = page.baseUrl.replaceFirst("http", "ws") |
| 155 | + val payload = |
| 156 | + buildJsonObject { |
| 157 | + put("gatewayUrl", gatewayUrl) |
| 158 | + page.token?.let { put("token", it) } |
| 159 | + page.password?.let { put("password", it) } |
| 160 | + } |
| 161 | + val script = |
| 162 | + """ |
| 163 | + (() => { |
| 164 | + try { |
| 165 | + Object.defineProperty(window, "__OPENCLAW_NATIVE_CONTROL_AUTH__", { |
| 166 | + value: $payload, |
| 167 | + configurable: true, |
| 168 | + }); |
| 169 | + } catch (e) {} |
| 170 | + })(); |
| 171 | + """.trimIndent() |
| 172 | + WebViewCompat.addDocumentStartJavaScript(webView, script, setOf(page.baseUrl)) |
| 173 | +} |
0 commit comments