|
| 1 | +package ai.openclaw.app.ui |
| 2 | + |
| 3 | +import ai.openclaw.app.MainViewModel |
| 4 | +import ai.openclaw.app.i18n.nativeString |
| 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 androidx.compose.foundation.layout.Arrangement |
| 9 | +import androidx.compose.foundation.layout.Box |
| 10 | +import androidx.compose.foundation.layout.Column |
| 11 | +import androidx.compose.foundation.layout.PaddingValues |
| 12 | +import androidx.compose.foundation.layout.Row |
| 13 | +import androidx.compose.foundation.layout.fillMaxSize |
| 14 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 15 | +import androidx.compose.foundation.layout.imePadding |
| 16 | +import androidx.compose.foundation.layout.padding |
| 17 | +import androidx.compose.material.icons.Icons |
| 18 | +import androidx.compose.material.icons.automirrored.filled.ArrowBack |
| 19 | +import androidx.compose.material.icons.outlined.DesktopWindows |
| 20 | +import androidx.compose.material3.Icon |
| 21 | +import androidx.compose.material3.Text |
| 22 | +import androidx.compose.runtime.Composable |
| 23 | +import androidx.compose.runtime.collectAsState |
| 24 | +import androidx.compose.runtime.getValue |
| 25 | +import androidx.compose.runtime.key |
| 26 | +import androidx.compose.ui.Alignment |
| 27 | +import androidx.compose.ui.Modifier |
| 28 | +import androidx.compose.ui.text.style.TextOverflow |
| 29 | +import androidx.compose.ui.unit.dp |
| 30 | +import androidx.core.net.toUri |
| 31 | + |
| 32 | +/** Full-height viewer for a gateway-observable desktop source. */ |
| 33 | +@Composable |
| 34 | +internal fun DesktopScreen( |
| 35 | + viewModel: MainViewModel, |
| 36 | + source: String? = null, |
| 37 | + onBack: () -> Unit, |
| 38 | +) { |
| 39 | + val isConnected by viewModel.isConnected.collectAsState() |
| 40 | + val controlPage by viewModel.gatewayControlPage.collectAsState() |
| 41 | + ClawScaffold( |
| 42 | + contentPadding = PaddingValues(start = ClawTheme.spacing.lg, top = 14.dp, end = ClawTheme.spacing.lg, bottom = 6.dp), |
| 43 | + ) { |
| 44 | + // The viewer's keyboard affordance opens the soft keyboard over the canvas; without |
| 45 | + // imePadding it would also cover the viewer's own touch toolbar. |
| 46 | + Column(modifier = Modifier.fillMaxSize().imePadding(), verticalArrangement = Arrangement.spacedBy(10.dp)) { |
| 47 | + Row( |
| 48 | + modifier = Modifier.fillMaxWidth(), |
| 49 | + verticalAlignment = Alignment.CenterVertically, |
| 50 | + horizontalArrangement = Arrangement.spacedBy(9.dp), |
| 51 | + ) { |
| 52 | + ClawPlainIconButton( |
| 53 | + icon = Icons.AutoMirrored.Filled.ArrowBack, |
| 54 | + contentDescription = nativeString("Back"), |
| 55 | + onClick = onBack, |
| 56 | + ) |
| 57 | + Text( |
| 58 | + text = nativeString("Desktop"), |
| 59 | + style = ClawTheme.type.title, |
| 60 | + color = ClawTheme.colors.text, |
| 61 | + modifier = Modifier.weight(1f), |
| 62 | + maxLines = 1, |
| 63 | + overflow = TextOverflow.Ellipsis, |
| 64 | + ) |
| 65 | + Icon( |
| 66 | + imageVector = Icons.Outlined.DesktopWindows, |
| 67 | + contentDescription = null, |
| 68 | + tint = ClawTheme.colors.textMuted, |
| 69 | + ) |
| 70 | + } |
| 71 | + Box(modifier = Modifier.fillMaxWidth().weight(1f)) { |
| 72 | + val page = controlPage |
| 73 | + if (isConnected && page != null) { |
| 74 | + // GatewayControlPage equality includes credentials and the accepted TLS pin. |
| 75 | + key(page, source) { |
| 76 | + ControlUiWebView( |
| 77 | + page = page, |
| 78 | + url = desktopUrl(baseUrl = page.baseUrl, source = source), |
| 79 | + modifier = Modifier.fillMaxSize(), |
| 80 | + ) |
| 81 | + } |
| 82 | + } else { |
| 83 | + Column( |
| 84 | + modifier = Modifier.fillMaxWidth().padding(top = 48.dp), |
| 85 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 86 | + verticalArrangement = Arrangement.spacedBy(6.dp), |
| 87 | + ) { |
| 88 | + Text( |
| 89 | + text = nativeString("Desktop needs a connected gateway"), |
| 90 | + style = ClawTheme.type.section, |
| 91 | + color = ClawTheme.colors.text, |
| 92 | + ) |
| 93 | + Text( |
| 94 | + text = nativeString("Connect to your gateway to view a machine screen."), |
| 95 | + style = ClawTheme.type.body, |
| 96 | + color = ClawTheme.colors.textMuted, |
| 97 | + ) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/** Builds the desktop document route; credentials stay in ControlUiWebView's startup script. */ |
| 106 | +internal fun desktopUrl( |
| 107 | + baseUrl: String, |
| 108 | + source: String? = null, |
| 109 | +): String { |
| 110 | + val baseUri = baseUrl.trimEnd('/').toUri() |
| 111 | + val routePath = "${baseUri.encodedPath.orEmpty().trimEnd('/')}/" |
| 112 | + val builder = |
| 113 | + baseUri |
| 114 | + .buildUpon() |
| 115 | + .encodedPath(routePath) |
| 116 | + .clearQuery() |
| 117 | + .fragment(null) |
| 118 | + .appendQueryParameter("view", "desktop") |
| 119 | + source?.let { builder.appendQueryParameter("source", it) } |
| 120 | + return builder.build().toString() |
| 121 | +} |
0 commit comments