GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix Android terminal WebView chrome and keyboard (#267) · opencode-mobile/opencode-mobile@1ab9518

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • android/app/src
    • main/java/dev/opencode/mobile/ui/terminal
      • TerminalEmulator.kt
      • TerminalScreen.kt
    • test/java/dev/opencode/mobile/ui/terminal
      • TerminalWebPageTest.kt
  • ios/OpenCode Mobile/OpenCode Mobile/UI/Terminal
    • TerminalEmulatorView.swift
  • shared/terminal-web
    • index.html
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎android/app/src/main/java/dev/opencode/mobile/ui/terminal/TerminalEmulator.kt‎

Copy file name to clipboardExpand all lines: android/app/src/main/java/dev/opencode/mobile/ui/terminal/TerminalEmulator.kt
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package dev.opencode.mobile.ui.terminal
22

33
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.os.Build
6+
import android.view.MotionEvent
7+
import android.view.View
48
import android.view.ViewGroup
9+
import android.view.inputmethod.InputMethodManager
510
import android.webkit.JavascriptInterface
611
import android.webkit.WebResourceRequest
712
import android.webkit.WebSettings
@@ -85,6 +90,7 @@ fun TerminalEmulator(
8590
ViewGroup.LayoutParams.MATCH_PARENT,
8691
ViewGroup.LayoutParams.MATCH_PARENT,
8792
)
93+
configureTerminalWebView(this, parseTerminalBackgroundColor(backgroundState))
8894
settings.javaScriptEnabled = true
8995
settings.allowFileAccess = false
9096
settings.allowContentAccess = false
@@ -133,6 +139,7 @@ fun TerminalEmulator(
133139
}
134140
},
135141
update = { target ->
142+
target.setBackgroundColor(parseTerminalBackgroundColor(backgroundState))
136143
if (pageReady.value) {
137144
applyTheme(target)
138145
}
@@ -167,3 +174,45 @@ private fun parseEmulatorMessage(message: String): TerminalEmulatorEvent? {
167174
else -> null
168175
}
169176
}
177+
178+
internal fun parseTerminalBackgroundColor(hex: String): Int {
179+
val clean = hex.trim().removePrefix("#")
180+
val rgb =
181+
when (clean.length) {
182+
6, 8 -> clean.take(6).toLongOrNull(16)
183+
else -> null
184+
} ?: return 0xFF000000.toInt()
185+
return (0xFF000000L or rgb).toInt()
186+
}
187+
188+
@SuppressLint("ClickableViewAccessibility")
189+
private fun configureTerminalWebView(
190+
webView: WebView,
191+
backgroundColor: Int,
192+
) {
193+
webView.setBackgroundColor(backgroundColor)
194+
webView.overScrollMode = View.OVER_SCROLL_NEVER
195+
webView.isVerticalScrollBarEnabled = false
196+
webView.isHorizontalScrollBarEnabled = false
197+
webView.isNestedScrollingEnabled = false
198+
webView.scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
199+
webView.settings.useWideViewPort = true
200+
webView.settings.loadWithOverviewMode = false
201+
webView.settings.textZoom = 100
202+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
203+
@Suppress("DEPRECATION")
204+
webView.settings.forceDark = WebSettings.FORCE_DARK_OFF
205+
}
206+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
207+
webView.settings.isAlgorithmicDarkeningAllowed = false
208+
}
209+
webView.setOnTouchListener { view, event ->
210+
if (event.action == MotionEvent.ACTION_DOWN) {
211+
view.requestFocus()
212+
val imm =
213+
view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
214+
imm?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
215+
}
216+
false
217+
}
218+
}
Collapse file

‎android/app/src/main/java/dev/opencode/mobile/ui/terminal/TerminalScreen.kt‎

Copy file name to clipboardExpand all lines: android/app/src/main/java/dev/opencode/mobile/ui/terminal/TerminalScreen.kt
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.opencode.mobile.ui.terminal
22

3+
import androidx.compose.foundation.background
34
import androidx.compose.foundation.layout.Arrangement
45
import androidx.compose.foundation.layout.Column
56
import androidx.compose.foundation.layout.Spacer
@@ -18,6 +19,7 @@ import androidx.lifecycle.compose.LifecycleEventEffect
1819
import androidx.lifecycle.compose.collectAsStateWithLifecycle
1920
import dev.opencode.mobile.theme.LocalOpenCodeDarkTheme
2021
import dev.opencode.mobile.theme.ThemeManager
22+
import dev.opencode.mobile.theme.ThemeResolver
2123
import dev.opencode.mobile.theme.letterAccentForWorktree
2224
import dev.opencode.mobile.theme.letterCharacterForProject
2325
import dev.opencode.mobile.ui.components.OpenCodeButton
@@ -105,7 +107,10 @@ private fun TerminalLivePane(viewModel: TerminalViewModel) {
105107
is TerminalEmulatorEvent.Resize -> viewModel.resize(event.cols, event.rows)
106108
}
107109
},
108-
modifier = Modifier.weight(1f),
110+
modifier =
111+
Modifier
112+
.weight(1f)
113+
.background(ThemeResolver.parseHexColor(tokens["background-base"])),
109114
)
110115
TerminalControlStrip(onSend = viewModel::sendInput)
111116
}
Collapse file

