Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(mobile): keyboard no longer closes on Create (Android)

purple1e9c0638fdfe
diff --git a/e2e/mobile/create.mobile.ts b/e2e/mobile/create.mobile.ts
index 4bf6f8c9..80f58d11 100644
--- a/e2e/mobile/create.mobile.ts
+++ b/e2e/mobile/create.mobile.ts
@@ -3904,3 +3904,48 @@ test.describe('create fits on one screen (round 45)', () => {
fs.writeFileSync(path.join(R45_DIR, `messung-${label}.json`), `${JSON.stringify(rows, null, 1)}\n`)
})
})
+
+// Customer mail of 30.10. (package M1, 3.0.9): "the keyboard keeps closing" on Create, Android. Android shrinks the
+// window by the keyboard (adjustResize, MainActivity pads by the IME inset), which the viewport below does too. Below
+// 30 text units of height Create went to one scrolling column, the prompt field was built anew in it, lost the focus,
+// and Android took the keyboard away. Screenshots for the report: LU_M1_SHOTS=<folder>.
+test.describe('the keyboard stays open on Create (M1)', () => {
+ for (const v of [
+ { name: '360x740', w: 360, h: 740, kb: 300, fs: 100 },
+ { name: '412x840 text 115', w: 412, h: 840, kb: 330, fs: 115 },
+ ]) {
+ for (const tool of ['image', 'video'] as const) {
+ test(`${v.name}, ${tool}: the prompt keeps its place and the focus while the window shrinks`, async ({ page }) => {
+ await page.setViewportSize({ width: v.w, height: v.h })
+ const cloud = new Cloud()
+ await openCreate(page, cloud, { fontScale: v.fs })
+ if (tool !== 'image') await chooseTool(page, tool)
+ const prompt = tid(page, 'm-create-prompt')
+ await prompt.tap()
+ await page.keyboard.type('a cat')
+ await prompt.evaluate((n) => ((n as HTMLElement & { m1?: boolean }).m1 = true))
+ const shots = process.env.LU_M1_SHOTS
+ const slug = `${v.name.replace(/\s+/g, '-')}-${tool}`
+ if (shots) await page.screenshot({ path: path.join(shots, `${slug}-1-tastatur-zu.png`) })
+
+ await page.setViewportSize({ width: v.w, height: v.h - v.kb })
+ // Android sends more than one resize while the keyboard slides in; give React every one of them.
+ await page.waitForTimeout(400)
+ const after = await page.evaluate(() => {
+ const el = document.activeElement as (HTMLElement & { m1?: boolean }) | null
+ return { focused: el?.dataset.testid ?? null, same: el?.m1 === true, layout: document.querySelector('[data-testid="m-create-layout"]')?.getAttribute('data-state') ?? 'tall' }
+ })
+ expect(after).toEqual({ focused: 'm-create-prompt', same: true, layout: 'tall' })
+ await page.keyboard.type(' on a roof')
+ await expect(prompt).toHaveValue('a cat on a roof')
+ if (shots) await page.screenshot({ path: path.join(shots, `${slug}-2-tastatur-offen.png`) })
+
+ // The keyboard goes with the focus: the page measures again.
+ await prompt.evaluate((n) => (n as HTMLElement).blur())
+ await page.setViewportSize({ width: v.w, height: v.h })
+ await expect(prompt).toHaveValue('a cat on a roof')
+ await expect(tid(page, 'm-create-layout')).toHaveCount(0)
+ })
+ }
+ }
+})
diff --git a/src/mobile/screens/create/__tests__/keyboard-keeps-shape.dom.test.tsx b/src/mobile/screens/create/__tests__/keyboard-keeps-shape.dom.test.tsx
new file mode 100644
index 00000000..fa2cc3b8
--- /dev/null
+++ b/src/mobile/screens/create/__tests__/keyboard-keeps-shape.dom.test.tsx
@@ -0,0 +1,92 @@
+// @vitest-environment jsdom
+/**
+ * Customer mail of 30.10. (package M1, 3.0.9): on Android the keyboard kept
+ * closing on Create. Android shrinks the window by the keyboard
+ * (adjustResize, MainActivity pads the content by the IME inset). On a usual
+ * phone that is less than 30 text units of height, so Create went from the
+ * fixed composer (tall) to one scrolling column (low). The composer stands in
+ * another place there, React built the prompt field anew, the new field had
+ * no focus and Android took the keyboard away. The window grew back, tall
+ * returned, and the next tap started it all again.
+ *
+ * Run: npx vitest run src/mobile/screens/create/__tests__/keyboard-keeps-shape.dom.test.tsx
+ */
+import { afterEach, describe, expect, it } from 'vitest'
+import { act, cleanup, render, screen } from '@testing-library/react'
+import { useScreenShape } from '../parts/ui'
+
+/** Create in small: the prompt sits in the footer when tall and at the top of the column otherwise (CreatePage Surface). */
+function Harness() {
+ const shape = useScreenShape()
+ const field = <textarea data-testid="prompt" defaultValue="" />
+ return (
+ <div data-shape={shape} data-testid="page">
+ {shape === 'tall' ? (
+ <>
+ <div>stage</div>
+ <footer>{field}</footer>
+ </>
+ ) : (
+ <div className="m-k-low">
+ <div>{field}</div>
+ <div>stage</div>
+ </div>
+ )}
+ </div>
+ )
+}
+
+function windowSize(width: number, height: number): void {
+ Object.defineProperty(window, 'innerWidth', { configurable: true, value: width })
+ Object.defineProperty(window, 'innerHeight', { configurable: true, value: height })
+ window.dispatchEvent(new Event('resize'))
+}
+
+afterEach(() => {
+ cleanup()
+})
+
+describe('M1: the keyboard does not move the composer of Create', () => {
+ it('Android 360x730, the keyboard takes 300 px: the prompt keeps its place and its focus', async () => {
+ windowSize(360, 730)
+ render(<Harness />)
+ expect(screen.getByTestId('page').dataset.shape).toBe('tall')
+ const field = screen.getByTestId('prompt')
+ field.focus()
+ expect(document.activeElement).toBe(field)
+
+ await act(async () => windowSize(360, 430))
+
+ expect(screen.getByTestId('page').dataset.shape).toBe('tall')
+ expect(screen.getByTestId('prompt')).toBe(field)
+ expect(document.activeElement).toBe(field)
+ })
+
+ it('without a field in focus a low window still gets the scrolling column', async () => {
+ windowSize(360, 730)
+ render(<Harness />)
+ await act(async () => windowSize(360, 430))
+ expect(screen.getByTestId('page').dataset.shape).toBe('low')
+ })
+
+ it('the keyboard gone and the focus with it: the measured shape counts again', async () => {
+ windowSize(360, 730)
+ render(<Harness />)
+ const field = screen.getByTestId('prompt')
+ field.focus()
+ await act(async () => windowSize(360, 430))
+ field.blur()
+ await act(async () => windowSize(360, 730))
+ expect(screen.getByTestId('page').dataset.shape).toBe('tall')
+ await act(async () => windowSize(360, 430))
+ expect(screen.getByTestId('page').dataset.shape).toBe('low')
+ })
+
+ it('turned on its side while typing: the width changed, the shape follows', async () => {
+ windowSize(360, 730)
+ render(<Harness />)
+ screen.getByTestId('prompt').focus()
+ await act(async () => windowSize(730, 360))
+ expect(screen.getByTestId('page').dataset.shape).not.toBe('tall')
+ })
+})
diff --git a/src/mobile/screens/create/parts/ui.tsx b/src/mobile/screens/create/parts/ui.tsx
index ec0dafff..21dda37c 100644
--- a/src/mobile/screens/create/parts/ui.tsx
+++ b/src/mobile/screens/create/parts/ui.tsx
@@ -15,12 +15,12 @@ import { useCreateStore, type GalleryItem } from '../../../../stores/createStore
import { galleryLabelShort } from '../../../../lib/render/gallery-label'
import { nav, useSheet } from '../../../nav'
import { Icon, type IconType } from '../../../ui'
-import { onTextZoomChange, textUnitPx } from '../../../ui/text-zoom'
+import { useHeldScreenShape } from '../../../ui/screen-shape'
import { CREATE_STRINGS as T } from '../../../strings/create'
import { itemUrl, resignItem } from './adapters/result-url'
import { isComputerItem } from './computer-media'
import { CheckerBoard, useChecker } from './checker'
-import { screenShape, type ScreenShape } from './layout'
+import type { ScreenShape } from './layout'
import type { ToolId } from './tools'
import './create.css'
@@ -152,28 +152,18 @@ export function itemName(item: GalleryItem, max = 48): string {
return galleryLabelShort(item, max)
}
-function shapeNow(): ScreenShape {
- if (typeof window === 'undefined') return 'tall'
- // The size text is drawn at: on Android the root stays at 16 px and the WebView enlarges every letter (ui/text-zoom.ts).
- return screenShape(window.innerWidth, window.innerHeight, textUnitPx())
+function layoutHeight(): number {
+ return window.innerHeight
}
-/** The arrangement that fits the screen right now. Follows rotation, the keyboard and the text size. */
+/**
+ * The arrangement that fits the screen right now. Follows rotation and the text size, not the keyboard:
+ * the composer of Create moves between the arrangements, and a move while typing closed the keyboard on
+ * Android (ui/screen-shape.ts nextShapeState). The size text is drawn at counts: on Android the root stays
+ * at 16 px and the WebView enlarges every letter (ui/text-zoom.ts).
+ */
export function useScreenShape(): ScreenShape {
- const [shape, setShape] = useState<ScreenShape>(shapeNow)
- useEffect(() => {
- const update = () => setShape(shapeNow())
- update()
- window.addEventListener('resize', update)
- window.addEventListener('orientationchange', update)
- const offZoom = onTextZoomChange(update)
- return () => {
- window.removeEventListener('resize', update)
- window.removeEventListener('orientationchange', update)
- offZoom()
- }
- }, [])
- return shape
+ return useHeldScreenShape(layoutHeight)
}
/**
diff --git a/src/mobile/ui/__tests__/screen-shape-hold.test.ts b/src/mobile/ui/__tests__/screen-shape-hold.test.ts
new file mode 100644
index 00000000..ea77ec83
--- /dev/null
+++ b/src/mobile/ui/__tests__/screen-shape-hold.test.ts
@@ -0,0 +1,23 @@
+/**
+ * The arrangement of a page holds while the keyboard moves (package M1, 3.0.9).
+ * The page that showed it: src/mobile/screens/create/__tests__/keyboard-keeps-shape.dom.test.tsx
+ *
+ * Run: npx vitest run src/mobile/ui/__tests__/screen-shape-hold.test.ts
+ */
+import { describe, expect, it } from 'vitest'
+import { nextShapeState } from '../screen-shape'
+
+describe('nextShapeState (pure)', () => {
+ const tall = nextShapeState(null, { width: 360, height: 730, unitPx: 16, editing: false })
+
+ it('holds the shape while a field is edited and only the height changed', () => {
+ expect(tall.shape).toBe('tall')
+ expect(nextShapeState(tall, { width: 360, height: 430, unitPx: 16, editing: true })).toBe(tall)
+ })
+
+ it('measures again without a field in focus, at another width or another text size', () => {
+ expect(nextShapeState(tall, { width: 360, height: 430, unitPx: 16, editing: false }).shape).toBe('low')
+ expect(nextShapeState(tall, { width: 730, height: 360, unitPx: 16, editing: true }).shape).toBe('wide')
+ expect(nextShapeState(tall, { width: 360, height: 430, unitPx: 20, editing: true }).shape).toBe('low')
+ })
+})
diff --git a/src/mobile/ui/screen-shape.ts b/src/mobile/ui/screen-shape.ts
index aa888cc7..a5cddb7d 100644
--- a/src/mobile/ui/screen-shape.ts
+++ b/src/mobile/ui/screen-shape.ts
@@ -31,18 +31,44 @@ export function screenShape(width: number, height: number, rootPx: number): Scre
return 'low'
}
-function shapeNow(): ScreenShape {
- if (typeof window === 'undefined') return 'tall'
- // The visible height: with the keyboard open the window is as high as before on iOS.
- const height = window.visualViewport?.height ?? window.innerHeight
- return screenShape(window.innerWidth, height, textUnitPx())
+export interface ShapeSample {
+ width: number
+ height: number
+ unitPx: number
+ /** A text field has the focus, so a keyboard may have taken part of the height. */
+ editing: boolean
}
-/** The arrangement that fits the screen right now. Follows rotation, the keyboard and the text size. */
-export function useScreenShape(): ScreenShape {
- const [shape, setShape] = useState<ScreenShape>(shapeNow)
+export interface ShapeState {
+ shape: ScreenShape
+ width: number
+ unitPx: number
+}
+
+/**
+ * Pure: the next arrangement from the last one and a new measurement. While a
+ * field is being edited and neither the width nor the text size changed, only
+ * the keyboard moved: the arrangement stays. A page that puts its input
+ * somewhere else in another arrangement (Create: in the fixed footer when
+ * tall, at the top of the column when low) would otherwise build the field
+ * anew the moment the keyboard opens. The new field has no focus, Android
+ * closes the keyboard, the window grows back, the old arrangement returns,
+ * and so on: the keyboard kept closing on Create (customer mail of 30.10.,
+ * package M1).
+ */
+export function nextShapeState(prev: ShapeState | null, sample: ShapeSample): ShapeState {
+ if (prev && sample.editing && sample.width === prev.width && sample.unitPx === prev.unitPx) return prev
+ return { shape: screenShape(sample.width, sample.height, sample.unitPx), width: sample.width, unitPx: sample.unitPx }
+}
+
+/** The arrangement of a page, held while the keyboard moves. `height` reads the height the page goes by. */
+export function useHeldScreenShape(height: () => number): ScreenShape {
+ const sample = (editing: boolean): ShapeSample => ({ width: window.innerWidth, height: height(), unitPx: textUnitPx(), editing })
+ const [state, setState] = useState<ShapeState>(() =>
+ typeof window === 'undefined' ? { shape: 'tall', width: 0, unitPx: 16 } : nextShapeState(null, sample(false)),
+ )
useEffect(() => {
- const update = () => setShape(shapeNow())
+ const update = () => setState((prev) => nextShapeState(prev, sample(editingNow())))
update()
const viewport = window.visualViewport
window.addEventListener('resize', update)
@@ -57,8 +83,15 @@ export function useScreenShape(): ScreenShape {
viewport?.removeEventListener('resize', update)
offZoom()
}
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- read at every event, the reader of the page stays the same
}, [])
- return shape
+ return state.shape
+}
+
+/** The arrangement that fits the screen right now. Follows rotation and the text size; the keyboard only once the field lets go. */
+export function useScreenShape(): ScreenShape {
+ // The visible height: with the keyboard open the window is as high as before on iOS.
+ return useHeldScreenShape(() => window.visualViewport?.height ?? window.innerHeight)
}
// ---------- where the footer of a page goes ----------
@@ -108,7 +141,7 @@ export function nextFootState(prev: FootState | null, sample: FootSample): FootS
return { scrolls: footScrolls(sample.width, sample.height, sample.unitPx), width: sample.width, unitPx: sample.unitPx }
}
-function editingNow(): boolean {
+export function editingNow(): boolean {
const el = typeof document === 'undefined' ? null : document.activeElement
if (!el) return false
if (el.tagName === 'TEXTAREA') return true