Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ Code v99.99.999

## Unreleased

### Added

- On-demand on-screen keyboard for the Monaco editor on touch devices. The
editor starts in a read mode where taps scroll and position the caret
without summoning the OS keyboard; a double-tap inside the editor opts
into typing mode, and a tap outside the editor (or the OS reporting the
keyboard dismissed) exits back to read mode. Makes code-server practical
to use as an actual editor on a phone instead of just as a viewer.

## [4.146.0](https://github.com/coder/code-server/releases/tag/v4.146.0) - 2026-11-20

Code v1.146.0
Expand Down
336 changes: 336 additions & 0 deletions patches/mobile-on-demand-keyboard.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,336 @@
Add on-demand on-screen keyboard for the Monaco editor on touch devices

Adds a small zero-dep module (mobileOnDemandKeyboard.ts) that suppresses
the OS virtual keyboard from popping up on a tap into the Monaco editor
and instead enters a read-only "view mode" by default on touch devices.
The user explicitly opts into typing mode with a double-tap inside the
editor; tapping outside the editor, or the browser/OS reporting the
on-screen keyboard dismissed, exits typing mode back to view mode.

Suppression is enforced with three cooperating layers against Monaco's
focus path (both the legacy `textarea.inputarea` and the modern
`div.native-edit-context`): a `.focus()` method override on the input
prototypes, a capture-phase `focusin` blur guard, and `inputmode="none"`
stamping via synchronous setter hooks on `Element.prototype` plus a
MutationObserver safety net. The module is imported and invoked once
from the existing workbench.ts bundle entry, so no new <script> tag is
needed.

Fixes #8166
Index: code-server/lib/vscode/src/vs/code/browser/workbench/workbench.ts
===================================================================
--- code-server.orig/lib/vscode/src/vs/code/browser/workbench/workbench.ts
+++ code-server/lib/vscode/src/vs/code/browser/workbench/workbench.ts
@@ -23,6 +23,7 @@ import { AuthenticationSessionInfo } fro
import { extractLocalHostUriMetaDataForPortMapping, TunnelOptions, TunnelCreationOptions } from '../../../platform/tunnel/common/tunnel.js';
import type { IURLCallbackProvider } from '../../../workbench/services/url/browser/urlService.js';
import { create } from '../../../workbench/workbench.web.main.internal.js';
+import { init as initMobileOnDemandKeyboard } from './mobileOnDemandKeyboard.js';