‎android/app/src/test/java/dev/opencode/mobile/ui/terminal/TerminalWebPageTest.kt‎

Copy file name to clipboard
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package dev.opencode.mobile.ui.terminal
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Assert.assertTrue
5+
import org.junit.Test
6+
import java.io.File
7+
8+
class TerminalWebPageTest {
9+
@Test
10+
fun androidHostPinsViewportAndKeepsImeTextareaOnscreen() {
11+
val html = terminalWebIndexHtml()
12+
assertTrue(html.contains("html.android"))
13+
assertTrue(html.contains("position: fixed"))
14+
assertTrue(html.contains("left: 0 !important"))
15+
assertTrue(html.contains("if (window.AndroidTerminal) document.documentElement.classList.add(\"android\")"))
16+
assertTrue(html.contains("window.visualViewport && !window.AndroidTerminal"))
17+
}
18+
19+
@Test
20+
fun parseTerminalBackgroundColorReadsRgbHex() {
21+
assertEquals(0xFF1A1A1A.toInt(), parseTerminalBackgroundColor("#1A1A1A"))
22+
assertEquals(0xFF000000.toInt(), parseTerminalBackgroundColor("not-a-color"))
23+
}
24+
25+
private fun terminalWebIndexHtml(): String {
26+
val candidates =
27+
listOf(
28+
File("src/main/assets/terminal/index.html"),
29+
File("android/app/src/main/assets/terminal/index.html"),
30+
)
31+
val file = candidates.firstOrNull { it.isFile } ?: error("terminal index.html not found")
32+
return file.readText()
33+
}
34+
}
Collapse file

‎ios/OpenCode Mobile/OpenCode Mobile/UI/Terminal/TerminalEmulatorView.swift‎

Copy file name to clipboardExpand all lines: ios/OpenCode Mobile/OpenCode Mobile/UI/Terminal/TerminalEmulatorView.swift
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import SwiftUI
2+
import UIKit
23
import WebKit
34

45
struct TerminalEmulatorView: UIViewRepresentable {
@@ -22,7 +23,8 @@ struct TerminalEmulatorView: UIViewRepresentable {
2223
config.preferences.setValue(false, forKey: "allowFileAccessFromFileURLs")
2324
let webView = WKWebView(frame: .zero, configuration: config)
2425
webView.isOpaque = false
25-
webView.backgroundColor = .black
26+
webView.backgroundColor = UIColor(Color(hex: backgroundHex))
27+
webView.scrollView.backgroundColor = UIColor(Color(hex: backgroundHex))
2628
webView.scrollView.isScrollEnabled = false
2729
webView.scrollView.bounces = false
2830
webView.navigationDelegate = context.coordinator
@@ -43,6 +45,8 @@ struct TerminalEmulatorView: UIViewRepresentable {
4345
context.coordinator.foregroundHex = foregroundHex
4446
context.coordinator.onData = onData
4547
context.coordinator.onResize = onResize
48+
uiView.backgroundColor = UIColor(Color(hex: backgroundHex))
49+
uiView.scrollView.backgroundColor = UIColor(Color(hex: backgroundHex))
4650
if context.coordinator.pageReady {
4751
context.coordinator.applyTheme()
4852
context.coordinator.fit()
Collapse file

‎shared/terminal-web/index.html‎

Copy file name to clipboardExpand all lines: shared/terminal-web/index.html
+24-1Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,28 @@
1616
padding: 4px;
1717
box-sizing: border-box;
1818
}
19+
html.android,
20+
html.android body {
21+
position: fixed;
22+
inset: 0;
23+
}
24+
html.android .xterm .xterm-helper-textarea {
25+
left: 0 !important;
26+
top: 0 !important;
27+
width: 8px !important;
28+
height: 8px !important;
29+
opacity: 0 !important;
30+
}
31+
html.android .xterm-char-measure-element {
32+
left: 0 !important;
33+
}
34+
html.android .xterm .xterm-viewport {
35+
background-color: transparent !important;
36+
}
1937
</style>
38+
<script>
39+
if (window.AndroidTerminal) document.documentElement.classList.add("android");
40+
</script>
2041
</head>
2142
<body>
2243
<div id="terminal"></div>
@@ -54,6 +75,8 @@
5475
if (theme.background) {
5576
document.body.style.background = theme.background;
5677
document.documentElement.style.background = theme.background;
78+
var terminalEl = document.getElementById("terminal");
79+
if (terminalEl) terminalEl.style.background = theme.background;
5780
}
5881
if (term) {
5982
term.options.theme = theme;
@@ -88,7 +111,7 @@
88111
window.addEventListener("resize", function () {
89112
fitAddon.fit();
90113
});
91-
if (window.visualViewport) {
114+
if (window.visualViewport && !window.AndroidTerminal) {
92115
window.visualViewport.addEventListener("resize", function () {
93116
fitAddon.fit();
94117
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.