interface ISecretStorageCrypto {
seal(data: string): Promise<string>;
@@ -598,6 +599,8 @@ class WorkspaceProvider implements IWork
// eslint-disable-next-line no-restricted-syntax
mainWindow.document.getElementById('vscode-startup-fallback')?.remove();

+ initMobileOnDemandKeyboard();
+
// Find config by checking for DOM
// eslint-disable-next-line no-restricted-syntax
const configElement = mainWindow.document.getElementById('vscode-workbench-web-configuration');
Index: code-server/lib/vscode/src/vs/code/browser/workbench/mobileOnDemandKeyboard.ts
===================================================================
--- /dev/null
+++ code-server/lib/vscode/src/vs/code/browser/workbench/mobileOnDemandKeyboard.ts
@@ -0,0 +1,291 @@
+// On-demand on-screen keyboard for the Monaco editor on touch devices.
+//
+// On a touch device the OS virtual keyboard pops up whenever focus enters
+// a text input. For a code editor that makes the editor practically
+// unreadable: half the viewport is covered by the keyboard the moment the
+// user taps a line to scroll, position the caret, or just look at it.
+//
+// This module makes the keyboard on-demand instead: the editor starts in a
+// read mode where taps scroll and position the caret without summoning the
+// keyboard, and the user opts into a typing mode with a double-tap inside
+// the editor. Tapping outside the editor, or the browser reporting the
+// keyboard dismissed, exits typing mode back to read mode.
+//
+// Monaco exposes two text-input shapes across versions:
+// - `textarea.inputarea` — the legacy hidden textarea (older Chrome).
+// - `div.native-edit-context` — the EditContext-API based element used
+// in modern Chrome when the editor feature is enabled.
+// Both need the same treatment, so every selector below targets them
+// together.
+//
+// Three cooperating layers of suppression — each covers a path the others
+// can miss:
+// (a) `.focus()` method override on `HTMLTextAreaElement.prototype` and
+// `HTMLElement.prototype`: silently drops focus calls against a
+// Monaco input while read mode is on. Catches Monaco's own direct
+// `el.focus()` calls before any DOM focus event is dispatched.
+// (b) capture-phase `focusin` blur guard: last-chance net for a focus
+// that still landed on a Monaco input (e.g. via a browser-level
+// path the override can't see).
+// (c) `inputmode="none"` stamping via synchronous setter hooks on
+// `Element.prototype` (setAttribute, setAttributeNS, className) plus
+// a MutationObserver: tells the OS to skip the virtual keyboard
+// even if focus does land on the input. Must stamp in the same
+// synchronous block Monaco adds its class in, before Monaco's next
+// line (its `focus()` call) runs — hence the setter hooks and not
+// just an observer.
+
+const MONACO_INPUT_SELECTOR = "textarea.inputarea, div.native-edit-context"
+
+const DOUBLE_TAP_MS = 350
+const DOUBLE_TAP_PX = 30
+
+// Only shrink events bigger than this count as "the on-screen keyboard
+// opened/closed" — ignores the small resize events the browser's own URL
+// bar emits when it collapses on scroll.
+const VIEWPORT_KEYBOARD_DELTA_PX = 80
+
+const IS_TOUCH = "ontouchstart" in window || navigator.maxTouchPoints > 0
+
+// typingMode is the whole user-facing state. `false` = read mode (keyboard
+// suppressed); `true` = typing mode (keyboard allowed). Starts in read
+// mode on touch devices and typing mode on everything else, so the module
+// is a no-op on desktop.
+let typingMode = !IS_TOUCH
+
+function isMonacoInputTarget(el: Element | null | undefined): boolean {
+ if (!el || !el.classList) {
+ return false
+ }
+ return el.classList.contains("inputarea") || el.classList.contains("native-edit-context")
+}
+
+function stampOne(el: Element): void {
+ if (!typingMode) {
+ el.setAttribute("inputmode", "none")
+ } else {
+ el.removeAttribute("inputmode")
+ }
+}
+
+function stampAll(): void {
+ const list = document.querySelectorAll(MONACO_INPUT_SELECTOR)
+ for (let i = 0; i < list.length; i++) {
+ stampOne(list[i])
+ }
+}
+
+// Monkey-patch the DOM setters that change an element's class so
+// `inputmode="none"` is set in the SAME synchronous block that Monaco
+// adds `class="inputarea"` to its textarea, before Monaco's own next
+// line (the focus() call) runs. The MutationObserver below stays in
+// place as a safety net for paths this doesn't cover.
+function installSyncStamper(): void {
+ const E = Element.prototype
+
+ const origSetAttr = E.setAttribute
+ E.setAttribute = function (name: string, value: string): void {
+ origSetAttr.call(this, name, value)
+ if (name === "class" && isMonacoInputTarget(this)) {
+ stampOne(this)
+ }
+ }
+
+ const origSetAttrNS = E.setAttributeNS
+ E.setAttributeNS = function (ns: string | null, name: string, value: string): void {
+ origSetAttrNS.call(this, ns, name, value)
+ const local = typeof name === "string" ? name.slice(name.indexOf(":") + 1) : name
+ if (local === "class" && isMonacoInputTarget(this)) {
+ stampOne(this)
+ }
+ }
+
+ const desc = Object.getOwnPropertyDescriptor(E, "className")
+ if (desc && desc.set) {
+ const origSet = desc.set
+ Object.defineProperty(E, "className", {
+ configurable: true,
+ enumerable: true,
+ get: desc.get,
+ set: function (v: string): void {
+ origSet.call(this, v)
+ if (isMonacoInputTarget(this)) {
+ stampOne(this)
+ }
+ },
+ })
+ }
+}
+
+function installFocusMethodOverride(): void {
+ const origTAFocus = HTMLTextAreaElement.prototype.focus
+ HTMLTextAreaElement.prototype.focus = function (options?: FocusOptions): void {
+ if (!typingMode && isMonacoInputTarget(this)) {
+ return
+ }
+ return origTAFocus.call(this, options)
+ }
+
+ const origElFocus = HTMLElement.prototype.focus
+ HTMLElement.prototype.focus = function (options?: FocusOptions): void {
+ if (!typingMode && isMonacoInputTarget(this)) {
+ return
+ }
+ return origElFocus.call(this, options)
+ }
+}
+
+function installFocusinBlurGuard(): void {
+ document.addEventListener(
+ "focusin",
+ (e) => {
+ const t = e.target as Element | null
+ if (!typingMode && isMonacoInputTarget(t)) {
+ const el = t as HTMLElement
+ if (el.blur) {
+ el.blur()
+ }
+ }
+ },
+ true,
+ )
+}
+
+function installMutationStamper(): void {
+ const obs = new MutationObserver((mutations) => {
+ for (const m of mutations) {
+ for (const node of m.addedNodes) {
+ if (!node || node.nodeType !== 1) {
+ continue
+ }
+ const el = node as Element
+ if (el.matches && el.matches(MONACO_INPUT_SELECTOR)) {
+ stampOne(el)
+ }
+ if (el.querySelectorAll) {
+ const inner = el.querySelectorAll(MONACO_INPUT_SELECTOR)
+ for (let i = 0; i < inner.length; i++) {
+ stampOne(inner[i])
+ }
+ }
+ }
+ }
+ })
+ obs.observe(document.body, { subtree: true, childList: true })
+ stampAll()
+}
+
+function toggleTypingMode(): void {
+ typingMode = !typingMode
+ stampAll()
+ if (!typingMode) {
+ const ae = document.activeElement as HTMLElement | null
+ if (ae && ae.blur) {
+ ae.blur()
+ }
+ }
+}
+
+// Tap-gesture layer. Double-tap inside the editor flips typing mode.
+// Any tap outside the editor while typing mode is on exits it.
+function installTapGestures(): void {
+ let lastEndT = 0
+ let lastEndX = 0
+ let lastEndY = 0
+
+ document.addEventListener(
+ "touchstart",
+ (e) => {
+ if (e.touches.length !== 1) {
+ return
+ }
+ const target = e.target as Element | null
+ const inEditor = target && target.closest ? target.closest(".monaco-editor") : null
+ if (!inEditor && typingMode) {
+ toggleTypingMode()
+ return
+ }
+ if (!inEditor) {
+ return
+ }
+ const t = e.touches[0]
+ const now = Date.now()
+ const dx = Math.abs(t.clientX - lastEndX)
+ const dy = Math.abs(t.clientY - lastEndY)
+ if (now - lastEndT < DOUBLE_TAP_MS && dx < DOUBLE_TAP_PX && dy < DOUBLE_TAP_PX) {
+ lastEndT = 0
+ toggleTypingMode()
+ }
+ },
+ { capture: true },
+ )
+
+ document.addEventListener(
+ "touchend",
+ (e) => {
+ if (e.changedTouches.length !== 1) {
+ return
+ }
+ const target = e.target as Element | null
+ if (!target || !target.closest || !target.closest(".monaco-editor")) {
+ return
+ }
+ const t = e.changedTouches[0]
+ lastEndT = Date.now()
+ lastEndX = t.clientX
+ lastEndY = t.clientY
+ },
+ { capture: true },
+ )
+}
+
+// If visualViewport reports a large height growth while we're in typing
+// mode, the on-screen keyboard just dismissed (user hit the down-arrow
+// or back gesture). Exit typing mode so the next editor tap doesn't
+// re-open it.
+function installViewportKeyboardListener(): void {
+ if (!window.visualViewport) {
+ return
+ }
+ let lastH = window.visualViewport.height
+ window.visualViewport.addEventListener("resize", () => {
+ const h = window.visualViewport!.height
+ const delta = h - lastH
+ if (Math.abs(delta) > VIEWPORT_KEYBOARD_DELTA_PX) {
+ const closing = delta > 0
+ if (closing && typingMode) {
+ toggleTypingMode()
+ }
+ }
+ lastH = h
+ })
+}
+
+function onReady(cb: () => void): void {
+ if (document.querySelector(".monaco-workbench")) {
+ cb()
+ return
+ }
+ const obs = new MutationObserver(() => {
+ if (document.querySelector(".monaco-workbench")) {
+ obs.disconnect()
+ cb()
+ }
+ })
+ obs.observe(document.documentElement, { childList: true, subtree: true })
+}
+
+export function init(): void {
+ // The two prototype patches MUST run before the workbench boots so
+ // they're in place by the time Monaco first stamps a textarea class
+ // or calls .focus() on it.
+ installSyncStamper()
+ installFocusMethodOverride()
+
+ onReady(() => {
+ installMutationStamper()
+ installFocusinBlurGuard()
+ installTapGestures()
+ installViewportKeyboardListener()
+ })
+}
1 change: 1 addition & 0 deletions patches/series
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ remote-storage.diff
remote-secret-storage.diff
disable-file-cache.diff
csp-hashes.diff
mobile-on-demand-keyboard.diff
Loading