Compare commits
28
Commits
8bdf916d8a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bd5588ade | ||
|
|
7a6b130078 | ||
|
|
a9183a0f73 | ||
|
|
9d5c02db03 | ||
|
|
d38a2ee05f | ||
|
|
a83cc1a72f | ||
|
|
51344b9a5b | ||
|
|
cb8ebc003f | ||
|
|
dd9493bdbd | ||
|
|
36b6a86873 | ||
|
|
f54ca2f81a | ||
|
|
4c8a13b779 | ||
|
|
ef54486683 | ||
|
|
e96464ec79 | ||
|
|
4a3ba37075 | ||
|
|
6d3e070c2d | ||
|
|
16740b0de0 | ||
|
|
5e40378072 | ||
|
|
06b1444207 | ||
|
|
180fa966c8 | ||
|
|
0f1b6e6290 | ||
|
|
6968f6a284 | ||
|
|
fdbffc9f5c | ||
|
|
2bdff5c8e8 | ||
|
|
2eb18b5c70 | ||
|
|
03595af27d | ||
|
|
e3690d6b98 | ||
|
|
f31f849665 |
@@ -13,6 +13,10 @@
|
||||
cmd/pad/pad
|
||||
cmd/pad/classes
|
||||
|
||||
# touchinject harness build artifacts (regenerated by build.sh)
|
||||
tools/touchinject/build/
|
||||
tools/touchinject/dex/
|
||||
|
||||
# IDE
|
||||
.idea/
|
||||
.vscode/
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 123 KiB |
+38
-2
@@ -1,3 +1,4 @@
|
||||
//go:build !darwin || !linux
|
||||
// +build !darwin !linux
|
||||
|
||||
package main
|
||||
@@ -17,22 +18,38 @@ import (
|
||||
"unsafe"
|
||||
|
||||
"gioui.org/app"
|
||||
"gioui.org/io/event"
|
||||
_ "gioui.org/app/permission/storage"
|
||||
"gioui.org/io/event"
|
||||
|
||||
"pad/internal/editor"
|
||||
)
|
||||
|
||||
type JNIEnv = C.JNIEnv
|
||||
|
||||
var (
|
||||
startpath = "/storage/emulated/0/Notes"
|
||||
jvm uintptr
|
||||
theJVM *C.JavaVM
|
||||
)
|
||||
|
||||
// activeLogic is set in main before the window loop: the onStop JNI hook
|
||||
// (pad_flush_session, below) needs a handle to the logic it serves.
|
||||
var activeLogic *editor.Logic
|
||||
|
||||
func impl_start() {}
|
||||
|
||||
func handleEvent(e event.Event) {
|
||||
switch e := e.(type) {
|
||||
case app.AndroidViewEvent:
|
||||
// View == 0 is Gio's DETACH signal (window.detach sends an empty
|
||||
// AndroidViewEvent when the GioView is destroyed — e.g. the activity
|
||||
// going away on a recents-wipe). There is nothing to register for a
|
||||
// detached view; passing the null ref on would abort in JNI
|
||||
// (GetObjectClass on null). A re-attach arrives as a fresh event
|
||||
// with a live view.
|
||||
if e.View == 0 {
|
||||
return
|
||||
}
|
||||
theJVM = (*C.JavaVM)(unsafe.Pointer(app.JavaVM()))
|
||||
RunInJVM(func(env *JNIEnv) {
|
||||
C.registerFragment(env, (C.jobject)(unsafe.Pointer(e.View)))
|
||||
@@ -68,6 +85,19 @@ func RunInJVM(f func(env *C.JNIEnv)) {
|
||||
// an edge handle are not stolen by the system back gesture (see
|
||||
// jni_android.c). It is called from the frame loop, which runs on the Android
|
||||
// UI thread. No-op (C-side) before API 29.
|
||||
// pad_flush_session is the cgo export behind GioActivity.onStop (the
|
||||
// build script patches onStop into the smali and declares the matching
|
||||
// static native method). The OS gives no user-space hook for the process
|
||||
// kill that follows a recents-wipe, so onStop is the last reliable moment
|
||||
// to persist the session snapshot.
|
||||
//
|
||||
//export pad_flush_session
|
||||
func pad_flush_session() {
|
||||
if activeLogic != nil {
|
||||
activeLogic.FlushSession()
|
||||
}
|
||||
}
|
||||
|
||||
func SetGestureExclusions(rects [][4]int) {
|
||||
if theJVM == nil {
|
||||
return
|
||||
@@ -87,6 +117,13 @@ func SetGestureExclusions(rects [][4]int) {
|
||||
})
|
||||
}
|
||||
|
||||
// sessionFilePath is where the relaunch session file (spec §7) lives. The
|
||||
// primary storage dir already carries the app's caches (.pad browser index
|
||||
// dirs, PadPerf), and the app holds the storage permission.
|
||||
func sessionFilePath() string {
|
||||
return "/storage/emulated/0/Pad/session.json"
|
||||
}
|
||||
|
||||
func OpenFile(path string) {
|
||||
var env *C.JNIEnv
|
||||
var detach bool
|
||||
@@ -109,4 +146,3 @@ func OpenFile(path string) {
|
||||
C.open_file_in_termux(env, cpath)
|
||||
C.free(unsafe.Pointer(cpath))
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
//go:build !android
|
||||
// +build !android
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"pad/internal/editor"
|
||||
"path/filepath"
|
||||
|
||||
"gioui.org/io/event"
|
||||
)
|
||||
|
||||
@@ -10,6 +15,16 @@ var (
|
||||
startpath = "."
|
||||
)
|
||||
|
||||
// sessionFilePath is where the relaunch session file (spec §7) lives, off
|
||||
// Android: $HOME/.pad/session.json, falling back to the temp dir when there
|
||||
// is no home.
|
||||
func sessionFilePath() string {
|
||||
if d, err := os.UserHomeDir(); err == nil && d != "" {
|
||||
return filepath.Join(d, ".pad", "session.json")
|
||||
}
|
||||
return filepath.Join(os.TempDir(), "pad", "session.json")
|
||||
}
|
||||
|
||||
func handleEvent(e event.Event) {}
|
||||
|
||||
func OpenFile(path string) {}
|
||||
@@ -17,3 +32,8 @@ func OpenFile(path string) { }
|
||||
// SetGestureExclusions is a no-op off Android (system gesture exclusion
|
||||
// rects are an Android API 29+ feature).
|
||||
func SetGestureExclusions(rects [][4]int) {}
|
||||
|
||||
// activeLogic exists on the Android build (the onStop JNI hook, see
|
||||
// impl_android.go); on other platforms it is unused but main assigns it.
|
||||
var activeLogic *editor.Logic
|
||||
var _ = activeLogic // keep staticcheck quiet on non-Android builds
|
||||
|
||||
+17
-2
@@ -15,14 +15,18 @@ static jobject g_view = NULL;
|
||||
|
||||
void
|
||||
registerFragment(JNIEnv *env, jobject view) {
|
||||
if (view != NULL) {
|
||||
if (view == NULL) {
|
||||
// Detach signal (Gio sends an empty AndroidViewEvent when the
|
||||
// view is destroyed). The Go side already filters these; this
|
||||
// guard keeps the JNI calls safe if one ever slips through.
|
||||
return;
|
||||
}
|
||||
if (g_view == NULL) {
|
||||
g_view = (*env)->NewGlobalRef(env, view);
|
||||
} else if (g_view != view) {
|
||||
(*env)->DeleteGlobalRef(env, g_view);
|
||||
g_view = (*env)->NewGlobalRef(env, view);
|
||||
}
|
||||
}
|
||||
jclass cls = (*env)->GetObjectClass(env, view);
|
||||
jmethodID mid = (*env)->GetMethodID(env, cls, "getContext", "()Landroid/content/Context;");
|
||||
jobject l_ctx = (*env)->CallObjectMethod(env, view, mid);
|
||||
@@ -191,3 +195,14 @@ void SetGestureExclusions(JNIEnv *env, jint nrects, const jint *xyxy) {
|
||||
(*env)->DeleteLocalRef(env, runnable);
|
||||
(*env)->DeleteLocalRef(env, list);
|
||||
}
|
||||
|
||||
// Java_org_gioui_GioActivity_padFlushSession
|
||||
//
|
||||
// Called from GioActivity.onStop (injected by the build script): persist
|
||||
// the session snapshot before the process is likely killed. The Go side
|
||||
// (pad_flush_session) only enqueues a request on the logic goroutine's
|
||||
// channel, so this returns immediately on the UI thread.
|
||||
void
|
||||
Java_org_gioui_GioActivity_padFlushSession(JNIEnv *env, jclass cls) {
|
||||
pad_flush_session();
|
||||
}
|
||||
|
||||
+170
-1
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io"
|
||||
"log"
|
||||
@@ -59,6 +60,7 @@ func run(w *app.Window) error {
|
||||
log.Printf("using filesystem at / (startup directory: %s)", startAbs)
|
||||
|
||||
logic := editor.NewLogic(fs, startAbs, OpenFile)
|
||||
activeLogic = logic
|
||||
renderer := ui.New(ui.Theme{FontSize: 14}, shaper)
|
||||
|
||||
// In-app frame profiler (default off). Enabled by the presence of a marker
|
||||
@@ -106,6 +108,41 @@ func run(w *app.Window) error {
|
||||
var findEditor widget.Editor
|
||||
renderer.RegisterGioEditor("find_bar", &findEditor)
|
||||
|
||||
// Relaunch state restoration (spec §7): a tiny JSON file holds the last
|
||||
// file, cursor, scroll, selection and find bar state. The cmd layer owns
|
||||
// the file (sessionFilePath, per platform); the logic layer owns the
|
||||
// snapshot (editor.SessionState) and calls the saver rate-limited and at
|
||||
// Shutdown. A session taken on the editor page re-opens the last file
|
||||
// straight into the editor; one taken on the browser page lands back in
|
||||
// the browser at the saved directory.
|
||||
sessPath := sessionFilePath()
|
||||
logic.SetSessionSaver(newSessionSaver(sessPath))
|
||||
if sess, ok := loadSession(sessPath); ok {
|
||||
if sess.InBrowser {
|
||||
log.Printf("restoring session: browser path=%q", sess.BrowserPath)
|
||||
// The saved directory may have vanished (deleted, synced away) or
|
||||
// be unreadable by this app (stat succeeds but the permission bits
|
||||
// deny the read — e.g. /storage/emulated itself, which is
|
||||
// media_rw:media_rw 0750; only /storage/emulated/0 is app-exposed).
|
||||
// Probe with a real directory read — the same operation the
|
||||
// browser's index does — rather than os.Stat, which passes for
|
||||
// unreadable dirs. Fall back to the startup directory otherwise.
|
||||
if _, err := os.ReadDir(sess.BrowserPath); err == nil {
|
||||
logic.BeginBrowserRestore(sess.BrowserPath)
|
||||
}
|
||||
} else {
|
||||
log.Printf("restoring session: file=%s cursor=%d scroll=%v find=%q", sess.File, sess.Cursor, sess.Scroll, sess.FindQuery)
|
||||
logic.BeginRestore(sess)
|
||||
// The find bar's input is a main-owned widget and the input source
|
||||
// of truth: seed it with the restored query so its first frame
|
||||
// matches the logic-side query (an empty widget would forward ""
|
||||
// and clear the restored query).
|
||||
if sess.FindQuery != "" {
|
||||
findEditor.SetText(sess.FindQuery)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clipboard plumbing (architecture.md §6.3): the logic goroutine only
|
||||
// REQUESTS clipboard operations through channels (copy/cut write, paste
|
||||
// read); the main goroutine executes the Gio ops during a frame and
|
||||
@@ -136,9 +173,13 @@ func run(w *app.Window) error {
|
||||
// applied to the main-owned find input (edge-triggered, see
|
||||
// Frame.FindClearSeq).
|
||||
var lastFindClearSeq int
|
||||
// lastFrameW/H hold the previous FrameEvent's window size (px); 0 = no
|
||||
// frame yet. Used to spot shrink frames (see ZeroWheelScroll below).
|
||||
var lastFrameW, lastFrameH int
|
||||
|
||||
for {
|
||||
switch e := w.Event().(type) {
|
||||
e := w.Event()
|
||||
switch e := e.(type) {
|
||||
case app.DestroyEvent:
|
||||
if prof != nil {
|
||||
prof.Stop()
|
||||
@@ -216,6 +257,8 @@ func run(w *app.Window) error {
|
||||
curScale = 1 // no frame yet
|
||||
}
|
||||
curFontScale := frame.FontScale // 0 until the logic has the value
|
||||
// App-local pinch font scale for the editor text (1.0 = default).
|
||||
renderer.SetAppFontScale(frame.AppFontScale)
|
||||
renderer.Draw(gtx, frame.Elems, curScale)
|
||||
glyphLayout := renderer.GlyphLayout()
|
||||
// Send search query update to the logic goroutine when it changes.
|
||||
@@ -234,7 +277,19 @@ func run(w *app.Window) error {
|
||||
}
|
||||
newFind := findEditor.Text()
|
||||
sendFind := newFind != frame.FindQuery
|
||||
// On a frame that shrinks the window (the IME opening under
|
||||
// adjustResize), Gio's window synthesizes a scroll-to-focus
|
||||
// pointer.Scroll via RevealFocus — it reads the focused field's
|
||||
// stale pre-resize bounds and nudges the editor content. Flag the
|
||||
// frame so CheckGestures drains that one synthetic event before the
|
||||
// scroll gesture consumes it (Renderer.ZeroWheelScroll); finger
|
||||
// scroll and the flinger are unaffected, and normal frames are
|
||||
// untouched.
|
||||
renderer.ZeroWheelScroll = lastFrameH > 0 &&
|
||||
(e.Size.Y < lastFrameH || e.Size.X < lastFrameW)
|
||||
lastFrameW, lastFrameH = e.Size.X, e.Size.Y
|
||||
events := renderer.CheckGestures(e.Source, gtx.Metric)
|
||||
renderer.ZeroWheelScroll = false
|
||||
// Keep frames flowing while a long press is pending: a stationary
|
||||
// finger generates no pointer events, so without this the window
|
||||
// would sleep and the long-press threshold would never be reached.
|
||||
@@ -316,6 +371,32 @@ func run(w *app.Window) error {
|
||||
})
|
||||
}
|
||||
case key.EditEvent:
|
||||
if focusedID == "editor_text" {
|
||||
// Apply the commit to the pushed snippet model
|
||||
// IMMEDIATELY (see Renderer.ApplyIMECommitToModel):
|
||||
// gioui's EditEvent callback already advanced its
|
||||
// own window state, and without this the op queue
|
||||
// lags it by one commit — every frame event in
|
||||
// the gap regresses the state and sends a
|
||||
// restartInput with pre-commit text per keystroke
|
||||
// (the random-caps bug).
|
||||
renderer.ApplyIMECommitToModel(gtx, k, frame.EditSeq)
|
||||
// The commit range is in absolute file runes (the
|
||||
// coordinate space of the pushed snippet, whose
|
||||
// Range.Start is the context start). The logic maps
|
||||
// it to bytes against the whole buffer — exact
|
||||
// even mid-fling — and applies the drift guard
|
||||
// (see editor.HandleIMECommit).
|
||||
events = append(events, ui.InputEvent{
|
||||
Handler: editor.HandleIMECommit,
|
||||
Data: editor.IMECommit{
|
||||
StartRune: k.Range.Start,
|
||||
EndRune: k.Range.End,
|
||||
Text: k.Text,
|
||||
},
|
||||
})
|
||||
break
|
||||
}
|
||||
events = append(events, ui.InputEvent{
|
||||
Handler: reg.Handler,
|
||||
Data: k,
|
||||
@@ -332,8 +413,22 @@ func run(w *app.Window) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Flush the IME snippet/selection for the focused editor field
|
||||
// (see Renderer.FlushIME): after the key events above were drained
|
||||
// and mirrored into the IME model, and before e.Frame below flushes
|
||||
// the ops to the OS (which is when gioui compares the pushed state
|
||||
// against the IME's own state.
|
||||
if focusedID == "editor_text" {
|
||||
for _, el := range frame.Elems {
|
||||
if tf, ok := el.(ui.TextField); ok && tf.ID() == "editor_text" {
|
||||
renderer.FlushIME(gtx, tf)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
e.Frame(&ops)
|
||||
mu.Unlock()
|
||||
|
||||
if newScale != curScale || newFontScale != curFontScale {
|
||||
logic.ConfigChan() <- editor.ScaleEvent{Scale: newScale, FontScale: newFontScale}
|
||||
}
|
||||
@@ -346,12 +441,25 @@ func run(w *app.Window) error {
|
||||
if sendFind {
|
||||
logic.FindQueryChan() <- newFind
|
||||
}
|
||||
// Skip layout feedback for frames built before the window size
|
||||
// was known (frame.ViewportDegenerate, set at frame-build time —
|
||||
// feedback delivery lags shaping by a frame, so checking the
|
||||
// current size here would miss them): a zero-width shape wraps
|
||||
// every line into many visual lines, and feeding those counts
|
||||
// back would poison the WrapIndex for the window's lines
|
||||
// (applied once, corrected only if those lines are re-shaped at a
|
||||
// real width — a restored scroll that moves the viewport away
|
||||
// never re-shapes them, and the poisoned counts then map a
|
||||
// legitimate scroll offset to the wrong line).
|
||||
if !frame.ViewportDegenerate {
|
||||
logic.LayoutChan() <- ui.LayoutFeedback{
|
||||
GlyphLayout: glyphLayout,
|
||||
WindowText: frame.WindowText,
|
||||
WindowStartByte: frame.WindowStartByte,
|
||||
WindowStartLine: frame.WindowStartLine,
|
||||
EditSeq: frame.EditSeq,
|
||||
ScrollOffset: frame.ScrollOffset,
|
||||
}
|
||||
}
|
||||
default:
|
||||
handleEvent(e)
|
||||
@@ -360,6 +468,67 @@ func run(w *app.Window) error {
|
||||
}
|
||||
|
||||
// exclEqual reports whether two exclusion-rect sets are identical.
|
||||
// newSessionSaver builds the relaunch-session file writer registered with
|
||||
// the logic goroutine (spec §7). The file is a tiny JSON snapshot; a torn
|
||||
// write is rejected by loadSession's parse on the next launch, so a plain
|
||||
// write is safe (no temp+rename needed).
|
||||
func newSessionSaver(path string) func(editor.SessionState) {
|
||||
return func(s editor.SessionState) {
|
||||
b, err := json.Marshal(s)
|
||||
if err != nil {
|
||||
log.Printf("session: marshal: %v", err)
|
||||
return
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
log.Printf("session: mkdir: %v", err)
|
||||
return
|
||||
}
|
||||
if err := os.WriteFile(path, b, 0o600); err != nil {
|
||||
log.Printf("session: write: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// loadSession reads and sanity-checks the relaunch session file (spec §7).
|
||||
// ok=false when there is no session, it is corrupt, or nothing is restorable
|
||||
// — in which case the app starts in the browser at the startup directory
|
||||
// as before. Browser-page sessions carry no file; an editor-page session
|
||||
// (the pre-InBrowser legacy shape included) needs one.
|
||||
func loadSession(path string) (editor.SessionState, bool) {
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return editor.SessionState{}, false
|
||||
}
|
||||
var s editor.SessionState
|
||||
s.ScrollLine = -1 // pre-line session files lack the key; 0 would restore to the top
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return editor.SessionState{}, false
|
||||
}
|
||||
// A partial/corrupt file must not restore garbage positions.
|
||||
if s.Cursor < 0 {
|
||||
s.Cursor = 0
|
||||
}
|
||||
if s.Scroll < 0 {
|
||||
s.Scroll = 0
|
||||
}
|
||||
if s.SelStart < -1 || s.SelEnd < -1 || s.SelEnd <= s.SelStart {
|
||||
s.SelStart, s.SelEnd = -1, -1
|
||||
}
|
||||
if s.FindCurByte < -1 {
|
||||
s.FindCurByte = -1
|
||||
}
|
||||
if s.ScrollLine < -1 {
|
||||
s.ScrollLine = -1
|
||||
}
|
||||
if s.ScrollSub < 0 {
|
||||
s.ScrollSub = 0
|
||||
}
|
||||
if !s.InBrowser && s.File == "" {
|
||||
return editor.SessionState{}, false
|
||||
}
|
||||
return s, true
|
||||
}
|
||||
|
||||
func exclEqual(a, b [][4]int) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
|
||||
+20
-1
@@ -1,6 +1,6 @@
|
||||
# Pad documentation
|
||||
|
||||
Three documents, kept at the level of *what, why, and invariants* — not
|
||||
Five documents, kept at the level of *what, why, and invariants* — not
|
||||
line-by-line code — so they stay true as the implementation evolves.
|
||||
|
||||
| Doc | What it is |
|
||||
@@ -8,6 +8,8 @@ line-by-line code — so they stay true as the implementation evolves.
|
||||
| [`spec.md`](./spec.md) | What the app **actually does**, measured performance, the code layout, and an explicit list of deferred features. |
|
||||
| [`architecture.md`](./architecture.md) | How it works: single-owner concurrency model, channel topology, Frame handoff contract, ownership rules, editor/browser/render internals. |
|
||||
| [`development_plan.md`](./development_plan.md) | The active plan: completed phases, remaining work, and the on-device observation loop. |
|
||||
| [`release.md`](./release.md) | The release process (`scripts/release.sh`): the gates, the install-to-all-devices policy, and the rule that on-device profiling/testing is diagnostic and needs explicit approval. |
|
||||
| [`ime.md`](./ime.md) | The IME contract: the three text models, the invariants that keep commits landing where they should, stock-gioui structural behaviors (worked around, not patched), log signatures for diagnosing IME desync, and the test layers and what each can catch. |
|
||||
|
||||
## Package inventory
|
||||
|
||||
@@ -110,6 +112,23 @@ There are TWO independent scale factors, not one:
|
||||
(window start, sub-line remainder, tap mapping, scroll clamp, caret,
|
||||
handles, highlight). Change it and the line pitch on screen changes
|
||||
(57 px/line at 1.3× vs 44 px/line at 1.0× on this AVD).
|
||||
- **App-local pinch font scale** (a third factor, in-app only): a two-finger
|
||||
pinch in the editor multiplies a continuous float32 scale (1.0 default,
|
||||
clamped 0.5–3.0, never rounded) on top of the two factors above; the
|
||||
rendered line pitch is `16.8 × fontScale × appFontScale` dp. The logic
|
||||
side folds it into `EffectiveLineHeight()` (system × app) and the
|
||||
renderer multiplies the editor's sp size by it. The pinch CENTER is the
|
||||
anchor: the logic captures the **content point** there (the glyph byte +
|
||||
offset from its baseline, with a line/fragment/sub-line fallback) and
|
||||
re-anchors it under every newly shaped layout — including the re-wrap a
|
||||
few frames after the font change — so the character under the fingers
|
||||
holds still, not merely its (rewrap-moved) visual line. It is persisted in
|
||||
the relaunch session (`AppFontScale`), and
|
||||
the session's `ScrollSub` is stored as a *fraction* of the line height so
|
||||
scroll restore is font-independent. Test hook: `scripts/emu.sh cmd pinch
|
||||
<F>` (relative, anchored at the editor region center) / `fontsize <F>`
|
||||
(absolute, top-anchored) drive the same `HandleFontPinch` path a real
|
||||
pinch delivers.
|
||||
|
||||
On-device verification notes:
|
||||
|
||||
|
||||
+98
-1
@@ -292,6 +292,34 @@ Only the visible byte range is shaped and drawn each frame:
|
||||
- Max scroll is `TotalVisuals()·lh − regionH + lh/2` with the
|
||||
font-scale-effective line height; it grows incrementally as shaped
|
||||
counts arrive (pre-shaping it equals the no-wrap estimate).
|
||||
- **Relaunch snapshot (spec §2.4):** the counts are process-local (rebuilt
|
||||
from the all-ones estimate on relaunch; only the visible window is ever
|
||||
shaped), so a persisted pixel scroll offset cannot be restored by
|
||||
re-mapping — with every count above the restored viewport at the
|
||||
estimate, `LineForVisual` maps the offset 1:1 to a logical line deeper
|
||||
by all the wrapped continuations above it, with no self-correction
|
||||
(those lines never enter the window). The snapshot therefore persists
|
||||
the wrap-independent coordinates: the logical line at the viewport top
|
||||
(derived with the same scrollDecompose + LineForVisual mapping the
|
||||
layout uses, against the CURRENT index) plus the sub-line remainder;
|
||||
the restore re-derives the offset as `line·lh + sub` — mapped through
|
||||
`VisualsBefore(line)` under the index as it stands at apply time. The
|
||||
raw offset is kept as the fallback for pre-line-coordinate session
|
||||
files. The pin is still needed after apply: on device the first frames
|
||||
after relaunch render the TOP-OF-FILE window (the scroll is armed until
|
||||
scale+size+content are all known, which can be ~600 ms), and that
|
||||
window's shaping feedback — real wrap counts for the lines ABOVE the
|
||||
restored line — lands before or just after the offset is applied.
|
||||
Those counts are correct data, but they change `V(line)`: the offset
|
||||
synthesized for the estimate index then maps to a shallower line (the
|
||||
on-device "lands further up" report). So while the restore settles,
|
||||
every accepted correction re-derives the offset as
|
||||
`VisualsBefore(line)·lh + sub` (no MaxScroll clamp — the correction
|
||||
just grew the index, so the pre-layout clamp value is stale; the
|
||||
layout of the emitted frame clamps to the fresh one), and the pin
|
||||
stands down when the restored window's own feedback arrives, after a
|
||||
2 s timeout, or when the user scrolls or a search takes over the
|
||||
viewport.
|
||||
- **Font-scale axis.** The shaper draws baselines in sp, so on Android the
|
||||
rendered line pitch in density-dp is `EditorLineHeight()*fontScale`
|
||||
(`fontScale = Metric.PxPerSp/PxPerDp`, the user font-size setting).
|
||||
@@ -444,6 +472,14 @@ Only the visible byte range is shaped and drawn each frame:
|
||||
(crash leftovers, plus the legacy deterministic "`.<name>.tmp`" name).
|
||||
Unique temp names make same-file interleaving structurally impossible even
|
||||
if the serialization regressed.
|
||||
- **Post-rename timestamp:** immediately after the rename, `WriteFileAtomic`
|
||||
sets the file's atime/mtime to now (`os.Chtimes`, best-effort). On Android
|
||||
shared storage the FUSE/MediaProvider layer serves the **previous** file's
|
||||
mtime after a rename (its media-scan DB row is not updated on rename), and
|
||||
stat-comparing change detectors (syncthing) then miss the new content
|
||||
until the next full rescan. The explicit utimensat publishes the current
|
||||
mtime and is the update the watcher reacts to immediately; it is also the
|
||||
timestamp contract `TestWriteFileAtomic_PublishesCurrentMtime` pins.
|
||||
- Write failures are tracked per file (`writeFailed`, `retryAttempts`) and
|
||||
retried via `retryChan` (exponential backoff 1 s … 30 s; the timer sends a
|
||||
non-blocking token and the owner re-snapshots at fire time). There is no
|
||||
@@ -457,11 +493,45 @@ Only the visible byte range is shaped and drawn each frame:
|
||||
- A browser tap sends the path on `OpenFileChan`. The logic goroutine creates
|
||||
the `ChunkedBuffer`, dispatches `StatFile` (size guard) and the read +
|
||||
`BuildLineIndex` tasks, switches `page` to the editor, and sets
|
||||
`justOpenedAt`.
|
||||
`justOpenedAt`. The relaunch restore (below) reuses the same `openFile`
|
||||
path.
|
||||
- **Opening-tap swallow:** the tap that opens the file is also delivered as an
|
||||
editor tap in the same frame. A short time window (`justOpenedAt`) swallows
|
||||
it so the viewport does not jump to the tapped (often EOF) position.
|
||||
|
||||
### 6.7 State restoration on relaunch (`session.go`)
|
||||
|
||||
- The restorable state is a plain comparable value, `SessionState` (last
|
||||
file, cursor byte, scroll Dp, selection range, find query/visibility/
|
||||
current-match byte). The search results themselves are not stored; they are
|
||||
re-scanned on restore.
|
||||
- **Ownership:** the logic goroutine owns snapshot content. The cmd layer
|
||||
(`cmd/pad/main.go`) owns the JSON file and registers the writer via
|
||||
`Logic.SetSessionSaver`; the owner invokes it rate-limited (≤ 1/s, only on
|
||||
change) from `emitFrame` and unconditionally at `Shutdown` (post-exit,
|
||||
single-threaded, next to `FlushAll`). Satisfies the single-owner rule:
|
||||
the callback receives a value copy, and the file I/O is a tiny synchronous
|
||||
write in the cmd layer's closure.
|
||||
- **Restore:** the cmd layer reads the file at startup and calls
|
||||
`Logic.BeginRestore(s)` BEFORE `Run()` (single-threaded window, like
|
||||
`NewLogic` itself): it sets `Filename`, shows the editor page, and arms the
|
||||
scroll. `Run()` then re-opens the file through the normal `openFile` path.
|
||||
- **Stat success:** the find state lands (query, visibility, current-match
|
||||
byte via `Find.Restoring`/`Find.RestoreMatch`, so the re-scan's first
|
||||
result re-selects the saved match WITHOUT re-scrolling the restored
|
||||
viewport).
|
||||
- **Content arrival:** cursor/selection land, clamped to the file length
|
||||
(path-guarded, so a late result for a replaced file cannot apply the
|
||||
snapshot to the wrong buffer); the query re-scans only if the find bar
|
||||
was open (a closed-bar scan would be dropped and leave `Scanning` stuck).
|
||||
- **Scroll offset:** applied only after the first `ScaleEvent` has been
|
||||
laid out: the size `ConfigEvent` precedes it, and a layout in between
|
||||
computes the viewport in the wrong unit, whose one-way `MaxScroll` clamp
|
||||
would corrupt the offset.
|
||||
- **File gone** (stat failure): the restore is abandoned and the app lands
|
||||
on the browser page with a clean editor. Any user open of another file
|
||||
cancels an in-flight restore for the same reason as the path-guard.
|
||||
|
||||
## 7. Browser internals (`internal/browser`)
|
||||
|
||||
- `BrowserState` is **embedded by value** in `State` (single owner; no
|
||||
@@ -474,6 +544,29 @@ Only the visible byte range is shaped and drawn each frame:
|
||||
- The browser renders via `ListView`/`ListItem` elements; rows carry
|
||||
`Interaction` handlers that send channel requests (tap) or mutate state
|
||||
directly when the handler runs on the owner (scroll).
|
||||
- **Live refresh.** While the browser page is on screen the current directory
|
||||
is watched with **inotify** (`dirWatcher`, `internal/editor/dirwatcher.go`):
|
||||
a create / delete / rename / modify / attrib event there — including an
|
||||
inbound sync that changes a file's mtime — re-reads the directory within tens
|
||||
of ms, re-sorted in the *current* sort mode. A slow **backstop rescan** (30 s)
|
||||
covers the rare event the FUSE inotify layer drops; it is the only remaining
|
||||
periodic component and, by the invariant below, emits no frame when nothing
|
||||
changed. The watch is re-pointed whenever the directory changes (a no-op while
|
||||
the path is unchanged) and is closed when the logic goroutine exits. Invariant:
|
||||
**a re-read that finds the directory unchanged rebuilds nothing and emits no
|
||||
frame**, so the event-driven emission contract (§2;
|
||||
`TestNoFramesWhileIdle_Browser`) still holds while the browser idles. A
|
||||
landing (re)index is applied in place: the visible+prefetch pages are
|
||||
repopulated synchronously from the fresh index (no async gap / flicker), the
|
||||
scroll offset is preserved then clamped, and an active search is re-filtered
|
||||
without a scroll jump. Bounded to **one read per directory**: a re-read
|
||||
requested while a read of the same directory is in flight coalesces (skips),
|
||||
and a stale result for a directory the user has since left is dropped rather
|
||||
than clobbering the current view. A failed *refresh* keeps the last good view
|
||||
(the next event or backstop tick retries); a failed *navigation* still recovers
|
||||
to the previous directory. Returning to the browser from the editor triggers
|
||||
one immediate re-read so the list is current on the frame you land back (events
|
||||
that fired while the editor was on screen are no-ops there).
|
||||
|
||||
## 8. Render pipeline (`internal/ui`)
|
||||
|
||||
@@ -586,6 +679,10 @@ cannot measure this app because it renders into a `SurfaceView`).
|
||||
app and seeds/removes a file in its storage, and the developer may be using
|
||||
the phone while a release runs. It exists to investigate a suspected
|
||||
frame/battery problem, not to certify a release.
|
||||
|
||||
The full release flow (gates → build → install to all connected devices,
|
||||
and the policy that a phone is install-only during a release) is
|
||||
`doc/release.md` + `scripts/release.sh`.
|
||||
- The profiler is owned by the goroutine that creates it and is single-goroutine
|
||||
(no locks). It does **not** `Sync()` the CSV per flush (only per row batch) to
|
||||
avoid periodic fsync hitches in the logic path.
|
||||
|
||||
@@ -1048,3 +1048,298 @@ Verified on device: with the exclusion active, horizontal drags from the
|
||||
left-edge start handle no longer trigger `startBackNavigation`
|
||||
(`dumpsys window` shows the exclusion region; logcat shows zero
|
||||
back-gesture previews).
|
||||
|
||||
## 18. Pinch-to-change-font-size, continuous (2026-08-22)
|
||||
|
||||
A two-finger pinch in the editor now changes the app's font size smoothly,
|
||||
without snapping to whole points. The app-local scale is a float32
|
||||
(default 1.0, clamped 0.5–3.0) layered on top of the system user font
|
||||
setting; it is never rounded anywhere in the pipeline.
|
||||
|
||||
**Renderer (`internal/ui`).** Gio v0.10 has no two-finger pinch primitive,
|
||||
so the Renderer owns a probe event tag clipped to the editor text region
|
||||
(next to the long-press probe). It tracks the active pointers across frames
|
||||
(window-px positions keyed by pointer ID) and emits one relative factor per
|
||||
frame — `pinchDist(cur)/pinchDist(prev)` — as `ui.FontPinchEvent` to the
|
||||
editor's new `ui.Pinch` interaction handler. `pinchDist` (two lowest-ID
|
||||
pointers) is a pure function, unit-tested. While a pinch is active, scroll
|
||||
emission is suppressed so the first finger does not drag the text.
|
||||
`drawWrappedText` multiplies the editor's sp size by the frame's
|
||||
`AppFontScale` (main goroutine feeds it via `SetAppFontScale` before
|
||||
Draw); ascent/line-height/highlight/caret/handles all follow because they
|
||||
derive from the same size.
|
||||
|
||||
**Logic (`internal/editor`).** `State.appFontScale` + `HandleFontPinch`
|
||||
(multiply by the per-frame factor and clamp). The anchor is the pinch
|
||||
CENTER, not the viewport top — and it is a **content point**, not a layout
|
||||
point: `State.captureContentPin` names the glyph under the midpoint (the
|
||||
ABSOLUTE buffer byte — the layout's `ByteOffsets` are window-relative, so
|
||||
the capture adds `IMEWindowStartByte` — plus the point's offset from that
|
||||
glyph's baseline), captured under the pre-change layout. A rewrap moves
|
||||
the *text* of a visual line (the same fragment index holds different bytes
|
||||
after the rewrap), so pinning (line, fragment) would leave a different
|
||||
character at the center; naming the byte does not. The offset is applied
|
||||
in two phases: (1) immediately, `rescaleScrollAnchored` rescales `S + m`
|
||||
about the center (continuous, valid until rewrap lands); (2) on **every
|
||||
newly shaped layout at the current scale** — the re-shape after the font
|
||||
change and the rewrap corrections that follow it — `refineContentPin`
|
||||
recomputes the offset from the pinned byte's fresh baseline:
|
||||
`S' = vk·lh + Y + Dy − m`, where `vk = VisualsBefore(WindowStartLine)` is
|
||||
the window's FIRST visual line (the window top sits at content `vk·lh`,
|
||||
NOT `floor(S/lh)·lh` — a different line whenever the viewport top lands
|
||||
mid-way through a wrapped logical line) and `Y` is the byte's baseline in
|
||||
the fresh layout (located by its absolute byte, window start from
|
||||
`LayoutFeedback.WindowStartByte`). That lands the byte exactly on the
|
||||
center and is a fixed point when the layout already agrees (no drift, no
|
||||
oscillation). Two stale-data traps had to be closed: the scale change
|
||||
**invalidates the last shaped layout** (`invalidateShapedLayout`) —
|
||||
otherwise the next frame computes its window start with the OLD line
|
||||
height and the NEW rescaled offset, a window ~10k lines off — and
|
||||
`refreshFontPin` skips feedback shaped at a different scale (during a
|
||||
pinch every frame changes the scale, so all but the latest feedback are
|
||||
stale). While armed (2 s, refreshed by feedback) the pin rides every
|
||||
frame; a (line, fragment, sub-line) anchor (`captureFontPin`/`applyFontPin`)
|
||||
stands in for points off any glyph; an edit (EditSeq mismatch), a scroll,
|
||||
or the timeout disarms it. `SetAppFontScale` (the `fontsize` debug
|
||||
command) keeps the top-anchored behavior (no fingers to center on) and
|
||||
invalidates the stale layout too. `EffectiveLineHeight()` is now system ×
|
||||
app; every geometry consumer (window start, tap mapping, scroll clamp,
|
||||
restore) was already routed through it. `Frame.AppFontScale` carries the
|
||||
value to the renderer; `LayoutFeedback.ScrollOffset` carries the shaped
|
||||
scroll back (used by the fallback path and diagnostics).
|
||||
|
||||
**Persistence.** The session snapshot gains `AppFontScale`, and
|
||||
`ScrollSub` is now stored as a *fraction* of the line height (font-
|
||||
independent; legacy Dp values > 1 are converted on restore).
|
||||
|
||||
**Testing.** `pinch_font_test.go` (continuous product of small factors,
|
||||
clamp at both ends, center-anchor invariance, glyph hit-testing, content-
|
||||
pin capture, the refine keeping the pinned BYTE on the center across a
|
||||
rewrap that moves it to another fragment, the fixed-point property, the
|
||||
(line, fragment) fallback, fragment clamp on pinch-out, bad-data no-op),
|
||||
`pinch_test.go` (pinchDist/pinchMid geometry,
|
||||
factor-series telescoping). On the emulator
|
||||
(`scripts/emu.sh cmd pinch <F>` / `fontsize <F>` — one-shot commands that
|
||||
drive the same `HandleFontPinch` path a real pinch delivers, since adb has
|
||||
no two-finger input; `pinch` anchors at the editor-region center): five
|
||||
×1.05 steps produced line pitches 44→46→49→51→54→56 px (autocorrelation-
|
||||
measured) — continuous, no whole-point snapping; on a 40k-line wrapped
|
||||
file scrolled to the middle, a pinch in/out cycle (×1.5 → ×0.75 →
|
||||
×1.125) exercised the rewrap in BOTH directions (1→2 and 2→1 fragments
|
||||
per line) and the pin converged to a fixed point within 2–3 layout-
|
||||
feedback frames at every step, keeping the captured BYTE's line on the
|
||||
region center (verified against the app's own geometry, not the pixels);
|
||||
the ground-truth tap test (tap a line, type a marker, read the file)
|
||||
passed at a 1.125× scale after two rewrapping pinches; `fontsize` keeps
|
||||
the top anchor across a 1→2 rewrap; font scale, file, cursor and scroll
|
||||
all survive a full restart; 0.5/3.0 clamps hold; one-finger scroll is
|
||||
unaffected (and takes over the viewport, disarming the pin).
|
||||
|
||||
**Bugs found by the emulator round (both would have passed the unit
|
||||
suite).** (1) `GlyphLayout.ByteOffsets` are window-relative, not absolute:
|
||||
capturing the pin's byte without adding the window start made the pin
|
||||
chase a moving offset and never converge. (2) The scale change left the
|
||||
old `GlyphLayout` in place; the next frame computed its window start with
|
||||
the OLD line height and the NEW rescaled offset — a window ~10k lines
|
||||
from the viewport (visible as a ~1000-line jump). Fixed by
|
||||
`invalidateShapedLayout()` on every scale change.
|
||||
|
||||
**Bug found by the on-device round (the emulator round could never have
|
||||
found it — adb has no two-finger input, and the debug `pinch` command
|
||||
bypasses the probe entirely).** On the phone a real two-finger pinch did
|
||||
nothing. On-device logcat (probe event logs + per-frame event counts)
|
||||
showed the scroll gesture receiving every finger move while both probe
|
||||
tags received zero events. Root cause: the probes were declared as
|
||||
`struct{}` fields of the Renderer. The unnamed fieldless `struct{}` is a
|
||||
SINGLE canonical Go type, so `pressProbe`, `pinchProbe` (and a
|
||||
diagnostic third) were the SAME tag value. Gio's router keys handlers by
|
||||
tag value, so all three `event.Op` registrations collapsed into one
|
||||
handler; the press probe's drain (which runs first in `CheckGestures`)
|
||||
consumed every event for that tag and the pinch probe was structurally
|
||||
starved. Fixed by giving each probe its own named type
|
||||
(`pressProbeTag`, `pinchProbeTag`), with a regression test
|
||||
(`TestProbeTagIdentity`) asserting the tags remain distinct map keys, and
|
||||
`TestRealDrawOpsProbeHit`, which runs the real `Renderer.Draw` op stream
|
||||
through a real `input.Router` and asserts the probe tags receive the
|
||||
pointer press. Verified on the Pixel 9 Pro: 927 probe events across a
|
||||
multi-pinch session, 137 per-frame factors emitted and applied (net
|
||||
scale 1.70×, font visibly enlarged), scroll suppressed mid-pinch,
|
||||
anchor held.
|
||||
|
||||
## 19. Pinch tracker: explicit pair, two-mover formation, slow frames (2026-08-23)
|
||||
|
||||
The §18 probe design ("two lowest-ID pointers") was replaced by an explicit
|
||||
pair state machine (`internal/ui/pinch_tracker.go`, pure and unit-tested;
|
||||
the renderer's `consumePinchProbe` is now a thin adapter that feeds events,
|
||||
executes the tracker's grabs, and emits its factor). Four on-device failure
|
||||
modes drove the rewrite, plus a whole class of slow-frame bugs only visible
|
||||
on the ~1 fps emulator:
|
||||
|
||||
1. **Single-finger scroll changed the font** — the pair was re-derived from
|
||||
whatever pointers happened to be present, so a scroll finger got paired
|
||||
with a stale pointer and its drags became "pinch".
|
||||
2. **Scroll-down enlarged the font** — same root: the scroll finger's
|
||||
distance to a stale second pointer grows as it moves.
|
||||
3. **Two fingers produced a sudden zoom before the pinch** — the baseline
|
||||
(`prevDist`) survived from the previous pinch, so a new pinch 2.5× wider
|
||||
emitted 2.5× on its first frame.
|
||||
4. **Pinch-out stopped and became a scroll** — the pair was not explicit;
|
||||
once a finger moved past the scroll slop the router handed the pointer
|
||||
to the scroll gesture and the "pair" silently switched composition.
|
||||
|
||||
**Explicit, stable pair + grabs.** When the pair forms, the adapter issues
|
||||
`pointer.GrabCmd` for BOTH fingers (exclusive delivery to the probe:
|
||||
releases arrive even off-clip, and scroll/click are dropped with a Cancel —
|
||||
so the pair can never be stolen mid-gesture, failure mode 4). The pair's
|
||||
composition and its baseline are never re-derived from ambient pointers.
|
||||
`factor()` = current pair distance / previous frame's distance, one factor
|
||||
per frame (the Android driver replays historical samples — several drags per
|
||||
frame — so the font sees one factor per frame, not per sample). A sanity
|
||||
clamp drops factors outside 0.1–10 and advances the baseline, so a
|
||||
teleporting pointer (ID-reuse noise) cannot jump the font. When a pair
|
||||
finger lifts, the other becomes the **survivor**: it stays grabbed (Gio
|
||||
v0.10 has no release-grab) and its drags are forwarded as a plain scroll
|
||||
delta (`survivorScroll`), so the finger is not dead. A second finger that
|
||||
returns re-forms the pair with a fresh baseline.
|
||||
|
||||
**Formation requires TWO MOVING fingers — and nothing else.** The dominant
|
||||
real-world case is a palm edge already down when the two pinch fingers
|
||||
land; a static rule about "which finger is the palm" (the oldest? the
|
||||
newest? the still one?) cannot survive both palm-first and palm-last hand
|
||||
lands. Movement is the only signal that works for both: while 2–3 fresh
|
||||
fingers are down the tracker is *pending*; the pair forms — at `factor()`
|
||||
time, after the whole frame's events, never per-event (per-event locking
|
||||
in the first mover pair seen would pair a finger with a drifting palm) —
|
||||
when two pending fingers have each moved more than `pinchMoveEps` (10 px)
|
||||
from where they pressed, and it is the mover pair whose **distance**
|
||||
changed most (a drifting palm's distance to a finger changes little; a
|
||||
pinch's does). A lone mover is a scroll, never a pair; a unison movement
|
||||
(a two-finger slide) leaves the distance unchanged and forms nothing.
|
||||
Consequences verified: a resting (pruned, >300 ms) or still palm can never
|
||||
enter the distance; the three fresh-fingers case pairs the pinch fingers;
|
||||
the re-form candidate (a finger landing on a survivor) must also move
|
||||
before it becomes the pair.
|
||||
|
||||
**Baseline = the PRESS distance.** Any spread that happened before the pair
|
||||
starts is owed, not lost: the formation frame emits `d_current /
|
||||
d_press`, and a pinch that breaks before its first `factor()` settles the
|
||||
same owed factor at the break.
|
||||
|
||||
**Slow frames (the ~1 fps emulator batches a whole gesture into one
|
||||
drain).** (a) *Born-and-dead in one frame*: presses, drags and BOTH
|
||||
releases in one drain — pending releases are held **lazy** (`released`
|
||||
map) until `factor()`: the pair forms at the fingers' final positions,
|
||||
then breaks there (no survivor when both released), settling the owed
|
||||
factor. (b) *Pair broke mid-frame*: `brokeFactor`/`brokeMid` are settled
|
||||
at the break (against `prevDist`, or the press distance when fresh) and
|
||||
emitted by the subsequent `factor()` call, which would otherwise see
|
||||
`on==false` and drop the frame's movement.
|
||||
|
||||
**Emulator multi-touch injection.** adb has no two-finger input, so the
|
||||
failure modes could not be tested end-to-end until `tools/touchinject`: a
|
||||
platform-signed (AOSP test key, `INJECT_EVENTS` granted) toy app whose
|
||||
broadcast receiver injects a scripted `MotionEvent` stream
|
||||
(`down/move/up/wait`, display px) through
|
||||
`InputManager.injectInputEvent` — `/dev/uhid` is a dead end (no kernel
|
||||
module in the image). Two known flakes, both documented in the harness:
|
||||
the receiver process is "cached" and the 1.5 GB emulator OOM-kills it
|
||||
mid-script occasionally (the harness verifies `=== done` in logcat and
|
||||
re-runs; service routing is blocked by Android 12+ background-start
|
||||
restrictions, and the AVD's locked bootloader blocks the system-app
|
||||
escalation); and burst drags (all moves within one frame's drain) do not
|
||||
scroll — the app is on-demand-rendering at ~1 fps, so scroll tests space
|
||||
the moves ~80 ms apart, which also matches what a real finger produces
|
||||
over several frames.
|
||||
|
||||
**End-to-end results (real injected MotionEvents, big wrapped file).**
|
||||
Single-finger drag: zero factors, scale unchanged (font), content scrolls.
|
||||
Two-finger pinch-out 300→596 px: exactly one factor 596/300 = 1.98667,
|
||||
font ~2×. Palm-first three fingers (palm resting and still, pinch fingers
|
||||
landing 80/120 ms later): pair is the two pinch fingers — the factor
|
||||
tracks the pinch, the palm never enters the distance. Lift one finger
|
||||
mid-pinch: the factor stream stops at the lift (font frozen), the
|
||||
survivor's 600 px drag scrolls the content 600/3.5 = 171.4 dp. The
|
||||
one-frame leak at formation (the pair's own drags of the formation frame
|
||||
still reach scroll, since the grabs commit next frame) is bounded by the
|
||||
scroll slop — the deliberate cost of not grabbing on press, which would
|
||||
kill single-finger scrolls.
|
||||
|
||||
**Testing.** `pinch_test.go` now covers: factor-series telescoping
|
||||
(baseline = press distance), single-finger never scales, resting/stale
|
||||
palm excluded, extra finger during an active pinch ignored, fresh baseline
|
||||
per pinch, sanity clamp, the three slow-frame shapes (full pinch in one
|
||||
frame, stationary born-dead, born-and-dead), palm-first three fingers,
|
||||
and survivor scroll + re-form (candidate must move).
|
||||
`real_draw_probe_test.go` runs the real `Renderer.Draw` op stream through
|
||||
a real `input.Router`: press frame (pending, nothing), formation frame
|
||||
(grabs + owed factor, one-frame scroll leak), post-formation frames (scroll
|
||||
sees nothing of the pair), off-clip survival, release via the grab,
|
||||
survivor scroll forwarding, re-form.
|
||||
|
||||
## 20. IME open: content must not shift (2026-08-23)
|
||||
|
||||
**Bug.** With the soft keyboard open (adjustResize), the editor content
|
||||
jumped up by exactly 32 dp (112 px) every time the keyboard appeared.
|
||||
Top-anchored layout keeps the window start line put when only the
|
||||
viewport height changes, so the shift was not our layout: KBW
|
||||
instrumentation of every `ScrollOffset` writer showed
|
||||
`HandleScroll` receiving a single +112 px delta at the resize frame.
|
||||
|
||||
**Root cause (Gio, not the app).** `gioui.org/app` `window.go`, on every
|
||||
frame whose viewport *shrank*, calls `Router.RevealFocus(viewport)` —
|
||||
"scroll the focused widget into view". For a text editor the focused
|
||||
field's registered bounds (stale — from the pre-resize, taller frame)
|
||||
extend below the new viewport, so RevealFocus synthesizes a
|
||||
`pointer.Scroll` event (`Source: Touch`, position (0,0), Y = the nudge)
|
||||
delivered to the focused field's scroll handler. `gesture.Scroll`
|
||||
consumes it like any wheel scroll → `HandleScroll` → the 32 dp jump.
|
||||
The event is invisible to the app: it never enters the pointer queue
|
||||
(no `MotionEvent` on the Android side), it is manufactured by the router
|
||||
during `processEvent(frameEvent)`, before the app's frame handler runs.
|
||||
Reproduced at the router level: `RevealFocus` on a shrunken viewport
|
||||
queues exactly one scroll event for the gesture's tag.
|
||||
|
||||
**Why not the obvious fixes.**
|
||||
- Zeroing the scroll *range* on the shrink frame does nothing: the router
|
||||
UNIONs scroll ranges into the handler's filter across frames
|
||||
(`pointerFilter.Add`/`Merge`), so the historical max can never shrink
|
||||
back to zero — the clamp stays at ±∞ forever.
|
||||
- Patching `app/window.go` to drop the shrink→RevealFocus call would mean
|
||||
shipping a forked gioui (the build constraint is clean v0.10.0).
|
||||
- `adjustNothing` removes the resize but hides the cursor line under the
|
||||
keyboard.
|
||||
|
||||
**Fix (app-side, two files).**
|
||||
- `cmd/pad/main.go`: on each `FrameEvent`, detect a shrink
|
||||
(`e.Size` smaller than the previous frame's) and set
|
||||
`renderer.ZeroWheelScroll` for that one frame.
|
||||
- `internal/ui/render.go` (`CheckGestures`): when flagged, drain
|
||||
`pointer.Scroll` events for the editor scroll gesture's tag
|
||||
(`q.Event(pointer.Filter{Target: reg.scroll, Kinds: pointer.Scroll})`)
|
||||
before `gesture.Scroll.Update` consumes anything. Only the synthesized
|
||||
nudge matches: finger scroll is `pointer.Drag`, inertia is the
|
||||
flinger, and on a phone there is no trackpad wheel. Normal frames are
|
||||
untouched.
|
||||
|
||||
**Verification.**
|
||||
- `reveal_focus_drain_test.go`: real `input.Router` + real
|
||||
`Renderer.Draw` ops; the focused field (KeyDown interaction required —
|
||||
it records the `event.Op` tag reference, and a per-frame
|
||||
`key.FocusFilter` consumer marks the handler focusable, else the key
|
||||
queue clears the focus each frame), shrunken viewport, `RevealFocus` →
|
||||
exactly one synthetic scroll queued for the gesture tag; the drain
|
||||
terminates and `gesture.Scroll.Update` then returns 0.
|
||||
- Emulator E2E (real injected taps, keyboard really opens, window
|
||||
2560→1527 px): pre-fix the scroll gesture emitted delta=112 on the
|
||||
shrink frame and a screenshot cross-correlation showed a 112 px content
|
||||
shift; post-fix the drain consumes the event, the gesture delta is 0,
|
||||
and the cross-correlation shift is 0 (corr 0.987). The perf-CSV
|
||||
(ScrollDP per logic frame) shows no 32 dp step when the keyboard opens
|
||||
on the final build.
|
||||
|
||||
**Notes.** The `aosp_atd` emulator later started ANR-ing Pad on first
|
||||
frame — the ANR trace shows the main thread in
|
||||
`GioView.onFrameCallback` → `glDeleteBuffers` → gfxstream guest →
|
||||
`madvise` (91 s system time): the emulated GPU's buffer-free path,
|
||||
unrelated to input handling. Final verification therefore used a small
|
||||
file (fast first frame) plus the router-level test.
|
||||
|
||||
+247
@@ -0,0 +1,247 @@
|
||||
# The IME contract and its failure modes
|
||||
|
||||
What the editor relies on when talking to Android's IME (Gboard), what
|
||||
stock gioui does structurally, and the failure modes observed on device —
|
||||
with their log signatures and the invariants that keep the system safe.
|
||||
This is the map for the next time text lands in the wrong place.
|
||||
|
||||
## The three models
|
||||
|
||||
An IME session involves **three independent copies of the text** that must
|
||||
be kept consistent:
|
||||
|
||||
1. **The file** (logic goroutine, the truth).
|
||||
2. **The pushed snippet** — the 32 KB window around the caret that we push
|
||||
to the IME (`key.SnippetCmd`). All IME commit positions are in *this*
|
||||
coordinate space (absolute file runes, `Range.Start` = window start).
|
||||
3. **Gboard's local model** — Gboard's private copy of the snippet plus its
|
||||
own caret/selection/composition state, which it updates from our pushes
|
||||
*and* from its own bookkeeping of the commits it sends.
|
||||
|
||||
Every bug in this area is a loss of consistency between model 3 and model
|
||||
1. The commit that arrives names a range in model 3; applying it to model 1
|
||||
is only correct if the two still agree.
|
||||
|
||||
## Invariants the code must maintain
|
||||
|
||||
- **Commits map against the whole buffer, never a stale window.**
|
||||
`HandleIMECommit` converts the commit's rune range to bytes with a
|
||||
whole-buffer scan (`runeToByteWhole`), so it stays exact while the
|
||||
visible window moves (a fling in flight). A renderer-side IME model was
|
||||
tried and abandoned: it drifts whenever the render window moves
|
||||
(commit `f54ca2f`).
|
||||
- **The snippet window is hysteresis-gated and decoupled from the render
|
||||
window** (`State.computeIMESnippetWindow`): 32 KB, re-anchored only when
|
||||
the caret comes within 4 KB of an edge. Re-anchoring reaches Gboard as a
|
||||
`restartInput`, which starts a fresh input session that re-derives
|
||||
auto-capitalization — a per-scroll or per-tap re-push is what caused the
|
||||
random mid-word caps (commit `dd9493b`).
|
||||
- **The post-commit caret is `Range.Start + len(text)`**, not
|
||||
`Range.End + len(text)`. Identical for insertions; for a replacement
|
||||
(autocorrect over a selection) the old formula lands `(End−Start)` runes
|
||||
past the inserted text and desynchronizes the IME at the exact moment its
|
||||
model is being updated (commit `51344b9`). The logic side
|
||||
(`applyIMECommitBytes`) and the renderer's immediate push
|
||||
(`ApplyIMECommitToModel`) must compute the *same* caret.
|
||||
- **A legitimate commit is always local**: at the caret, inside the live
|
||||
selection, or a correction a few runes left of the caret. The guard
|
||||
(`HandleIMECommit`, `maxCommitDistance = 1024`) snaps anything else to
|
||||
the caret and arms the resync. A 5-rune autocorrect 19,000 runes from
|
||||
the caret is never legitimate; applying it verbatim clobbers distant
|
||||
text (commit `a83cc1a`).
|
||||
- **Key-event edits desync the IME exactly like a dropped commit — and the
|
||||
IME's next commit is anchored at the pre-edit caret.** Gboard sends
|
||||
Enter as a KEY EVENT, not a commit (the logic's `handleKey(NameReturn)`
|
||||
inserts the `\n`); the IME's model never sees that edit unless a
|
||||
re-syncing snippet push reaches it first, and the push can lose the race
|
||||
— the next commit can arrive in the very same input batch as the key
|
||||
event, so no frame has drawn. While one such edit is pending
|
||||
(`EditorState.imeStaleEditByte`), the guard accepts a small (≤ 2 rune)
|
||||
TEXT commit that ends at or before the edit position (`IME STALE-OK`):
|
||||
everything before the edit is byte-identical in both models, so applying
|
||||
it verbatim is exact (the usual shape is the IME appending to, or
|
||||
correcting, the word it last committed). The pending edit is consumed and
|
||||
a resync is armed. Never relaxed for: empty-text commits (a stale range
|
||||
deletion is how documents get clobbered), commits ending past the edit
|
||||
(the range crosses the shift), or anything without a pending edit
|
||||
(commit `a83cc1a`'s guard stays intact otherwise). The pending edit is
|
||||
set by `noteNonIMEEdit` (Enter, hardware backspace/delete, cut, paste),
|
||||
consumed by the next IME commit or key edit, cleared by a tap.
|
||||
- **A desynchronized IME is healed only by a restart.** See the structural
|
||||
fact below: selection pushes after a commit are deduplicated away, so
|
||||
the *only* mechanism that makes Gboard re-read the truth is a snippet
|
||||
change (`restartInput`). `IMEForceResync` arms on the **first**
|
||||
anomaly, not a streak: the next frame ships the snippet trimmed by one
|
||||
rune (one restart), the following frame ships the full window (one more),
|
||||
then quiet. One-shot, so normal typing never pays for it
|
||||
(commits `cb8ebc0`, `a83cc1a`).
|
||||
- **Pre-commit frames must not flush the IME — and the hold is armed with
|
||||
the drained frame's own seq.** `ApplyIMECommitToModel` updates the pushed
|
||||
model on the drain pass and arms `imeHold` with the **EditSeq of the
|
||||
frame being drained** (it is the pre-commit frame — built before the
|
||||
logic processed the commit — and its snippet would clobber the just-
|
||||
updated model). `FlushIME` skips frames whose EditSeq is at most the
|
||||
armed one and resumes on the next higher; the logic's `HandleIMECommit`
|
||||
always markDirties, so the post-commit frame exists and always advances
|
||||
the seq. The hold is bounded (8 frames) so a stalled logic can never
|
||||
stick the renderer. Two details that are load-bearing: the frame's
|
||||
`ui.TextField.EditSeq` is copied from the state in `EditorLayout` —
|
||||
without it every frame reads 0, `EditSeq <= holdSeq` matches *every*
|
||||
frame, the hold releases only via the 8-frame timeout, and resync pushes
|
||||
(and all IME sync) are silently swallowed (this is why the resync armed
|
||||
by the 2026-09-19 drift-snap never reached Gboard, letting it keep
|
||||
committing against a stale model); and the seq must be the drained
|
||||
frame's, not "the last drawn frame's" — the drained frame is pre-commit
|
||||
even when its seq already differs from earlier ones (a key event landed
|
||||
in between), and arming with the older value releases the hold on the
|
||||
very frame whose snippet is stale.
|
||||
|
||||
## What stock gioui does structurally (worked around, not patched)
|
||||
|
||||
All of the following is stock v0.10.2 behavior. The fixes live entirely in
|
||||
pad; the fork at `~/gioui` carries **diagnostic logging only** (the
|
||||
`PADIME` lines) and can be dropped for stock gioui at any time without
|
||||
behavioral change.
|
||||
|
||||
1. **Post-commit selection pushes are deduplicated away.** The commit
|
||||
callback (`callbacks.EditorReplace`) advances the window's stored
|
||||
`imeState.Selection` *before* the frame's state comparison, so a
|
||||
`SelectionCmd` pushed in the same frame as a commit is "no change" and
|
||||
`imm.updateSelection` is never called. Consequence: after a commit, the
|
||||
IME's caret can only be corrected by a snippet restart. This is why the
|
||||
resync exists and why "just re-push the selection" is not a fix.
|
||||
2. **`restartInput` shadows `updateSelection`.** In
|
||||
`window.EditorStateChanged`, a snippet change takes an early-return
|
||||
branch: the selection update in the same state change is dropped.
|
||||
Gboard re-fetches the selection on restart, so this is harmless — but
|
||||
it means a restart frame is the *only* frame where selection and text
|
||||
are guaranteed to be re-read together.
|
||||
3. **Selection commands are focus-gated.** `keyQueue.setSelection`
|
||||
silently drops a `SelectionCmd` whose tag is not the current key-focus
|
||||
(`req.Tag != state.focus`). A selection push can vanish with no log and
|
||||
no feedback — the IME never acks selections. If taps stop moving
|
||||
Gboard's caret, check focus first.
|
||||
4. **There is no ack anywhere.** Snippet pushes, selection pushes, and
|
||||
commits are fire-and-forget. The system can only *detect* desync
|
||||
indirectly (a commit landing where no text expects it), which is what
|
||||
the drift guard does.
|
||||
|
||||
## Gboard behaviors observed
|
||||
|
||||
- Gboard sends Enter as a **key event** (InputConnection.sendKeyEvent),
|
||||
not a text commit: the app inserts the `\n` via `handleKey(NameReturn)`
|
||||
and the IME's model never learns about it until a re-syncing snippet
|
||||
push (see the key-event-edit invariant above). The emulator's Gboard
|
||||
does the same; its word-appending is milder than the phone's AICore
|
||||
Gboard, so the desync shows as a caret-off-by-one there and as
|
||||
duplicated text on the phone.
|
||||
- Gboard keeps a word selection on its own (a tap near a word selects it)
|
||||
and commits autocorrect as a **replacement over its local selection** —
|
||||
the commit range is then as wide as the word, not a point insertion.
|
||||
- While composing, Gboard re-sends the whole word on every keystroke
|
||||
(`"t"`, `"th"`, `"thi"`, … each replacing the previous), and on
|
||||
backspace it re-sends the shrinking word down to `text=""`. Both are
|
||||
normal; the drift guard must not treat them as anomalies (they are at
|
||||
the caret).
|
||||
- When its local model desynchronizes, Gboard can enter an endless
|
||||
empty-fix-up loop (`text=""`, one commit per ~150 ms) trying to
|
||||
reconcile text that is not in its model. The file is never damaged (the
|
||||
guard snaps each one to the caret); the resync breaks the loop.
|
||||
- The phone's AICore Gboard is stricter/more aggressive than the emulator's
|
||||
Gboard. A flow that is clean on the emulator can still desync on device.
|
||||
|
||||
## Log signatures → diagnosis
|
||||
|
||||
Permanent, low-volume lines (see "Diagnostics"):
|
||||
|
||||
| Signature in logcat | Meaning |
|
||||
|---|---|
|
||||
| `IME TAP … cursor=N winStart=M` | A tap moved the logic cursor (bytes). Compare with the next `IME COMMIT` ranges (runes) — they should be in the same neighborhood (rune ≈ byte − multibyte count, **compute it, don't assume a constant**). |
|
||||
| `IME COMMIT range=[a,b) text=… -> [x,y)` | The logic applied the commit at bytes [x,y). The pre-`->` range is Gboard's coordinate space; the post-`->` range is where it actually landed. |
|
||||
| `IME DRIFT-SNAP … snap to caret + resync` | Anomaly detected: a commit far from the caret/selection (or a stale small commit) was applied at the caret instead. One = a tap/scroll desync just happened; a run = Gboard was looping. | | `IME STALE-OK … staleEdit=N -> apply as-is + resync` | A key-event edit (usually Enter) is pending and the IME's commit was anchored at the pre-edit caret; it was applied at its reported range (byte-identical there) and a resync armed. Expected once per key-edit → keystroke sequence; a run without intervening commits = something is replaying. |
|
||||
| `IME PUSH snippet=[a,b) … (restart)` | We re-anchored the IME window → Gboard restarts its input session. Should be rare: app open, file switch, caret crossing a window margin. Frequent restarts = the hysteresis gate is being defeated (check render-window coupling). |
|
||||
| `IME PUSH sel=[s,e)` | We pushed a caret/selection to Gboard. If a tap does not produce one (or it repeats forever), the selection pipeline is broken upstream (focus gate, dedup, float-noise re-emit). |
|
||||
| `PADIME EditorStateChanged sel A -> B` / `PADIME updateSelection sel=[…]` / `PADIME restartInput` | The gio→Gboard boundary: what actually crossed into Android. If `IME PUSH` shows a value the `PADIME` lines never show, the drop is inside gioui (focus gate / dedup); if `PADIME` shows it and Gboard still misbehaves, the IME ignored it. |
|
||||
|
||||
**Capture the log at incident time.** Logcat rotates fast; the incident
|
||||
that started this document was only reconstructable because the capture
|
||||
happened minutes later.
|
||||
|
||||
## Test layers and what each can catch
|
||||
|
||||
| Layer | What it exercises | Catches |
|
||||
|---|---|---|
|
||||
| e2e harness (`internal/test/e2e`) | Logic + renderer headless; commits injected directly (`HandleIMECommit`) | Commit mapping math, window invariants, guard/resync logic, edit/scroll/selection state. **Never** the IME contract: Gboard is not in the loop. |
|
||||
| Emulator stress loops (`/tmp/loop4.sh`, `/tmp/stress.sh`) | Real app + adb touches, real Gboard | Fling/tap/typing interplay, restart storms, caps regressions. Coarse: no boundary visibility, scenario shape matters (warm-session fling-tap-type was the original shape; it missed the cold-restore → far-scroll → tap → fast-typing shape entirely). |
|
||||
| Full-path scenario (`/tmp/s8.sh`) | Cold open with restored cursor → far scroll → tap into a known word → fast Gboard typing → backspaces; asserts tap position vs commit ranges vs **exact file diff** | The desync class: text landing anywhere but the tap. The byte→rune conversion must be computed from the file, never assumed. |
|
||||
|
||||
Rule: **a bug in the IME boundary is invisible to every layer except the
|
||||
last one.** When IME behavior is touched, run S8.
|
||||
|
||||
## Operational pitfalls hit in these sessions
|
||||
|
||||
- The launcher activity is `pad.pad/org.gioui.GioActivity`, not
|
||||
`.MainActivity`. `am start` failures are silent if stderr is redirected —
|
||||
check the output once, trust it after.
|
||||
- `adb install`/build steps fail with "no adb device" when the emulator is
|
||||
mid-reconnect; the APK may be built but not installed. Verify
|
||||
`dumpsys package … lastUpdateTime` after installs.
|
||||
- The "All files access" settings toggle can be a red herring: `appops
|
||||
set <pkg> MANAGE_EXTERNAL_STORAGE allow` is the reliable grant, and
|
||||
`appops get` output has been observed to lag; `dumpsys appops` is
|
||||
authoritative.
|
||||
- `difflib.SequenceMatcher` on ~1 MB strings is minutes slow; use a linear
|
||||
first-diff scan for test assertions on large files.
|
||||
- Float-equality re-emit is a recurring trap (LastLineY, caret px): any
|
||||
"re-emit when changed" comparison on a float sum needs an epsilon gate,
|
||||
or it spins a loop that re-pushes IME state every frame and starves real
|
||||
work.
|
||||
- When a tap's pixel→text mapping is in question, the `IME TAP` line gives
|
||||
both the local point and the resulting cursor — log both, or the mapping
|
||||
bug is undiagnosable from the outside.
|
||||
|
||||
## Diagnostics currently enabled (temporary)
|
||||
|
||||
- Pad-side, permanent: `IME TAP`, `IME COMMIT`, `IME DRIFT-SNAP`,
|
||||
`IME PUSH snippet/sel/commit-range` (bounded: only on change).
|
||||
- Fork-side, **temporary** (in `~/gioui`, gated by the `replace` in
|
||||
`go.mod`): `PADIME` boundary logs in `GioView.updateSelection`,
|
||||
`GioView.restartInput`, and `window.EditorStateChanged`.
|
||||
|
||||
### Cleanup steps (when informal on-device testing is settled)
|
||||
|
||||
The fork is stock v0.10.2 plus the `PADIME` lines and nothing else
|
||||
(verify first: `git -C ~/gioui diff --stat` must show only
|
||||
`app/GioView.java` and `app/os_android.go`); behavior is identical on
|
||||
stock gioui.
|
||||
|
||||
```sh
|
||||
# 1. Restore the fork to clean v0.10.2
|
||||
git -C ~/gioui checkout -- .
|
||||
git -C ~/gioui diff --stat # must be empty
|
||||
|
||||
# 2. Drop the replace and re-tidy pad against stock gioui
|
||||
cd ~/pad
|
||||
go mod edit -dropreplace gioui.org
|
||||
go mod tidy
|
||||
|
||||
# 3. Verify everything green on stock gioui
|
||||
go build ./...
|
||||
go test -race ./...
|
||||
./scripts/check.sh
|
||||
|
||||
# 4. Rebuild both APKs and smoke-test
|
||||
./scripts/build_emu.sh # install to emulator
|
||||
bash /tmp/s8.sh # full-path scenario must still PASS
|
||||
./scripts/build_phone.sh # install to phone, type + tap once
|
||||
|
||||
# 5. Commit and push the go.mod/go.sum change
|
||||
git add -A && git commit -m "build: drop the temporary gioui diagnostic fork" && git push origin main
|
||||
|
||||
# 6. Remove this cleanup section (and the "temporary" fork-side bullet
|
||||
# above) from ime.md; commit + push.
|
||||
```
|
||||
|
||||
If step 1 shows more than the two diagnostic files, do NOT `checkout --
|
||||
.` blindly: review the diff and keep anything that is a real (non-log)
|
||||
change, porting it upstream or documenting it in this file first.
|
||||
@@ -0,0 +1,74 @@
|
||||
# Release process
|
||||
|
||||
A release is **`./scripts/release.sh`** — that one command is the
|
||||
executable form of this document. If this document and the script ever
|
||||
disagree, fix both in the same change.
|
||||
|
||||
## What a release does
|
||||
|
||||
1. **Gate 1 — static checks**: `scripts/check.sh` (go vet + staticcheck).
|
||||
2. **Gate 2 — test suite**: `go test -count=1 ./...` — includes
|
||||
`TestNoFramesWhileIdle` (internal/test/e2e), the headless
|
||||
frame-regression guard.
|
||||
3. **Gate 3 — frame-regression profile, EMULATOR only**:
|
||||
`scripts/profile_emulator.sh` (auto-selects an emulator; it never
|
||||
auto-selects a phone). See architecture.md §11 for what it measures and
|
||||
why the budgets are shaped the way they are.
|
||||
4. **Build**: `scripts/build_phone.sh --no-install` (arm64+arm APK →
|
||||
`cmd/pad/pad-phone.apk`; static checks are skipped here, already gate 1).
|
||||
5. **Install to every connected device** — emulators **and phones**.
|
||||
|
||||
Any gate failure aborts before the install. The working tree may be dirty;
|
||||
the script warns, but the gates then certify the *current* (possibly
|
||||
uncommitted) state — commit the release work before shipping.
|
||||
|
||||
## Install policy
|
||||
|
||||
- Pushing a release build to the developer's phone is the **default and
|
||||
expected** behavior — `release.sh` installs to all `adb devices` entries,
|
||||
which is what makes the phone just work without knowing its serial
|
||||
(the wireless-debugging `IP:port` changes on every reconnect; the
|
||||
install loop sidesteps that entirely).
|
||||
- On a phone that has never had the app, grant **"All files access"**
|
||||
(MANAGE_EXTERNAL_STORAGE) in system settings after the first install.
|
||||
|
||||
## On-device profiling/testing: NOT part of a release
|
||||
|
||||
The release touches a phone **only to install the APK**. It never
|
||||
profiles, tests, force-stops, or otherwise drives a phone:
|
||||
|
||||
- On-device runs (e.g. `scripts/profile_emulator.sh -s <phone serial>`)
|
||||
are **diagnostics**: they force-stop the app, seed and remove a file in
|
||||
its storage, and read its profiler output. The developer may be using the
|
||||
phone while a release runs.
|
||||
- Therefore an on-device run requires **explicit approval in the moment**
|
||||
(ask first, name the serial), and its results are informational, never a
|
||||
gate. The frame-regression *gate* is tier 2 (headless) + tier 3
|
||||
(emulator) only.
|
||||
|
||||
## Failure handling
|
||||
|
||||
| Gate | Fails on | First things to check |
|
||||
|---|---|---|
|
||||
| 1 static | vet/staticcheck findings | The findings; don't waive without a reason in the commit message |
|
||||
| 2 tests | any test | `go test -count=1 -run <Name> ./...` to isolate; e2e tests are deterministic — a flake is a bug in the test |
|
||||
| 3 profile | a phase over its frame budget | The printed phases + `PERF`/`PERF-PRESENT` logcat lines (a spinner is usually obvious); confirm the emulator wasn't under host load (rerun once before investigating) |
|
||||
| build | gogio/apktool/sign | Toolchain notes in `scripts/build_phone.sh` header |
|
||||
|
||||
If the profile gate is in question, a diagnostic run **on the phone**
|
||||
(requires approval) is the escalation path — not a replacement for the gate.
|
||||
|
||||
## Versioning (known gap)
|
||||
|
||||
The APK is currently built with the gogio-default version
|
||||
(`1.0.0.1`); releases do **not** bump a version yet. When release
|
||||
distribution starts mattering, add a version step here (and to
|
||||
`release.sh`) — until then, "which release" is identified by the git
|
||||
commit the APK was built from.
|
||||
|
||||
## Emulator requirements
|
||||
|
||||
Gate 3 needs a running emulator (`adb devices` shows `emulator-*`).
|
||||
`scripts/emu.sh` manages the AVD (see its header); the AVD must have had
|
||||
"All files access" granted once (it has). No phone needs to be connected
|
||||
for a release to succeed — the install step simply reports no devices.
|
||||
+78
-6
@@ -60,6 +60,34 @@ elsewhere.
|
||||
hardware keyboard, arrow keys, Home/End, and Page Up/Down move the cursor
|
||||
(verified on the Android emulator; Gio's mobile focus-navigation default
|
||||
for arrow keys is overridden — see architecture.md §2.1).
|
||||
- **Soft keyboard does not shift content:** opening or closing the IME
|
||||
resizes the window (adjustResize); the editor is top-anchored, so the
|
||||
visible text stays put. Gio's window otherwise synthesizes a scroll-to-
|
||||
focus nudge on any frame the viewport shrinks (RevealFocus, aimed at the
|
||||
focused field's stale pre-resize bounds), which would shift the content
|
||||
up; the app drains that one synthetic scroll on the shrink frame
|
||||
(development_plan.md §20).
|
||||
- **Pinch to change font size:** a two-finger pinch inside the editor text
|
||||
changes the app's font size continuously — the rendered size tracks the
|
||||
inter-finger distance with no snapping to whole points (the scale is a
|
||||
float32 multiplied by the per-frame distance ratio, clamped to 0.5×–3.0×
|
||||
of the 14sp base). It is an *app-local* scale layered on top of the
|
||||
system user font setting. The CONTENT UNDER THE PINCH CENTER STAYS
|
||||
FIXED: the logic captures the **content point** under the finger
|
||||
midpoint — the glyph byte and the point's offset from that glyph's
|
||||
baseline — and re-derives the scroll offset to keep that point on the
|
||||
center. A content point, not a layout point: when the font change
|
||||
re-wraps a line, a *visual* line's text moves (fragment 2 of the new
|
||||
wrap is different text), so pinning (line, fragment) would leave a
|
||||
different character at the center. The pin therefore names the
|
||||
character itself (byte + baseline offset) and re-anchors it under every
|
||||
newly shaped layout — the re-wrap included — which is what keeps the
|
||||
character under the fingers through the rewrap. (A (line, fragment,
|
||||
sub-line) anchor stands in as fallback for points off any glyph.)
|
||||
Single-finger scroll is suppressed mid-pinch (the pinch owns the two
|
||||
fingers) and takes over the viewport on the first scroll after the
|
||||
pinch. The scale is part of the relaunch session (§2.4) and survives
|
||||
restarts.
|
||||
- **Text selection:** two input paths. **Touch** (the Android-native model):
|
||||
long-press selects the word under the finger (on a blank spot it places the
|
||||
caret and offers a paste-only menu); double-tap selects the word; the
|
||||
@@ -95,7 +123,8 @@ elsewhere.
|
||||
the newer content when it completes ("latest state wins"). Writes stage to
|
||||
a unique per-call temp file and rename into place, so a crash or a
|
||||
concurrent reader never observes a partial file. Failed writes are retried.
|
||||
This is the only persistence mechanism.
|
||||
This is the only document persistence mechanism (the app's own state is a
|
||||
separate tiny session file — §2.4).
|
||||
|
||||
### 2.3 Large files (measured)
|
||||
|
||||
@@ -110,6 +139,46 @@ elsewhere.
|
||||
in-range files); edits splice only affected chunks. Details:
|
||||
`architecture.md` §6.
|
||||
|
||||
### 2.4 State restoration on relaunch
|
||||
|
||||
- On launch, Pad lands exactly where the last session ended. If the app was
|
||||
in the editor, it re-opens the last file straight on the editor page,
|
||||
restoring the cursor, scroll position, live selection, and the find bar
|
||||
(query, open/closed, current match) — a relaunch therefore never requires
|
||||
re-browsing to the last file. If the app was on the browser page, it lands
|
||||
back in the browser at the saved directory (`InBrowser`/`BrowserPath` in
|
||||
the snapshot; a deleted directory falls back to the startup directory).
|
||||
- The scroll position is persisted as the LOGICAL line at the viewport top
|
||||
plus the sub-line remainder (the raw pixel offset is kept too, for
|
||||
pre-line-coordinate session files). The pixel offset alone is not
|
||||
restorable: it lives in visual-line space, and the wrap counts that map
|
||||
it to a line are rebuilt from estimates on relaunch — lines above the
|
||||
restored viewport are never shaped, so the offset would land a line
|
||||
deeper by every wrapped continuation above it. The inverse drift also
|
||||
happens in the first frames after relaunch: the pre-restore (top-of-file)
|
||||
window is what gets shaped first, and its real wrap counts landing below
|
||||
the restored line would drag the line-derived offset to a shallower line.
|
||||
The restore therefore pins the logical line: while the restore settles
|
||||
(until the restored window itself has shaped, a 2 s timeout, or the user
|
||||
scrolls / a search jumps), every wrap-count correction re-derives the
|
||||
offset as `V(line)·lh + sub` under the current index, so the viewport
|
||||
stays on the restored line regardless of which counts have landed.
|
||||
- The snapshot is a tiny JSON file (a few hundred bytes) written by the
|
||||
cmd layer: `$HOME/.pad/session.json` off-Android, and
|
||||
`/storage/emulated/0/Pad/session.json` on Android (the dir that already
|
||||
carries the browser's `.pad` index caches). Search results are NOT stored:
|
||||
they are regenerated by re-scanning the restored query, and the current
|
||||
match is re-selected by byte offset.
|
||||
- The snapshot is written rate-limited (250 ms, only on change) while the
|
||||
app runs; a file change or a selection appearing/vanishing saves
|
||||
immediately; on Android the activity's onStop (recents-wipe, app switch)
|
||||
flushes it one last time; and it is written unconditionally at shutdown.
|
||||
A kill shortly after a change therefore loses at most a fraction of a
|
||||
second of state.
|
||||
- If the restored file no longer exists (deleted/moved, e.g. by Syncthing),
|
||||
the app falls back to the browser page; positions that exceed a shrunk
|
||||
file are clamped to its new end.
|
||||
|
||||
## 3. Code organization (actual)
|
||||
|
||||
```
|
||||
@@ -119,7 +188,8 @@ internal/
|
||||
browser/ # BrowserState, BrowserManager, sort, search,
|
||||
# pagination, layout, handlers
|
||||
editor/ # Logic goroutine, State, ChunkedBuffer, LineIndex,
|
||||
# IME handling, autosave, Frame handoff (frame.go)
|
||||
# IME handling, autosave, relaunch session (session.go),
|
||||
# Frame handoff (frame.go)
|
||||
io/pool/ # Worker pool (priority lanes), task types,
|
||||
# real/ — real filesystem (rooted at /)
|
||||
# mock/ — in-memory FS for tests
|
||||
@@ -153,9 +223,12 @@ Full details, channel topology, and ownership rules: [`architecture.md`](./archi
|
||||
inflates memory (this caused a 1 GB leak, fixed in Phase 3).
|
||||
4. **The IME snippet is the visible window**, and rune↔byte conversion happens
|
||||
in one place (`HandleReplaceRange` / `RuneIndexToByte`).
|
||||
5. **Autosave is the only persistence.** If state persistence (last file,
|
||||
cursor, scroll) is added later, it must go through the same
|
||||
owner-dispatches-a-task pattern.
|
||||
5. **Persistence goes through the owner.** Autosave dispatches `WriteFile`
|
||||
tasks for document content; the relaunch session (last file, cursor,
|
||||
scroll, selection, find state) is marshaled by the owner and written by
|
||||
the cmd layer's saver callback (tiny JSON, synchronous, torn writes
|
||||
rejected on load). If session state ever grows beyond a few KB, move the
|
||||
write to a worker-pool task (autosave pattern).
|
||||
6. **Logic work stays < 16 ms.** Anything that can block or scan more than the
|
||||
viewport goes to the worker pool.
|
||||
7. **Scroll offset is always clamped to `[0, maxScroll]`,** where
|
||||
@@ -183,7 +256,6 @@ recorded here so future rounds don't mistake doc text for behavior:
|
||||
| Feature | Status | Notes |
|
||||
|---|---|---|
|
||||
| Undo (any) | **not implemented** | No undo stack exists; the old `SaveUndoTask` is dead code. |
|
||||
| State restoration on relaunch | **not implemented** | Last file / cursor / scroll are not persisted. |
|
||||
| External change detection | **not implemented** | No mtime compare on open/resume, no watcher, no Keep/Reload prompt. |
|
||||
| Syncthing conflict handling | **not implemented** | No `.sync-conflict-*` file detection or merging. |
|
||||
| File-system watcher | **not implemented** | Browser does not live-refresh; it re-scans on navigation. |
|
||||
|
||||
@@ -3,15 +3,17 @@ module pad
|
||||
go 1.24.2
|
||||
|
||||
require (
|
||||
gioui.org v0.10.0
|
||||
gioui.org v0.10.2
|
||||
golang.org/x/image v0.26.0
|
||||
golang.org/x/sys v0.39.0
|
||||
)
|
||||
|
||||
require (
|
||||
gioui.org/shader v1.0.8 // indirect
|
||||
gioui.org/shader v1.0.9 // indirect
|
||||
github.com/go-text/typesetting v0.3.4 // indirect
|
||||
golang.org/x/exp/shiny v0.0.0-20250408133849-7e4ce0ab07d0 // indirect
|
||||
golang.org/x/net v0.48.0 // indirect
|
||||
golang.org/x/sys v0.39.0 // indirect
|
||||
golang.org/x/text v0.32.0 // indirect
|
||||
)
|
||||
|
||||
replace gioui.org => /home/gmp/gioui
|
||||
|
||||
@@ -1,19 +1,12 @@
|
||||
eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d h1:ARo7NCVvN2NdhLlJE9xAbKweuI9L6UgfTbYb0YwPacY=
|
||||
eliasnaur.com/font v0.0.0-20230308162249-dd43949cb42d/go.mod h1:OYVuxibdk9OSLX8vAqydtRPP87PyTFcT9uH3MlEGBQA=
|
||||
gioui.org v0.9.0 h1:4u7XZwnb5kzQW91Nz/vR0wKD6LdW9CaVF96r3rfy4kc=
|
||||
gioui.org v0.9.0/go.mod h1:CjNig0wAhLt9WZxOPAusgFD8x8IRvqt26LdDBa3Jvao=
|
||||
gioui.org v0.10.0 h1:kV6NKGbEp0JSPLtgoT/xk5dMexVDzvEOErXA40gUxrU=
|
||||
gioui.org v0.10.0/go.mod h1:MZJZsdEPkTBzChdqeE8CiiQhreUQBj43qusDxQNDf7k=
|
||||
gioui.org/cpu v0.0.0-20210808092351-bfe733dd3334/go.mod h1:A8M0Cn5o+vY5LTMlnRoK3O5kG+rH0kWfJjeKd9QpBmQ=
|
||||
gioui.org/shader v1.0.8 h1:6ks0o/A+b0ne7RzEqRZK5f4Gboz2CfG+mVliciy6+qA=
|
||||
gioui.org/shader v1.0.8/go.mod h1:mWdiME581d/kV7/iEhLmUgUK5iZ09XR5XpduXzbePVM=
|
||||
github.com/go-text/typesetting v0.3.0 h1:OWCgYpp8njoxSRpwrdd1bQOxdjOXDj9Rqart9ML4iF4=
|
||||
github.com/go-text/typesetting v0.3.0/go.mod h1:qjZLkhRgOEYMhU9eHBr3AR4sfnGJvOXNLt8yRAySFuY=
|
||||
gioui.org/shader v1.0.9 h1:XxnqIfmClWpN49kizxH2W0JcCFrrEP4q3jZmNYaltbs=
|
||||
gioui.org/shader v1.0.9/go.mod h1:mWdiME581d/kV7/iEhLmUgUK5iZ09XR5XpduXzbePVM=
|
||||
github.com/go-text/typesetting v0.3.4 h1:YYurUOtEb9kGSOz4uE3k4OpBGsp1dDL8+fjCeaFamAU=
|
||||
github.com/go-text/typesetting v0.3.4/go.mod h1:4qZCQphq4KSgGTAeI0uMEkVbROgfah8BuyF5LRYr7XY=
|
||||
github.com/go-text/typesetting-utils v0.0.0-20241103174707-87a29e9e6066 h1:qCuYC+94v2xrb1PoS4NIDe7DGYtLnU2wWiQe9a1B1c0=
|
||||
github.com/go-text/typesetting-utils v0.0.0-20241103174707-87a29e9e6066/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o=
|
||||
github.com/go-text/typesetting-utils v0.0.0-20260223113751-2d88ac90dae3 h1:drBZzMgdYPbmyXqOto4YhhJGrFIQCX94FpR4MzTCsos=
|
||||
github.com/go-text/typesetting-utils v0.0.0-20260223113751-2d88ac90dae3/go.mod h1:3/62I4La/HBRX9TcTpBj4eipLiwzf+vhI+7whTc9V7o=
|
||||
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 h1:R84qjqJb5nVJMxqWYb3np9L5ZsaDtB+a39EqjV0JSUM=
|
||||
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0/go.mod h1:S9Xr4PYopiDyqSyp5NjCrhFrqg6A5zA2E/iPHPhqnS8=
|
||||
golang.org/x/exp/shiny v0.0.0-20250408133849-7e4ce0ab07d0 h1:tMSqXTK+AQdW3LpCbfatHSRPHeW6+2WuxaVQuHftn80=
|
||||
@@ -22,11 +15,7 @@ golang.org/x/image v0.26.0 h1:4XjIFEZWQmCZi6Wv8BoxsDhRU3RVnLX04dToTDAEPlY=
|
||||
golang.org/x/image v0.26.0/go.mod h1:lcxbMFAovzpnJxzXS3nyL83K27tmqtKzIJpctK8YO5c=
|
||||
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
|
||||
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
|
||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
|
||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
|
||||
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
|
||||
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
|
||||
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package browser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -25,6 +28,51 @@ func LoadInitialPages(s *BrowserState) {
|
||||
}
|
||||
}
|
||||
|
||||
// directorySignature is a cheap, order-independent fingerprint of a directory's
|
||||
// real entries (name, size, mtime, isdir), used to tell a no-op periodic
|
||||
// refresh from a real change. The synthetic ".." row is excluded: its mtime is
|
||||
// rebuilt on every pass and would otherwise make the signature differ each time.
|
||||
func directorySignature(entries []Entry) uint64 {
|
||||
names := make([]string, 0, len(entries))
|
||||
byName := make(map[string]Entry, len(entries))
|
||||
for _, e := range entries {
|
||||
if e.Name == ".." {
|
||||
continue
|
||||
}
|
||||
names = append(names, e.Name)
|
||||
byName[e.Name] = e
|
||||
}
|
||||
sort.Strings(names)
|
||||
h := fnv.New64a()
|
||||
fmt.Fprintf(h, "%d\n", len(names))
|
||||
for _, n := range names {
|
||||
e := byName[n]
|
||||
fmt.Fprintf(h, "%s|%d|%d|%v\n", n, e.Size, e.ModTime.UnixNano(), e.IsDir)
|
||||
}
|
||||
return h.Sum64()
|
||||
}
|
||||
|
||||
// replaceVisiblePagesFromIndex drops the current pages and synchronously
|
||||
// reloads the visible+prefetch pages from the (possibly new) SortIndex, so the
|
||||
// frame that carries a fresh index already carries its rows — no async gap and
|
||||
// no flicker on a periodic refresh. Distant pages are dropped and reload from
|
||||
// the fresh index on scroll. Called from the owner goroutine when a directory
|
||||
// (re)index lands (see BrowserManager.handleBuildIndexSuccess).
|
||||
func replaceVisiblePagesFromIndex(s *BrowserState) {
|
||||
if s.SortIndex == nil || s.TotalEntries == 0 {
|
||||
s.Pages = make(map[int]*Page)
|
||||
return
|
||||
}
|
||||
minPage, maxPage := computeVisiblePageRange(s)
|
||||
fresh := make(map[int]*Page, maxPage-minPage+1)
|
||||
for i := minPage; i <= maxPage; i++ {
|
||||
if page := loadPageFromIndex(s, i); page != nil {
|
||||
fresh[i] = page
|
||||
}
|
||||
}
|
||||
s.Pages = fresh
|
||||
}
|
||||
|
||||
// loadPageFromIndex creates a Page from the sorted index at the given page index.
|
||||
// Uses position maps to convert sorted indices to raw indices for entry lookup.
|
||||
// Returns nil if the page index is out of bounds.
|
||||
@@ -164,4 +212,3 @@ func filterEntriesByQuery(entries []Entry, query string) []int {
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
|
||||
@@ -224,5 +224,3 @@ func TestBuildIndex_LargeDirectory(t *testing.T) {
|
||||
t.Errorf("buildIndex took %v, expected < 5s", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -160,8 +160,6 @@ func recomputeSearchResults(s *BrowserState) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ComputeVisibleEntriesForTest is exported for testing purposes.
|
||||
// It builds the list of ui.ListItem entries that should be rendered.
|
||||
func ComputeVisibleEntriesForTest(state *BrowserState) []ui.ListItem {
|
||||
|
||||
@@ -45,20 +45,15 @@ func TestLazyLoadingLargeDirectory(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for index build result and process it
|
||||
// Wait for index build result and process it. The visible+prefetch pages
|
||||
// are populated synchronously from the fresh index inside HandleResult (no
|
||||
// separate initial LoadPages result is dispatched).
|
||||
res, ok := getResult(15 * time.Second)
|
||||
if !ok {
|
||||
t.Fatal("Timeout waiting for BuildIndex result")
|
||||
}
|
||||
bm.HandleResult(res)
|
||||
|
||||
// Wait for initial page load result and process it
|
||||
res, ok = getResult(15 * time.Second)
|
||||
if !ok {
|
||||
t.Fatal("Timeout waiting for initial LoadPages result")
|
||||
}
|
||||
bm.HandleResult(res)
|
||||
|
||||
// 2. Verify only initial pages loaded (visible + prefetch)
|
||||
if len(state.Pages) == 0 {
|
||||
t.Error("Expected pages to be loaded")
|
||||
@@ -104,8 +99,7 @@ func TestPrefetchOnScroll(t *testing.T) {
|
||||
}
|
||||
|
||||
bm.NavigateTo("/dir")
|
||||
// Process BuildIndex and initial load
|
||||
bm.HandleResult(<-wp.ResultChan())
|
||||
// Process BuildIndex (visible+prefetch pages are populated synchronously).
|
||||
bm.HandleResult(<-wp.ResultChan())
|
||||
|
||||
// Current visible ~page 0. Pages 0, 1, 2 should be loaded.
|
||||
|
||||
+134
-23
@@ -15,6 +15,20 @@ type BrowserManager struct {
|
||||
state *BrowserState
|
||||
workerPool *pool.WorkerPool
|
||||
fs pool.FileSystem
|
||||
// indexInFlightPath is the directory whose BuildIndex read is currently
|
||||
// in flight ("" if none); indexInFlightIsNav records whether that read
|
||||
// was a user navigation (recover-on-failure) or a background refresh
|
||||
// (retry-silently). Both are owner-goroutine-only. Together they keep at
|
||||
// most one read in flight per directory and let a stale, late result be
|
||||
// dropped instead of clobbering the directory the user has since moved to.
|
||||
indexInFlightPath string
|
||||
indexInFlightIsNav bool
|
||||
// lastIndexSignature/lastIndexPath describe the last directory index that
|
||||
// actually changed the view. A refresh whose entries match is a no-op (no
|
||||
// rebuild, no frame), keeping emission event-driven while the browser
|
||||
// idles. Owner-goroutine-only.
|
||||
lastIndexPath string
|
||||
lastIndexSignature uint64
|
||||
}
|
||||
|
||||
func NewBrowserManager(state *BrowserState, wp *pool.WorkerPool, fs pool.FileSystem) (*BrowserManager, error) {
|
||||
@@ -25,6 +39,15 @@ func NewBrowserManager(state *BrowserState, wp *pool.WorkerPool, fs pool.FileSys
|
||||
}, nil
|
||||
}
|
||||
|
||||
// dispatchIndex records an in-flight directory read and sends its task to the
|
||||
// worker pool. isNav marks a user navigation (recover on failure) vs a
|
||||
// background refresh (retry silently on the next tick). Owner-goroutine-only.
|
||||
func (bm *BrowserManager) dispatchIndex(dirPath string, isNav bool) {
|
||||
bm.indexInFlightPath = dirPath
|
||||
bm.indexInFlightIsNav = isNav
|
||||
bm.workerPool.Dispatch(pool.NewBuildIndexTask(dirPath, bm.fs))
|
||||
}
|
||||
|
||||
func (bm *BrowserManager) NavigateTo(dirPath string) {
|
||||
// Save the previous path so we can restore it on navigation failure.
|
||||
bm.state.History = append(bm.state.History, bm.state.CurrentPath)
|
||||
@@ -33,10 +56,38 @@ func (bm *BrowserManager) NavigateTo(dirPath string) {
|
||||
navigateToDirectory(bm.state, dirPath)
|
||||
bm.state.Loading = true
|
||||
|
||||
// Dispatch BuildIndexTask to the worker pool.
|
||||
// In a real app, this would be a task that reads and sorts the directory.
|
||||
task := pool.NewBuildIndexTask(dirPath, bm.fs)
|
||||
bm.workerPool.Dispatch(task)
|
||||
// Re-read the directory; the landing result rebuilds the sorted index and
|
||||
// position maps, so the list and its order always reflect disk.
|
||||
bm.dispatchIndex(dirPath, true)
|
||||
}
|
||||
|
||||
// Load performs the initial directory index for an already-set CurrentPath
|
||||
// (startup). Like a navigation it recovers on failure, but without touching
|
||||
// History. Owner-goroutine-only.
|
||||
func (bm *BrowserManager) Load(dirPath string) {
|
||||
bm.state.CurrentPath = dirPath
|
||||
bm.state.Loading = true
|
||||
bm.dispatchIndex(dirPath, true)
|
||||
}
|
||||
|
||||
// Refresh re-reads the current directory to pick up external changes — files
|
||||
// added, removed, renamed, or mtime-changed (including inbound syncs) — so the
|
||||
// list and its sort order stay live while the browser is on screen. Unlike
|
||||
// NavigateTo it does not touch History or reset the scroll offset, so the
|
||||
// user's position is preserved; the landing result (handleBuildIndexSuccess)
|
||||
// rebuilds the index in place and re-sorts. Bounded to one read per directory:
|
||||
// a refresh while a read of the same directory is in flight is skipped (its
|
||||
// result will refresh the view). Owner-goroutine-only.
|
||||
func (bm *BrowserManager) Refresh(dirPath string) {
|
||||
if dirPath == "" {
|
||||
return
|
||||
}
|
||||
if bm.indexInFlightPath == dirPath {
|
||||
return
|
||||
}
|
||||
bm.state.CurrentPath = dirPath
|
||||
bm.state.Loading = true
|
||||
bm.dispatchIndex(dirPath, false)
|
||||
}
|
||||
|
||||
func (bm *BrowserManager) OnScroll() {
|
||||
@@ -63,28 +114,45 @@ func (bm *BrowserManager) OnScroll() {
|
||||
bm.state.EvictPages()
|
||||
}
|
||||
|
||||
func (bm *BrowserManager) HandleResult(result pool.Result) {
|
||||
// HandleResult applies a completed browser task result to the state. It returns
|
||||
// whether the browser view changed and so a frame should be emitted. A periodic
|
||||
// refresh that finds the directory unchanged returns false, keeping emission
|
||||
// event-driven even while the browser idles.
|
||||
func (bm *BrowserManager) HandleResult(result pool.Result) bool {
|
||||
if !result.IsBrowserResult() {
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
switch result.TaskType {
|
||||
case pool.TypeBuildIndex:
|
||||
if result.Success {
|
||||
bm.handleBuildIndexSuccess(result)
|
||||
} else {
|
||||
bm.handleError(result)
|
||||
return bm.handleBuildIndexSuccess(result)
|
||||
}
|
||||
return bm.handleError(result)
|
||||
case pool.TypeLoadPages:
|
||||
if result.Success {
|
||||
bm.handleLoadPagesSuccess(result)
|
||||
} else {
|
||||
bm.handleError(result)
|
||||
return true
|
||||
}
|
||||
return bm.handleError(result)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (bm *BrowserManager) handleBuildIndexSuccess(result pool.Result) bool {
|
||||
// A read of this directory has landed; clear the in-flight marker.
|
||||
if bm.indexInFlightPath == result.DirPath {
|
||||
bm.indexInFlightPath = ""
|
||||
bm.indexInFlightIsNav = false
|
||||
}
|
||||
// A directory we have since left produced a late result (a navigation
|
||||
// overtook it). Drop it so it cannot clobber the current directory's index
|
||||
// and pages. (Production results always carry DirPath; the empty check
|
||||
// keeps hand-built test results without one applying as before.)
|
||||
if result.DirPath != "" && result.DirPath != bm.state.CurrentPath {
|
||||
return false
|
||||
}
|
||||
|
||||
func (bm *BrowserManager) handleBuildIndexSuccess(result pool.Result) {
|
||||
bm.state.Loading = false
|
||||
|
||||
// In the mock implementation, BuildIndexTask returns []types.DirEntry.
|
||||
@@ -116,6 +184,25 @@ func (bm *BrowserManager) handleBuildIndexSuccess(result pool.Result) {
|
||||
})
|
||||
}
|
||||
|
||||
// A refresh of an unchanged directory is a no-op: skip the rebuild and
|
||||
// report "no change" so no frame is emitted (keeps emission event-driven
|
||||
// while the browser idles). The signature covers the real entries only —
|
||||
// name, size, mtime, isdir — and excludes the synthetic ".." row, whose
|
||||
// mtime is rebuilt on every pass and would otherwise always differ.
|
||||
sig := directorySignature(browserEntries)
|
||||
// The no-change fast path assumes the view already shows this content.
|
||||
// A failed navigation's recovery (handleError) breaks that: it resets the
|
||||
// rows (navigateToDirectory zeros TotalEntries/Pages) and re-reads the
|
||||
// previous directory, whose index is unchanged — without this guard the
|
||||
// re-read would take the fast path and the browser would sit on an empty
|
||||
// list forever. A torn-down view always takes the full rebuild below.
|
||||
if bm.state.TotalEntries != 0 &&
|
||||
bm.lastIndexPath == bm.state.CurrentPath && bm.lastIndexSignature == sig {
|
||||
return false
|
||||
}
|
||||
bm.lastIndexPath = bm.state.CurrentPath
|
||||
bm.lastIndexSignature = sig
|
||||
|
||||
// Build the sorted index with position maps
|
||||
bm.state.SortIndex = &DirectoryIndex{
|
||||
Path: bm.state.CurrentPath,
|
||||
@@ -134,8 +221,21 @@ func (bm *BrowserManager) handleBuildIndexSuccess(result pool.Result) {
|
||||
|
||||
bm.state.TotalEntries = len(browserEntries)
|
||||
|
||||
// Trigger initial page load
|
||||
// Refresh the visible rows synchronously from the fresh index so the frame
|
||||
// that carries the new index already carries its rows — no async gap and no
|
||||
// flicker on a periodic refresh. The scroll offset is preserved (a refresh,
|
||||
// not a jump to top) then clamped in case the list shrank; an active search
|
||||
// is re-filtered against the new order without yanking the scroll. Distant
|
||||
// pages are dropped and reload on scroll from the fresh index.
|
||||
replaceVisiblePagesFromIndex(bm.state)
|
||||
if bm.state.Query != "" {
|
||||
prev := bm.state.ScrollOffset
|
||||
recomputeSearchResults(bm.state)
|
||||
bm.state.ScrollOffset = prev
|
||||
}
|
||||
clampScrollOffset(bm.state)
|
||||
bm.OnScroll()
|
||||
return true
|
||||
}
|
||||
|
||||
func (bm *BrowserManager) handleLoadPagesSuccess(result pool.Result) {
|
||||
@@ -152,25 +252,36 @@ func (bm *BrowserManager) handleLoadPagesSuccess(result pool.Result) {
|
||||
}
|
||||
}
|
||||
|
||||
func (bm *BrowserManager) handleError(result pool.Result) {
|
||||
func (bm *BrowserManager) handleError(result pool.Result) bool {
|
||||
fmt.Printf("handleError: TaskType=%d, Error=%v\n", result.TaskType, result.Error)
|
||||
|
||||
// A read of this directory has (failed) landed; clear the in-flight marker.
|
||||
wasNav := bm.indexInFlightIsNav
|
||||
if bm.indexInFlightPath == result.DirPath {
|
||||
bm.indexInFlightPath = ""
|
||||
bm.indexInFlightIsNav = false
|
||||
}
|
||||
bm.state.Loading = false
|
||||
|
||||
// If the failed task was a BuildIndex (directory navigation), restore
|
||||
// the previous path so the user is sent back to where they were.
|
||||
// A failed BACKGROUND REFRESH: keep the current (last good) view and let
|
||||
// the refresh loop retry on its next tick. Do NOT teleport the user or
|
||||
// reset the list over a transient read error. No visible change, no frame.
|
||||
if result.TaskType == pool.TypeBuildIndex && !wasNav {
|
||||
return false
|
||||
}
|
||||
|
||||
// A failed NAVIGATION: restore the previous path so the user is sent back
|
||||
// to where they were.
|
||||
if result.TaskType == pool.TypeBuildIndex && len(bm.state.History) > 0 {
|
||||
prevPath := bm.state.History[len(bm.state.History)-1]
|
||||
bm.state.History = bm.state.History[:len(bm.state.History)-1]
|
||||
bm.state.CurrentPath = prevPath
|
||||
navigateToDirectory(bm.state, prevPath)
|
||||
// Re-dispatch the build index for the previous directory so the
|
||||
// browser renders the old contents again.
|
||||
task := pool.NewBuildIndexTask(prevPath, bm.fs)
|
||||
bm.workerPool.Dispatch(task)
|
||||
return
|
||||
bm.dispatchIndex(prevPath, true)
|
||||
return true
|
||||
}
|
||||
|
||||
// For other errors, just reset the state or log the error.
|
||||
// In production, we'd show a toast or error message.
|
||||
// Anything else: reset the state (or, in production, show a toast).
|
||||
bm.state.Reset()
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package browser
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -9,6 +10,30 @@ import (
|
||||
"pad/internal/io/pool/types"
|
||||
)
|
||||
|
||||
// testDirEntry / testFileInfo are minimal types.DirEntry implementations so a
|
||||
// browser BuildIndex result can be constructed directly (no worker-pool round
|
||||
// trip, which avoids the follow-up LoadPages tasks that a real landing result
|
||||
// triggers via OnScroll).
|
||||
type testDirEntry struct {
|
||||
name string
|
||||
isDir bool
|
||||
size int64
|
||||
mod time.Time
|
||||
}
|
||||
|
||||
func (e testDirEntry) Name() string { return e.name }
|
||||
func (e testDirEntry) IsDir() bool { return e.isDir }
|
||||
func (e testDirEntry) Info() (os.FileInfo, error) { return testFileInfo{e}, nil }
|
||||
|
||||
type testFileInfo struct{ e testDirEntry }
|
||||
|
||||
func (f testFileInfo) Name() string { return f.e.name }
|
||||
func (f testFileInfo) Size() int64 { return f.e.size }
|
||||
func (f testFileInfo) Mode() os.FileMode { return 0o644 }
|
||||
func (f testFileInfo) ModTime() time.Time { return f.e.mod }
|
||||
func (f testFileInfo) IsDir() bool { return f.e.isDir }
|
||||
func (f testFileInfo) Sys() any { return nil }
|
||||
|
||||
// TestNewBrowserManager verifies that a BrowserManager can be constructed
|
||||
// with valid parameters and has correct initial state.
|
||||
func TestNewBrowserManager(t *testing.T) {
|
||||
@@ -121,9 +146,153 @@ func TestHandleResult_BuildIndexSuccess(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleResult_BuildIndexFailure verifies that a failed BuildIndex
|
||||
// result is handled gracefully.
|
||||
func TestHandleResult_BuildIndexFailure(t *testing.T) {
|
||||
// TestHandleResult_BuildIndexRefreshFailure verifies that a failed BACKGROUND
|
||||
// REFRESH keeps the current (last good) view rather than teleporting the user
|
||||
// or resetting the list over a transient read error.
|
||||
func TestHandleResult_BuildIndexRefreshFailure(t *testing.T) {
|
||||
state := NewBrowserState()
|
||||
state.CurrentPath = "/dir"
|
||||
state.TotalEntries = 5
|
||||
state.Pages[0] = NewPage(0, []Entry{{Name: "a.txt"}})
|
||||
fs := mock.NewFileSystem()
|
||||
wp := pool.NewWorkerPool(1)
|
||||
|
||||
bm, err := NewBrowserManager(state, wp, fs)
|
||||
if err != nil {
|
||||
t.Fatalf("NewBrowserManager failed: %v", err)
|
||||
}
|
||||
|
||||
// Model an in-flight background refresh of the current directory (as
|
||||
// BrowserManager.Refresh would), then feed it a failed result.
|
||||
bm.indexInFlightPath = "/dir"
|
||||
bm.indexInFlightIsNav = false
|
||||
result := pool.Result{
|
||||
TaskType: pool.TypeBuildIndex,
|
||||
Success: false,
|
||||
Error: ErrDirNotFound,
|
||||
DirPath: "/dir",
|
||||
}
|
||||
|
||||
bm.HandleResult(result)
|
||||
|
||||
// The view must be preserved: same directory, same pages, no reset.
|
||||
if state.CurrentPath != "/dir" {
|
||||
t.Errorf("CurrentPath = %q after refresh failure, want /dir (view must be kept)", state.CurrentPath)
|
||||
}
|
||||
if len(state.Pages) != 1 {
|
||||
t.Errorf("Pages = %d after refresh failure, want 1 (view must be kept)", len(state.Pages))
|
||||
}
|
||||
if bm.indexInFlightPath != "" {
|
||||
t.Errorf("indexInFlightPath = %q after refresh failure, want cleared", bm.indexInFlightPath)
|
||||
}
|
||||
}
|
||||
|
||||
// waitBrowserResult is a test helper: read one result from the pool's channel
|
||||
// with a deadline, fatal on timeout.
|
||||
func waitBrowserResult(t *testing.T, wp *pool.WorkerPool) pool.Result {
|
||||
t.Helper()
|
||||
select {
|
||||
case res := <-wp.ResultChan():
|
||||
return res
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("timeout waiting for browser result")
|
||||
return pool.Result{}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRefresh_PicksUpExternalChanges verifies the periodic-refresh contract:
|
||||
// the initial load reports a change; an unchanged refresh reports NO change
|
||||
// (so no frame is emitted while idle) and preserves the scroll offset; and a
|
||||
// refresh after an external add picks up the new file and re-sorts.
|
||||
func TestRefresh_PicksUpExternalChanges(t *testing.T) {
|
||||
t.Parallel()
|
||||
fs := mock.NewFileSystem()
|
||||
fs.AddDir("/Notes", time.Now()) // only used by the OnScroll LoadPages, harmless
|
||||
wp := pool.NewWorkerPool(2)
|
||||
wp.Start()
|
||||
defer wp.Stop()
|
||||
|
||||
state := NewBrowserState()
|
||||
state.CurrentPath = "/Notes"
|
||||
bm, err := NewBrowserManager(state, wp, fs)
|
||||
if err != nil {
|
||||
t.Fatalf("NewBrowserManager failed: %v", err)
|
||||
}
|
||||
|
||||
mod := time.Date(2026, 5, 1, 12, 0, 0, 0, time.UTC)
|
||||
buildIndex := func(dirPath string, files ...string) pool.Result {
|
||||
entries := make([]types.DirEntry, 0, len(files))
|
||||
for _, f := range files {
|
||||
entries = append(entries, testDirEntry{name: f, size: 1, mod: mod})
|
||||
}
|
||||
return pool.Result{TaskType: pool.TypeBuildIndex, Success: true, Data: entries, DirPath: dirPath}
|
||||
}
|
||||
|
||||
// Initial load: reports a change.
|
||||
if !bm.HandleResult(buildIndex("/Notes", "a.txt")) {
|
||||
t.Fatal("initial load should report a change")
|
||||
}
|
||||
if state.TotalEntries != 2 { // ".." + a.txt
|
||||
t.Fatalf("TotalEntries = %d after load, want 2", state.TotalEntries)
|
||||
}
|
||||
|
||||
// Unchanged refresh: reports NO change and preserves the scroll offset.
|
||||
state.ScrollOffset = 123
|
||||
if bm.HandleResult(buildIndex("/Notes", "a.txt")) {
|
||||
t.Fatal("unchanged refresh should report no change")
|
||||
}
|
||||
if state.ScrollOffset != 123 {
|
||||
t.Errorf("ScrollOffset = %v after no-op refresh, want 123 (preserved)", state.ScrollOffset)
|
||||
}
|
||||
|
||||
// An external add: the next refresh picks it up and re-sorts.
|
||||
if !bm.HandleResult(buildIndex("/Notes", "a.txt", "b.txt")) {
|
||||
t.Fatal("refresh after external add should report a change")
|
||||
}
|
||||
if state.TotalEntries != 3 { // ".." + a.txt + b.txt
|
||||
t.Fatalf("TotalEntries = %d after add, want 3", state.TotalEntries)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRefresh_CoalescesInFlight verifies that a refresh requested while a read
|
||||
// of the same directory is in flight does not dispatch a second read.
|
||||
func TestRefresh_CoalescesInFlight(t *testing.T) {
|
||||
t.Parallel()
|
||||
fs := mock.NewFileSystem()
|
||||
fs.AddDir("/Notes", time.Now())
|
||||
fs.SetDelay(300 * time.Millisecond) // slow the read so we can observe it
|
||||
wp := pool.NewWorkerPool(2)
|
||||
wp.Start()
|
||||
defer wp.Stop()
|
||||
|
||||
state := NewBrowserState()
|
||||
state.CurrentPath = "/Notes"
|
||||
bm, err := NewBrowserManager(state, wp, fs)
|
||||
if err != nil {
|
||||
t.Fatalf("NewBrowserManager failed: %v", err)
|
||||
}
|
||||
|
||||
bm.Load("/Notes") // dispatches one read; it is in flight for ~300ms
|
||||
if bm.indexInFlightPath != "/Notes" {
|
||||
t.Fatalf("expected an in-flight read of /Notes, got %q", bm.indexInFlightPath)
|
||||
}
|
||||
// A refresh while that read is in flight must coalesce (no second read).
|
||||
bm.Refresh("/Notes")
|
||||
if bm.indexInFlightPath != "/Notes" {
|
||||
t.Fatalf("refresh should not replace the in-flight path, got %q", bm.indexInFlightPath)
|
||||
}
|
||||
// Only ONE result should arrive for the coalesced reads.
|
||||
res := waitBrowserResult(t, wp)
|
||||
bm.HandleResult(res)
|
||||
if bm.indexInFlightPath != "" {
|
||||
t.Fatalf("in-flight marker should be cleared after the result, got %q", bm.indexInFlightPath)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleResult_BuildIndexNavFailureRestoresPrevious verifies that a failed
|
||||
// NAVIGATION restores the previous directory (the existing recovery behavior)
|
||||
// instead of leaving the user on a directory that could not be read.
|
||||
func TestHandleResult_BuildIndexNavFailureRestoresPrevious(t *testing.T) {
|
||||
state := NewBrowserState()
|
||||
fs := mock.NewFileSystem()
|
||||
wp := pool.NewWorkerPool(1)
|
||||
@@ -133,18 +302,23 @@ func TestHandleResult_BuildIndexFailure(t *testing.T) {
|
||||
t.Fatalf("NewBrowserManager failed: %v", err)
|
||||
}
|
||||
|
||||
// Simulate a failed BuildIndex result
|
||||
// Model a navigation from "/" to a directory that then fails to read.
|
||||
state.History = append(state.History, "/")
|
||||
state.CurrentPath = "/missing"
|
||||
bm.indexInFlightPath = "/missing"
|
||||
bm.indexInFlightIsNav = true
|
||||
|
||||
result := pool.Result{
|
||||
TaskType: pool.TypeBuildIndex,
|
||||
Success: false,
|
||||
Error: ErrDirNotFound,
|
||||
DirPath: "/missing",
|
||||
}
|
||||
|
||||
bm.HandleResult(result)
|
||||
|
||||
// Should not panic, state should be recoverable
|
||||
if state.CurrentPath != "" {
|
||||
t.Errorf("Expected empty CurrentPath on error, got %q", state.CurrentPath)
|
||||
// The user is sent back to the previous directory.
|
||||
if state.CurrentPath != "/" {
|
||||
t.Errorf("CurrentPath = %q after nav failure, want / (previous dir restored)", state.CurrentPath)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -300,3 +474,80 @@ func TestHandleResult_UnknownType(t *testing.T) {
|
||||
// Should not panic
|
||||
bm.HandleResult(result)
|
||||
}
|
||||
|
||||
// TestHandleResult_NavigationFailureRecoveryReloads reproduces the empty-list
|
||||
// bug: navigating into an unreadable directory fails, the manager reverts to
|
||||
// the previous path and re-reads it; that re-read finds the directory
|
||||
// UNCHANGED, and the no-change fast path must not skip restoring the rows the
|
||||
// failed navigation's reset wiped — or the browser sits on an empty list
|
||||
// forever (header correct, list dead).
|
||||
func TestHandleResult_NavigationFailureRecoveryReloads(t *testing.T) {
|
||||
state := NewBrowserState()
|
||||
fs := mock.NewFileSystem()
|
||||
wp := pool.NewWorkerPool(1)
|
||||
// Started (unlike the direct-result tests above): the recovery path
|
||||
// dispatches REAL follow-up tasks (BuildIndex, LoadPages), and three of
|
||||
// them would overflow the unstarted pool's high-priority channel.
|
||||
wp.Start()
|
||||
defer wp.Stop()
|
||||
|
||||
bm, err := NewBrowserManager(state, wp, fs)
|
||||
if err != nil {
|
||||
t.Fatalf("NewBrowserManager failed: %v", err)
|
||||
}
|
||||
|
||||
// A FIXED mtime: the two index results must carry byte-identical content
|
||||
// (same directorySignature) so the recovery re-read genuinely hits the
|
||||
// no-change fast path that the bug lived in.
|
||||
fixed := time.Date(2026, 5, 15, 10, 0, 0, 0, time.UTC)
|
||||
entries := func() []types.DirEntry {
|
||||
return []types.DirEntry{
|
||||
testDirEntry{name: "file1.txt", size: 8, mod: fixed},
|
||||
testDirEntry{name: "subdir", isDir: true, mod: fixed},
|
||||
}
|
||||
}
|
||||
indexResult := func(dirPath string) pool.Result {
|
||||
return pool.Result{TaskType: pool.TypeBuildIndex, Success: true, DirPath: dirPath, Data: entries()}
|
||||
}
|
||||
|
||||
// Land in /test/docs with a populated view.
|
||||
state.CurrentPath = "/test/docs"
|
||||
bm.indexInFlightPath = "/test/docs"
|
||||
bm.indexInFlightIsNav = true
|
||||
if !bm.HandleResult(indexResult("/test/docs")) {
|
||||
t.Fatal("initial index did not change the view")
|
||||
}
|
||||
if state.TotalEntries != 3 { // 2 entries + ".."
|
||||
t.Fatalf("TotalEntries = %d, want 3", state.TotalEntries)
|
||||
}
|
||||
|
||||
// The user taps into an unreadable directory (as NavigateTo would set up).
|
||||
state.History = append(state.History, state.CurrentPath)
|
||||
navigateToDirectory(state, "/missing")
|
||||
state.Loading = true
|
||||
bm.indexInFlightPath = "/missing"
|
||||
bm.indexInFlightIsNav = true
|
||||
|
||||
// That read fails: the manager must revert to /test/docs.
|
||||
failed := pool.Result{TaskType: pool.TypeBuildIndex, Success: false, Error: ErrDirNotFound, DirPath: "/missing"}
|
||||
bm.HandleResult(failed)
|
||||
if state.CurrentPath != "/test/docs" {
|
||||
t.Fatalf("CurrentPath = %q, want reverted /test/docs", state.CurrentPath)
|
||||
}
|
||||
if state.TotalEntries != 0 {
|
||||
t.Fatalf("TotalEntries = %d after failed navigation, want 0 (rows reset)", state.TotalEntries)
|
||||
}
|
||||
|
||||
// The recovery re-read of /test/docs lands with the directory unchanged.
|
||||
// This is where the bug lived: the no-change fast path returned early and
|
||||
// the view stayed empty.
|
||||
if !bm.HandleResult(indexResult("/test/docs")) {
|
||||
t.Fatal("recovery re-read reported no view change; the list would stay empty")
|
||||
}
|
||||
if state.TotalEntries != 3 {
|
||||
t.Fatalf("TotalEntries = %d after recovery re-read, want 3", state.TotalEntries)
|
||||
}
|
||||
if page, ok := state.Pages[0]; !ok || page == nil || !page.Loaded {
|
||||
t.Fatal("page 0 not restored after recovery re-read")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,4 +69,3 @@ func comparator(mode SortMode) func(a, b Entry) int {
|
||||
return func(a, b Entry) int { return 0 }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -452,10 +452,12 @@ func (cb *ChunkedBuffer) VisibleByteRange(scrollOffset ui.Dp, byteOffset int, vi
|
||||
// released). That is the root cause of the ~1GB "Unknown" memory on large
|
||||
// files.
|
||||
//
|
||||
// Word wrap does not require a separate path: each real line produces at
|
||||
// least one visual line, so shaping viewportHeight/lineHeight real lines
|
||||
// always yields at least as many visual lines as fit in the viewport. The
|
||||
// extra wrapped lines are simply clipped by the renderer.
|
||||
// Word wrap does not require a separate path: the viewport top and bottom
|
||||
// are each expressed in VISUAL-line space and mapped to the logical lines
|
||||
// that contain them (WrapIndex.LineForVisual). Because each logical line
|
||||
// produces at least one visual line, that logical range always covers the
|
||||
// viewport; the fetch is viewport-bounded no matter how many wraps lie
|
||||
// above it, and any spill past the bottom edge is clipped by the renderer.
|
||||
lineH := lineHeight
|
||||
if lineH <= 0 {
|
||||
lineH = EffectiveLineHeight()
|
||||
@@ -564,10 +566,31 @@ func (cb *ChunkedBuffer) visibleByteRangePrecise(scrollOffset ui.Dp, viewportHei
|
||||
// (the legacy no-wrap behavior).
|
||||
v0, _ := scrollDecompose(scrollOffset, lineHeight)
|
||||
startLine = int(v0)
|
||||
if w := cb.WrapIndex; w != nil {
|
||||
startLine = w.LineForVisual(int32(v0))
|
||||
}
|
||||
endLine := int(math.Ceil(float64(scrollOffset+viewportHeight) / float64(lineHeight)))
|
||||
if w := cb.WrapIndex; w != nil {
|
||||
// The window TOP is a visual line (the viewport top): map it to the
|
||||
// logical line that contains it.
|
||||
//
|
||||
// The window BOTTOM must NOT be mapped through the index the same
|
||||
// way: the wrap counts for lines INSIDE the viewport land precisely
|
||||
// while this window is being shaped (their measured layouts feed the
|
||||
// index), so the mapped bottom would oscillate frame to frame as
|
||||
// measurements arrive, resizing the window every frame. Each
|
||||
// resize changes the pushed IME snippet, and gioui turns a snippet
|
||||
// change into a restartInput — a restart on every frame reset the
|
||||
// IME's per-word input session, the source of random mid-word
|
||||
// capitalization.
|
||||
//
|
||||
// Instead use a fixed span of logical lines from the stable top:
|
||||
// each logical line produces at least one visual line, so the
|
||||
// viewport's bottom visual line lies at or above
|
||||
// startLine + (bottomVisual - topVisual); the window always covers
|
||||
// the viewport, over-fetching only by the wrapped lines inside it
|
||||
// (bounded by the viewport height; the renderer clips).
|
||||
bottomVisual := endLine
|
||||
startLine = w.LineForVisual(int32(v0))
|
||||
endLine = startLine + (bottomVisual - int(v0))
|
||||
}
|
||||
|
||||
if startLine < 0 {
|
||||
startLine = 0
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package editor
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// browserWatchMask covers the changes the browser list cares about: a file or
|
||||
// subdirectory appearing (CREATE/MOVED_TO), disappearing (DELETE/MOVED_FROM),
|
||||
// being rewritten (MODIFY), or having its mtime/attributes change (ATTRIB —
|
||||
// this is the signal for an inbound sync that updates a file in place). Any
|
||||
// event in this set is enough to justify a full re-read of the directory.
|
||||
const browserWatchMask = unix.IN_CREATE | unix.IN_DELETE |
|
||||
unix.IN_MOVED_TO | unix.IN_MOVED_FROM |
|
||||
unix.IN_MODIFY | unix.IN_ATTRIB
|
||||
|
||||
// dirWatcher watches a single directory for filesystem changes via inotify and
|
||||
// reports them as coarse "something changed" tokens on its event channel. It
|
||||
// backs the browser's live list: while the browser page is on screen the
|
||||
// current directory is watched, and any event there triggers a re-read (see
|
||||
// Logic.refreshBrowserDir).
|
||||
//
|
||||
// Ownership: only the owner goroutine calls SetDir and Close. The run goroutine
|
||||
// only reads the inotify fd and sends on event. The inotify fd is the single
|
||||
// synchronisation point between them: Close() closes the fd, which unblocks
|
||||
// run()'s blocking Read so the goroutine exits promptly. curDir/curWd are
|
||||
// therefore owner-only (no locking needed).
|
||||
type dirWatcher struct {
|
||||
event chan struct{}
|
||||
fd int
|
||||
curDir string // directory currently watched (owner-only)
|
||||
curWd uint32 // its inotify watch descriptor, for removal on re-point
|
||||
closeOnce sync.Once
|
||||
}
|
||||
|
||||
// newDirWatcher initialises an inotify instance and starts the run goroutine
|
||||
// (no directory is watched yet; call SetDir to begin). It returns nil if
|
||||
// inotify is unavailable, in which case the caller's slow backstop rescan is
|
||||
// the only refresh path. The returned watcher must be Closed on shutdown.
|
||||
func newDirWatcher(event chan struct{}) *dirWatcher {
|
||||
fd, err := unix.InotifyInit1(0)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
w := &dirWatcher{event: event, fd: fd}
|
||||
go w.run()
|
||||
return w
|
||||
}
|
||||
|
||||
// SetDir (re)points the watch at dir, removing the previous watch. It is
|
||||
// best-effort: a failure leaves the prior watch in place and is covered by the
|
||||
// slow backstop rescan. Owner-goroutine-only.
|
||||
func (w *dirWatcher) SetDir(dir string) {
|
||||
if w == nil || dir == "" || dir == w.curDir {
|
||||
return
|
||||
}
|
||||
if w.curWd > 0 {
|
||||
unix.InotifyRmWatch(w.fd, w.curWd) // best-effort
|
||||
w.curWd = 0
|
||||
}
|
||||
wd, err := unix.InotifyAddWatch(w.fd, dir, browserWatchMask)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
w.curDir, w.curWd = dir, uint32(wd)
|
||||
}
|
||||
|
||||
// Close stops the watcher: closing the inotify fd unblocks run()'s Read (EBADF)
|
||||
// so the goroutine exits. Safe to call multiple times. Owner-goroutine-only.
|
||||
func (w *dirWatcher) Close() {
|
||||
if w == nil {
|
||||
return
|
||||
}
|
||||
w.closeOnce.Do(func() { unix.Close(w.fd) })
|
||||
}
|
||||
|
||||
// run blocks reading inotify events for the life of the fd. It does not parse
|
||||
// events: any non-empty read means "the watched directory changed", which is all
|
||||
// the browser needs (it re-reads the whole directory and re-sorts). The send is
|
||||
// non-blocking into a buffer-1 channel, so a burst of events coalesces to at
|
||||
// most one pending token — the manager's in-flight marker then bounds the
|
||||
// actual re-reads.
|
||||
func (w *dirWatcher) run() {
|
||||
buf := make([]byte, 16*1024)
|
||||
for {
|
||||
n, err := unix.Read(w.fd, buf)
|
||||
if n > 0 {
|
||||
select {
|
||||
case w.event <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return // fd closed by Close() (EBADF) or an inotify error
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package editor
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"pad/internal/ui"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEditorLayout_Filename(t *testing.T) {
|
||||
|
||||
@@ -26,6 +26,10 @@ type Frame struct {
|
||||
Elems []ui.Element
|
||||
Scale float32
|
||||
FontScale float32 // user font-size setting the logic bookkeeping used
|
||||
// AppFontScale is the app-local pinch font scale (1.0 = default, 0 =
|
||||
// not set yet). The renderer multiplies the editor font size by it;
|
||||
// FontScale is already folded into gtx.Metric (PxPerSp).
|
||||
AppFontScale float32
|
||||
FocusedElementID string
|
||||
Query string
|
||||
// FindQuery: the in-file search query the logic goroutine has processed
|
||||
@@ -36,6 +40,10 @@ type Frame struct {
|
||||
// FindClearSeq mirrors EditorState.Find.ClearSeq: main wipes the widget
|
||||
// input once per NEW value (the X button cleared the logic-side query).
|
||||
FindClearSeq int
|
||||
// ScrollOffset is the editor scroll offset this frame's elements laid
|
||||
// out at, shipped with the shaped glyph layout (LayoutFeedback) so the
|
||||
// logic can express layout positions in content coordinates.
|
||||
ScrollOffset ui.Dp
|
||||
// WindowStartByte / WindowStartLine / EditSeq: the editor window this
|
||||
// frame's elements describe. The main goroutine forwards them with the
|
||||
// shaped glyph layout (LayoutFeedback) so the logic goroutine can apply
|
||||
@@ -45,6 +53,14 @@ type Frame struct {
|
||||
WindowStartLine int // -1 when the frame has no editor window
|
||||
WindowText string // the editor window this frame's text element holds
|
||||
EditSeq uint64
|
||||
// ViewportDegenerate is set when this frame was built before the
|
||||
// window's pixel size was known (0x0). Its editor window, if shaped at
|
||||
// all, was shaped at zero width: the shaper wraps every line into many
|
||||
// visual lines, and feeding those counts back (LayoutFeedback) would
|
||||
// poison the WrapIndex for the window's lines. The main goroutine
|
||||
// drops the feedback for such frames.
|
||||
ViewportDegenerate bool
|
||||
|
||||
}
|
||||
|
||||
// frameOf wraps a computed element tree with the current view-state
|
||||
@@ -54,14 +70,17 @@ func (l *Logic) frameOf(elems []ui.Element) Frame {
|
||||
Elems: elems,
|
||||
Scale: l.state.scale,
|
||||
FontScale: l.state.fontScale,
|
||||
AppFontScale: l.state.appFontScale,
|
||||
FocusedElementID: l.state.FocusedElementID,
|
||||
Query: l.state.Browser.Query,
|
||||
FindQuery: l.state.Editor.Find.Query,
|
||||
FindClearSeq: l.state.Editor.Find.ClearSeq,
|
||||
ScrollOffset: l.state.ScrollOffset,
|
||||
WindowStartByte: l.state.Editor.IMEWindowStartByte,
|
||||
WindowStartLine: l.state.WindowStartLine,
|
||||
WindowText: l.state.Editor.IMEWindowText,
|
||||
EditSeq: l.state.Editor.EditSeq,
|
||||
ViewportDegenerate: l.state.PixelWidth <= 0 || l.state.PixelHeight <= 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -209,17 +209,21 @@ func TestHandleReplaceRange_SwappedBounds(t *testing.T) {
|
||||
}
|
||||
|
||||
// TestHandleReplaceRange_WindowOffset_Chunked verifies the scrolled-viewport
|
||||
// path: the IME snippet is a window that does NOT start at buffer byte 0, so
|
||||
// the EditEvent.Range is window-relative and must be offset by the window
|
||||
// start to address the buffer.
|
||||
// path: the IME snippet starts mid-file, so the EditEvent.Range (absolute
|
||||
// file runes, addressed from IMEOffsetRune) must be mapped through the
|
||||
// leading context and the window to address the buffer.
|
||||
func TestHandleReplaceRange_WindowOffset_Chunked(t *testing.T) {
|
||||
newChunkedState(t, "Hello World, this is a test.", 8)
|
||||
// Simulate a scrolled viewport: layout would have set the visible window
|
||||
// to start at byte 6 ("World, this is a test.").
|
||||
// to start at byte 6 ("World, this is a test."), with no leading context
|
||||
// (all ASCII, so rune offset == byte offset).
|
||||
TheState.Editor.IMEWindowStartByte = 6
|
||||
TheState.Editor.IMEWindowText = "World, this is a test."
|
||||
// Replace runes [0,5) of the window ("World") with "There".
|
||||
HandleReplaceRange(0, 5, "There")
|
||||
TheState.Editor.IMEContext = ""
|
||||
TheState.Editor.IMEContextStartByte = 6
|
||||
TheState.Editor.IMEOffsetRune = 6
|
||||
// The IME reports absolute file runes: "World" is runes [6,11).
|
||||
HandleReplaceRange(6, 11, "There")
|
||||
cb := TheState.Editor.ChunkedBuffer
|
||||
got, err := cb.FullContent()
|
||||
if err != nil {
|
||||
@@ -234,17 +238,110 @@ func TestHandleReplaceRange_WindowOffset_Chunked(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleReplaceRange_WindowOffset_String verifies the same window-relative
|
||||
// logic on the small-file (string) buffer.
|
||||
// TestHandleReplaceRange_WindowOffset_String verifies the same absolute-rune
|
||||
// mapping on the small-file (string) buffer.
|
||||
func TestHandleReplaceRange_WindowOffset_String(t *testing.T) {
|
||||
st := newStringState("Hello World, this is a test.")
|
||||
st.Editor.IMEWindowStartByte = 6
|
||||
st.Editor.IMEWindowText = "World, this is a test."
|
||||
HandleReplaceRange(0, 5, "There")
|
||||
st.Editor.IMEContext = ""
|
||||
st.Editor.IMEContextStartByte = 6
|
||||
st.Editor.IMEOffsetRune = 6
|
||||
HandleReplaceRange(6, 11, "There")
|
||||
if want := "Hello There, this is a test."; st.Editor.Buffer != want {
|
||||
t.Fatalf("windowed replace: got %q, want %q", st.Editor.Buffer, want)
|
||||
}
|
||||
if cp := st.Editor.CursorPosition; cp != 11 {
|
||||
if cp := TheState.Editor.CursorPosition; cp != 11 {
|
||||
t.Fatalf("cursor = %d, want 11", cp)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleReplaceRange_WithContext verifies the leading-context mapping:
|
||||
// the snippet is context+window and the IME's absolute rune offsets may land
|
||||
// inside the context (before the window). The context contains a multi-byte
|
||||
// rune so rune != byte there, which pins the rune->byte walk. A second, fresh
|
||||
// state covers a range that spans the context/window boundary.
|
||||
func TestHandleReplaceRange_WithContext(t *testing.T) {
|
||||
// File "héllo world, this" (18 bytes): h(0) é(1-2) l(3) l(4) o(5)
|
||||
// sp(6) w(7) o(8) r(9) l(10) d(11) ,(12) sp(13) t(14) h(15) i(16) s(17).
|
||||
// Window starts at byte 6 (the space), so window = " world, this"; the
|
||||
// context is the preceding runes "héllo" (5 runes, 6 bytes), starting at
|
||||
// byte 0. The snippet spans absolute runes [0, ...), so IMEOffsetRune=0.
|
||||
st := newStringState("héllo world, this")
|
||||
st.Editor.IMEWindowStartByte = 6
|
||||
st.Editor.IMEWindowText = " world, this"
|
||||
st.Editor.IMEContext = "héllo"
|
||||
st.Editor.IMEContextStartByte = 0
|
||||
st.Editor.IMEOffsetRune = 0
|
||||
// Replace absolute rune [1,2) ("é", bytes 1..3) with "@": inside context.
|
||||
HandleReplaceRange(1, 2, "@")
|
||||
if want := "h@llo world, this"; st.Editor.Buffer != want {
|
||||
t.Fatalf("context replace: got %q, want %q", st.Editor.Buffer, want)
|
||||
}
|
||||
if cp := TheState.Editor.CursorPosition; cp != 2 {
|
||||
t.Fatalf("cursor = %d, want 2", cp)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleReplaceRange_SpansContextWindow verifies a replace range that
|
||||
// straddles the context/window boundary maps correctly through both.
|
||||
func TestHandleReplaceRange_SpansContextWindow(t *testing.T) {
|
||||
st := newStringState("héllo world, this")
|
||||
st.Editor.IMEWindowStartByte = 6
|
||||
st.Editor.IMEWindowText = " world, this"
|
||||
st.Editor.IMEContext = "héllo"
|
||||
st.Editor.IMEContextStartByte = 0
|
||||
st.Editor.IMEOffsetRune = 0
|
||||
// Absolute runes [4,7): rune4='o'(context, byte5) + rune5=sp(window,
|
||||
// byte6) + rune6='w'(window, byte7) -> bytes [5,8) = "o w". Keeping
|
||||
// bytes 0..4 ("héll") and 8+ ("orld, this").
|
||||
HandleReplaceRange(4, 7, "ZZ")
|
||||
if want := "héllZZorld, this"; st.Editor.Buffer != want {
|
||||
t.Fatalf("spanning replace: got %q, want %q", st.Editor.Buffer, want)
|
||||
}
|
||||
if cp := TheState.Editor.CursorPosition; cp != 7 {
|
||||
t.Fatalf("cursor = %d, want 7", cp)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRuneAnchorTracking verifies the incremental rune-count cache: it must
|
||||
// follow edits (imeAnchorEdit) and agree with a naive full count after a
|
||||
// sequence of mutations at various positions.
|
||||
func TestRuneAnchorTracking(t *testing.T) {
|
||||
st := newChunkedState(t, "hello wörld, this is a test", 8)
|
||||
check := func(what string) {
|
||||
t.Helper()
|
||||
full, _ := st.Editor.ChunkedBuffer.FullContent()
|
||||
for _, pos := range []int{0, 3, 9, len(full)} {
|
||||
if got, want := st.imeRuneOffsetAt(pos), countRunes(full[:pos]); got != want {
|
||||
t.Fatalf("%s: imeRuneOffsetAt(%d) = %d, want %d", what, pos, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
check("initial")
|
||||
testInsertAt(t, st, 3, "é")
|
||||
check("insert mid")
|
||||
testInsertAt(t, st, int(st.Editor.ChunkedBuffer.FileLen()), "é")
|
||||
check("insert end")
|
||||
testDeleteAt(t, st, 2, 4)
|
||||
check("delete")
|
||||
}
|
||||
|
||||
// testInsertAt inserts text at absolute byte pos on the test state, adjusting
|
||||
// the IME rune anchor the same way the production edit paths do.
|
||||
func testInsertAt(t *testing.T, st *State, pos int, text string) {
|
||||
t.Helper()
|
||||
st.Editor.ChunkedBuffer.Insert(pos, text)
|
||||
st.Editor.ChunkedBuffer.UpdateLineIndexAfterInsert(pos, text)
|
||||
st.Editor.CursorPosition = pos + len(text)
|
||||
imeAnchorEdit(pos, "", text)
|
||||
}
|
||||
|
||||
// testDeleteAt deletes n bytes at absolute byte pos on the test state.
|
||||
func testDeleteAt(t *testing.T, st *State, pos, n int) {
|
||||
t.Helper()
|
||||
del := st.Editor.ChunkedBuffer.Content(pos, pos+n)
|
||||
st.Editor.ChunkedBuffer.Delete(pos, n)
|
||||
st.Editor.ChunkedBuffer.UpdateLineIndexAfterDelete(pos, pos+n)
|
||||
imeAnchorEdit(pos, del, "")
|
||||
}
|
||||
|
||||
+352
-19
@@ -73,6 +73,11 @@ type Logic struct {
|
||||
openFileChan chan string
|
||||
retryChan chan string // auto-save retries
|
||||
autosaveChan chan struct{} // auto-save debounce ticks (timer -> owner)
|
||||
|
||||
// flushSession: one-shot request to persist the session snapshot now
|
||||
// (the OS activity onStop hook, see FlushSession). Buffered 1 so the
|
||||
// requester never blocks, even if an earlier flush is still queued.
|
||||
flushSession chan struct{}
|
||||
inspectChan chan *inspectReq
|
||||
|
||||
// Per-file write protocol (see requestSave). Workers are a shared pool and
|
||||
@@ -89,8 +94,69 @@ type Logic struct {
|
||||
done chan struct{}
|
||||
exitWg sync.WaitGroup
|
||||
saveTimer *time.Timer // auto-save debounce timer; non-nil while pending
|
||||
// Browser live-list (see dirwatcher.go). The primary trigger is inotify:
|
||||
// browserWatcher watches the current browser directory and sends a token on
|
||||
// browserEventChan when it changes (file created/removed/renamed or
|
||||
// mtime-changed, incl. inbound syncs). browserRefreshTimer is a slow
|
||||
// safety-net rescan that covers the rare event the FUSE inotify layer drops.
|
||||
// Both are owner-goroutine-only and funnel into refreshBrowserDir.
|
||||
browserEventChan chan struct{} // inotify: dirWatcher -> owner (buffered 1)
|
||||
browserWatcher *dirWatcher // inotify watch on the current browser dir
|
||||
browserRefreshChan chan struct{} // backstop rescan tick (timer -> owner)
|
||||
browserRefreshTimer *time.Timer // backstop rescan timer; non-nil while pending
|
||||
lastEmit time.Time // time of the last frame emission (profiler cadence)
|
||||
debugCmdC chan string // one-shot debug commands from the cmd-file poller; nil = disabled
|
||||
|
||||
// Relaunch state restoration (spec §7, see session.go): session is the
|
||||
// snapshot handed in by the cmd layer via BeginRestore (zero = none),
|
||||
// restoreFile names the open whose stat/read results are the restore's
|
||||
// ("" = none; any other open cancels it). sessionSaver is the cmd layer's
|
||||
// file writer; lastSession/lastSessionSave drive the rate-limited
|
||||
// change-detected save from emitFrame. All touched only on the owner.
|
||||
session SessionState
|
||||
restoreFile string
|
||||
// restoreScroll/restoreScrollArmed hold the snapshot's scroll offset
|
||||
// until it is safe to apply it: only on an emitFrame whose layout pass
|
||||
// saw a trustworthy viewport (scale known, size known, restored content
|
||||
// present — the layout computes MaxScroll and one-way-clamps the offset,
|
||||
// and on device the first ScaleEvent can precede the size ConfigEvent).
|
||||
// scaleSeen tracks the first ScaleEvent; restoreContentLanded marks the
|
||||
// read result that filled the buffer (FileLen alone is set earlier, by
|
||||
// the stat result, and is not a content-arrival signal).
|
||||
restoreScroll ui.Dp
|
||||
restoreScrollArmed bool
|
||||
// restoreArmedAt is when the restore scroll was armed; bounds how long
|
||||
// maybeApplyRestoreScroll may wait for a trustworthy MaxScroll (see
|
||||
// there) before landing best-effort.
|
||||
restoreArmedAt time.Time
|
||||
// restoreScrollLine/restoreScrollSub/restorePinDeadline implement the
|
||||
// restore line-pin (see refreshRestorePin in session.go): the
|
||||
// line-derived scroll offset is only consistent with the all-estimate
|
||||
// WrapIndex, so while relaunch restore is settling, every wrap-count
|
||||
// correction landing below the pinned line shifts the offset-to-line
|
||||
// mapping and would drag the viewport off the restored line. Until the
|
||||
// restored window itself has shaped (or a timeout, or the user or a
|
||||
// search takes over), the offset is re-derived from the pinned line
|
||||
// after each correction.
|
||||
restoreScrollLine int
|
||||
restoreScrollSub float64
|
||||
restorePinDeadline time.Time
|
||||
// fontPin/fontPinM implement the pinch font-pin (see
|
||||
// setFontPin/refreshFontPin in session.go): the CONTENT point under the
|
||||
// pinch center (glyph byte + offset from its baseline, with a
|
||||
// line/fragment/sub-line fallback) and the center's region-relative Y
|
||||
// (dp). While armed, every shaped layout re-derives the scroll offset to
|
||||
// keep that content point under the center as the font scale — and, a
|
||||
// few frames later, the rewrap — changes.
|
||||
fontPin contentPin
|
||||
fontPinM float64
|
||||
fontPinArmed bool
|
||||
fontPinDeadline time.Time
|
||||
scaleSeen bool
|
||||
restoreContentLanded bool
|
||||
sessionSaver func(SessionState)
|
||||
lastSession SessionState
|
||||
lastSessionSave time.Time
|
||||
}
|
||||
|
||||
// NewLogic creates a new Logic instance, accepting an optional mockFS.
|
||||
@@ -132,6 +198,9 @@ func NewLogic(mfs pool.FileSystem, path string, openfunc func(string)) *Logic {
|
||||
openFileChan: make(chan string),
|
||||
retryChan: make(chan string, 1), // Buffered channel
|
||||
autosaveChan: make(chan struct{}),
|
||||
browserEventChan: make(chan struct{}, 1),
|
||||
browserRefreshChan: make(chan struct{}),
|
||||
flushSession: make(chan struct{}, 1),
|
||||
inspectChan: make(chan *inspectReq),
|
||||
writeInFlight: make(map[string]int),
|
||||
savePending: make(map[string]bool),
|
||||
@@ -207,9 +276,29 @@ var TheLogic *Logic
|
||||
func (l *Logic) Run() {
|
||||
l.exitWg.Add(1)
|
||||
defer l.exitWg.Done()
|
||||
// Close the inotify watcher on any exit. Run is the owner goroutine, so at
|
||||
// return the watcher is single-threaded and safe to close here (covers both
|
||||
// Shutdown and the test-harness Done+WaitForExit path). The closure reads
|
||||
// browserWatcher at exit time, after emitFrame has created it if the browser
|
||||
// was ever shown.
|
||||
defer func() {
|
||||
if l.browserWatcher != nil {
|
||||
l.browserWatcher.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
// Dispatch initial directory index build on startup
|
||||
l.workerPool.Dispatch(pool.NewBuildIndexTask(l.state.Browser.CurrentPath, l.mockFS))
|
||||
// Dispatch the initial directory index build on startup, routed through the
|
||||
// manager so the in-flight marker is set (a later refresh of the same
|
||||
// directory coalesces, and a late result is dropped once we navigate).
|
||||
l.browserManager.Load(l.state.Browser.CurrentPath)
|
||||
|
||||
// Relaunch restoration (spec §7): re-open the last file if a session
|
||||
// was handed in before Run (BeginRestore). The browser index is still
|
||||
// built: the back button must land on a populated browser.
|
||||
if l.session.File != "" {
|
||||
l.restoreFile = l.session.File
|
||||
l.openFile(l.session.File)
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
@@ -220,8 +309,16 @@ func (l *Logic) Run() {
|
||||
// the final flush and promote a stale snapshot.
|
||||
l.drainWrites()
|
||||
return
|
||||
case <-l.flushSession:
|
||||
// Persist now, bypassing the rate limit: the activity is going
|
||||
// away (recents-wipe or app switch) and the process may die
|
||||
// shortly after this returns.
|
||||
l.flushSessionSave()
|
||||
case update := <-l.configChan:
|
||||
update.apply(l.state)
|
||||
if _, ok := update.(ScaleEvent); ok {
|
||||
l.scaleSeen = true
|
||||
}
|
||||
l.emitFrame()
|
||||
case fb := <-l.layoutChan:
|
||||
// Store the full GlyphLayout on editor state.
|
||||
@@ -238,6 +335,24 @@ func (l *Logic) Run() {
|
||||
// lines since shaping (fb.EditSeq correlates with the content).
|
||||
if fb.EditSeq == l.state.Editor.EditSeq {
|
||||
l.state.applyWrapCounts(fb)
|
||||
// Restore line-pin (see refreshRestorePin): a correction
|
||||
// landing below the pinned line shifted the offset-to-line
|
||||
// mapping, so re-derive the offset from the pinned line under
|
||||
// the corrected index. The restored window's own shaping means
|
||||
// its neighborhood is real and the pin can stand down.
|
||||
if l.restoreScrollLine >= 0 {
|
||||
if fb.WindowStartLine == l.restoreScrollLine {
|
||||
l.restoreScrollLine = -1
|
||||
} else {
|
||||
l.refreshRestorePin()
|
||||
}
|
||||
}
|
||||
// Pinch font-pin (see refreshFontPin): the fresh layout may
|
||||
// have rewrapped the pinned line; re-derive so the pinned
|
||||
// content point stays under the pinch center.
|
||||
if l.fontPinArmed {
|
||||
l.refreshFontPin(fb)
|
||||
}
|
||||
// Search settle (see EditorState.findSettle): the shaping above
|
||||
// may have corrected the wrap counts around a find-jumped
|
||||
// viewport; re-scroll while the correction still matters.
|
||||
@@ -245,7 +360,12 @@ func (l *Logic) Run() {
|
||||
l.emitFrame()
|
||||
}
|
||||
}
|
||||
if derivedLastLineY != l.state.LastLineY {
|
||||
// Epsilon-gated: the derived value is a float sum of line
|
||||
// heights, and exact-equality re-emit spun a re-emit -> shape ->
|
||||
// re-emit loop whenever the sum landed on a different float
|
||||
// representation frame to frame (each loop iteration re-drew the
|
||||
// editor, re-pushed IME state, and starved real work).
|
||||
if d := derivedLastLineY - l.state.LastLineY; d > 0.5 || d < -0.5 {
|
||||
l.state.LastLineY = derivedLastLineY
|
||||
l.emitFrame()
|
||||
}
|
||||
@@ -266,17 +386,7 @@ func (l *Logic) Run() {
|
||||
l.state.Editor.findSetQuery(q)
|
||||
l.emitFrame()
|
||||
case path := <-l.openFileChan:
|
||||
// Discard find results for the previous file; the query is kept
|
||||
// (see EditorState.findReset) and re-scanned against the new file.
|
||||
TheState.Editor.findReset()
|
||||
// Create chunked buffer for virtual scrolling
|
||||
chunkSize := DefaultChunkSize
|
||||
cb := NewChunkedBuffer(path, chunkSize, l.mockFS, "")
|
||||
cb.SetWorkerPool(l.workerPool)
|
||||
TheState.Editor.ChunkedBuffer = cb
|
||||
|
||||
// Dispatch stat task to get file size
|
||||
l.workerPool.Dispatch(pool.NewStatFileTask(path, l.mockFS))
|
||||
l.openFile(path)
|
||||
case filename := <-l.retryChan:
|
||||
log.Printf("Logic: Retrying save for %s", filename)
|
||||
delete(l.retryScheduled, filename)
|
||||
@@ -286,6 +396,18 @@ func (l *Logic) Run() {
|
||||
// the owner snapshots content and dispatches the write.
|
||||
l.saveTimer = nil
|
||||
l.requestSave(l.state.Editor.Filename)
|
||||
case <-l.browserEventChan:
|
||||
// inotify: the current browser directory changed (an external create,
|
||||
// delete, rename, or mtime change). Re-read it; no-op while the
|
||||
// editor is on screen (the change is picked up on the next return).
|
||||
l.refreshBrowserDir()
|
||||
case <-l.browserRefreshChan:
|
||||
// Slow backstop rescan tick (safety net for a dropped inotify event).
|
||||
// The timer goroutine only sent a token; the owner re-reads the current
|
||||
// directory (bounded) and re-arms. No-op while the editor is on screen.
|
||||
l.browserRefreshTimer = nil
|
||||
l.refreshBrowserDir()
|
||||
l.armBrowserRefresh()
|
||||
case p := <-l.state.pasteChan:
|
||||
// Clipboard content arrived from the main goroutine: insert it
|
||||
// (replacing any live selection, per the selection-aware edit rule).
|
||||
@@ -304,12 +426,74 @@ func (l *Logic) Run() {
|
||||
}
|
||||
}
|
||||
|
||||
// openFile starts loading path in the editor (a browser row tap or the
|
||||
// relaunch restore). Any open of a file that is not the in-flight restore
|
||||
// cancels the restore: its late stat/read results must not re-apply the
|
||||
// snapshot's positions to a different file. Must be called on the logic
|
||||
// goroutine.
|
||||
func (l *Logic) openFile(path string) {
|
||||
if l.restoreFile != "" && l.restoreFile != path {
|
||||
l.abortRestore() // a different open cancels the in-flight one
|
||||
}
|
||||
l.releaseFontPin() // the pin names lines of the previous file
|
||||
// Discard find results for the previous file; the query is kept
|
||||
// (see EditorState.findReset) and re-scanned against the new file.
|
||||
TheState.Editor.findReset()
|
||||
// Create chunked buffer for virtual scrolling
|
||||
chunkSize := DefaultChunkSize
|
||||
cb := NewChunkedBuffer(path, chunkSize, l.mockFS, "")
|
||||
cb.SetWorkerPool(l.workerPool)
|
||||
TheState.Editor.ChunkedBuffer = cb
|
||||
// New file: the previous file's IME rune-count cache and window bookkeeping
|
||||
// are meaningless. Reset the anchor so the next imeRuneOffsetAt recomputes
|
||||
// from scratch (the window/context fields are recomputed by buildFrame).
|
||||
TheState.Editor.imeRuneCacheByte = 0
|
||||
TheState.Editor.imeRuneCacheCount = 0
|
||||
TheState.Editor.imeSnipWS, TheState.Editor.imeSnipWE, TheState.Editor.imeSnipArmed = 0, 0, false
|
||||
TheState.Editor.IMESnippetText = ""
|
||||
TheState.Editor.IMECaretRune, TheState.Editor.IMESelStartRune, TheState.Editor.IMESelEndRune = 0, -1, -1
|
||||
TheState.Editor.IMEContext = ""
|
||||
TheState.Editor.IMEContextStartByte = 0
|
||||
TheState.Editor.IMEOffsetRune = 0
|
||||
|
||||
// Dispatch stat task to get file size
|
||||
l.workerPool.Dispatch(pool.NewStatFileTask(path, l.mockFS))
|
||||
}
|
||||
|
||||
// emitFrame computes the current frame, records a profiler probe (if enabled),
|
||||
// and hands it to the main goroutine. Centralizing emission here ensures the
|
||||
// in-app profiler (PerfRecord) sees every frame exactly once, on the owner
|
||||
// goroutine. Must be called on the logic goroutine.
|
||||
func (l *Logic) emitFrame() {
|
||||
// Relaunch snapshot (spec §7): persist when the state has changed and
|
||||
// the rate limit elapsed (tiny JSON file, see session.go).
|
||||
l.saveSessionIfChanged()
|
||||
// While the browser is on screen, keep its directory fresh so external
|
||||
// adds/removes/renames/mtime changes (incl. inbound syncs) appear live, in
|
||||
// the current sort order. Point the inotify watch at the current directory
|
||||
// (created on first use; re-pointed on navigation, a no-op while the path is
|
||||
// unchanged) and arm the slow backstop rescan. Runs on every browser frame,
|
||||
// covering startup, back-from-editor, and sort toggle.
|
||||
if l.state.page == BrowserPage {
|
||||
if l.browserWatcher == nil {
|
||||
l.browserWatcher = newDirWatcher(l.browserEventChan)
|
||||
}
|
||||
l.browserWatcher.SetDir(l.state.Browser.CurrentPath)
|
||||
l.armBrowserRefresh()
|
||||
}
|
||||
elems := l.state.layout(l.browserManager)
|
||||
// Relaunch restore (spec §7): land the armed restore scroll AFTER the
|
||||
// layout pass above (it refreshed MaxScroll for the current viewport and
|
||||
// one-way-clamps any offset set against an earlier, smaller one). The
|
||||
// guard defers the application until the viewport is trustworthy — on
|
||||
// device the first ScaleEvent can precede the size ConfigEvent. The
|
||||
// re-layout makes this frame carry the restored viewport. The restore is
|
||||
// complete once the scroll lands; drop restoreFile so a later open of
|
||||
// this same file is treated as a fresh one.
|
||||
if l.restoreScrollArmed && l.maybeApplyRestoreScroll() {
|
||||
l.restoreFile = ""
|
||||
elems = l.state.layout(l.browserManager)
|
||||
}
|
||||
now := time.Now()
|
||||
if PerfRecord != nil {
|
||||
var delta time.Duration
|
||||
@@ -396,10 +580,11 @@ func (l *Logic) EnableDebugCmdPoll(dir string) {
|
||||
}()
|
||||
}
|
||||
|
||||
// applyDebugCmd applies a one-shot debug scroll command from the cmd-file
|
||||
// poller. Commands: "open <path>" (any page), and, on the editor page,
|
||||
// "top", "bottom", "frac <0..1>", "dp <int>". Must be called on the logic
|
||||
// goroutine.
|
||||
// applyDebugCmd applies a one-shot debug command from the cmd-file poller.
|
||||
// Commands: "open <path>" (any page), and, on the editor page, "top",
|
||||
// "bottom", "frac <0..1>", "dp <int>", "pinch <factor>" (relative app font
|
||||
// scale, as the renderer's pinch probe would deliver) and "fontsize <v>"
|
||||
// (absolute app font scale). Must be called on the logic goroutine.
|
||||
func (l *Logic) applyDebugCmd(cmd string) {
|
||||
s := l.state
|
||||
fields := strings.Fields(cmd)
|
||||
@@ -419,6 +604,45 @@ func (l *Logic) applyDebugCmd(cmd string) {
|
||||
l.emitFrame() // OpenFile only mutates state (the tap path emits via its handler)
|
||||
return
|
||||
}
|
||||
// App-local font scale (pinch zoom). Drives the full logic->frame->render
|
||||
// path the same way a real pinch does (HandleFontPinch is the handler a
|
||||
// FontPinchEvent carries); adb has no two-finger input, so these
|
||||
// commands are the on-emulator test hook. `pinch` anchors at the CENTER
|
||||
// of the editor region (where a real pinch usually starts); `fontsize`
|
||||
// is an absolute top-anchored set.
|
||||
if fields[0] == "pinch" || fields[0] == "fontsize" {
|
||||
if s.page != EditorPage {
|
||||
log.Printf("DebugCmd: %q ignored (not on editor page)", cmd)
|
||||
return
|
||||
}
|
||||
if len(fields) < 2 {
|
||||
log.Printf("DebugCmd: %s needs a value", fields[0])
|
||||
return
|
||||
}
|
||||
f, err := strconv.ParseFloat(fields[1], 32)
|
||||
if err != nil || f <= 0 {
|
||||
log.Printf("DebugCmd: bad %s value %q", fields[0], fields[1])
|
||||
return
|
||||
}
|
||||
if fields[0] == "pinch" {
|
||||
if f > 100 { // a single frame's pinch never spans this much
|
||||
log.Printf("DebugCmd: pinch factor out of range: %v", f)
|
||||
return
|
||||
}
|
||||
HandleFontPinch(ui.FontPinchEvent{
|
||||
Scale: float32(f),
|
||||
Center: ui.Point{
|
||||
X: s.EditorRegion.X + s.EditorRegion.W/2,
|
||||
Y: s.EditorRegion.Y + s.EditorRegion.H/2,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
SetAppFontScale(float32(f))
|
||||
}
|
||||
log.Printf("DebugCmd: %q -> appFontScale=%.4f scroll=%d", cmd, s.appFontScale, int(s.ScrollOffset))
|
||||
l.emitFrame()
|
||||
return
|
||||
}
|
||||
if s.page != EditorPage {
|
||||
log.Printf("DebugCmd: %q ignored (not on editor page)", cmd)
|
||||
return
|
||||
@@ -459,6 +683,7 @@ func (l *Logic) applyDebugCmd(cmd string) {
|
||||
if target > s.MaxScroll {
|
||||
target = s.MaxScroll
|
||||
}
|
||||
l.releaseFontPin() // a debug scroll takes over the viewport
|
||||
s.ScrollOffset = target
|
||||
log.Printf("DebugCmd: %q -> scroll=%d maxScroll=%d", cmd, int(s.ScrollOffset), int(s.MaxScroll))
|
||||
l.emitFrame()
|
||||
@@ -500,10 +725,52 @@ func (l *Logic) markDirty() {
|
||||
})
|
||||
}
|
||||
|
||||
// browserBackstopInterval is how often the visible browser directory is
|
||||
// re-read as a SLOW safety net while the browser page is on screen. The primary
|
||||
// trigger is inotify (browserWatcher, see dirwatcher.go), which re-reads within
|
||||
// ~tens of ms of a real change; this rescan only covers the rare event the FUSE
|
||||
// inotify layer drops. One directory read per interval is a single ReadDir plus
|
||||
// a per-entry stat; the manager's in-flight marker keeps it to one read at a
|
||||
// time, and its change-detection emits no frame when nothing changed.
|
||||
const browserBackstopInterval = 30 * time.Second
|
||||
|
||||
// refreshBrowserDir re-reads the current browser directory (bounded to one in
|
||||
// flight by the manager). Triggered by either an inotify event
|
||||
// (browserEventChan) or the slow backstop tick (browserRefreshChan). No-op
|
||||
// while the editor is on screen — there is no list to keep fresh there.
|
||||
func (l *Logic) refreshBrowserDir() {
|
||||
if l.state.page != BrowserPage {
|
||||
return
|
||||
}
|
||||
l.browserManager.Refresh(l.state.Browser.CurrentPath)
|
||||
}
|
||||
|
||||
// armBrowserRefresh starts (or leaves running) the slow backstop rescan timer.
|
||||
// Idempotent: at most one timer at a time. The timer goroutine only sends a
|
||||
// token on browserRefreshChan; the owner does the re-read. The send is
|
||||
// non-blocking so the timer goroutine can never block, even after the logic
|
||||
// loop has exited (the token is then simply dropped). Owner-goroutine-only.
|
||||
func (l *Logic) armBrowserRefresh() {
|
||||
if l.browserRefreshTimer != nil {
|
||||
return
|
||||
}
|
||||
l.browserRefreshTimer = time.AfterFunc(browserBackstopInterval, func() {
|
||||
select {
|
||||
case l.browserRefreshChan <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// handleWorkerResult processes results from the worker pool.
|
||||
func (l *Logic) handleWorkerResult(res pool.Result) {
|
||||
// A browser result re-emits only when it actually changed the view; a
|
||||
// periodic refresh of an unchanged directory is dropped here, keeping
|
||||
// emission event-driven while the browser idles. Other results always
|
||||
// re-emit.
|
||||
emit := true
|
||||
if res.IsBrowserResult() {
|
||||
l.browserManager.HandleResult(res)
|
||||
emit = l.browserManager.HandleResult(res)
|
||||
} else if res.TaskType == pool.TypeReadFile {
|
||||
if res.Success {
|
||||
if content, ok := res.Data.([]byte); ok {
|
||||
@@ -518,7 +785,38 @@ func (l *Logic) handleWorkerResult(res pool.Result) {
|
||||
// Fallback: populate the deprecated Buffer field
|
||||
l.state.Editor.Buffer = string(content)
|
||||
}
|
||||
// Relaunch restoration (spec §7): the content is in memory, so
|
||||
// land the snapshot's cursor/selection (clamped to the file)
|
||||
// and re-scan the restored find query. Path-guarded: a read
|
||||
// result for a file the user has since replaced must not apply
|
||||
// the snapshot to the replacement's buffer.
|
||||
if res.FilePath == l.state.Editor.Filename && l.restoreFile == res.FilePath {
|
||||
l.applyRestorePositions(len(content))
|
||||
l.restoreContentLanded = true
|
||||
// The snapshot has landed: resume session saving. restoreFile
|
||||
// and the armed scroll stay until the scroll itself lands in
|
||||
// the emitFrame hook (which needs this content for a real
|
||||
// MaxScroll), so a different open in between still aborts via
|
||||
// openFile's restoreFile guard.
|
||||
// Re-scan the restored query only when the bar was open:
|
||||
// with the bar closed, the result would be dropped (the apply
|
||||
// gate requires Visible) and Scanning would stay stuck true;
|
||||
// findShow re-scans on the next open instead.
|
||||
if f := &l.state.Editor.Find; f.Visible && f.Query != "" && !f.Scanning {
|
||||
l.state.Editor.findDispatchScan()
|
||||
}
|
||||
// No armed scroll: the snapshot has fully landed with the
|
||||
// content; lift the save suppression now (the emitFrame
|
||||
// hook does this when a scroll does land).
|
||||
if !l.restoreScrollArmed {
|
||||
l.session = SessionState{}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if res.FilePath == l.restoreFile {
|
||||
// The restored file's content could not be read: drop the
|
||||
// restore (the editor keeps the empty file view it has).
|
||||
l.abortRestore()
|
||||
}
|
||||
} else if res.TaskType == pool.TypeReadChunk {
|
||||
// In-range files load fully via SetContent, so this is only a fallback.
|
||||
@@ -527,11 +825,20 @@ func (l *Logic) handleWorkerResult(res pool.Result) {
|
||||
|
||||
if res.Success {
|
||||
if chunk, ok := res.Data.([]byte); ok {
|
||||
// A full-file load (SetContent) may have finished while this
|
||||
// lazy chunk read was in flight; its resident chunks are the
|
||||
// authoritative content and must not be clobbered by a stale
|
||||
// lazy result (which can also be a short read). Applying it
|
||||
// would truncate/shift the chunk and move every byte offset
|
||||
// after it — taps and IME commits landing at the wrong
|
||||
// position (text corruption).
|
||||
if !cb.fullyLoaded {
|
||||
for len(cb.chunks) <= res.ChunkIdx {
|
||||
cb.chunks = append(cb.chunks, nil)
|
||||
}
|
||||
cb.chunks[res.ChunkIdx] = chunk
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Printf("Logic: ReadChunkTask failed for %s chunk %d: %v", res.FilePath, res.ChunkIdx, res.Error)
|
||||
}
|
||||
@@ -539,9 +846,22 @@ func (l *Logic) handleWorkerResult(res pool.Result) {
|
||||
} else if res.TaskType == pool.TypeStatFile {
|
||||
if res.Success {
|
||||
if stat, ok := res.Data.(*pool.FileStat); ok {
|
||||
// Relaunch restoration (spec §7): the file exists, so land
|
||||
// the snapshot's find state now (the cursor/selection land
|
||||
// with the content, the query re-scans against the new file).
|
||||
if res.FilePath == l.restoreFile && l.restoreFile != "" {
|
||||
f := &l.state.Editor.Find
|
||||
f.Query = l.session.FindQuery
|
||||
f.Visible = l.session.FindVisible
|
||||
f.RestoreMatch = l.session.FindCurByte
|
||||
f.Restoring = l.session.FindVisible
|
||||
}
|
||||
// Size guard: refuse to edit files above the limit. The browser can
|
||||
// still list them; the editor shows a "too large to edit" notice.
|
||||
if stat.Size > MaxEditableFileSize {
|
||||
if l.restoreFile != "" {
|
||||
l.abortRestore() // no content load follows; nothing lands
|
||||
}
|
||||
l.state.Editor.TooLarge = true
|
||||
l.state.Editor.TooLargeSize = stat.Size
|
||||
log.Printf("Logic: %s is %d bytes, exceeds the %d-byte edit limit", stat.Path, stat.Size, MaxEditableFileSize)
|
||||
@@ -555,6 +875,11 @@ func (l *Logic) handleWorkerResult(res pool.Result) {
|
||||
l.workerPool.Dispatch(pool.NewBuildLineIndexTask(stat.Path, l.mockFS))
|
||||
}
|
||||
}
|
||||
} else if res.FilePath == l.restoreFile && l.restoreFile != "" {
|
||||
// The restored file is gone (deleted/moved since the last
|
||||
// session): fall back to the browser instead of showing an
|
||||
// empty editor.
|
||||
l.abandonRestore()
|
||||
}
|
||||
} else if res.TaskType == pool.TypeBuildLineIndex {
|
||||
if res.Success {
|
||||
@@ -620,8 +945,10 @@ func (l *Logic) handleWorkerResult(res pool.Result) {
|
||||
l.state.Editor.applySearchResult(res)
|
||||
}
|
||||
}
|
||||
if emit {
|
||||
l.emitFrame()
|
||||
}
|
||||
}
|
||||
|
||||
// State returns the current state.
|
||||
func (l *Logic) State() *State {
|
||||
@@ -768,5 +1095,11 @@ func (l *Logic) Shutdown() {
|
||||
l.Done()
|
||||
l.WaitForExit()
|
||||
l.FlushAll()
|
||||
// Final relaunch snapshot (spec §7): after exit the caller owns the
|
||||
// state single-threaded, like FlushAll above, so one unconditional
|
||||
// save makes the file exact for the next launch.
|
||||
if l.sessionSaver != nil {
|
||||
l.sessionSaver(l.SnapshotSession())
|
||||
}
|
||||
l.workerPool.Stop()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,386 @@
|
||||
package editor
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// These tests cover the app-local pinch font scale: EffectiveLineHeight must
|
||||
// follow the product of the system font scale and the app scale, and the
|
||||
// pinch handler must apply the relative factor continuously (no rounding to
|
||||
// whole points) while keeping the viewport top anchored (scroll offset
|
||||
// rescaled in lockstep with the line height).
|
||||
|
||||
func TestEffectiveLineHeight_FollowsAppFontScale(t *testing.T) {
|
||||
TheLogic = nil
|
||||
TheState = NewState()
|
||||
defer func() { TheState.appFontScale = 1.0 }()
|
||||
|
||||
cases := []struct {
|
||||
sys, app float32
|
||||
want float64
|
||||
}{
|
||||
{0, 1.0, float64(EditorLineHeight())}, // both unknown/default
|
||||
{1.3, 1.0, float64(EditorLineHeight()) * 1.3},
|
||||
{1.0, 1.7, float64(EditorLineHeight()) * 1.7},
|
||||
{1.3, 1.7, float64(EditorLineHeight()) * 1.3 * 1.7},
|
||||
{1.0, 0.5, float64(EditorLineHeight()) * 0.5},
|
||||
}
|
||||
for _, c := range cases {
|
||||
TheState.fontScale = c.sys
|
||||
TheState.appFontScale = c.app
|
||||
got := float64(EffectiveLineHeight())
|
||||
if math.Abs(got-c.want) > 1e-5 {
|
||||
t.Errorf("sys=%v app=%v: EffectiveLineHeight=%v want %v", c.sys, c.app, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleFontPinch_ContinuousAndAnchored(t *testing.T) {
|
||||
TheLogic = nil
|
||||
TheState = NewState()
|
||||
defer func() { TheState.appFontScale = 1.0; TheState.ScrollOffset = 0 }()
|
||||
|
||||
// Start scrolled to a line boundary: 100 lines at the default pitch.
|
||||
lh := float64(EffectiveLineHeight())
|
||||
TheState.ScrollOffset = ui.Dp(100 * lh)
|
||||
|
||||
// A sequence of small relative factors: the product is 1.1^5 ~ 1.61051,
|
||||
// a non-representable float32 — rounding to whole points (or to any
|
||||
// fixed step) would not land here.
|
||||
for i := 0; i < 5; i++ {
|
||||
HandleFontPinch(ui.FontPinchEvent{Scale: 1.1})
|
||||
}
|
||||
want := float64(1.1) * 1.1 * 1.1 * 1.1 * 1.1
|
||||
if math.Abs(float64(TheState.appFontScale)-want) > 1e-6 {
|
||||
t.Errorf("appFontScale=%v want %v (continuous, no snapping)", TheState.appFontScale, want)
|
||||
}
|
||||
// The scroll offset rescaled by the same ratio: the same line (100) at
|
||||
// the same sub-line fraction (0) stays on top.
|
||||
wantOff := 100 * lh * want
|
||||
if got := float64(TheState.ScrollOffset); math.Abs(got-wantOff) > 1e-3 {
|
||||
t.Errorf("ScrollOffset=%v want %v (anchor preserved)", got, wantOff)
|
||||
}
|
||||
// EffectiveLineHeight follows the new scale.
|
||||
wantLH := lh * want
|
||||
if got := float64(EffectiveLineHeight()); math.Abs(got-wantLH) > 1e-5 {
|
||||
t.Errorf("EffectiveLineHeight=%v want %v", got, wantLH)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleFontPinch_Clamps(t *testing.T) {
|
||||
TheLogic = nil
|
||||
TheState = NewState()
|
||||
defer func() { TheState.appFontScale = 1.0; TheState.ScrollOffset = 0 }()
|
||||
|
||||
// Zoom out past the minimum.
|
||||
HandleFontPinch(ui.FontPinchEvent{Scale: 0.1})
|
||||
if TheState.appFontScale != MinAppFontScale {
|
||||
t.Errorf("appFontScale=%v want MinAppFontScale %v", TheState.appFontScale, MinAppFontScale)
|
||||
}
|
||||
// Zoom in past the maximum.
|
||||
HandleFontPinch(ui.FontPinchEvent{Scale: 100})
|
||||
if TheState.appFontScale != MaxAppFontScale {
|
||||
t.Errorf("appFontScale=%v want MaxAppFontScale %v", TheState.appFontScale, MaxAppFontScale)
|
||||
}
|
||||
// At the maximum, further zoom-in is a no-op (including the scroll).
|
||||
TheState.ScrollOffset = ui.Dp(123)
|
||||
HandleFontPinch(ui.FontPinchEvent{Scale: 1.5})
|
||||
if TheState.appFontScale != MaxAppFontScale {
|
||||
t.Errorf("appFontScale=%v want MaxAppFontScale %v", TheState.appFontScale, MaxAppFontScale)
|
||||
}
|
||||
if float64(TheState.ScrollOffset) != 123 {
|
||||
t.Errorf("ScrollOffset=%v want 123 (unchanged at the clamp)", TheState.ScrollOffset)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetAppFontScale_Absolute(t *testing.T) {
|
||||
TheLogic = nil
|
||||
TheState = NewState()
|
||||
defer func() { TheState.appFontScale = 1.0; TheState.ScrollOffset = 0 }()
|
||||
|
||||
lh := float64(EffectiveLineHeight())
|
||||
TheState.ScrollOffset = ui.Dp(50 * lh)
|
||||
SetAppFontScale(2.0)
|
||||
if TheState.appFontScale != 2.0 {
|
||||
t.Fatalf("appFontScale=%v want 2.0", TheState.appFontScale)
|
||||
}
|
||||
if got, want := float64(TheState.ScrollOffset), 50*lh*2.0; math.Abs(got-want) > 1e-3 {
|
||||
t.Errorf("ScrollOffset=%v want %v", got, want)
|
||||
}
|
||||
SetAppFontScale(0) // clamps to the minimum
|
||||
if TheState.appFontScale != MinAppFontScale {
|
||||
t.Errorf("appFontScale=%v want MinAppFontScale", TheState.appFontScale)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleFontPinch_IgnoresBadData(t *testing.T) {
|
||||
TheLogic = nil
|
||||
TheState = NewState()
|
||||
defer func() { TheState.appFontScale = 1.0 }()
|
||||
HandleFontPinch("not an event")
|
||||
HandleFontPinch(ui.FontPinchEvent{Scale: 0})
|
||||
HandleFontPinch(ui.FontPinchEvent{Scale: -2})
|
||||
if TheState.appFontScale != 1.0 {
|
||||
t.Errorf("appFontScale=%v want unchanged 1.0", TheState.appFontScale)
|
||||
}
|
||||
}
|
||||
|
||||
// The pinch CENTER stays fixed: the continuous content coordinate under the
|
||||
// center (display-line units) is invariant through the scale change. A
|
||||
// top-anchored zoom would move that coordinate by m*(1-ratio) display
|
||||
// lines — for a center 400dp below the region top at ×1.5 that is ~59
|
||||
// display lines off, which is exactly what this test rejects.
|
||||
func TestHandleFontPinch_CenterAnchor(t *testing.T) {
|
||||
TheLogic = nil
|
||||
TheState = NewState()
|
||||
defer func() {
|
||||
TheState.appFontScale = 1.0
|
||||
TheState.ScrollOffset = 0
|
||||
TheState.EditorRegion = ui.Region{}
|
||||
}()
|
||||
TheState.EditorRegion = ui.Region{Y: 100, H: 800}
|
||||
|
||||
lh := float64(EffectiveLineHeight())
|
||||
TheState.ScrollOffset = ui.Dp(100 * lh)
|
||||
// Center 400dp below the region top.
|
||||
const m = 400.0
|
||||
wantU := (float64(TheState.ScrollOffset) + m) / lh // content coord under the center
|
||||
|
||||
HandleFontPinch(ui.FontPinchEvent{Scale: 1.5, Center: ui.Point{Y: 100 + m}})
|
||||
|
||||
lhNew := float64(EffectiveLineHeight())
|
||||
gotU := (float64(TheState.ScrollOffset) + m) / lhNew
|
||||
// Tolerance is float32-scale + ui.Dp rounding accumulated over two
|
||||
// conversions — still 4 orders of magnitude smaller than the ~59-line
|
||||
// drift a top-anchored zoom would produce here.
|
||||
if math.Abs(gotU-wantU) > 1e-4 {
|
||||
t.Errorf("content under center: u=%v want %v (center must stay fixed)", gotU, wantU)
|
||||
}
|
||||
// Top-anchor sanity: the TOP moved (that is the point of center anchoring).
|
||||
if float64(TheState.ScrollOffset) == 100*lh*1.5 {
|
||||
t.Errorf("scroll=%v equals the top-anchored value; the center was not the anchor", TheState.ScrollOffset)
|
||||
}
|
||||
}
|
||||
|
||||
// With word wrap, the anchor must be the LOGICAL line under the center, not
|
||||
// the display line: when the font change re-wraps the lines above the
|
||||
// anchor (their fragment counts grow), the display-line index under the
|
||||
// center changes, but the same logical line / fragment / sub-line must stay
|
||||
// under it. applyFontPin is what the logic re-runs as rewrap corrections
|
||||
// land (the font-pin).
|
||||
func TestFontPin_SurvivesRewrap(t *testing.T) {
|
||||
TheLogic = nil
|
||||
TheState = NewState()
|
||||
defer func() {
|
||||
TheState.appFontScale = 1.0
|
||||
TheState.ScrollOffset = 0
|
||||
TheState.Editor.ChunkedBuffer = nil
|
||||
}()
|
||||
|
||||
// 1000 logical lines, each wrapped into 2 fragments at the current size.
|
||||
w := NewWrapIndex(1000)
|
||||
for i := 0; i < 1000; i++ {
|
||||
w.Set(i, 2)
|
||||
}
|
||||
TheState.Editor.ChunkedBuffer = &ChunkedBuffer{WrapIndex: w}
|
||||
|
||||
lh := float64(EffectiveLineHeight())
|
||||
const m = 200.0 // region top (EditorRegion zero) + 200dp
|
||||
// Anchor: logical line 60, its 1st fragment, 0.5 down it.
|
||||
TheState.ScrollOffset = ui.Dp(float64(w.VisualsBefore(60))*lh + 0.5*lh - m)
|
||||
|
||||
line, frag, sub, ok := TheState.captureFontPin(m)
|
||||
if !ok || line != 60 || frag != 0 || math.Abs(sub-0.5) > 1e-5 {
|
||||
t.Fatalf("capture: line=%d frag=%d sub=%v ok=%v, want line 60 frag 0 sub 0.5", line, frag, sub, ok)
|
||||
}
|
||||
|
||||
// The font grows ×1.5 (the line under the fingers keeps its 2 fragments
|
||||
// for now): the same point stays under m.
|
||||
TheState.appFontScale = 1.5
|
||||
TheState.applyFontPin(line, frag, sub, m)
|
||||
lhNew := float64(EffectiveLineHeight())
|
||||
u := (float64(TheState.ScrollOffset) + m) / lhNew
|
||||
if got := int(w.LineForVisual(int32(u))); got != 60 {
|
||||
t.Errorf("after scale: line under center=%d want 60", got)
|
||||
}
|
||||
|
||||
// Rewrap lands: every line now wraps into 3 fragments. The display-line
|
||||
// index under the center moves from 120.5 to 180.5, but re-applying the
|
||||
// pin must keep logical line 60 (fragment 0, sub 0.5) under the center.
|
||||
for i := 0; i < 1000; i++ {
|
||||
w.Set(i, 3)
|
||||
}
|
||||
TheState.applyFontPin(line, frag, sub, m)
|
||||
u = (float64(TheState.ScrollOffset) + m) / lhNew
|
||||
if got := int(w.LineForVisual(int32(u))); got != 60 {
|
||||
t.Errorf("after rewrap: line under center=%d want 60 (display-line anchoring would fail here)", got)
|
||||
}
|
||||
// And a display-line anchor (what NOT to do) would be off: at the new
|
||||
// counts, display line 120.5 is logical line 40, not 60.
|
||||
if got := int(w.LineForVisual(120)); got == 60 {
|
||||
t.Errorf("test is stale: display line 120 unexpectedly maps to line 60")
|
||||
}
|
||||
}
|
||||
|
||||
// fabLayout builds a GlyphLayout the way the shaper produces one: glyphsPerFrag
|
||||
// glyphs per fragment, baseline of fragment f at (f+0.8)*lh (ascent < lh, the
|
||||
// shaper's convention), 20dp advance each.
|
||||
func fabLayout(lh float64, fragments, glyphsPerFrag int, byte0 int) ui.GlyphLayout {
|
||||
gl := ui.GlyphLayout{LineHeight: ui.Dp(lh)}
|
||||
for f := 0; f < fragments; f++ {
|
||||
for g := 0; g < glyphsPerFrag; g++ {
|
||||
gl.ByteOffsets = append(gl.ByteOffsets, byte0+f*glyphsPerFrag+g)
|
||||
gl.X = append(gl.X, ui.Dp(10+20*float64(g)))
|
||||
gl.Y = append(gl.Y, ui.Dp((float64(f)+0.8)*lh))
|
||||
gl.Advance = append(gl.Advance, ui.Dp(20))
|
||||
}
|
||||
}
|
||||
return gl
|
||||
}
|
||||
|
||||
func TestGlyphAtLocalPoint(t *testing.T) {
|
||||
gl := fabLayout(16.8, 2, 4, 0) // bytes 0-3 on fragment 0, 4-7 on fragment 1
|
||||
if i, ok := glyphAtLocalPoint(gl, 50, 25); !ok || i != 6 {
|
||||
t.Errorf("(50,25): i=%d ok=%v, want glyph 6 (byte 6, X=50 on fragment 1)", i, ok)
|
||||
}
|
||||
if _, ok := glyphAtLocalPoint(gl, 5, 25); ok {
|
||||
t.Error("(5,25): left margin has no glyph, want not-ok")
|
||||
}
|
||||
if i, ok := glyphAtLocalPoint(gl, 1000, 25); !ok || i != 7 {
|
||||
t.Errorf("(1000,25): past the line end pins its last glyph, got i=%d ok=%v want 7", i, ok)
|
||||
}
|
||||
if _, ok := glyphAtLocalPoint(ui.GlyphLayout{}, 50, 25); ok {
|
||||
t.Error("empty layout: want not-ok")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCaptureContentPin(t *testing.T) {
|
||||
TheLogic = nil
|
||||
TheState = NewState()
|
||||
defer func() {
|
||||
TheState.appFontScale = 1.0
|
||||
TheState.ScrollOffset = 0
|
||||
TheState.Editor.GlyphLayout = ui.GlyphLayout{}
|
||||
}()
|
||||
TheState.Editor.GlyphLayout = fabLayout(16.8, 2, 4, 0)
|
||||
TheState.ScrollOffset = 0
|
||||
|
||||
// Point at (x=50, y=25): fragment 1 (baseline 30.24), the glyph at X=50.
|
||||
pin := TheState.captureContentPin(50, 25)
|
||||
if !pin.HaveGlyph || pin.Byte != 6 {
|
||||
t.Fatalf("pin: byte=%d haveGlyph=%v, want byte 6", pin.Byte, pin.HaveGlyph)
|
||||
}
|
||||
if math.Abs(pin.Dy-(25-30.24)) > 1e-5 { // ui.Dp stores float32-precision values
|
||||
t.Errorf("dy=%v want %v (offset from the glyph baseline)", pin.Dy, 25-30.24)
|
||||
}
|
||||
// Fallback anchor (no WrapIndex): line 1, sub = 25/16.8 - 1 ~ 0.488.
|
||||
if pin.Line != 1 || pin.Frag != 0 || math.Abs(pin.Sub-(25/16.8-1)) > 1e-6 {
|
||||
t.Errorf("fallback: line=%d frag=%d sub=%v, want line 1 frag 0 sub %v", pin.Line, pin.Frag, pin.Sub, 25/16.8-1)
|
||||
}
|
||||
}
|
||||
|
||||
// The REFINEMENT is what makes the content point stay fixed across a rewrap:
|
||||
// the pinned byte moves to a different fragment in the new layout, and the
|
||||
// refined offset places that byte's baseline (plus the captured dy) exactly
|
||||
// at the pinch center — where the (line, fragment) anchor would leave a
|
||||
// different character there.
|
||||
func TestRefineContentPin_RewrapKeepsContentPoint(t *testing.T) {
|
||||
const m = 25.0 // region-relative pinch center
|
||||
const dy = 25.0 - 30.24 // from TestCaptureContentPin's point (above baseline)
|
||||
|
||||
// OLD layout (font 1.0x): byte 6 on fragment 1. (Capture would have
|
||||
// returned Byte=6, Dy=dy.)
|
||||
_ = fabLayout(16.8, 2, 4, 0)
|
||||
|
||||
// Font grows to 1.5x: the line re-wraps to 3 fragments x 3 glyphs; byte 6
|
||||
// lands on fragment 2 (a DIFFERENT fragment than the old fragment 1 it
|
||||
// was captured on... old frag index was 1, new is 2).
|
||||
glNew := fabLayout(25.2, 3, 3, 0)
|
||||
// The new layout is shaped for the window starting at document byte 0,
|
||||
// whose first visual line is visual line 3 of the document (vk=3):
|
||||
// the window top sits at content 3*25.2.
|
||||
vk := 3
|
||||
|
||||
off, ok := refineContentPin(glNew, vk, 0, dy, m, 6)
|
||||
if !ok {
|
||||
t.Fatal("refine: want ok")
|
||||
}
|
||||
// The pinned point (byte 6's baseline + dy) must sit exactly at m.
|
||||
contentY := float64(off) + m // content coordinate of the region-relative m
|
||||
// byte 6's baseline: window y = (2+0.8)*25.2, window top at content vk*lh.
|
||||
baselineContent := float64(vk)*float64(glNew.LineHeight) + float64(glNew.Y[6])
|
||||
if math.Abs(baselineContent+dy-contentY) > 1e-5 { // float32-precision layout Y
|
||||
t.Errorf("pinned point at content %v, want %v (baseline %v + dy)", contentY, baselineContent+dy, baselineContent)
|
||||
}
|
||||
// (A (line, fragment) anchor would have failed here: the byte captured on
|
||||
// the old fragment 1 sits on the NEW fragment 2, a full line further down.
|
||||
// Pinning the old fragment index would leave a different character at the
|
||||
// center — exactly the drift the content pin exists to remove.)
|
||||
}
|
||||
|
||||
// Steady state: a layout shaped at offset S with the pinned point at the
|
||||
// center must refine back to exactly S (the fixed point — no drift, no
|
||||
// oscillation while the pin is armed).
|
||||
func TestRefineContentPin_FixedPoint(t *testing.T) {
|
||||
const m = 25.0
|
||||
lh := ui.Dp(25.2)
|
||||
S := ui.Dp(100)
|
||||
vk, r := scrollDecompose(S, lh) // window's first visual line = v0 (no wrap)
|
||||
const dy = 3.0
|
||||
// Place byte 4 at window y = m + r - dy so the point sits at m when the
|
||||
// window (top at content vk*lh) is shaped at S.
|
||||
wantY := m + r - dy
|
||||
gl := ui.GlyphLayout{LineHeight: lh}
|
||||
for b := 0; b < 6; b++ {
|
||||
gl.ByteOffsets = append(gl.ByteOffsets, b)
|
||||
gl.X = append(gl.X, ui.Dp(10+20*float64(b%3)))
|
||||
gl.Y = append(gl.Y, ui.Dp(wantY)) // one line's worth for this test
|
||||
gl.Advance = append(gl.Advance, ui.Dp(20))
|
||||
}
|
||||
off, ok := refineContentPin(gl, int(vk), 0, dy, m, 4)
|
||||
if !ok {
|
||||
t.Fatal("refine: want ok")
|
||||
}
|
||||
if math.Abs(float64(off)-float64(S)) > 1e-9 {
|
||||
t.Errorf("fixed point: refine(S)=%v want S=%v (no drift)", off, S)
|
||||
}
|
||||
}
|
||||
|
||||
// Pinch-out clamp of the pinned fragment: a line that re-wraps to FEWER
|
||||
// fragments must not pin a fragment that no longer exists.
|
||||
func TestFontPin_FragmentClampedOnPinchOut(t *testing.T) {
|
||||
TheLogic = nil
|
||||
TheState = NewState()
|
||||
defer func() {
|
||||
TheState.appFontScale = 1.0
|
||||
TheState.ScrollOffset = 0
|
||||
TheState.Editor.ChunkedBuffer = nil
|
||||
}()
|
||||
w := NewWrapIndex(100)
|
||||
for i := 0; i < 100; i++ {
|
||||
w.Set(i, 3)
|
||||
}
|
||||
TheState.Editor.ChunkedBuffer = &ChunkedBuffer{WrapIndex: w}
|
||||
|
||||
// Pin line 5, fragment 2 (the third fragment).
|
||||
TheState.appFontScale = 1.0
|
||||
const m = 100.0
|
||||
TheState.applyFontPin(5, 2, 0.25, m)
|
||||
|
||||
// Pinch out: the line now wraps into 2 fragments.
|
||||
for i := 0; i < 100; i++ {
|
||||
w.Set(i, 2)
|
||||
}
|
||||
TheState.appFontScale = 0.8
|
||||
TheState.applyFontPin(5, 2, 0.25, m) // frag 2 must clamp to 1
|
||||
lh := float64(EffectiveLineHeight())
|
||||
u := (float64(TheState.ScrollOffset) + m) / lh
|
||||
// The anchor is now line 5, its LAST fragment (index 1), 0.25 down it.
|
||||
wantU := float64(w.VisualsBefore(5)+1) + 0.25
|
||||
if math.Abs(u-wantU) > 1e-6 {
|
||||
t.Errorf("anchor u=%v want %v (last remaining fragment of line 5)", u, wantU)
|
||||
}
|
||||
}
|
||||
@@ -55,6 +55,16 @@ type FindState struct {
|
||||
// and the logic storing it (a round-trip away), wiping the input while
|
||||
// typing.
|
||||
ClearSeq int
|
||||
|
||||
// --- Relaunch restoration (spec §7, see session.go) ---
|
||||
// Restoring is set while a restored session's find scan is in flight:
|
||||
// its first result selects the restored current match (RestoreMatch) but
|
||||
// does NOT scroll — the viewport stays where the restored scroll offset
|
||||
// put it. applySearchResult consumes both; findReset drops them.
|
||||
Restoring bool
|
||||
// RestoreMatch is the byte offset of the pre-launch current find match
|
||||
// (the snapshot's FindCurByte); -1 = none.
|
||||
RestoreMatch int
|
||||
}
|
||||
|
||||
// ToggleFind opens the find bar (or closes it when open). It is the tap
|
||||
@@ -205,19 +215,42 @@ func (e *EditorState) applySearchResult(res pool.Result) {
|
||||
f.Cur = i
|
||||
}
|
||||
}
|
||||
// Relaunch restore: select the pre-launch current match by byte
|
||||
// offset (spec §7) — the one containing the byte, else the first
|
||||
// match after it, else the last (its text may have been edited
|
||||
// away, so a fallback is always defined).
|
||||
if f.Cur < 0 && f.RestoreMatch >= 0 {
|
||||
i := sort.Search(len(f.Matches), func(i int) bool {
|
||||
return f.Matches[i][0] > f.RestoreMatch
|
||||
})
|
||||
switch {
|
||||
case i > 0 && f.Matches[i-1][1] > f.RestoreMatch:
|
||||
f.Cur = i - 1
|
||||
case i < len(f.Matches):
|
||||
f.Cur = i
|
||||
default:
|
||||
f.Cur = len(f.Matches) - 1
|
||||
}
|
||||
}
|
||||
if f.Cur < 0 {
|
||||
f.Cur = 0
|
||||
}
|
||||
// The first discovery of matches (matches went 0 -> N) selects and
|
||||
// scrolls the view to the current match; while the user keeps
|
||||
// typing, results update in place and the view moves only on
|
||||
// explicit next/prev.
|
||||
// The first discovery of matches (matches went 0 -> N) selects the
|
||||
// current match and scrolls the view to it — except during a
|
||||
// relaunch restore (Restoring), where the viewport must stay where
|
||||
// the restored scroll offset put it. While the user keeps typing,
|
||||
// results update in place and the view moves only on explicit
|
||||
// next/prev.
|
||||
if wasEmpty {
|
||||
SetSelection(f.Matches[f.Cur][0], f.Matches[f.Cur][1])
|
||||
if !f.Restoring {
|
||||
scrollToFindMatch(f.Matches[f.Cur][0])
|
||||
}
|
||||
}
|
||||
}
|
||||
f.Restoring = false
|
||||
f.RestoreMatch = -1
|
||||
}
|
||||
|
||||
// findStep selects the next (dir>0) or previous (dir<0) match, wrapping
|
||||
// around. With no current match yet, "next" goes to the first match at or
|
||||
@@ -322,6 +355,9 @@ func scrollToFindMatch(absByte int) {
|
||||
e.Find.SettleByte = -1
|
||||
return
|
||||
}
|
||||
if TheLogic != nil {
|
||||
TheLogic.releaseRestorePin() // search takes over the viewport
|
||||
}
|
||||
TheState.ScrollOffset = target
|
||||
if e.Find.Visible {
|
||||
e.Find.SettleByte = absByte
|
||||
|
||||
@@ -72,7 +72,7 @@ func TestSelectionMenu_FollowsTextAcrossScroll(t *testing.T) {
|
||||
// The menu sits ABOVE the anchor's line: lineTop(line 5) = reg.Y + 5*lh
|
||||
// (reg.Y = 32: the top bar is margin-free, the anchor's visual line
|
||||
// index is 5).
|
||||
wantY0 := ui.Dp(32) + ui.Dp(5*lh) - menuH - 8
|
||||
wantY0 := TopBarHeight + ui.Dp(5*lh) - menuH - 8
|
||||
if !dpeq(y0, wantY0) {
|
||||
t.Fatalf("initial menu Y = %v, want %v (above the anchor line)", y0, wantY0)
|
||||
}
|
||||
@@ -109,19 +109,20 @@ func TestSelectionMenu_ClampsWhenTextLeavesView(t *testing.T) {
|
||||
}
|
||||
yTop := e.MenuRect.Y // above line 5
|
||||
|
||||
// Scroll 10 lines: line 5 is 5 lines ABOVE the viewport top. Both ends
|
||||
// Scroll 11 lines: line 5 is 6 lines ABOVE the viewport top. Both ends
|
||||
// of the selection stay inside the shaped window (small file = whole
|
||||
// buffer) so the menu tracks: the preferred above-placement overflows
|
||||
// (the line is off-screen top) and the menu flips below the line, which
|
||||
// lands just under the window top (lineTop + lh + handleDropDp + 8).
|
||||
TheState.ScrollOffset = ui.Dp(10 * lh)
|
||||
// overflows the window top too (lineTop + lh + handleDropDp + 8 = 0 < 8)
|
||||
// and clamps to it.
|
||||
TheState.ScrollOffset = ui.Dp(11 * lh)
|
||||
EditorLayout(ui.Dp(1000), ui.Dp(2000), false)
|
||||
|
||||
if !e.MenuVisible {
|
||||
t.Fatal("menu hidden, but the selection is still in the shaped window")
|
||||
}
|
||||
// Below-placement: lineTop(32) + 5*lh - 10*lh + lh + handleDropDp + 8
|
||||
// = 6.8 < 8, so the menu clamps to the window top.
|
||||
// Below-placement: lineTop(TopBarHeight) + 5*lh - 11*lh + lh + handleDropDp + 8
|
||||
// = 0 < 8, so the menu clamps to the window top.
|
||||
if got := e.MenuRect.Y; !dpeq(got, 8) {
|
||||
t.Fatalf("menu Y = %v, want 8 (below-placement clamped to the window top)", got)
|
||||
}
|
||||
@@ -171,9 +172,10 @@ func TestSelectionMenu_FlipsBelowOnFirstLine(t *testing.T) {
|
||||
if !e.MenuVisible {
|
||||
t.Fatal("menu not shown")
|
||||
}
|
||||
// Line 0 top = reg.Y = 32; above would be 32-52-8 = -28 < 8, so the menu
|
||||
// flips below the selection HANDLES: line bottom + handleDropDp + 8.
|
||||
wantY := ui.Dp(32) + EffectiveLineHeight() + ui.Dp(handleDropDp) + 8
|
||||
// Line 0 top = reg.Y = TopBarHeight; above would be TopBarHeight-52-8 =
|
||||
// -18 < 8, so the menu flips below the selection HANDLES: line bottom +
|
||||
// handleDropDp + 8.
|
||||
wantY := TopBarHeight + EffectiveLineHeight() + ui.Dp(handleDropDp) + 8
|
||||
if !dpeq(e.MenuRect.Y, wantY) {
|
||||
t.Fatalf("menu Y = %v, want %v (flipped below the first line, clear of the handles)", e.MenuRect.Y, wantY)
|
||||
}
|
||||
@@ -220,7 +222,7 @@ func TestSelectionMenu_AnchorsToStableEnd(t *testing.T) {
|
||||
t.Fatal("menu not shown")
|
||||
}
|
||||
// Above line 3 (the stable start), NOT above line 5 (the moving end).
|
||||
wantY := ui.Dp(32) + ui.Dp(3*lh) - menuH - 8
|
||||
wantY := TopBarHeight + ui.Dp(3*lh) - menuH - 8
|
||||
if !dpeq(e.MenuRect.Y, wantY) {
|
||||
t.Fatalf("menu Y = %v, want %v (above the selection's TOP line)", e.MenuRect.Y, wantY)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,575 @@
|
||||
// State restoration on relaunch (spec §7).
|
||||
//
|
||||
// The restorable state is a small snapshot (SessionState) the logic
|
||||
// goroutine takes on demand: which page the app was on, and, on the editor
|
||||
// page, the last opened file, the cursor byte offset,
|
||||
// the editor scroll offset, the live selection, and the find bar (query,
|
||||
// open/closed, current match). The search RESULTS themselves are not part
|
||||
// of the snapshot: on relaunch they are regenerated by re-scanning the
|
||||
// restored query, and the current match is re-selected by byte offset
|
||||
// (FindState.Restoring/RestoreMatch), without re-scrolling the restored
|
||||
// viewport.
|
||||
//
|
||||
// Ownership (architecture.md §1): the logic goroutine owns the snapshot
|
||||
// content. The cmd layer (cmd/pad/main.go) owns the tiny JSON file that
|
||||
// stores it and registers the writer (Logic.SetSessionSaver); the logic
|
||||
// calls it rate-limited from emitFrame and unconditionally at Shutdown, so
|
||||
// the file is current even if the process is killed shortly after a change.
|
||||
// On startup the cmd layer reads the file and hands it to Logic.BeginRestore
|
||||
// (before Run, single-threaded), which re-opens the file and shows the
|
||||
// editor immediately; the cursor/selection/find state land as the file's
|
||||
// stat and content arrive (handleWorkerResult). A restore whose file is
|
||||
// gone falls back to the browser page.
|
||||
|
||||
package editor
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// sessionSaveInterval rate-limits the periodic session save. The file is
|
||||
// tiny (a few hundred bytes), so a few writes a second at most is
|
||||
// negligible. The window bounds how stale the persisted state can be if
|
||||
// the process is killed (recents-wipe) right after a change: at 1s, a
|
||||
// kill less than a second after the last edit lost the cursor, selection
|
||||
// and final scroll. See saveSessionIfChanged for the urgent path.
|
||||
const sessionSaveInterval = 250 * time.Millisecond
|
||||
|
||||
// SessionState is the relaunch snapshot (see the file doc above). All
|
||||
// positions are absolute file byte offsets; Scroll is in Dp. It is a plain
|
||||
// comparable value (used with == for change detection).
|
||||
//
|
||||
// Scroll is backed by ScrollLine: the pixel offset lives in VISUAL-line
|
||||
// space (a wrapped line occupies several visual lines, and the scroll-to-
|
||||
// line mapping runs through the WrapIndex), but on relaunch the counts of
|
||||
// every line above the restored viewport are still the estimate (1) until
|
||||
// the line is shaped, and lines above the visible window are never shaped.
|
||||
// Mapping the saved offset through that fresh index would land a logical
|
||||
// line DEEPER by all the wrapped continuations above the viewport, with no
|
||||
// self-correction. The logical line at the viewport top and the sub-line
|
||||
// remainder are wrap-independent, so they are the restorable unit; Scroll
|
||||
// is kept as the pre-line-coordinate value (legacy session files).
|
||||
type SessionState struct {
|
||||
File string // last opened file ("" = nothing to restore)
|
||||
Cursor int // cursor byte offset
|
||||
Scroll float64 // editor scroll offset, Dp
|
||||
ScrollLine int // logical line at the viewport top (-1 = unknown)
|
||||
ScrollSub float64 // Scroll's sub-line remainder as a FRACTION of the line height (0 <= f < 1); font-independent. Snapshots written before the pinch feature stored Dp instead; restore converts values > 1.
|
||||
SelStart int // selection start byte (-1 = no selection)
|
||||
SelEnd int // selection end byte, exclusive
|
||||
FindQuery string // find bar query ("" = none)
|
||||
FindVisible bool // find bar was open
|
||||
FindCurByte int // start byte of the current find match (-1 = none)
|
||||
// AppFontScale is the app-local pinch font scale (0 = default 1.0).
|
||||
AppFontScale float64
|
||||
// InBrowser records that the app was sitting on the BROWSER page when
|
||||
// the snapshot was taken: the relaunch must land back in the browser at
|
||||
// BrowserPath instead of re-opening the last file in the editor. The
|
||||
// flag (not "File == "") disambiguates from pre-flag session files, whose
|
||||
// absence of the key unmarshals to false and restores the editor as
|
||||
// before. When true, File is "" and the editor-side fields are stale.
|
||||
InBrowser bool
|
||||
BrowserPath string
|
||||
}
|
||||
|
||||
// SetSessionSaver registers the callback that persists snapshots (the cmd
|
||||
// layer's JSON file writer). It is invoked from the logic goroutine:
|
||||
// rate-limited from emitFrame, and unconditionally at Shutdown. The
|
||||
// callback must be fast (a tiny file write).
|
||||
func (l *Logic) SetSessionSaver(f func(SessionState)) {
|
||||
l.sessionSaver = f
|
||||
}
|
||||
|
||||
// SnapshotSession captures the current relaunch snapshot. The cursor is
|
||||
// clamped to the file length; the selection and current find match are -1/
|
||||
// -1 when inactive. Must be called on the logic goroutine (or after it has
|
||||
// fully exited, as Shutdown does).
|
||||
func (l *Logic) SnapshotSession() SessionState {
|
||||
s := l.state
|
||||
e := &s.Editor
|
||||
cur := e.CursorPosition
|
||||
if cb := e.ChunkedBuffer; cb != nil {
|
||||
if fl := int(cb.FileLen()); cur > fl {
|
||||
cur = fl
|
||||
}
|
||||
}
|
||||
f := &e.Find
|
||||
curByte := -1
|
||||
if f.Cur >= 0 && f.Cur < len(f.Matches) {
|
||||
curByte = f.Matches[f.Cur][0]
|
||||
}
|
||||
// Persist the scroll in wrap-independent coordinates (see the
|
||||
// SessionState doc). The derivation mirrors visibleByteRangePrecise
|
||||
// (scrollDecompose, then LineForVisual against the CURRENT index), so
|
||||
// the line is exactly the one the layout is showing at this offset.
|
||||
scrollLine := -1
|
||||
var scrollSub float64
|
||||
if cb := e.ChunkedBuffer; cb != nil && !e.TooLarge {
|
||||
lh := EffectiveLineHeight()
|
||||
if gl := e.GlyphLayout; gl.LineHeight > 0 {
|
||||
lh = gl.LineHeight
|
||||
}
|
||||
v0, r0 := scrollDecompose(s.ScrollOffset, lh)
|
||||
scrollLine = int(v0)
|
||||
if w := cb.WrapIndex; w != nil {
|
||||
scrollLine = w.LineForVisual(int32(v0))
|
||||
}
|
||||
// Store the sub-line remainder as a fraction of the line height so
|
||||
// it stays valid if the font scale changes between save and restore
|
||||
// (r0 < lh always, so the fraction is < 1 — that is also the legacy
|
||||
// Dp discriminator used on restore).
|
||||
scrollSub = r0 / float64(lh)
|
||||
}
|
||||
inBrowser := l.state.page == BrowserPage
|
||||
file := e.Filename
|
||||
if inBrowser {
|
||||
// Nothing to restore in the editor: leaving the last file in File
|
||||
// would let a reader that ignores InBrowser re-open it, and the
|
||||
// editor->browser transition is then also an urgent save (File
|
||||
// change), so the "back in the browser" state hits disk immediately
|
||||
// instead of waiting out the rate limit.
|
||||
file = ""
|
||||
}
|
||||
return SessionState{
|
||||
File: file,
|
||||
InBrowser: inBrowser,
|
||||
BrowserPath: l.state.Browser.CurrentPath,
|
||||
Cursor: cur,
|
||||
Scroll: float64(s.ScrollOffset),
|
||||
ScrollLine: scrollLine,
|
||||
ScrollSub: scrollSub,
|
||||
SelStart: e.SelectionStart,
|
||||
SelEnd: e.SelectionEnd,
|
||||
FindQuery: f.Query,
|
||||
FindVisible: f.Visible,
|
||||
FindCurByte: curByte,
|
||||
AppFontScale: float64(s.appFontScale),
|
||||
}
|
||||
}
|
||||
|
||||
// saveSessionIfChanged persists the snapshot when it differs from the last
|
||||
// saved one and the rate limit has elapsed. Must be called on the logic
|
||||
// goroutine (it is called from emitFrame).
|
||||
//
|
||||
// While a restore is still pending (l.session set, not yet landed or
|
||||
// abandoned) saves are suppressed: the pre-land snapshot has zeroed
|
||||
// cursor/selection, and persisting it would clobber the positions being
|
||||
// restored if the process is killed during startup.
|
||||
func (l *Logic) saveSessionIfChanged() {
|
||||
if l.sessionSaver == nil {
|
||||
return
|
||||
}
|
||||
if l.session.File != "" {
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
s := l.SnapshotSession()
|
||||
if s == l.lastSession {
|
||||
return
|
||||
}
|
||||
// Save immediately when the file changes or a selection appears or
|
||||
// vanishes — low-frequency, high-value changes (the user just opened a
|
||||
// file or highlighted/cleared text). Everything else (scroll ticks,
|
||||
// cursor moves, selection-handle drags) is high-frequency and stays
|
||||
// rate-limited; a kill within the window then loses at most a fraction
|
||||
// of a second of motion, not the edit state.
|
||||
urgent := s.File != l.lastSession.File ||
|
||||
(s.SelStart >= 0) != (l.lastSession.SelStart >= 0)
|
||||
if !urgent && now.Sub(l.lastSessionSave) < sessionSaveInterval {
|
||||
return
|
||||
}
|
||||
l.writeSession(s, now)
|
||||
}
|
||||
|
||||
// flushSessionSave persists the current snapshot now, bypassing the rate
|
||||
// limit (still honoring the restore-pending suppression: a pre-land
|
||||
// snapshot would clobber the positions being restored). Must be called on
|
||||
// the logic goroutine.
|
||||
func (l *Logic) flushSessionSave() {
|
||||
if l.sessionSaver == nil || l.session.File != "" {
|
||||
return
|
||||
}
|
||||
s := l.SnapshotSession()
|
||||
if s == l.lastSession {
|
||||
return
|
||||
}
|
||||
l.writeSession(s, time.Now())
|
||||
}
|
||||
|
||||
// writeSession is the shared persist tail: hand the snapshot to the cmd
|
||||
// layer's saver and record it as the new baseline. Must be called on the
|
||||
// logic goroutine.
|
||||
func (l *Logic) writeSession(s SessionState, now time.Time) {
|
||||
l.sessionSaver(s)
|
||||
l.lastSession = s
|
||||
l.lastSessionSave = now
|
||||
}
|
||||
|
||||
// FlushSession requests an immediate session persist from any goroutine.
|
||||
// The Android activity's onStop (recents-wipe, app switch) calls it via
|
||||
// JNI: the OS gives no user-space hook for the subsequent process kill,
|
||||
// so onStop is the last reliable moment to flush. Buffered delivery means
|
||||
// the caller never blocks; if an earlier flush is still queued, one
|
||||
// snapshot is coalesced into it (the state is the same frame's anyway).
|
||||
func (l *Logic) FlushSession() {
|
||||
select {
|
||||
case l.flushSession <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// BeginRestore prepares the state for relaunch restoration: the last file
|
||||
// is re-opened and the editor page shown immediately. The cursor,
|
||||
// selection, find state and scroll land as the file's stat/content arrive
|
||||
// (handleWorkerResult); the scroll offset is applied now and clamped by
|
||||
// EditorLayout once the line index sets the real MaxScroll. Must be called
|
||||
// after NewLogic and before Run (the cmd layer does both before starting
|
||||
// the logic goroutine), so it touches state single-threaded.
|
||||
func (l *Logic) BeginRestore(s SessionState) {
|
||||
if s.File == "" {
|
||||
return
|
||||
}
|
||||
// The zero-value ScrollLine (0) is ambiguous: a genuine top-of-file
|
||||
// snapshot always has Scroll < lineHeight (no line above line 0
|
||||
// contributes visual lines), so line 0 with a deep offset is an unset
|
||||
// field (an in-process caller or test built the snapshot without it) —
|
||||
// fall back to the pixel offset rather than snapping to the top.
|
||||
if s.ScrollLine == 0 && s.Scroll >= 2*float64(EffectiveLineHeight()) {
|
||||
s.ScrollLine = -1
|
||||
}
|
||||
l.session = s
|
||||
e := &l.state.Editor
|
||||
e.Filename = s.File
|
||||
e.CursorPosition = 0 // the restored cursor lands with the content
|
||||
e.TooLarge = false
|
||||
e.TooLargeSize = 0
|
||||
e.SelectionAnchor = -1
|
||||
e.SelectionStart = -1
|
||||
e.SelectionEnd = -1
|
||||
// Fresh per-file find state; the snapshot's query and visibility land
|
||||
// with the stat (the file must exist first).
|
||||
e.findReset()
|
||||
// The scroll offset is armed, not applied: it lands in the emitFrame
|
||||
// hook, once the layout pass has a trustworthy viewport (see the Logic
|
||||
// struct).
|
||||
l.restoreScroll = ui.Dp(s.Scroll)
|
||||
l.restoreScrollArmed = s.Scroll > 0
|
||||
if l.restoreScrollArmed {
|
||||
l.restoreArmedAt = time.Now()
|
||||
}
|
||||
// The app-local font scale lands immediately, BEFORE any layout: the
|
||||
// restore's line-based offset math reads it through EffectiveLineHeight.
|
||||
if as := s.AppFontScale; as > 0 {
|
||||
if as < MinAppFontScale {
|
||||
as = MinAppFontScale
|
||||
}
|
||||
if as > MaxAppFontScale {
|
||||
as = MaxAppFontScale
|
||||
}
|
||||
l.state.appFontScale = float32(as)
|
||||
}
|
||||
l.state.page = EditorPage
|
||||
l.state.FocusedElementID = "editor_text"
|
||||
l.state.justOpenedAt = time.Now()
|
||||
l.emitFrame() // show the editor page now; the content loads async
|
||||
}
|
||||
|
||||
// BeginBrowserRestore makes a relaunch land in the browser at dirPath
|
||||
// instead of the startup directory (see SessionState.InBrowser). It is
|
||||
// synchronous: the directory is dispatched for its index by Run, which reads
|
||||
// CurrentPath, so it must be called after NewLogic and before Run (the cmd
|
||||
// layer does both before starting the logic goroutine). An empty dirPath
|
||||
// keeps the startup directory.
|
||||
func (l *Logic) BeginBrowserRestore(dirPath string) {
|
||||
if dirPath != "" {
|
||||
l.state.Browser.CurrentPath = dirPath
|
||||
}
|
||||
}
|
||||
|
||||
// abortRestore ends a restore without landing it (the user opened another
|
||||
// file, the read failed, or the file is too large): clears the pending
|
||||
// snapshot so session saving resumes (see saveSessionIfChanged). The editor
|
||||
// state itself is left alone. Must be called on the logic goroutine.
|
||||
func (l *Logic) abortRestore() {
|
||||
l.restoreFile = ""
|
||||
l.restoreScrollArmed = false
|
||||
l.restoreContentLanded = false
|
||||
l.restoreScrollLine = -1
|
||||
l.session = SessionState{}
|
||||
}
|
||||
|
||||
// maybeApplyRestoreScroll lands the armed restore scroll offset once the
|
||||
// viewport is trustworthy, clamped to the current MaxScroll (the layout
|
||||
// clamps again if the line index shrinks it later). It is called from
|
||||
// emitFrame AFTER the layout pass, because that pass is where MaxScroll is
|
||||
// computed for the current viewport — applying the offset before it (or in a
|
||||
// pass with an unknown viewport) would hit the one-way clamp and lose the
|
||||
// offset. On device the first ScaleEvent can precede the size ConfigEvent
|
||||
// (and both can precede the restored content), so the offset stays armed
|
||||
// until scale, size and editor content are all known; it is applied on the
|
||||
// first such emitFrame. restoreContentLanded (not FileLen, which the stat
|
||||
// result sets before the content arrives) marks that the buffer holds the
|
||||
// restored file; clearing restoreFile any earlier would make the read
|
||||
// result's path guard miss the just-loaded file. Returns true if it
|
||||
// applied. Must be called on the logic goroutine.
|
||||
func (l *Logic) maybeApplyRestoreScroll() bool {
|
||||
if !l.restoreScrollArmed {
|
||||
return false
|
||||
}
|
||||
if !l.scaleSeen {
|
||||
return false
|
||||
}
|
||||
if l.state.PixelWidth <= 0 || l.state.scale <= 0 {
|
||||
return false
|
||||
}
|
||||
if l.state.Page() != EditorPage || !l.restoreContentLanded {
|
||||
return false
|
||||
}
|
||||
// The clamp below needs a trustworthy MaxScroll. For a chunked buffer it
|
||||
// is measured from the line index (or the large pre-index estimate) and
|
||||
// is valid as soon as the content has landed. For a string buffer it
|
||||
// derives from the shaper's last baseline, which is still 0 until the
|
||||
// first non-empty frame has shaped — applying now would clamp the
|
||||
// offset to 0 and the scroll position would be silently lost. Wait for
|
||||
// that frame (its feedback triggers the emitFrame that re-runs this);
|
||||
// a deadline covers content that never produces a baseline (empty or
|
||||
// newlines-only), which lands best-effort.
|
||||
if l.state.Editor.ChunkedBuffer == nil && l.state.LastLineY <= 0 &&
|
||||
time.Since(l.restoreArmedAt) < restoreScrollArmTimeout {
|
||||
return false
|
||||
}
|
||||
// Re-derive the offset from the persisted logical line (see the
|
||||
// SessionState doc): the snapshot's pixel offset mapped to its position
|
||||
// only while the saving session's WrapIndex was valid. On relaunch the
|
||||
// counts above the restored viewport are all the estimate until shaped,
|
||||
// and those lines never are (shaping covers the visible window only),
|
||||
// so the raw offset would land a line deeper by every wrapped
|
||||
// continuation above the viewport. The line-based offset lands on the
|
||||
// saved line regardless of wrap state.
|
||||
s := l.session
|
||||
if s.ScrollLine >= 0 {
|
||||
lh := EffectiveLineHeight()
|
||||
if gl := l.state.Editor.GlyphLayout; gl.LineHeight > 0 {
|
||||
lh = gl.LineHeight
|
||||
}
|
||||
// The offset for a logical line is V(L)*lh + sub under the CURRENT
|
||||
// WrapIndex (V(L) = L while the index is all estimates). Wrapping
|
||||
// below the line was already corrected before the offset could be
|
||||
// applied (pre-apply frames shaped the top-of-file window), so
|
||||
// mapping through V(L) lands on the line under either state.
|
||||
base := float64(s.ScrollLine)
|
||||
if cb := l.state.Editor.ChunkedBuffer; cb != nil && cb.WrapIndex != nil && s.ScrollLine < cb.WrapIndex.Len() {
|
||||
base = float64(cb.WrapIndex.VisualsBefore(s.ScrollLine))
|
||||
}
|
||||
sub := s.ScrollSub
|
||||
if sub > 1 {
|
||||
// Legacy snapshot: the sub-line remainder was stored in Dp, not
|
||||
// as a fraction. Convert with the current line height (the
|
||||
// error is a fraction of one sub-line line, at most).
|
||||
sub = sub / float64(lh)
|
||||
}
|
||||
l.restoreScroll = ui.Dp(base*float64(lh) + sub*float64(lh))
|
||||
// Arm the line-pin (see refreshRestorePin): wrap-count corrections
|
||||
// landing below this line would shift the mapping and drag the
|
||||
// viewport off the restored line while the index settles.
|
||||
l.restoreScrollLine = s.ScrollLine
|
||||
l.restoreScrollSub = sub // fraction of the line height
|
||||
l.restorePinDeadline = time.Now().Add(restorePinTimeout)
|
||||
} else {
|
||||
l.restoreScrollLine = -1
|
||||
}
|
||||
if l.restoreScroll > l.state.MaxScroll {
|
||||
l.restoreScroll = l.state.MaxScroll
|
||||
}
|
||||
l.state.ScrollOffset = l.restoreScroll
|
||||
l.restoreScrollArmed = false
|
||||
log.Printf("RESTORE scroll off=%.1f line=%d maxScroll=%.1f", l.restoreScroll, s.ScrollLine, l.state.MaxScroll)
|
||||
// The snapshot has fully landed (cursor/selection/find with the
|
||||
// content, scroll now): lift the save suppression.
|
||||
l.session = SessionState{}
|
||||
return true
|
||||
}
|
||||
|
||||
// restorePinTimeout bounds the line-pin: after this long the index around
|
||||
// the restored window has either settled or the user has moved on.
|
||||
const restorePinTimeout = 2 * time.Second
|
||||
|
||||
// restoreScrollArmTimeout bounds how long maybeApplyRestoreScroll may wait
|
||||
// for a trustworthy MaxScroll before landing the offset best-effort (see
|
||||
// the guard there). Shaping a real file completes in well under this; it
|
||||
// exists so degenerate content (empty, newlines-only) cannot leave the
|
||||
// restore armed forever.
|
||||
const restoreScrollArmTimeout = 2 * time.Second
|
||||
|
||||
// releaseRestorePin clears the restore line-pin (user or search took over
|
||||
// the viewport). Must be called on the logic goroutine.
|
||||
func (l *Logic) releaseRestorePin() {
|
||||
l.restoreScrollLine = -1
|
||||
}
|
||||
|
||||
// refreshRestorePin re-derives the scroll offset from the pinned line under
|
||||
// the CURRENT WrapIndex (V(L)*lh + sub, the same mapping the layout uses),
|
||||
// so wrap-count corrections landing below the pinned line cannot drag the
|
||||
// viewport off it. Releases the pin when the deadline passes. Must be
|
||||
// called on the logic goroutine, after a wrap correction has been applied.
|
||||
func (l *Logic) refreshRestorePin() {
|
||||
if l.restoreScrollLine < 0 {
|
||||
return
|
||||
}
|
||||
if time.Now().After(l.restorePinDeadline) {
|
||||
l.restoreScrollLine = -1
|
||||
return
|
||||
}
|
||||
cb := l.state.Editor.ChunkedBuffer
|
||||
if cb == nil || cb.WrapIndex == nil {
|
||||
return
|
||||
}
|
||||
w := cb.WrapIndex
|
||||
if l.restoreScrollLine >= w.Len() {
|
||||
return
|
||||
}
|
||||
lh := EffectiveLineHeight()
|
||||
if gl := l.state.Editor.GlyphLayout; gl.LineHeight > 0 {
|
||||
lh = gl.LineHeight
|
||||
}
|
||||
// No MaxScroll clamp here: the correction that triggered this refresh
|
||||
// just grew the index, so the layout's MaxScroll (computed before it) is
|
||||
// stale and would under-clamp the re-derived offset; the layout pass of
|
||||
// the emitted frame clamps to the fresh value. An edit shrinking the
|
||||
// file mid-pin is covered the same way.
|
||||
off := ui.Dp(float64(w.VisualsBefore(l.restoreScrollLine))*float64(lh) + l.restoreScrollSub*float64(lh))
|
||||
if off != l.state.ScrollOffset {
|
||||
l.state.ScrollOffset = off
|
||||
l.emitFrame()
|
||||
}
|
||||
}
|
||||
|
||||
// fontPinTimeout bounds the pinch font-pin: rewrap corrections keep landing
|
||||
// for a while after the last pinch frame (shaping lags the font change);
|
||||
// after this long the user has moved on and the pin stands down.
|
||||
const fontPinTimeout = 2 * time.Second
|
||||
|
||||
// setFontPin arms/updates the pinch font-pin from a pre-change capture
|
||||
// (see captureContentPin: the glyph under the pinch center + offset from its
|
||||
// baseline) and applies the IMMEDIATE anchor: the continuous content
|
||||
// coordinate under the center scaled by the font ratio. Exact until the
|
||||
// rewrap lands; the shaped-layout feedback (refreshFontPin) then snaps the
|
||||
// pinned character exactly onto the center. Must be called on the logic
|
||||
// goroutine.
|
||||
func (l *Logic) setFontPin(pin contentPin, m float64, ratio float64) {
|
||||
l.fontPin = pin
|
||||
l.fontPinM = m
|
||||
l.fontPinArmed = true
|
||||
l.fontPinDeadline = time.Now().Add(fontPinTimeout)
|
||||
s := l.state
|
||||
s.rescaleScrollAnchored(ratio, float64(s.EditorRegion.Y)+m)
|
||||
// No emitFrame here: the caller (input path or debug command) emits the
|
||||
// frame carrying the new scale and the rescaled offset.
|
||||
}
|
||||
|
||||
// refreshFontPin re-derives the scroll offset from a freshly shaped layout so
|
||||
// the pinned content point stays under the pinch center (see
|
||||
// refineContentPin). Runs on every layout feedback while armed: the font
|
||||
// change's own re-shaping is the first, and rewrap corrections follow it. If
|
||||
// the pinned byte is not in the shaped window (no layout yet, or the point
|
||||
// is in a blank margin), the (line, fragment, sub-line) fallback anchor
|
||||
// stands in. An edit landing since the capture (EditSeq mismatch) or leaving
|
||||
// the editor page disarms the pin: its byte no longer names the same content.
|
||||
// No MaxScroll clamp: the layout pass of the emitted frame clamps to the
|
||||
// fresh value. Must be called on the logic goroutine.
|
||||
func (l *Logic) refreshFontPin(fb ui.LayoutFeedback) {
|
||||
if !l.fontPinArmed {
|
||||
return
|
||||
}
|
||||
if time.Now().After(l.fontPinDeadline) {
|
||||
l.fontPinArmed = false
|
||||
return
|
||||
}
|
||||
s := l.state
|
||||
if s.page != EditorPage || fb.EditSeq != l.fontPin.EditSeq {
|
||||
l.fontPinArmed = false
|
||||
return
|
||||
}
|
||||
l.fontPinDeadline = time.Now().Add(fontPinTimeout)
|
||||
// Skip feedback shaped at a DIFFERENT scale: during a pinch every frame
|
||||
// changes the scale, so all but the latest feedback carry layouts whose
|
||||
// LineHeight no longer matches the geometry the pin computes in. A
|
||||
// stale-scale layout would place the point by the old geometry for one
|
||||
// frame (a visible jump) before the next corrects it.
|
||||
if fb.GlyphLayout.LineHeight > 0 &&
|
||||
math.Abs(float64(fb.GlyphLayout.LineHeight)-float64(EffectiveLineHeight())) > 0.5 {
|
||||
return
|
||||
}
|
||||
old := s.ScrollOffset
|
||||
vk := -1
|
||||
if cb := s.Editor.ChunkedBuffer; cb != nil && cb.WrapIndex != nil && fb.WindowStartLine >= 0 {
|
||||
vk = int(cb.WrapIndex.VisualsBefore(fb.WindowStartLine))
|
||||
}
|
||||
if l.fontPin.HaveGlyph && vk >= 0 {
|
||||
if off, ok := refineContentPin(fb.GlyphLayout, vk, fb.WindowStartByte, l.fontPin.Dy, l.fontPinM, l.fontPin.Byte); ok {
|
||||
s.ScrollOffset = off
|
||||
} else {
|
||||
s.applyFontPin(l.fontPin.Line, l.fontPin.Frag, l.fontPin.Sub, l.fontPinM)
|
||||
}
|
||||
} else {
|
||||
s.applyFontPin(l.fontPin.Line, l.fontPin.Frag, l.fontPin.Sub, l.fontPinM)
|
||||
}
|
||||
if s.ScrollOffset != old {
|
||||
l.emitFrame()
|
||||
}
|
||||
}
|
||||
|
||||
// releaseFontPin disarms the pinch font-pin: the user (a scroll, a debug
|
||||
// command, a file switch) has taken over the viewport. Must be called on
|
||||
// the logic goroutine.
|
||||
func (l *Logic) releaseFontPin() {
|
||||
l.fontPinArmed = false
|
||||
}
|
||||
|
||||
// applyRestorePositions clamps the restored snapshot's cursor and selection
|
||||
// to a file of n bytes and applies them. Must be called on the logic
|
||||
// goroutine.
|
||||
func (l *Logic) applyRestorePositions(n int) {
|
||||
s := l.session
|
||||
e := &l.state.Editor
|
||||
if c := s.Cursor; c >= 0 {
|
||||
if c > n {
|
||||
c = n
|
||||
}
|
||||
e.CursorPosition = c
|
||||
}
|
||||
if ss, se := s.SelStart, s.SelEnd; ss >= 0 && se > ss {
|
||||
if se > n {
|
||||
se = n
|
||||
}
|
||||
if ss < se {
|
||||
e.SelectionAnchor = ss
|
||||
e.SelectionStart = ss
|
||||
e.SelectionEnd = se
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// abandonRestore gives up on the relaunch restore (the file's stat failed:
|
||||
// it was deleted or moved since the last session): reset the editor and
|
||||
// land on the browser page, as if nothing had been restored. Must be called
|
||||
// on the logic goroutine.
|
||||
func (l *Logic) abandonRestore() {
|
||||
l.abortRestore()
|
||||
e := &l.state.Editor
|
||||
log.Printf("Logic: cannot restore %q; starting in the browser", e.Filename)
|
||||
e.Filename = ""
|
||||
e.ChunkedBuffer = nil
|
||||
e.CursorPosition = 0
|
||||
e.SelectionAnchor = -1
|
||||
e.SelectionStart = -1
|
||||
e.SelectionEnd = -1
|
||||
e.Find = FindState{SettleByte: -1}
|
||||
l.state.page = BrowserPage
|
||||
l.state.ScrollOffset = 0
|
||||
l.state.FocusedElementID = ""
|
||||
}
|
||||
+833
-88
File diff suppressed because it is too large
Load Diff
@@ -202,4 +202,3 @@ func byteOffsetOfLine(content string, i int) int {
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package real
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"pad/internal/io/pool/types"
|
||||
)
|
||||
@@ -63,11 +65,23 @@ func (fs *RealFileSystem) ReadFileAt(path string, offset, size int) ([]byte, err
|
||||
}
|
||||
defer f.Close()
|
||||
buf := make([]byte, size)
|
||||
n, err := f.ReadAt(buf, int64(offset))
|
||||
if err != nil && err != io.EOF {
|
||||
// ReadAt may return a SHORT read with a nil or io.EOF error (POSIX
|
||||
// allows it; the Android FUSE layer does it), so loop until the whole
|
||||
// range is read or the file truly ends. A silently short result would
|
||||
// truncate a chunk and shift every byte offset after it — taps and IME
|
||||
// commits would then land at the wrong buffer position (text corruption).
|
||||
total := 0
|
||||
for total < size {
|
||||
n, err := f.ReadAt(buf[total:], int64(offset)+int64(total))
|
||||
total += n
|
||||
if err != nil {
|
||||
if err == io.EOF || errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
break
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return buf[:n], nil
|
||||
}
|
||||
return buf[:total], nil
|
||||
}
|
||||
|
||||
func (fs *RealFileSystem) WriteFile(path string, content []byte) error {
|
||||
@@ -109,6 +123,23 @@ func (fs *RealFileSystem) WriteFileAtomic(path string, content []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// The Android shared-storage layer (FUSE backed by MediaProvider
|
||||
// metadata) re-serves the PREVIOUS file's mtime after a rename: its
|
||||
// media-scan DB row for the path is not updated when the staging file
|
||||
// is renamed into place (logcat: "Database update failed while
|
||||
// renaming"). Change detectors that combine inotify events with a
|
||||
// stat compare (syncthing) then treat the new content as unmodified
|
||||
// and skip it until the next full rescan — up to an hour of "edited
|
||||
// but not synced" on a phone. Setting the timestamps explicitly after
|
||||
// the rename publishes the current mtime through the FUSE layer, and
|
||||
// the timestamp update is the event the watcher acts on immediately
|
||||
// (verified on device: a plain rename's mtime is reverted to the old
|
||||
// file's, while utimensat sticks and triggers the sync at once).
|
||||
if err := os.Chtimes(fullPath, time.Now(), time.Now()); err != nil {
|
||||
// The content is already safely in place; the timestamps are a
|
||||
// best-effort hint for external change detectors, not data.
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func randomPayload(t *testing.T, seed int64, size int) []byte {
|
||||
@@ -209,4 +210,41 @@ func TestWriteFileAtomic_StaleTempFileIsConsumed(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestWriteFileAtomic_PublishesCurrentMtime pins the timestamp contract:
|
||||
// after a successful write the file's mtime is current, and a subsequent
|
||||
// write ADVANCES it. On Android shared storage a bare rename can leave the
|
||||
// PREVIOUS file's mtime visible through the FUSE layer (the MediaProvider
|
||||
// metadata row is not updated on rename), which makes stat-based change
|
||||
// detectors (syncthing) treat the new content as unmodified until the next
|
||||
// full rescan. WriteFileAtomic's post-rename Chtimes is what publishes the
|
||||
// current mtime; if that line is ever removed, this test documents what the
|
||||
// phone then experiences (it cannot reproduce the clobber itself on ext4).
|
||||
func TestWriteFileAtomic_PublishesCurrentMtime(t *testing.T) {
|
||||
d := t.TempDir()
|
||||
fs := NewRealFileSystem(d)
|
||||
if err := fs.WriteFileAtomic("/m.txt", []byte("v1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, err := os.Stat(filepath.Join(d, "m.txt"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if delta := time.Since(first.ModTime()); delta > 30*time.Second || delta < 0 {
|
||||
t.Fatalf("first write mtime not current: %v (delta %v)", first.ModTime(), delta)
|
||||
}
|
||||
// Cross a whole-second boundary: filesystems with 1 s mtime granularity
|
||||
// (Android FUSE) must still show an advance.
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
if err := fs.WriteFileAtomic("/m.txt", []byte("v2, longer content")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := os.Stat(filepath.Join(d, "m.txt"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !second.ModTime().After(first.ModTime()) {
|
||||
t.Fatalf("mtime did not advance after rewrite: first=%v second=%v (Android FUSE would report the stale mtime and syncthing would never see the change)", first.ModTime(), second.ModTime())
|
||||
}
|
||||
}
|
||||
|
||||
func itoa(n int) string { return strconv.Itoa(n) }
|
||||
|
||||
+55
-16
@@ -115,8 +115,6 @@ func (t TaskType) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// --- Specific Task Implementations ---
|
||||
|
||||
// ReadChunkTask reads a specific chunk of a file.
|
||||
@@ -169,7 +167,11 @@ func (t *ReadChunkTask) TaskID() string { return t.taskID }
|
||||
func (t *ReadChunkTask) DirPath() string { return filepath.Dir(t.Path) }
|
||||
func (t *ReadChunkTask) Context() context.Context { return t.ctx }
|
||||
func (t *ReadChunkTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
func (t *ReadChunkTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
func (t *ReadChunkTask) Cancel() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// StatFileTask retrieves file metadata (like size and modification time).
|
||||
// The plan indicates this replaces the full-file ReadFileTask for opening.
|
||||
@@ -216,7 +218,11 @@ func (t *StatFileTask) TaskID() string { return t.taskID }
|
||||
func (t *StatFileTask) DirPath() string { return filepath.Dir(t.Path) }
|
||||
func (t *StatFileTask) Context() context.Context { return t.ctx }
|
||||
func (t *StatFileTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
func (t *StatFileTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
func (t *StatFileTask) Cancel() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// FileStat holds file metadata.
|
||||
type FileStat struct {
|
||||
@@ -273,7 +279,11 @@ func (t *BuildLineIndexTask) TaskID() string { return t.taskID }
|
||||
func (t *BuildLineIndexTask) DirPath() string { return filepath.Dir(t.Path) }
|
||||
func (t *BuildLineIndexTask) Context() context.Context { return t.ctx }
|
||||
func (t *BuildLineIndexTask) Timeout() time.Duration { return 30 * time.Second }
|
||||
func (t *BuildLineIndexTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
func (t *BuildLineIndexTask) Cancel() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// BuildIndexTask builds the directory index for the browser.
|
||||
type BuildIndexTask struct {
|
||||
@@ -310,7 +320,11 @@ func (t *BuildIndexTask) TaskID() string { return t.taskID }
|
||||
func (t *BuildIndexTask) DirPath() string { return t.Dir }
|
||||
func (t *BuildIndexTask) Context() context.Context { return t.ctx }
|
||||
func (t *BuildIndexTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
func (t *BuildIndexTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
func (t *BuildIndexTask) Cancel() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// LoadPagesTask loads specific pages from a directory index.
|
||||
type LoadPagesTask struct {
|
||||
@@ -349,7 +363,11 @@ func (t *LoadPagesTask) TaskID() string { return t.taskID }
|
||||
func (t *LoadPagesTask) DirPath() string { return t.Dir }
|
||||
func (t *LoadPagesTask) Context() context.Context { return t.ctx }
|
||||
func (t *LoadPagesTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
func (t *LoadPagesTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
func (t *LoadPagesTask) Cancel() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// ReadDirTask reads directory entries.
|
||||
type ReadDirTask struct {
|
||||
@@ -387,7 +405,11 @@ func (t *ReadDirTask) TaskID() string { return t.taskID }
|
||||
func (t *ReadDirTask) DirPath() string { return t.Dir }
|
||||
func (t *ReadDirTask) Context() context.Context { return t.ctx }
|
||||
func (t *ReadDirTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
func (t *ReadDirTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
func (t *ReadDirTask) Cancel() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// SaveStateTask persists application state.
|
||||
type SaveStateTask struct {
|
||||
@@ -427,7 +449,11 @@ func (t *SaveStateTask) TaskID() string { return t.taskID }
|
||||
func (t *SaveStateTask) DirPath() string { return filepath.Dir(t.Path) }
|
||||
func (t *SaveStateTask) Context() context.Context { return t.ctx }
|
||||
func (t *SaveStateTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
func (t *SaveStateTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
func (t *SaveStateTask) Cancel() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// SaveUndoTask persists undo stack.
|
||||
type SaveUndoTask struct {
|
||||
@@ -467,7 +493,11 @@ func (t *SaveUndoTask) TaskID() string { return t.taskID }
|
||||
func (t *SaveUndoTask) DirPath() string { return filepath.Dir(t.Path) }
|
||||
func (t *SaveUndoTask) Context() context.Context { return t.ctx }
|
||||
func (t *SaveUndoTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
func (t *SaveUndoTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
func (t *SaveUndoTask) Cancel() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// ReadFileTask reads the full content of a file.
|
||||
// Used as a fallback for small files or initial load before chunking is set up.
|
||||
@@ -505,7 +535,11 @@ func (t *ReadFileTask) TaskID() string { return t.taskID }
|
||||
func (t *ReadFileTask) DirPath() string { return filepath.Dir(t.Path) }
|
||||
func (t *ReadFileTask) Context() context.Context { return t.ctx }
|
||||
func (t *ReadFileTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
func (t *ReadFileTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
func (t *ReadFileTask) Cancel() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// SearchData is the payload of a completed SearchTask: the byte ranges
|
||||
// [start, end) of every match of Query in the scanned Text, ascending and
|
||||
@@ -593,7 +627,11 @@ func (t *SearchTask) TaskID() string { return t.taskID }
|
||||
func (t *SearchTask) DirPath() string { return "" }
|
||||
func (t *SearchTask) Context() context.Context { return t.ctx }
|
||||
func (t *SearchTask) Timeout() time.Duration { return 10 * time.Second }
|
||||
func (t *SearchTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
func (t *SearchTask) Cancel() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// WriteFileTask writes content to a file.
|
||||
type WriteFileTask struct {
|
||||
@@ -632,7 +670,11 @@ func (t *WriteFileTask) TaskID() string { return t.taskID }
|
||||
func (t *WriteFileTask) DirPath() string { return filepath.Dir(t.Path) }
|
||||
func (t *WriteFileTask) Context() context.Context { return t.ctx }
|
||||
func (t *WriteFileTask) Timeout() time.Duration { return 5 * time.Second }
|
||||
func (t *WriteFileTask) Cancel() { if t.cancel != nil { t.cancel() } }
|
||||
func (t *WriteFileTask) Cancel() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// --- Mock File System (for testing/development) ---
|
||||
// MockFS implements the pool.FileSystem interface for testing.
|
||||
@@ -739,6 +781,3 @@ func (m *MockFS) ReadDir(path string) ([]types.DirEntry, error) {
|
||||
}
|
||||
|
||||
var _ FileSystem = (*MockFS)(nil) // Compile-time interface check
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -108,10 +108,10 @@ func TestRealFile_FindBarAndNavigation(t *testing.T) {
|
||||
t.Fatalf("focus %q, want find_bar", focus)
|
||||
}
|
||||
// The text area must shrink from the top while the find bar is open so
|
||||
// the first line is not covered: region top = top bar (32) + FindBarHeight.
|
||||
// the first line is not covered: region top = TopBarHeight + FindBarHeight.
|
||||
reg, _ := h.Inspect(func(st *editor.State) any { return st.EditorRegion })
|
||||
if got := reg.(ui.Region); got.Y != ui.Dp(32)+editor.FindBarHeight {
|
||||
t.Fatalf("editor region top %d, want %d (find bar must not cover text)", int(got.Y), int(ui.Dp(32)+editor.FindBarHeight))
|
||||
if got := reg.(ui.Region); got.Y != editor.TopBarHeight+editor.FindBarHeight {
|
||||
t.Fatalf("editor region top %d, want %d (find bar must not cover text)", int(got.Y), int(editor.TopBarHeight+editor.FindBarHeight))
|
||||
}
|
||||
// The editor field reports UNFOCUSED while the find bar is open (the
|
||||
// renderer hides its caret for unfocused fields; main hands key focus to
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
package e2e_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gioui.org/io/key"
|
||||
"pad/internal/editor"
|
||||
"pad/internal/test/e2e"
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// incidentContent is the file from the 2026-09-19 phone incident: "test"
|
||||
// plus five newlines (9 bytes). The tap target is byte 6 (the start of
|
||||
// line 3), exactly where the user tapped.
|
||||
const incidentContent = "test\n\n\n\n\n"
|
||||
|
||||
func setCursor(t *testing.T, h *e2e.Harness, pos int) {
|
||||
t.Helper()
|
||||
if err := h.WithState(func(st *editor.State) {
|
||||
st.Editor.CursorPosition = pos
|
||||
}); err != nil {
|
||||
t.Fatalf("set cursor: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// contentAfter sends the input batch (nil = none) and waits until the buffer
|
||||
// content reaches want; the logic goroutine applies the events
|
||||
// asynchronously, so a read straight after SendInput is a race.
|
||||
func contentAfter(t *testing.T, h *e2e.Harness, events []ui.InputEvent, want string) string {
|
||||
t.Helper()
|
||||
if events != nil {
|
||||
h.SendInput(events)
|
||||
}
|
||||
for i := 0; i < 300; i++ {
|
||||
c, err := h.FullContent()
|
||||
if err != nil {
|
||||
t.Fatalf("FullContent: %v", err)
|
||||
}
|
||||
if c == want {
|
||||
return c
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("buffer did not reach %q", want)
|
||||
return ""
|
||||
}
|
||||
|
||||
// TestIMECommitAfterEnterKeySameBatch is the exact phone incident: tap at
|
||||
// byte 6, type "a" (Gboard autocaps and commits [6,6)→"A"), press Enter
|
||||
// (Gboard sends it as a KEY EVENT, not a commit — the logic inserts "\n"
|
||||
// and the caret moves to 8, but Gboard's local model still lacks it), then
|
||||
// type "a" (Gboard, one edit behind, commits the word append [6,7)→"Aa"
|
||||
// against the pre-Enter caret). The key event and the append commit arrive
|
||||
// in the same input batch, so no frame has drawn between them.
|
||||
//
|
||||
// The append must be applied at its reported range — everything before the
|
||||
// pending key edit is byte-identical in both models — instead of being
|
||||
// drift-snapped to the new caret and duplicated (the incident produced
|
||||
// "test\n\nA\nAaAa\n\n\n").
|
||||
func TestIMECommitAfterEnterKeySameBatch(t *testing.T) {
|
||||
h, _ := realFileHarness(t, "test.txt", incidentContent)
|
||||
defer h.Cleanup()
|
||||
setCursor(t, h, 6)
|
||||
|
||||
// "a" typed with autocap: one clean IME commit.
|
||||
contentAfter(t, h, []ui.InputEvent{{
|
||||
Handler: editor.HandleIMECommit,
|
||||
Data: editor.IMECommit{StartRune: 6, EndRune: 6, Text: "A"},
|
||||
}}, "test\n\nA\n\n\n")
|
||||
|
||||
// Enter (key event) and the stale-model word append in ONE batch: the
|
||||
// logic processes the "\n" first, so when the append is guarded the
|
||||
// caret is already at 8 while the commit ends at 7. "A" must be replaced
|
||||
// by "Aa" in place and the Enter newline survive after it.
|
||||
contentAfter(t, h, []ui.InputEvent{
|
||||
{Handler: editor.HandleKeyDown, Data: ui.KeyEvent{Name: key.NameReturn}},
|
||||
{Handler: editor.HandleIMECommit, Data: editor.IMECommit{StartRune: 6, EndRune: 7, Text: "Aa"}},
|
||||
}, "test\n\nAa\n\n\n\n")
|
||||
|
||||
if pos, _ := h.CursorPosition(); pos != 8 {
|
||||
t.Errorf("cursor: got %d want 8 (end of the replaced word)", pos)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIMECommitAfterEnterKeyNextBatch is the same incident with the append
|
||||
// commit arriving in a LATER input batch than the Enter: a frame was
|
||||
// emitted for the Enter, but the resyncing snippet push only lands on the
|
||||
// next drawn frame, and the IME's commit can beat it. The pending key edit
|
||||
// must still be honored.
|
||||
func TestIMECommitAfterEnterKeyNextBatch(t *testing.T) {
|
||||
h, _ := realFileHarness(t, "test.txt", incidentContent)
|
||||
defer h.Cleanup()
|
||||
setCursor(t, h, 6)
|
||||
|
||||
contentAfter(t, h, []ui.InputEvent{{
|
||||
Handler: editor.HandleIMECommit,
|
||||
Data: editor.IMECommit{StartRune: 6, EndRune: 6, Text: "A"},
|
||||
}}, "test\n\nA\n\n\n")
|
||||
|
||||
contentAfter(t, h, []ui.InputEvent{{
|
||||
Handler: editor.HandleKeyDown,
|
||||
Data: ui.KeyEvent{Name: key.NameReturn},
|
||||
}}, "test\n\nA\n\n\n\n")
|
||||
|
||||
contentAfter(t, h, []ui.InputEvent{{
|
||||
Handler: editor.HandleIMECommit,
|
||||
Data: editor.IMECommit{StartRune: 6, EndRune: 7, Text: "Aa"},
|
||||
}}, "test\n\nAa\n\n\n\n")
|
||||
}
|
||||
|
||||
// TestIMEEmptyCommitStillSnappedWithPendingEdit: a pending key edit does
|
||||
// NOT relax the guard for range deletions (empty text). Deleting at a range
|
||||
// the IME sees but the app no longer expects there is how documents get
|
||||
// clobbered; such commits must still snap to the caret (a no-op there).
|
||||
func TestIMEEmptyCommitStillSnappedWithPendingEdit(t *testing.T) {
|
||||
h, _ := realFileHarness(t, "test.txt", incidentContent)
|
||||
defer h.Cleanup()
|
||||
setCursor(t, h, 6)
|
||||
|
||||
contentAfter(t, h, []ui.InputEvent{{
|
||||
Handler: editor.HandleIMECommit,
|
||||
Data: editor.IMECommit{StartRune: 6, EndRune: 6, Text: "A"},
|
||||
}}, "test\n\nA\n\n\n")
|
||||
|
||||
contentAfter(t, h, []ui.InputEvent{{
|
||||
Handler: editor.HandleKeyDown,
|
||||
Data: ui.KeyEvent{Name: key.NameReturn},
|
||||
}}, "test\n\nA\n\n\n\n")
|
||||
|
||||
// Gboard's stale model still shows "A" at [6,7) and asks to delete it.
|
||||
// The commit must snap to the caret (a no-op there), so the "A"
|
||||
// survives. The content is unchanged by a correct snap, so poll for
|
||||
// quiescence (a new frame after the input) before asserting.
|
||||
contentAfter(t, h, []ui.InputEvent{{
|
||||
Handler: editor.HandleIMECommit,
|
||||
Data: editor.IMECommit{StartRune: 6, EndRune: 7, Text: ""},
|
||||
}}, "test\n\nA\n\n\n\n")
|
||||
before := h.FrameCount()
|
||||
for h.FrameCount() == before {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
if c, _ := h.FullContent(); c != "test\n\nA\n\n\n\n" {
|
||||
t.Fatalf("empty commit must snap to the caret (no-op), got %q", c)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIMECommitPastPendingEditStillSnapped: a commit that ends PAST the
|
||||
// pending key edit addresses text the IME's model does not have (it is
|
||||
// shifted by the edit), so it is still anomalous and snaps to the caret.
|
||||
func TestIMECommitPastPendingEditStillSnapped(t *testing.T) {
|
||||
h, _ := realFileHarness(t, "test.txt", incidentContent)
|
||||
defer h.Cleanup()
|
||||
setCursor(t, h, 6)
|
||||
|
||||
contentAfter(t, h, []ui.InputEvent{{
|
||||
Handler: editor.HandleIMECommit,
|
||||
Data: editor.IMECommit{StartRune: 6, EndRune: 6, Text: "A"},
|
||||
}}, "test\n\nA\n\n\n")
|
||||
|
||||
contentAfter(t, h, []ui.InputEvent{{
|
||||
Handler: editor.HandleKeyDown,
|
||||
Data: ui.KeyEvent{Name: key.NameReturn},
|
||||
}}, "test\n\nA\n\n\n\n")
|
||||
|
||||
// Ends at 9, past the pending edit at 7: the range crosses the edit, so
|
||||
// the file content there is NOT what the IME sees. Snap to the caret
|
||||
// (8): "x" lands at 8, not 9.
|
||||
contentAfter(t, h, []ui.InputEvent{{
|
||||
Handler: editor.HandleIMECommit,
|
||||
Data: editor.IMECommit{StartRune: 9, EndRune: 9, Text: "x"},
|
||||
}}, "test\n\nA\nx\n\n\n")
|
||||
}
|
||||
@@ -172,9 +172,13 @@ func TestRealFile_Fuzz_EditSaveReload(t *testing.T) {
|
||||
type win struct {
|
||||
Start int
|
||||
Text string
|
||||
CtxStart int
|
||||
Context string
|
||||
OffsetRune int
|
||||
}
|
||||
raw, err := h.Inspect(func(st *editor.State) any {
|
||||
return win{st.Editor.IMEWindowStartByte, st.Editor.IMEWindowText}
|
||||
return win{st.Editor.IMEWindowStartByte, st.Editor.IMEWindowText,
|
||||
st.Editor.IMEContextStartByte, st.Editor.IMEContext, st.Editor.IMEOffsetRune}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("batch %d op %d: inspect: %v", batch, j, err)
|
||||
@@ -191,17 +195,39 @@ func TestRealFile_Fuzz_EditSaveReload(t *testing.T) {
|
||||
batch, j, w.Start, len(w.Text),
|
||||
compactMismatch(w.Text, model[w.Start:min(len(model), w.Start+len(w.Text))]))
|
||||
}
|
||||
wr := countRunes(w.Text)
|
||||
ra := rng.Intn(wr + 1)
|
||||
rb := ra + rng.Intn(wr-ra+1)
|
||||
// Context consistency: it is the slice immediately before the
|
||||
// window, and the snippet's Range.Start is the rune offset of
|
||||
// the context start.
|
||||
if len(w.Context) > 0 {
|
||||
if w.CtxStart+len(w.Context) != w.Start ||
|
||||
w.Context != model[w.CtxStart:w.CtxStart+len(w.Context)] {
|
||||
t.Fatalf("batch %d op %d: IME context desync: ctxStart=%d", batch, j, w.CtxStart)
|
||||
}
|
||||
}
|
||||
if w.OffsetRune != countRunes(model[:w.CtxStart]) {
|
||||
t.Fatalf("batch %d op %d: IME offset desync: got=%d want=%d", batch, j, w.OffsetRune, countRunes(model[:w.CtxStart]))
|
||||
}
|
||||
// The real IME reports ABSOLUTE file runes: the pushed snippet
|
||||
// (context+window) starts at OffsetRune, so a snippet-local
|
||||
// range [sa,sb) is absolute [OffsetRune+sa, OffsetRune+sb).
|
||||
ctxRunes := countRunes(w.Context)
|
||||
total := ctxRunes + countRunes(w.Text)
|
||||
sa := rng.Intn(total + 1)
|
||||
sb := sa + rng.Intn(total-sa+1)
|
||||
absByte := func(snipRune int) int {
|
||||
if snipRune <= ctxRunes {
|
||||
return w.CtxStart + runeToByte(w.Context, snipRune)
|
||||
}
|
||||
return w.Start + runeToByte(w.Text, snipRune-ctxRunes)
|
||||
}
|
||||
text := randomFuzzText(rng, 0, 12)
|
||||
if err := h.WithState(func(st *editor.State) {
|
||||
editor.HandleReplaceRange(ra, rb, text)
|
||||
editor.HandleReplaceRange(w.OffsetRune+sa, w.OffsetRune+sb, text)
|
||||
}); err != nil {
|
||||
t.Fatalf("batch %d op %d: replace: %v", batch, j, err)
|
||||
}
|
||||
bs := w.Start + runeToByte(w.Text, ra)
|
||||
be := w.Start + runeToByte(w.Text, rb)
|
||||
bs := absByte(sa)
|
||||
be := absByte(sb)
|
||||
model = model[:bs] + text + model[be:]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,307 @@
|
||||
package e2e_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"gioui.org/io/key"
|
||||
"pad/internal/editor"
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// largeFileContent generates a deterministic ~1.6 MB text file: 40,000 lines
|
||||
// of ~40 unique ASCII bytes each, with a multibyte rune ("é", "中") sprinkled
|
||||
// through every 500th line so rune offsets and byte offsets genuinely differ.
|
||||
// Generating in-test (t.TempDir, via realFileHarness) keeps the repository
|
||||
// small while still exercising the >500 KB chunked/IME path the same way a
|
||||
// real multi-hundred-kb note would.
|
||||
func largeFileContent(lines int) string {
|
||||
var b strings.Builder
|
||||
for i := 0; i < lines; i++ {
|
||||
b.WriteString(fmt.Sprintf("large file line %05d pad", i))
|
||||
if i%500 == 0 {
|
||||
b.WriteString(" é 中")
|
||||
}
|
||||
b.WriteString(strings.Repeat(string(rune('a'+i%26)), 12))
|
||||
b.WriteString("\n")
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// TestRealFile_LargeFile_IMECommits pins IME commit correctness on a large
|
||||
// (~1.6 MB, well over the 500 KB threshold that the on-device "needs a
|
||||
// gioui fork" claim referred to) file:
|
||||
//
|
||||
// - the IME window invariants (window text, leading context, absolute
|
||||
// rune offset) hold for a window deep in the file;
|
||||
// - a replacement commit (a selection, as a swipe/autocorrect would send)
|
||||
// lands exactly in the scrolled window and nowhere else;
|
||||
// - an insert commit (empty range at the caret, a plain typed char) in a
|
||||
// second, deeper window lands exactly at the caret.
|
||||
//
|
||||
// The full-content comparison after each commit catches the classic
|
||||
// corruption mode (the commit mapped through a stale/zero window start and
|
||||
// clobbering text near the top of the file).
|
||||
func TestRealFile_LargeFile_IMECommits(t *testing.T) {
|
||||
const (
|
||||
lines = 40000
|
||||
scrollLn = 20000 // scroll deep into the file
|
||||
editLn = 20008 // comfortably inside the ~18-line viewport window
|
||||
)
|
||||
model := largeFileContent(lines)
|
||||
h, path := realFileHarness(t, "large.txt", model)
|
||||
defer h.Cleanup()
|
||||
|
||||
h.SendConfig(780, 400)
|
||||
if _, err := h.WaitForFrame(5 * time.Second); err != nil {
|
||||
t.Fatalf("wait for frame: %v", err)
|
||||
}
|
||||
|
||||
// Scroll deep (5 dp into the line: avoid the float32/float64 line-pitch
|
||||
// boundary fragility, see TestRealFile_WindowSelectionAtScroll) and put a
|
||||
// selection on line editLn, 6 bytes into its filler.
|
||||
selStart := byteOffsetOfLine(t, model, editLn) + 31
|
||||
selEnd := selStart + 6
|
||||
if err := h.WithState(func(st *editor.State) {
|
||||
st.ScrollOffset = ui.Dp(16.8*scrollLn + 5)
|
||||
st.Editor.SelectionStart = selStart
|
||||
st.Editor.SelectionEnd = selEnd
|
||||
st.Editor.CursorPosition = selEnd
|
||||
}); err != nil {
|
||||
t.Fatalf("WithState: %v", err)
|
||||
}
|
||||
prev := h.FrameCount()
|
||||
h.SendConfig(780, 400)
|
||||
if _, err := h.WaitForFrameCount(prev+1, 5*time.Second); err != nil {
|
||||
t.Fatalf("wait for scrolled frame: %v", err)
|
||||
}
|
||||
|
||||
// 1) IME window invariants for a window deep in a large file.
|
||||
var win struct {
|
||||
Start int
|
||||
Text string
|
||||
Snip string
|
||||
}
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
e := &st.Editor
|
||||
return struct {
|
||||
Start int
|
||||
Text string
|
||||
Snip string
|
||||
}{e.IMEWindowStartByte, e.IMEWindowText, e.IMESnippetText}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
win = v.(struct {
|
||||
Start int
|
||||
Text string
|
||||
Snip string
|
||||
})
|
||||
if want := model[win.Start : win.Start+len(win.Text)]; win.Text != want {
|
||||
t.Fatalf("IME window text desync at start=%d", win.Start)
|
||||
}
|
||||
// The IME snippet is a hysteresis window around the caret (see
|
||||
// State.computeIMESnippetWindow); it must always be a fresh slice of
|
||||
// the model.
|
||||
if i := strings.Index(model, win.Snip); i < 0 {
|
||||
t.Fatalf("IME snippet not a slice of the model: %q", win.Snip[:min(40, len(win.Snip))])
|
||||
}
|
||||
|
||||
// 2) Replacement commit: absolute file runes (the coordinate space the
|
||||
// real IME uses; the multibyte sprinkles make runes != bytes).
|
||||
absStart := utf8.RuneCountInString(model[:selStart])
|
||||
absEnd := utf8.RuneCountInString(model[:selEnd])
|
||||
wantModel := model[:selStart] + "ZZ" + model[selEnd:]
|
||||
h.SendInput([]ui.InputEvent{{
|
||||
Handler: editor.HandleKeyDown,
|
||||
Data: key.EditEvent{
|
||||
Range: key.Range{Start: absStart, End: absEnd},
|
||||
Text: "ZZ",
|
||||
},
|
||||
}})
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
got, err := h.FullContent()
|
||||
if err != nil {
|
||||
t.Fatalf("FullContent: %v", err)
|
||||
}
|
||||
if got != wantModel {
|
||||
t.Fatalf("after replacement commit: content differs (first diff at %d)", firstDiff(wantModel, got))
|
||||
}
|
||||
|
||||
// 3) Insert commit in a deeper window: caret-only (empty range), the
|
||||
// plain "typed a character" case.
|
||||
deepLn := 30000
|
||||
caret := byteOffsetOfLine(t, wantModel, deepLn) + 20
|
||||
if err := h.WithState(func(st *editor.State) {
|
||||
st.ScrollOffset = ui.Dp(16.8*float64(deepLn) + 5)
|
||||
st.Editor.SelectionStart = -1
|
||||
st.Editor.SelectionEnd = -1
|
||||
st.Editor.CursorPosition = caret
|
||||
}); err != nil {
|
||||
t.Fatalf("WithState: %v", err)
|
||||
}
|
||||
prev = h.FrameCount()
|
||||
h.SendConfig(780, 400)
|
||||
if _, err := h.WaitForFrameCount(prev+1, 5*time.Second); err != nil {
|
||||
t.Fatalf("wait for second scrolled frame: %v", err)
|
||||
}
|
||||
absCaret := utf8.RuneCountInString(wantModel[:caret])
|
||||
wantModel2 := wantModel[:caret] + "Q" + wantModel[caret:]
|
||||
h.SendInput([]ui.InputEvent{{
|
||||
Handler: editor.HandleKeyDown,
|
||||
Data: key.EditEvent{
|
||||
Range: key.Range{Start: absCaret, End: absCaret},
|
||||
Text: "Q",
|
||||
},
|
||||
}})
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
got, err = h.FullContent()
|
||||
if err != nil {
|
||||
t.Fatalf("FullContent: %v", err)
|
||||
}
|
||||
if got != wantModel2 {
|
||||
t.Fatalf("after insert commit: content differs (first diff at %d)", firstDiff(wantModel2, got))
|
||||
}
|
||||
if cp, err := h.Inspect(func(st *editor.State) any { return st.Editor.CursorPosition }); err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
} else if cp.(int) != caret+1 {
|
||||
t.Fatalf("cursor = %d, want %d", cp.(int), caret+1)
|
||||
}
|
||||
|
||||
// 4) Persistence: the on-disk file matches the in-memory content.
|
||||
if err := h.Flush(); err != nil {
|
||||
t.Fatalf("flush: %v", err)
|
||||
}
|
||||
if disk := readDisk(t, path); disk != wantModel2 {
|
||||
t.Fatalf("disk content differs from in-memory (first diff at %d)", firstDiff(wantModel2, disk))
|
||||
}
|
||||
}
|
||||
|
||||
// byteOffsetOfLine returns the absolute byte offset of the start of line n
|
||||
// (0-indexed) in model. The generated lines are unique, so a plain index of
|
||||
// the line prefix is unambiguous.
|
||||
func byteOffsetOfLine(t *testing.T, model string, n int) int {
|
||||
t.Helper()
|
||||
prefix := fmt.Sprintf("large file line %05d", n)
|
||||
i := strings.Index(model, prefix)
|
||||
if i < 0 {
|
||||
t.Fatalf("line %d not found", n)
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// firstDiff returns the byte offset of the first difference between a and b
|
||||
// (min(len) when they are a common prefix), for useful failure messages.
|
||||
func firstDiff(a, b string) int {
|
||||
n := len(a)
|
||||
if len(b) < n {
|
||||
n = len(b)
|
||||
}
|
||||
for i := 0; i < n; i++ {
|
||||
if a[i] != b[i] {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// TestRealFile_IMEForceResync pins the desync recovery: the first
|
||||
// anomalous IME commit (a drift-snapped small commit, a distant commit —
|
||||
// the phone autocorrect that landed 19,000 runes from the caret — or an
|
||||
// empty fix-up) arms a forced re-syncing snippet re-push on the very next
|
||||
// frame (a desynced IME is healed only by a restart; a selection push is
|
||||
// deduplicated away after commits). A normal commit never arms it.
|
||||
func TestRealFile_IMEForceResync(t *testing.T) {
|
||||
model := largeFileContent(4000)
|
||||
h, _ := realFileHarness(t, "resync.txt", model)
|
||||
defer h.Cleanup()
|
||||
|
||||
h.SendConfig(780, 400)
|
||||
if _, err := h.WaitForFrame(5 * time.Second); err != nil {
|
||||
t.Fatalf("wait for frame: %v", err)
|
||||
}
|
||||
|
||||
const caretByte = 10000
|
||||
caretRune := utf8.RuneCountInString(model[:caretByte])
|
||||
if err := h.WithState(func(st *editor.State) {
|
||||
st.Editor.CursorPosition = caretByte
|
||||
}); err != nil {
|
||||
t.Fatalf("WithState: %v", err)
|
||||
}
|
||||
|
||||
commit := func(start, end int, text string) {
|
||||
h.SendInput([]ui.InputEvent{{
|
||||
Handler: editor.HandleIMECommit,
|
||||
Data: editor.IMECommit{StartRune: start, EndRune: end, Text: text},
|
||||
}})
|
||||
// The logic applies commits asynchronously; wait for the resulting
|
||||
// frame before asserting on the derived state.
|
||||
prev := h.FrameCount()
|
||||
if _, err := h.WaitForFrameCount(prev+1, 5*time.Second); err != nil {
|
||||
t.Fatalf("wait for commit frame: %v", err)
|
||||
}
|
||||
}
|
||||
forceInLatestFrame := func() bool {
|
||||
frames := h.GetFrames()
|
||||
if len(frames) == 0 {
|
||||
t.Fatal("no frames")
|
||||
}
|
||||
for _, el := range frames[len(frames)-1] {
|
||||
if tf, ok := el.(ui.TextField); ok && tf.ID() == "editor_text" {
|
||||
return tf.IMEForceResync
|
||||
}
|
||||
}
|
||||
t.Fatal("no editor_text element in latest frame")
|
||||
return false
|
||||
}
|
||||
|
||||
// A normal commit at the caret arms nothing.
|
||||
commit(caretRune, caretRune, "q")
|
||||
if forceInLatestFrame() {
|
||||
t.Fatal("resync armed by a normal commit")
|
||||
}
|
||||
|
||||
// A small drifted commit (Gboard fix-up at a stale position): the frame
|
||||
// emitted by that commit carries the resync flag.
|
||||
commit(caretRune+5, caretRune+5, "")
|
||||
if !forceInLatestFrame() {
|
||||
t.Fatal("resync not armed after a drift-snapped commit")
|
||||
}
|
||||
|
||||
// One-shot: a later frame carries no flag.
|
||||
prev := h.FrameCount()
|
||||
h.SendConfig(780, 400)
|
||||
if _, err := h.WaitForFrameCount(prev+1, 5*time.Second); err != nil {
|
||||
t.Fatalf("wait for frame: %v", err)
|
||||
}
|
||||
if forceInLatestFrame() {
|
||||
t.Fatal("resync flag not one-shot")
|
||||
}
|
||||
|
||||
// A distant replacement commit (width > 2: the autocorrect class that
|
||||
// the old guard let through) is snapped to the caret and arms the
|
||||
// resync.
|
||||
commit(caretRune+20000, caretRune+20005, "silly")
|
||||
if !forceInLatestFrame() {
|
||||
t.Fatal("resync not armed after a distant commit")
|
||||
}
|
||||
|
||||
// The distant commit must have landed at the caret, not at 20000:
|
||||
// the caret advances by exactly the inserted text length.
|
||||
var cur int
|
||||
if err := h.WithState(func(st *editor.State) {
|
||||
cur = st.Editor.CursorPosition
|
||||
}); err != nil {
|
||||
t.Fatalf("WithState: %v", err)
|
||||
}
|
||||
// "q" (1) + "silly" (5) inserted at the original caret.
|
||||
if want := caretByte + 6; cur != want {
|
||||
t.Fatalf("cursor = %d, want %d (distant commit must land at the caret)", cur, want)
|
||||
}
|
||||
}
|
||||
@@ -137,9 +137,11 @@ func TestRealFile_IMECommitAtScroll(t *testing.T) {
|
||||
t.Fatalf("wait for frame: %v", err)
|
||||
}
|
||||
|
||||
// The selection (absolute) and the matching window-relative EditEvent
|
||||
// range: the IME reports offsets relative to the pushed snippet, which
|
||||
// is the visible window.
|
||||
// The selection (absolute) and the matching EditEvent range: the IME
|
||||
// reports offsets in absolute file runes (the coordinate space of the
|
||||
// pushed snippet, which now starts at the leading context's true file
|
||||
// position). The content is ASCII, so rune == byte and the range is just
|
||||
// the absolute byte offsets.
|
||||
selStart := editLn*lineLen + 5
|
||||
selEnd := editLn*lineLen + 11
|
||||
// Scroll 5 dp into line 10 (see TestRealFile_WindowSelectionAtScroll for
|
||||
@@ -159,12 +161,14 @@ func TestRealFile_IMECommitAtScroll(t *testing.T) {
|
||||
}
|
||||
|
||||
// Typing replaces the selection: "ZZ" over the 6 selected filler chars
|
||||
// (in-line offsets [5,11)) on line 12. The pre-fix code (windowStart=0)
|
||||
// applied this at byte 47 — line 2 — corrupting the file.
|
||||
// (in-line offsets [5,11)) on line 12. The commit range is in absolute
|
||||
// file runes (== bytes here). The pre-fix code (windowStart=0, no
|
||||
// leading context) applied a mis-mapped range near the top of the file,
|
||||
// corrupting it.
|
||||
h.SendInput([]ui.InputEvent{{
|
||||
Handler: editor.HandleKeyDown,
|
||||
Data: key.EditEvent{
|
||||
Range: key.Range{Start: selStart - scrollLn*lineLen, End: selEnd - scrollLn*lineLen},
|
||||
Range: key.Range{Start: selStart, End: selEnd},
|
||||
Text: "ZZ",
|
||||
},
|
||||
}})
|
||||
|
||||
@@ -0,0 +1,901 @@
|
||||
package e2e_test
|
||||
|
||||
// Relaunch state restoration (spec §7): the last file, cursor, scroll,
|
||||
// selection and find bar state are persisted as a tiny JSON snapshot and
|
||||
// restored on the next launch. These tests drive the same entry points the
|
||||
// cmd layer uses: Logic.BeginRestore before Run (restore) and the session
|
||||
// saver callback (persist).
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pad/internal/editor"
|
||||
"pad/internal/io/pool/real"
|
||||
"pad/internal/test/e2e"
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// restoreHarness writes content to <tempdir>/name, builds a harness over the
|
||||
// real filesystem, hands sess to the logic BEFORE Run (as the cmd layer does
|
||||
// on relaunch), starts it with the standard 390x844@2x window, and waits for
|
||||
// the restored file to fully load (content + line index).
|
||||
func restoreHarness(t *testing.T, name, content string, sess editor.SessionState) *e2e.Harness {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
diskPath := filepath.Join(dir, name)
|
||||
if err := os.WriteFile(diskPath, []byte(content), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sess.File = "/" + name
|
||||
|
||||
h := e2e.NewHarness(e2e.WithFileSystem(real.NewRealFileSystem(dir), "/"))
|
||||
h.Logic().BeginRestore(sess)
|
||||
h.Run()
|
||||
h.SendConfig(780, 1688) // 390x844 @ 2x scale
|
||||
h.SendScale(2.0)
|
||||
|
||||
// "Loaded" means both async results are fully applied: the ReadFile
|
||||
// content AND the BuildLineIndex result (same contract as
|
||||
// realFileHarness: FileLen>0 && LineIndex!=nil alone is a race).
|
||||
for i := 0; i < 100; i++ {
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
cb := st.Editor.ChunkedBuffer
|
||||
if cb == nil || cb.FileLen() == 0 || cb.LineIndex == nil {
|
||||
return false
|
||||
}
|
||||
full, err := cb.FullContent()
|
||||
return err == nil && int64(len(full)) == cb.FileLen() &&
|
||||
cb.LineIndex.Size == cb.FileLen()
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
if v.(bool) {
|
||||
return h
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("timed out waiting for restored file to load")
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestRestore_FileCursorScrollSelection restores a file with a cursor, a
|
||||
// non-zero scroll and a live selection; all three must land once the content
|
||||
// arrives, on the editor page (not the browser).
|
||||
func TestRestore_FileCursorScrollSelection(t *testing.T) {
|
||||
content := strings.Repeat("hello world line\n", 100)
|
||||
h := restoreHarness(t, "notes.txt", content, editor.SessionState{
|
||||
Cursor: 123,
|
||||
Scroll: 100,
|
||||
SelStart: 100,
|
||||
SelEnd: 110,
|
||||
})
|
||||
defer h.Cleanup()
|
||||
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
return struct {
|
||||
Page editor.Page
|
||||
File string
|
||||
Cursor int
|
||||
Scroll ui.Dp
|
||||
SelStart int
|
||||
SelEnd int
|
||||
Anchor int
|
||||
}{st.Page(), st.Editor.Filename, st.Editor.CursorPosition, st.ScrollOffset,
|
||||
st.Editor.SelectionStart, st.Editor.SelectionEnd, st.Editor.SelectionAnchor}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
got := v.(struct {
|
||||
Page editor.Page
|
||||
File string
|
||||
Cursor int
|
||||
Scroll ui.Dp
|
||||
SelStart int
|
||||
SelEnd int
|
||||
Anchor int
|
||||
})
|
||||
if got.Page != editor.EditorPage {
|
||||
t.Errorf("page = %v, want EditorPage (restore lands straight in the editor)", got.Page)
|
||||
}
|
||||
if got.File != "/notes.txt" {
|
||||
t.Errorf("file = %q, want /notes.txt", got.File)
|
||||
}
|
||||
if got.Cursor != 123 {
|
||||
t.Errorf("cursor = %d, want 123", got.Cursor)
|
||||
}
|
||||
if got.Scroll != ui.Dp(100) {
|
||||
t.Errorf("scroll = %v, want 100", got.Scroll)
|
||||
}
|
||||
if got.SelStart != 100 || got.SelEnd != 110 || got.Anchor != 100 {
|
||||
t.Errorf("selection = [%d,%d) anchor %d, want [100,110) anchor 100",
|
||||
got.SelStart, got.SelEnd, got.Anchor)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestore_ScrollLandsWhenScalePrecedesSize reproduces the on-device
|
||||
// startup ordering, where the first FrameEvent (and hence the ScaleEvent)
|
||||
// arrives before the size ConfigEvent. The scroll must still land, clamped
|
||||
// by the layout at the real scale: an earlier revision applied it at the
|
||||
// ScaleEvent against a not-yet-sized viewport and lost it.
|
||||
func TestRestore_ScrollLandsWhenScalePrecedesSize(t *testing.T) {
|
||||
content := strings.Repeat("hello world line\n", 100)
|
||||
dir := t.TempDir()
|
||||
diskPath := filepath.Join(dir, "late.txt")
|
||||
if err := os.WriteFile(diskPath, []byte(content), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h := e2e.NewHarness(e2e.WithFileSystem(real.NewRealFileSystem(dir), "/"))
|
||||
h.Logic().BeginRestore(editor.SessionState{File: "/late.txt", Cursor: 123, Scroll: 100})
|
||||
h.Run()
|
||||
h.SendScale(2.0) // scale first
|
||||
h.SendConfig(780, 1688) // size second (on-device ordering)
|
||||
defer h.Cleanup()
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
cb := st.Editor.ChunkedBuffer
|
||||
if cb == nil || cb.FileLen() == 0 || cb.LineIndex == nil {
|
||||
return false
|
||||
}
|
||||
full, err := cb.FullContent()
|
||||
return err == nil && int64(len(full)) == cb.FileLen() &&
|
||||
cb.LineIndex.Size == cb.FileLen() && st.ScrollOffset == ui.Dp(100)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
if v.(bool) {
|
||||
return
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("timed out: scroll did not land when the scale event preceded the size")
|
||||
}
|
||||
|
||||
// TestRestore_ClampsToShorterFile: the file was truncated since the last
|
||||
// session; the cursor and selection must clamp to the shorter content, not
|
||||
// restore past EOF.
|
||||
func TestRestore_ClampsToShorterFile(t *testing.T) {
|
||||
content := "0123456789" // 10 bytes
|
||||
h := restoreHarness(t, "short.txt", content, editor.SessionState{
|
||||
Cursor: 57, // far past EOF
|
||||
SelStart: 5,
|
||||
SelEnd: 99, // spans EOF
|
||||
})
|
||||
defer h.Cleanup()
|
||||
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
return struct{ Cursor, SelStart, SelEnd int }{
|
||||
st.Editor.CursorPosition, st.Editor.SelectionStart, st.Editor.SelectionEnd,
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
got := v.(struct{ Cursor, SelStart, SelEnd int })
|
||||
if got.Cursor != 10 {
|
||||
t.Errorf("cursor = %d, want 10 (clamped to EOF)", got.Cursor)
|
||||
}
|
||||
if got.SelStart != 5 || got.SelEnd != 10 {
|
||||
t.Errorf("selection = [%d,%d), want [5,10) (end clamped to EOF)", got.SelStart, got.SelEnd)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestore_MissingFileFallsBackToBrowser: the restored file was deleted
|
||||
// since the last session; the app must land on the browser page with a clean
|
||||
// editor, not an empty editor page.
|
||||
func TestRestore_MissingFileFallsBackToBrowser(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
h := e2e.NewHarness(e2e.WithFileSystem(real.NewRealFileSystem(dir), "/"))
|
||||
h.Logic().BeginRestore(editor.SessionState{File: "/gone.txt", Cursor: 10})
|
||||
h.Run()
|
||||
defer h.Cleanup()
|
||||
h.SendConfig(780, 1688)
|
||||
h.SendScale(2.0)
|
||||
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
return st.Page() == editor.BrowserPage && st.Editor.Filename == "" &&
|
||||
st.Editor.ChunkedBuffer == nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
if v.(bool) {
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("timed out: restore of a missing file did not fall back to the browser")
|
||||
}
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestore_FindBar reopens the file with the find bar open, a query and a
|
||||
// current match: the query must re-scan, select the saved current match (by
|
||||
// byte offset) and NOT re-scroll the restored viewport.
|
||||
func TestRestore_FindBar(t *testing.T) {
|
||||
var b strings.Builder
|
||||
for i := 0; i < 50; i++ {
|
||||
fmt.Fprintf(&b, "filler line number %02d\n", i)
|
||||
}
|
||||
b.WriteString("needle at start of the interesting part\n")
|
||||
b.WriteString("middle filler\n")
|
||||
b.WriteString("needle in the middle of it\n")
|
||||
b.WriteString("tail filler\n")
|
||||
b.WriteString("needle at the end\n")
|
||||
for i := 0; i < 50; i++ {
|
||||
fmt.Fprintf(&b, "trailing filler %02d\n", i)
|
||||
}
|
||||
content := b.String()
|
||||
|
||||
// Offsets of the three "needle" occurrences.
|
||||
first := strings.Index(content, "needle")
|
||||
second := strings.Index(content[first+1:], "needle") + first + 1
|
||||
third := strings.Index(content[second+1:], "needle") + second + 1
|
||||
if first < 0 || second < 0 || third < 0 {
|
||||
t.Fatal("test content lost its needles")
|
||||
}
|
||||
|
||||
h := restoreHarness(t, "findme.txt", content, editor.SessionState{
|
||||
FindQuery: "needle",
|
||||
FindVisible: true,
|
||||
FindCurByte: second,
|
||||
// Cursor/scroll restored at the top: if the re-scan wrongly
|
||||
// re-scrolls to the match, ScrollOffset would no longer be 0.
|
||||
})
|
||||
defer h.Cleanup()
|
||||
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
f := &st.Editor.Find
|
||||
return struct {
|
||||
Visible, Scanning bool
|
||||
N, Cur, SelStart, SelEnd int
|
||||
Scroll ui.Dp
|
||||
}{f.Visible, f.Scanning, len(f.Matches), f.Cur,
|
||||
st.Editor.SelectionStart, st.Editor.SelectionEnd, st.ScrollOffset}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
got := v.(struct {
|
||||
Visible, Scanning bool
|
||||
N, Cur, SelStart, SelEnd int
|
||||
Scroll ui.Dp
|
||||
})
|
||||
if !got.Scanning && got.N == 3 {
|
||||
if !got.Visible {
|
||||
t.Errorf("find bar visible = false, want true")
|
||||
}
|
||||
if got.Cur != 1 {
|
||||
t.Errorf("current match = %d, want 1 (the saved current item)", got.Cur)
|
||||
}
|
||||
if got.SelStart != second || got.SelEnd != second+len("needle") {
|
||||
t.Errorf("selection = [%d,%d), want [%d,%d)",
|
||||
got.SelStart, got.SelEnd, second, second+len("needle"))
|
||||
}
|
||||
if got.Scroll != 0 {
|
||||
t.Errorf("scroll = %v, want 0 (restore must not re-scroll to the match)", got.Scroll)
|
||||
}
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("timed out waiting for the restore re-scan (matches=%d scanning=%v)", got.N, got.Scanning)
|
||||
}
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestore_FindBarClosed keeps the query but does not open the bar: no
|
||||
// scan may run at restore (it would be dropped and leave Scanning stuck);
|
||||
// opening the bar afterwards must re-scan and produce the matches.
|
||||
func TestRestore_FindBarClosed(t *testing.T) {
|
||||
var b strings.Builder
|
||||
for i := 0; i < 60; i++ {
|
||||
fmt.Fprintf(&b, "padding line %02d\n", i)
|
||||
}
|
||||
b.WriteString("needle one\nneedle two\nneedle three\n")
|
||||
content := b.String()
|
||||
|
||||
h := restoreHarness(t, "closed.txt", content, editor.SessionState{
|
||||
FindQuery: "needle",
|
||||
FindVisible: false,
|
||||
})
|
||||
defer h.Cleanup()
|
||||
|
||||
// Give any (wrong) eager scan time to dispatch, then check state.
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
f := &st.Editor.Find
|
||||
return struct {
|
||||
Visible, Scanning bool
|
||||
Query string
|
||||
N int
|
||||
}{f.Visible, f.Scanning, f.Query, len(f.Matches)}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
got := v.(struct {
|
||||
Visible, Scanning bool
|
||||
Query string
|
||||
N int
|
||||
})
|
||||
if got.Visible {
|
||||
t.Errorf("find bar visible = true, want false")
|
||||
}
|
||||
if got.Query != "needle" {
|
||||
t.Errorf("query = %q, want needle", got.Query)
|
||||
}
|
||||
if got.Scanning {
|
||||
t.Errorf("scanning = true after a closed-bar restore; the scan would be dropped and never re-run")
|
||||
}
|
||||
if got.N != 0 {
|
||||
t.Errorf("matches = %d, want 0 (no scan while the bar is closed)", got.N)
|
||||
}
|
||||
|
||||
// Opening the bar now must re-scan (findShow) and find all three.
|
||||
if err := h.WithState(func(st *editor.State) { editor.ToggleFind(nil) }); err != nil {
|
||||
t.Fatalf("ToggleFind: %v", err)
|
||||
}
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
n, err := h.Inspect(func(st *editor.State) any {
|
||||
return len(st.Editor.Find.Matches)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
if n.(int) == 3 {
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("timed out: opening the find bar did not re-scan the restored query")
|
||||
}
|
||||
time.Sleep(25 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestore_SaverSnapshot verifies the persist side: a state change
|
||||
// (cursor + selection + scroll) is picked up by the rate-limited saver as a
|
||||
// snapshot with the right values.
|
||||
func TestRestore_SaverSnapshot(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "small.txt"), []byte("hello world"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
saves := make(chan editor.SessionState, 16)
|
||||
h := e2e.NewHarness(e2e.WithFileSystem(real.NewRealFileSystem(dir), "/"))
|
||||
h.Logic().SetSessionSaver(func(s editor.SessionState) { saves <- s })
|
||||
h.Run()
|
||||
h.SendConfig(780, 1688)
|
||||
h.SendScale(2.0)
|
||||
defer h.Cleanup()
|
||||
|
||||
// Open the file the usual way (browser-row tap path).
|
||||
if err := h.WithState(func(st *editor.State) { editor.OpenFile("/small.txt") }); err != nil {
|
||||
t.Fatalf("OpenFile: %v", err)
|
||||
}
|
||||
time.Sleep(300 * time.Millisecond) // let the load settle
|
||||
|
||||
// Change state on the owner, then force a frame (emitFrame drives the
|
||||
// rate-limited save).
|
||||
if err := h.WithState(func(st *editor.State) {
|
||||
st.Editor.CursorPosition = 6
|
||||
st.Editor.SelectionAnchor = 0
|
||||
st.Editor.SelectionStart = 0
|
||||
st.Editor.SelectionEnd = 5
|
||||
st.ScrollOffset = 10
|
||||
}); err != nil {
|
||||
t.Fatalf("WithState: %v", err)
|
||||
}
|
||||
// The save is rate-limited to one per second; wait past the limit, then
|
||||
// force a frame (emitFrame drives the check).
|
||||
time.Sleep(1100 * time.Millisecond)
|
||||
h.SendConfig(780, 1688)
|
||||
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for {
|
||||
remaining := time.Until(deadline)
|
||||
if remaining <= 0 {
|
||||
t.Fatal("timed out: saver did not receive the expected snapshot")
|
||||
}
|
||||
select {
|
||||
case s := <-saves:
|
||||
if s.File == "/small.txt" && s.Cursor == 6 &&
|
||||
s.SelStart == 0 && s.SelEnd == 5 && s.Scroll == 10 {
|
||||
return
|
||||
}
|
||||
case <-time.After(remaining):
|
||||
t.Fatal("timed out: saver did not receive the expected snapshot")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestore_ShutdownSavesFinalSnapshot verifies Shutdown's unconditional
|
||||
// final save: the last state is persisted even without a rate-limited tick.
|
||||
func TestRestore_ShutdownSavesFinalSnapshot(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("abcdef"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
saves := make(chan editor.SessionState, 16)
|
||||
l := editor.NewLogic(real.NewRealFileSystem(dir), "/", func(string) {})
|
||||
l.SetSessionSaver(func(s editor.SessionState) { saves <- s })
|
||||
go l.Run()
|
||||
|
||||
// Open the file (the goroutine-send keeps this off the owner's select).
|
||||
if _, ok := l.Inspect(func(st *editor.State) any {
|
||||
editor.OpenFile("/a.txt")
|
||||
return nil
|
||||
}); !ok {
|
||||
t.Fatal("Inspect timed out")
|
||||
}
|
||||
for i := 0; i < 100; i++ {
|
||||
loaded, ok := l.Inspect(func(st *editor.State) any {
|
||||
cb := st.Editor.ChunkedBuffer
|
||||
return cb != nil && cb.FileLen() == 6
|
||||
})
|
||||
if ok && loaded.(bool) {
|
||||
break
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if _, ok := l.Inspect(func(st *editor.State) any {
|
||||
st.Editor.CursorPosition = 3
|
||||
return nil
|
||||
}); !ok {
|
||||
t.Fatal("Inspect timed out")
|
||||
}
|
||||
l.Shutdown()
|
||||
|
||||
deadline := time.After(3 * time.Second)
|
||||
for {
|
||||
select {
|
||||
case s := <-saves:
|
||||
if s.File == "/a.txt" && s.Cursor == 3 {
|
||||
return
|
||||
}
|
||||
case <-deadline:
|
||||
t.Fatal("timed out: Shutdown did not persist a snapshot with the final state")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestore_ScrollSurvivesWrapState reproduces the on-device report
|
||||
// "relaunch lands further DOWN than where I left off": the snapshot's pixel
|
||||
// scroll lives in visual-line space (wrapped lines occupy several visual
|
||||
// lines, and the scroll-to-line mapping runs through the WrapIndex), but on
|
||||
// relaunch the counts of every line above the restored viewport are the
|
||||
// estimate (1) until shaped, and lines above the visible window are never
|
||||
// shaped. Mapping the saved offset through that fresh index lands a logical
|
||||
// line deeper by all the wrapped continuations above it. The snapshot
|
||||
// therefore persists the logical line at the viewport top (and the sub-line
|
||||
// remainder), and the restore derives the offset from the line.
|
||||
func TestRestore_ScrollSurvivesWrapState(t *testing.T) {
|
||||
const lines = 300
|
||||
var b strings.Builder
|
||||
for i := 0; i < lines; i++ {
|
||||
fmt.Fprintf(&b, "line %03d content\n", i)
|
||||
}
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "wrap.txt"), []byte(b.String()), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// First session: open the file, record the first 150 lines as wrapped
|
||||
// into 3 visual lines each (as a shaping pass would), and scroll so the
|
||||
// viewport top sits on logical line 200. In visual space that is line
|
||||
// 150*3 + 50 = 500, so the pixel offset is 500 line-heights.
|
||||
saves := make(chan editor.SessionState, 16)
|
||||
h1 := e2e.NewHarness(e2e.WithFileSystem(real.NewRealFileSystem(dir), "/"))
|
||||
h1.Logic().SetSessionSaver(func(s editor.SessionState) { saves <- s })
|
||||
h1.Run()
|
||||
h1.SendConfig(780, 1688)
|
||||
h1.SendScale(2.0)
|
||||
// The harness runs against the package globals (TheState/TheLogic), so
|
||||
// only ONE harness may be alive at a time: h1 is stopped (not deferred)
|
||||
// as soon as its snapshot is in hand, before h2's NewLogic overwrites
|
||||
// the globals out from under h1's still-running goroutines.
|
||||
h1Cleaned := false
|
||||
cleanup1 := func() {
|
||||
if !h1Cleaned {
|
||||
h1Cleaned = true
|
||||
h1.Cleanup()
|
||||
}
|
||||
}
|
||||
defer cleanup1()
|
||||
|
||||
if err := h1.WithState(func(st *editor.State) { editor.OpenFile("/wrap.txt") }); err != nil {
|
||||
t.Fatalf("OpenFile: %v", err)
|
||||
}
|
||||
for i := 0; i < 100; i++ {
|
||||
v, err := h1.Inspect(func(st *editor.State) any {
|
||||
cb := st.Editor.ChunkedBuffer
|
||||
return cb != nil && cb.FileLen() > 0 && cb.LineIndex != nil
|
||||
})
|
||||
if err == nil && v.(bool) {
|
||||
break
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
// Set the wrapped counts and the deep scroll on the owner; the line
|
||||
// height comes from the owner too (EffectiveLineHeight reads state).
|
||||
lhAny, err := h1.Inspect(func(st *editor.State) any {
|
||||
w := st.Editor.ChunkedBuffer.WrapIndex
|
||||
for i := 0; i < 150; i++ {
|
||||
w.Set(i, 3)
|
||||
}
|
||||
st.ScrollOffset = ui.Dp(500 * float64(editor.EffectiveLineHeight()))
|
||||
return float64(editor.EffectiveLineHeight())
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
lh := lhAny.(float64)
|
||||
// Wait past the rate limit and force a frame: the saver must capture
|
||||
// the deep scroll together with its logical line (200). Match on the
|
||||
// line, not the exact Dp: ui.Dp is float32-based, so ui.Dp(500*lh) and
|
||||
// 500*float64(lh) round differently.
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
h1.SendConfig(780, 1688)
|
||||
snap := editor.SessionState{}
|
||||
deadline := time.After(5 * time.Second)
|
||||
for {
|
||||
select {
|
||||
case s := <-saves:
|
||||
if s.File == "/wrap.txt" && s.ScrollLine == 200 && s.Scroll > 0 {
|
||||
snap = s
|
||||
goto got
|
||||
}
|
||||
case <-deadline:
|
||||
t.Fatal("timed out: saver did not capture the deep-scroll snapshot")
|
||||
}
|
||||
}
|
||||
got:
|
||||
if snap.Scroll < 499*lh || snap.Scroll > 501*lh {
|
||||
t.Fatalf("snapshot Scroll = %v, want ~500*lh (%v): the pixel offset must stay the visual-space value",
|
||||
snap.Scroll, 500*lh)
|
||||
}
|
||||
if snap.ScrollLine != 200 {
|
||||
t.Fatalf("snapshot ScrollLine = %d, want 200 (the logical line at the viewport top under wrap)", snap.ScrollLine)
|
||||
}
|
||||
if snap.ScrollSub >= 0.01 { // 0 apart from ui.Dp float32 rounding
|
||||
t.Errorf("snapshot ScrollSub = %v, want ~0", snap.ScrollSub)
|
||||
}
|
||||
|
||||
// h1's job is done: stop it before h2 overwrites the package globals.
|
||||
cleanup1()
|
||||
|
||||
// Second session (the relaunch): the WrapIndex is fresh, every count the
|
||||
// estimate (1). The restore must land on logical line 200, not on visual
|
||||
// line 500 mapped 1:1 (which would be line 500, far deeper).
|
||||
h2 := e2e.NewHarness(e2e.WithFileSystem(real.NewRealFileSystem(dir), "/"))
|
||||
h2.Logic().BeginRestore(snap)
|
||||
h2.Run()
|
||||
h2.SendConfig(780, 1688)
|
||||
h2.SendScale(2.0)
|
||||
defer h2.Cleanup()
|
||||
|
||||
// The restore must land the scroll within one line of 200*lh (the ui.Dp
|
||||
// float32 rounding keeps an exact match out of reach); the exact
|
||||
// assertion is the window start line below.
|
||||
for i := 0; i < 100; i++ {
|
||||
v, err := h2.Inspect(func(st *editor.State) any {
|
||||
cb := st.Editor.ChunkedBuffer
|
||||
if cb == nil || cb.FileLen() == 0 || cb.LineIndex == nil {
|
||||
return false
|
||||
}
|
||||
s := float64(st.ScrollOffset)
|
||||
return s > 199*lh && s < 201*lh
|
||||
})
|
||||
if err == nil && v.(bool) {
|
||||
break
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
// One more frame so the layout pass records the window start line for
|
||||
// the landed scroll.
|
||||
h2.SendConfig(780, 1688)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
v, err := h2.Inspect(func(st *editor.State) any {
|
||||
return struct {
|
||||
Scroll ui.Dp
|
||||
WinLine int
|
||||
}{st.ScrollOffset, st.WindowStartLine}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
got := v.(struct {
|
||||
Scroll ui.Dp
|
||||
WinLine int
|
||||
})
|
||||
if s := float64(got.Scroll); s < 199*lh || s > 201*lh {
|
||||
t.Errorf("restored scroll = %v, want ~200*lh (%v): the saved logical line's offset",
|
||||
got.Scroll, 200*lh)
|
||||
}
|
||||
if got.WinLine != 200 {
|
||||
t.Errorf("window starts at line %d, want 200 (the saved line); a deeper line is the pre-fix visual-space mapping", got.WinLine)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestore_LinePinHoldsAcrossLateWrapFeedback reproduces the on-device
|
||||
// report "relaunch lands further UP than where I left off" (Pixel 9 Pro):
|
||||
// while the restore scroll is still armed, the top-of-file window is what's
|
||||
// rendered, and its shaping feedback (real wrap counts for the lines ABOVE
|
||||
// the restored line) can land before or just after the offset is applied.
|
||||
// Those counts are correct data, but they change the offset-to-line mapping:
|
||||
// the line-derived offset was synthesized for the all-estimate index, so
|
||||
// once counts land below the pinned line the same offset maps to a shallower
|
||||
// line and the viewport drifts up. The restore therefore pins the logical
|
||||
// line: every wrap correction re-derives the offset as V(L)*lh + sub until
|
||||
// the restored window itself has shaped (or a timeout, or the user scrolls).
|
||||
func TestRestore_LinePinHoldsAcrossLateWrapFeedback(t *testing.T) {
|
||||
const lines = 300
|
||||
const lineLen = 17 // "line %03d content\n"
|
||||
var b strings.Builder
|
||||
for i := 0; i < lines; i++ {
|
||||
fmt.Fprintf(&b, "line %03d content\n", i)
|
||||
}
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "wrap.txt"), []byte(b.String()), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// The harness runs at the default font scale, so the stateless variant
|
||||
// matches what the owner will compute.
|
||||
lh := float64(editor.EffectiveLineHeightAt(1))
|
||||
snap := editor.SessionState{
|
||||
File: "/wrap.txt",
|
||||
Cursor: 0,
|
||||
Scroll: float64(ui.Dp(500 * lh)), // the saving session's visual-space offset
|
||||
ScrollLine: 200, // the logical line at the viewport top
|
||||
ScrollSub: 0,
|
||||
}
|
||||
|
||||
h := e2e.NewHarness(e2e.WithFileSystem(real.NewRealFileSystem(dir), "/"))
|
||||
h.Logic().BeginRestore(snap)
|
||||
h.Run()
|
||||
h.SendConfig(780, 1688)
|
||||
h.SendScale(2.0)
|
||||
defer h.Cleanup()
|
||||
|
||||
// Wait for the restore to land: with the fresh all-estimate index the
|
||||
// offset maps to exactly line 200.
|
||||
var winLine int
|
||||
for i := 0; i < 100; i++ {
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
cb := st.Editor.ChunkedBuffer
|
||||
if cb == nil || cb.FileLen() == 0 || cb.LineIndex == nil {
|
||||
return -1
|
||||
}
|
||||
return st.WindowStartLine
|
||||
})
|
||||
if err == nil {
|
||||
winLine = v.(int)
|
||||
if winLine == 200 {
|
||||
break
|
||||
}
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if winLine != 200 {
|
||||
t.Fatalf("restore did not land on line 200 (window at %d)", winLine)
|
||||
}
|
||||
|
||||
// Replay the on-device interleaving: the top-of-file window (lines
|
||||
// 0-49) was shaped while the scroll was still armed, and its feedback —
|
||||
// real wrap counts (3 visual lines each for those lines) — arrives now,
|
||||
// after the offset was applied.
|
||||
editSeq, err := h.Inspect(func(st *editor.State) any {
|
||||
return st.Editor.EditSeq
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
var wt strings.Builder
|
||||
for i := 0; i < 50; i++ {
|
||||
fmt.Fprintf(&wt, "line %03d content\n", i)
|
||||
}
|
||||
windowText := wt.String()
|
||||
var starts []int
|
||||
for i := 0; i < 50; i++ {
|
||||
base := i * lineLen
|
||||
starts = append(starts, base, base+5, base+10) // 3 visual lines per line
|
||||
}
|
||||
h.Logic().LayoutChan() <- ui.LayoutFeedback{
|
||||
GlyphLayout: ui.GlyphLayout{
|
||||
VisualLineStarts: starts,
|
||||
},
|
||||
WindowText: windowText,
|
||||
WindowStartByte: 0,
|
||||
WindowStartLine: 0,
|
||||
EditSeq: editSeq.(uint64),
|
||||
}
|
||||
|
||||
// The correction must NOT drag the viewport: the pin re-derives the
|
||||
// offset as V(200)*lh, where V(200) = 50*3 + 150 = 300 under the
|
||||
// corrected index, keeping line 200 at the top. Pre-fix, the applied
|
||||
// offset (200*lh) mapped to line ~66 under the corrected index.
|
||||
for i := 0; i < 100; i++ {
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
return struct {
|
||||
Scroll float64
|
||||
WinLine int
|
||||
}{float64(st.ScrollOffset), st.WindowStartLine}
|
||||
})
|
||||
if err == nil {
|
||||
got := v.(struct {
|
||||
Scroll float64
|
||||
WinLine int
|
||||
})
|
||||
if got.WinLine == 200 && got.Scroll > 299*lh && got.Scroll < 301*lh {
|
||||
return // held
|
||||
}
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
return struct {
|
||||
Scroll float64
|
||||
WinLine int
|
||||
}{float64(st.ScrollOffset), st.WindowStartLine}
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
got := v.(struct {
|
||||
Scroll float64
|
||||
WinLine int
|
||||
})
|
||||
if got.WinLine != 200 {
|
||||
t.Errorf("window at line %d after late wrap feedback, want 200: the line-pin must hold the viewport on the restored line (a shallower line is the pre-fix drift)", got.WinLine)
|
||||
}
|
||||
if got.Scroll < 499*lh || got.Scroll > 501*lh {
|
||||
t.Errorf("scroll = %v after late wrap feedback, want ~300*lh (%v): V(200) under the corrected index", got.Scroll, 300*lh)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestore_UrgentSaveOnSelection verifies the kill-race fix: a selection
|
||||
// appearing (the user just highlighted text) saves immediately, even inside
|
||||
// the rate-limit window that the file-open save opened. Without the urgent
|
||||
// path, killing the app (recents-wipe) less than a second after highlighting
|
||||
// lost the selection and cursor.
|
||||
func TestRestore_UrgentSaveOnSelection(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("abcdef"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
saves := make(chan editor.SessionState, 16)
|
||||
l := editor.NewLogic(real.NewRealFileSystem(dir), "/", func(string) {})
|
||||
l.SetSessionSaver(func(s editor.SessionState) { saves <- s })
|
||||
go l.Run()
|
||||
defer l.Shutdown()
|
||||
|
||||
// Open the file: the file change is itself urgent, so a save lands as
|
||||
// the load settles. Drain everything up to and including the loaded
|
||||
// snapshot.
|
||||
if _, ok := l.Inspect(func(st *editor.State) any {
|
||||
editor.OpenFile("/a.txt")
|
||||
return nil
|
||||
}); !ok {
|
||||
t.Fatal("Inspect timed out")
|
||||
}
|
||||
loaded := false
|
||||
for i := 0; i < 100 && !loaded; i++ {
|
||||
v, ok := l.Inspect(func(st *editor.State) any {
|
||||
cb := st.Editor.ChunkedBuffer
|
||||
return cb != nil && cb.FileLen() == 6
|
||||
})
|
||||
if ok {
|
||||
loaded = v.(bool)
|
||||
}
|
||||
if loaded {
|
||||
break
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if !loaded {
|
||||
t.Fatal("file did not load")
|
||||
}
|
||||
// Drain saves up to the first one describing the loaded file.
|
||||
deadline := time.After(3 * time.Second)
|
||||
for {
|
||||
select {
|
||||
case s := <-saves:
|
||||
if s.File == "/a.txt" {
|
||||
goto drainDone
|
||||
}
|
||||
case <-deadline:
|
||||
t.Fatal("timed out: no save for the opened file")
|
||||
}
|
||||
}
|
||||
drainDone:
|
||||
// Immediately (well inside any rate-limit window after the saves above),
|
||||
// make a selection appear and force a frame: the urgent path must save it
|
||||
// now, not after the interval elapses.
|
||||
if _, ok := l.Inspect(func(st *editor.State) any {
|
||||
st.Editor.SelectionAnchor = 1
|
||||
st.Editor.SelectionStart = 1
|
||||
st.Editor.SelectionEnd = 4
|
||||
st.Editor.CursorPosition = 4
|
||||
st.ScrollOffset = 3
|
||||
return nil
|
||||
}); !ok {
|
||||
t.Fatal("Inspect timed out")
|
||||
}
|
||||
l.ConfigChan() <- editor.ConfigEvent{PixelWidth: 780, PixelHeight: 1688} // force emitFrame
|
||||
|
||||
select {
|
||||
case s := <-saves:
|
||||
if s.SelStart == 1 && s.SelEnd == 4 && s.Cursor == 4 {
|
||||
return
|
||||
}
|
||||
t.Fatalf("unexpected snapshot: %+v", s)
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("timed out: selection appearance was not saved immediately (urgent path)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRestore_BrowserPageSession covers the relaunch bug where a session
|
||||
// ended on the browser page still re-opened the last edited file: a
|
||||
// snapshot taken on the browser page must mark InBrowser (with the browser
|
||||
// directory and no file), and a relaunch handed that snapshot must land
|
||||
// back in the browser at the saved directory.
|
||||
func TestRestore_BrowserPageSession(t *testing.T) {
|
||||
// Snapshot on the browser page.
|
||||
h := e2e.NewHarnessWithDefaults()
|
||||
defer h.Cleanup()
|
||||
if err := h.WithState(func(st *editor.State) {
|
||||
st.Browser.CurrentPath = "/Documents/Work"
|
||||
}); err != nil {
|
||||
t.Fatalf("WithState: %v", err)
|
||||
}
|
||||
v, err := h.Inspect(func(st *editor.State) any {
|
||||
return h.Logic().SnapshotSession()
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
sess := v.(editor.SessionState)
|
||||
if !sess.InBrowser {
|
||||
t.Fatalf("browser-page snapshot: InBrowser = false, want true: %+v", sess)
|
||||
}
|
||||
if sess.File != "" {
|
||||
t.Errorf("browser-page snapshot: File = %q, want \"\"", sess.File)
|
||||
}
|
||||
if sess.BrowserPath != "/Documents/Work" {
|
||||
t.Errorf("browser-page snapshot: BrowserPath = %q, want \"/Documents/Work\"", sess.BrowserPath)
|
||||
}
|
||||
|
||||
// Relaunch with that session: land in the browser at the saved dir.
|
||||
h2 := e2e.NewHarness()
|
||||
defer h2.Cleanup()
|
||||
h2.Logic().BeginBrowserRestore(sess.BrowserPath)
|
||||
h2.Run()
|
||||
h2.SendConfig(780, 1688)
|
||||
h2.SendScale(2.0)
|
||||
if _, err := e2e.WaitForNewFrame(h2, 0, 5*time.Second); err != nil {
|
||||
t.Fatalf("timeout waiting for frames: %v", err)
|
||||
}
|
||||
v, err = h2.Inspect(func(st *editor.State) any {
|
||||
return st.Page() == editor.BrowserPage && st.Browser.CurrentPath == sess.BrowserPath
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Inspect: %v", err)
|
||||
}
|
||||
if !v.(bool) {
|
||||
t.Fatal("relaunch with a browser session did not land on the browser page at the saved directory")
|
||||
}
|
||||
}
|
||||
+58
-94
@@ -213,6 +213,30 @@ type TextField struct {
|
||||
// is running (the IME insets dispatches redraw the app), so the keyboard
|
||||
// could not be dismissed. See TextField.Draw.
|
||||
ShowIMESeq uint64
|
||||
// IME snippet (hysteresis window around the caret — see
|
||||
// State.computeIMESnippetWindow): IMESnippetText is the pushed text,
|
||||
// IMESnippet{Start,End}Rune its absolute file rune range, IMECaretRune
|
||||
// the caret's absolute rune, and IMESel*Rune the live selection's rune
|
||||
// range (-1/-1 when no selection).
|
||||
IMESnippetText string
|
||||
IMESnippetStartRune int
|
||||
IMESnippetEndRune int
|
||||
IMECaretRune int
|
||||
IMESelStartRune int
|
||||
IMESelEndRune int
|
||||
// IMEForceResync: re-push the snippet trimmed by one rune this frame
|
||||
// (a forced restartInput that heals a desynchronized IME; see
|
||||
// EditorState.IMEForceResync).
|
||||
IMEForceResync bool
|
||||
// EditSeq is the buffer edit sequence at frame-build time (0 for
|
||||
// non-editor fields); the renderer uses it to detect the frame that
|
||||
// carries an IME commit's result (see Renderer.imeHold).
|
||||
EditSeq uint64
|
||||
// WindowStartByte is the absolute file byte offset where Value (the
|
||||
// visible window) begins; -1/0 for whole-buffer fields. Used by the
|
||||
// renderer's IME model to translate snippet positions back to file bytes
|
||||
// (see Renderer.ModelTranslate).
|
||||
WindowStartByte int
|
||||
}
|
||||
|
||||
func (tf TextField) Type() string { return "textfield" }
|
||||
@@ -231,14 +255,6 @@ func (tf TextField) String() string {
|
||||
// and draws display lines inline — one LayoutString call, no double-shaping.
|
||||
func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
|
||||
if tf.Focused {
|
||||
// (Re-)gained focus for this field (or a different field than the one the
|
||||
// dedup state currently tracks): force a fresh snippet/selection push so
|
||||
// the IME starts from a known state.
|
||||
if r.lastIMEField != tf.id || !r.lastWasFocused {
|
||||
r.lastSnippet = key.Snippet{}
|
||||
r.lastSelStart = -1
|
||||
r.lastSelCaret = -1
|
||||
}
|
||||
r.lastIMEField = tf.id
|
||||
r.lastWasFocused = true
|
||||
// Issue key.FocusCmd only on a focus transition (see
|
||||
@@ -261,102 +277,32 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
|
||||
r.lastIMEShowSeq = tf.ShowIMESeq
|
||||
gtx.Execute(key.SoftKeyboardCmd{Show: true})
|
||||
}
|
||||
// IME wiring. The visible window (tf.Value) is pushed as the snippet
|
||||
// with Range {0, len}, so the IME treats the window as the document and
|
||||
// reports EditEvent.Range window-relative. This lets swipe/autocorrect
|
||||
// operate on the visible text without shipping the whole file to the IME.
|
||||
//
|
||||
// Item 4: tell the IME this is a text field (enables the text keyboard,
|
||||
// Tell the IME this is a text field (enables the text keyboard,
|
||||
// autocorrect, and suggestions).
|
||||
key.InputHintOp{Tag: tf.id, Hint: key.HintText}.Add(gtx.Ops)
|
||||
// Item 2: push the snippet (the visible window) for swipe/autocorrect,
|
||||
// but only when it changed. Re-pushing an unchanged snippet every frame
|
||||
// resets the IME's composition/cursor, which desyncs fast commits (see
|
||||
// widget.Editor's updateSnippet dedup).
|
||||
snippet := key.Snippet{
|
||||
Range: key.Range{Start: 0, End: runeCount(tf.Value, len(tf.Value))},
|
||||
Text: tf.Value,
|
||||
}
|
||||
if snippet != r.lastSnippet {
|
||||
r.lastSnippet = snippet
|
||||
gtx.Execute(key.SnippetCmd{Tag: tf.id, Snippet: snippet})
|
||||
}
|
||||
// Item 1: sync the caret/selection so the IME's selection matches.
|
||||
// Window-relative rune indices (tf.CursorPosition and the selection
|
||||
// bounds are byte offsets into tf.Value). With a selection, push the
|
||||
// full range so the IME highlights it and a commit replaces it (the
|
||||
// logic side unions the commit range with the selection, so the
|
||||
// replacement is deterministic regardless of what the IME reports).
|
||||
// Push only when the (start, end) pair changes, so a static selection
|
||||
// does not reset the IME every frame.
|
||||
var selStart, selEnd int
|
||||
if tf.SelectionStart >= 0 && tf.SelectionEnd > tf.SelectionStart {
|
||||
// Clamp to the window (the element may be built from a window that
|
||||
// does not fully contain the selection).
|
||||
s := tf.SelectionStart
|
||||
if s < 0 {
|
||||
s = 0
|
||||
}
|
||||
e := tf.SelectionEnd
|
||||
if e > len(tf.Value) {
|
||||
e = len(tf.Value)
|
||||
}
|
||||
selStart = runeCount(tf.Value, s)
|
||||
selEnd = runeCount(tf.Value, e)
|
||||
} else {
|
||||
selStart = -1
|
||||
selEnd = runeCount(tf.Value, tf.CursorPosition)
|
||||
}
|
||||
if selStart != r.lastSelStart || selEnd != r.lastSelCaret {
|
||||
// While a selection/caret handle drag is in progress, defer the
|
||||
// IME selection sync (see Renderer.selDragActive): pushing a
|
||||
// SelectionCmd every frame the drag moves the selection triggers the
|
||||
// input router's immediate-command replay of the frame's pointer
|
||||
// events, which re-injects the drag into every gesture and makes the
|
||||
// selection jump. The final selection is pushed on the first frame
|
||||
// after the drag ends (lastSelStart/lastSelCaret were not updated,
|
||||
// so the mismatch persists until then).
|
||||
if !r.selDragActive {
|
||||
r.lastSelStart = selStart
|
||||
r.lastSelCaret = selEnd
|
||||
rng := key.Range{Start: selStart, End: selEnd}
|
||||
if selStart < 0 {
|
||||
rng = key.Range{Start: selEnd, End: selEnd}
|
||||
}
|
||||
gtx.Execute(key.SelectionCmd{Tag: tf.id, Range: rng, Caret: key.Caret{}})
|
||||
}
|
||||
}
|
||||
// The IME snippet and selection are NOT pushed here. They are pushed
|
||||
// by Renderer.FlushIME, which the main loop calls after this frame's
|
||||
// key events were drained (and mirrored into the renderer's IME model)
|
||||
// and before the ops are flushed to the OS. Pushing the snippet from
|
||||
// Draw runs before the logic has applied the frame's IME edit, so the
|
||||
// commit frame would push the pre-edit text; gioui then sees the
|
||||
// mismatch with the IME's own post-commit snippet and calls
|
||||
// imm.restartInput() — resetting the keyboard's auto-cap state (the
|
||||
// random mid-word capitals) on every keystroke. See FlushIME.
|
||||
} else if r.lastIMEField == tf.id {
|
||||
// This (previously-focused) field lost focus: forget it so the next focus
|
||||
// pushes a fresh snippet/selection. Resetting lastIMEShowSeq makes the
|
||||
// next focus re-raise the keyboard even without a fresh pulse.
|
||||
// This (previously-focused) field lost focus: forget it so the next
|
||||
// focus pushes a fresh snippet/selection. Resetting lastIMEShowSeq
|
||||
// makes the next focus re-raise the keyboard even without a fresh
|
||||
// pulse; resetting lastSnippet makes the next focus re-push the
|
||||
// snippet even if the text is unchanged.
|
||||
r.lastIMEField = ""
|
||||
r.lastWasFocused = false
|
||||
r.lastSnippet = key.Snippet{}
|
||||
r.lastSelStart = -1
|
||||
r.lastSelCaret = -1
|
||||
r.lastIMEShowSeq = 0
|
||||
r.lastSnippet = key.Snippet{}
|
||||
}
|
||||
r.drawWrappedText(gtx, tf.Value, tf.region, tf.WordWrap, tf.WrapWidth, tf.ScrollOffset, tf.CursorPosition, tf.SelectionStart, tf.SelectionEnd, tf.CaretDrag, tf.MatchRanges, tf.CurrentMatch, tf.Focused)
|
||||
}
|
||||
|
||||
// runeCount returns the number of UTF-8 runes in s[:bytePos] (bytePos is a
|
||||
// byte offset, clamped to len(s)). A rune starts at an ASCII byte (<0x80) or a
|
||||
// multi-byte lead byte (>=0xC0); 0x80-0xBF are continuation bytes.
|
||||
func runeCount(s string, bytePos int) int {
|
||||
if bytePos > len(s) {
|
||||
bytePos = len(s)
|
||||
}
|
||||
n := 0
|
||||
for i := 0; i < bytePos; i++ {
|
||||
b := s[i]
|
||||
if b < 0x80 || b >= 0xC0 {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// NewTextField creates a visible multiline TextField.
|
||||
func NewTextField(id string, value string, region Region, wordWrap bool, wrapWidth Dp, scrollOffset Dp, cursorPos int, selStart, selEnd int, interactions []Interaction) TextField {
|
||||
return TextField{
|
||||
@@ -900,6 +846,11 @@ const (
|
||||
// The renderer registers the underlying gesture.Drag ops itself (it owns
|
||||
// the handle geometry); this interaction just delivers the logic handler.
|
||||
SelDrag
|
||||
// Pinch is a two-finger pinch inside the element's text region. The
|
||||
// renderer owns the probe (it needs raw two-pointer geometry that no
|
||||
// single gesture primitive in Gio v0.10 provides); this interaction just
|
||||
// delivers the logic handler, which receives FontPinchEvent.
|
||||
Pinch
|
||||
)
|
||||
|
||||
// Interaction pairs a gesture type with a handler function.
|
||||
@@ -950,6 +901,19 @@ type SelectionDragEvent struct {
|
||||
// SelectionDragEnd is emitted when a selection/caret drag is released.
|
||||
type SelectionDragEnd struct{}
|
||||
|
||||
// FontPinchEvent carries one frame's relative two-finger pinch factor
|
||||
// (current inter-finger distance / previous frame's distance, both in px).
|
||||
// The logic applies it as a multiplier to the app-local font scale; the
|
||||
// value is a plain float32 ratio with no rounding, so the font size is
|
||||
// continuous, never snapped to whole points. Center is the pinch midpoint
|
||||
// (the average of the two fingers) in app-local window Dp — the same space
|
||||
// as Point; the logic anchors the content under this point so it stays put
|
||||
// while the font scales.
|
||||
type FontPinchEvent struct {
|
||||
Scale float32
|
||||
Center Point
|
||||
}
|
||||
|
||||
// MenuItem is one button in the selection menu.
|
||||
// X/Y/W/H are relative to the Menu region.
|
||||
type MenuItem struct {
|
||||
|
||||
@@ -5,7 +5,7 @@ const (
|
||||
StatusBarLineHeight = Dp(24)
|
||||
StatusBarFilenameLine = Dp(24)
|
||||
StatusBarIconsLine = Dp(24)
|
||||
BottomBarHeight = Dp(24)
|
||||
BottomBarHeight = Dp(31) // ~30% taller than the old 24dp: the bar's only tap target (the wrap toggle) is a text label, so the bar doubles as its hit area
|
||||
IconSize = Dp(24)
|
||||
IconGap = Dp(36)
|
||||
Padding = Dp(8)
|
||||
|
||||
@@ -0,0 +1,510 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/io/pointer"
|
||||
)
|
||||
|
||||
// pev builds a synthetic pointer event. Time is explicit: the Android driver
|
||||
// supplies it per event and the freshness window runs on it.
|
||||
func pev(kind pointer.Kind, id pointer.ID, x, y float32, at time.Duration) pointer.Event {
|
||||
return pointer.Event{Kind: kind, PointerID: id, Position: f32.Point{X: x, Y: y}, Time: at}
|
||||
}
|
||||
|
||||
// ms/s are time helpers so test timelines read as intended (a bare integer
|
||||
// constant is nanoseconds — the first draft's "2000ms" was 2µs).
|
||||
func ms(n int) time.Duration { return time.Duration(n) * time.Millisecond }
|
||||
func s(n int) time.Duration { return time.Duration(n) * time.Second }
|
||||
|
||||
// runFrame feeds a batch of events (one frame's drain) through the tracker
|
||||
// and returns the frame's factor, the grabs it asked for, and the
|
||||
// survivor-finger scroll, mirroring consumePinchProbe.
|
||||
func runFrame(t *pinchTracker, evts ...pointer.Event) (f float32, mid f32.Point, ok bool, grabs []pointer.ID, scroll int) {
|
||||
for _, e := range evts {
|
||||
if s := t.step(e); len(s.grabs) > 0 {
|
||||
grabs = append(grabs, s.grabs...)
|
||||
}
|
||||
}
|
||||
f, mid, ok, g2 := t.factor()
|
||||
grabs = append(grabs, g2...)
|
||||
scroll = t.survivorScroll()
|
||||
return
|
||||
}
|
||||
|
||||
// approx reports whether a and b are within 1e-3.
|
||||
func approx(a, b float64) bool { return math.Abs(a-b) < 1e-3 }
|
||||
|
||||
// A clean two-finger pinch: the pair forms when BOTH fingers move (the
|
||||
// two-mover rule), owes the spread that happened by then (baseline = press
|
||||
// distance), then one factor per frame whose product telescopes to the total
|
||||
// distance ratio — the property that makes the font track the fingers.
|
||||
func TestTrackerPinchFactorSeries(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
// Frame 1: both fingers down 200px apart. Pending: no pair, no grabs,
|
||||
// no factor (a press alone is not a pinch).
|
||||
_, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1),
|
||||
)
|
||||
if ok || len(grabs) != 0 {
|
||||
t.Fatalf("press-only frame: ok=%v grabs=%v want nothing", ok, grabs)
|
||||
}
|
||||
// Frames 2-4: the pair spreads 200 -> 240 -> 300 -> 250. Frame 2 is the
|
||||
// formation: both fingers moved 20px (movers), grabs issued, and the
|
||||
// owed factor is measured against the PRESS distance (200).
|
||||
f2, _, ok2, g2, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 80, 100, 10),
|
||||
pev(pointer.Drag, 1, 320, 100, 11),
|
||||
)
|
||||
if !ok2 || !approx(float64(f2), 240.0/200) {
|
||||
t.Fatalf("f2=%v ok=%v want %v", f2, ok2, 240/200)
|
||||
}
|
||||
if len(g2) != 2 {
|
||||
t.Fatalf("formation frame must grab both fingers, got %v", g2)
|
||||
}
|
||||
f3, mid3, ok3, _, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 50, 100, 20),
|
||||
pev(pointer.Drag, 1, 350, 100, 21),
|
||||
)
|
||||
if !ok3 || !approx(float64(f3), 300.0/240) {
|
||||
t.Fatalf("f3=%v ok=%v want %v", f3, ok3, 300/240)
|
||||
}
|
||||
if mid3 != (f32.Point{X: 200, Y: 100}) {
|
||||
t.Fatalf("mid3=%v want (200,100)", mid3)
|
||||
}
|
||||
f4, _, ok4, _, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 125, 100, 30),
|
||||
pev(pointer.Drag, 1, 375, 100, 31),
|
||||
)
|
||||
if !ok4 || !approx(float64(f4), 250.0/300) {
|
||||
t.Fatalf("f4=%v ok=%v want %v", f4, ok4, 250.0/300)
|
||||
}
|
||||
total := f2 * f3 * f4
|
||||
if !approx(float64(total), 250.0/200) {
|
||||
t.Fatalf("product=%v want total ratio %v", total, 250/200)
|
||||
}
|
||||
}
|
||||
|
||||
// A single finger — press, hold, long scroll — must never produce a factor
|
||||
// or a grab. This is the device regression "one-finger scroll changes the
|
||||
// font": the old code paired the scroll finger with whatever stale pointer
|
||||
// was in the map.
|
||||
func TestTrackerSingleFingerNeverScales(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
// A pinch earlier in the session...
|
||||
runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1),
|
||||
)
|
||||
runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 80, 100, 10),
|
||||
pev(pointer.Drag, 1, 320, 100, 11),
|
||||
)
|
||||
runFrame(&tr,
|
||||
pev(pointer.Release, 0, 80, 100, 20),
|
||||
pev(pointer.Release, 1, 320, 100, 21),
|
||||
)
|
||||
// ...now a single finger scrolls for many frames.
|
||||
for i := 0; i < 20; i++ {
|
||||
y := float32(400 - i*20)
|
||||
_, _, ok, grabs, scroll := runFrame(&tr,
|
||||
pev(pointer.Drag, 5, 200, y, time.Duration(100+i*10)))
|
||||
if ok || len(grabs) != 0 || scroll != 0 {
|
||||
t.Fatalf("frame %d: single finger produced factor ok=%v grabs=%v scroll=%v",
|
||||
i, ok, grabs, scroll)
|
||||
}
|
||||
}
|
||||
// Fresh pointer IDs (Android resets them after full release) — still nothing.
|
||||
_, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Press, 0, 200, 500, 500),
|
||||
)
|
||||
if ok || len(grabs) != 0 {
|
||||
t.Fatalf("fresh single finger: ok=%v grabs=%v", ok, grabs)
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
_, _, ok, grabs, _ = runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 200, float32(500-i*15), time.Duration(510+i*10)))
|
||||
if ok || len(grabs) != 0 {
|
||||
t.Fatalf("scroll frame %d: ok=%v grabs=%v", i, ok, grabs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Device regression "sudden dramatic zoom before pinching": a finger that
|
||||
// has been RESTING (palm edge, parked pinky) must not be paired with a new
|
||||
// one. The freshness window prunes it; the pair forms only from fresh
|
||||
// fingers.
|
||||
func TestTrackerRestingFingerNotPaired(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
// Palm rests at t=0.
|
||||
_, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Press, 3, 600, 800, 0))
|
||||
if ok || len(grabs) != 0 {
|
||||
t.Fatal("one resting finger must not pinch")
|
||||
}
|
||||
// At t=2s the thumb lands: the palm is stale (>300ms) and pruned.
|
||||
_, _, ok, grabs, _ = runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, s(2)),
|
||||
pev(pointer.Drag, 3, 605, 800, s(2)+ms(1)), // palm still there, drifting
|
||||
)
|
||||
if ok || len(grabs) != 0 {
|
||||
t.Fatalf("resting palm + new finger must not form a pair (ok=%v grabs=%v)", ok, grabs)
|
||||
}
|
||||
// At t=2.1s the index lands: two fresh fingers -> pending (formation
|
||||
// waits for a third finger or the first movement; the palm is stale).
|
||||
_, _, _, grabs, _ = runFrame(&tr,
|
||||
pev(pointer.Press, 1, 300, 100, s(2)+ms(100)))
|
||||
if len(grabs) != 0 {
|
||||
t.Fatalf("pending pair must not grab yet, grabs=%v", grabs)
|
||||
}
|
||||
// The palm keeps drifting and the pair starts moving: the pair is
|
||||
// (thumb, index) and its distance is unaffected by the palm.
|
||||
f, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 3, 640, 810, s(2)+ms(200)), // palm moves (pruned, ignored)
|
||||
pev(pointer.Drag, 0, 80, 100, s(2)+ms(201)),
|
||||
pev(pointer.Drag, 1, 320, 100, s(2)+ms(202)),
|
||||
)
|
||||
if len(grabs) != 2 {
|
||||
t.Fatalf("thumb+index must form the pair on movement, grabs=%v", grabs)
|
||||
}
|
||||
if !ok || !approx(float64(f), 240.0/200) {
|
||||
t.Fatalf("f=%v ok=%v want %v (palm must not enter the distance)", f, ok, 240/200)
|
||||
}
|
||||
}
|
||||
|
||||
// Device regression "pinch dies and becomes scroll": the pair must survive
|
||||
// finger movement past the scroll slop. In the router this is guaranteed by
|
||||
// the grabs (scroll is dropped from the pair's path); here we assert the
|
||||
// state machine keeps emitting factors for a pair that moves a lot, and
|
||||
// ignores drags of pointers that are not the pair.
|
||||
func TestTrackerPinchSurvivesLargeMoves(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1))
|
||||
// Big outward moves (far past the ~30px scroll slop), 5 frames.
|
||||
want := []float32{1.5, 1.4, 1.3, 1.2, 1.1}
|
||||
d := 200.0
|
||||
for i := 0; i < 5; i++ {
|
||||
d *= float64(want[i])
|
||||
half := d / 2
|
||||
_, _, ok, _, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 200-float32(half), 100, time.Duration(10+i*10)),
|
||||
pev(pointer.Drag, 1, 200+float32(half), 100, time.Duration(11+i*10)),
|
||||
)
|
||||
if !ok {
|
||||
t.Fatalf("frame %d: factor stopped (pinch died)", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Lifting one finger of the pair: no more factors (the pair is gone), the
|
||||
// survivor's drags come out as scroll (forwarded), and returning a second
|
||||
// finger re-forms the pair with a fresh baseline.
|
||||
func TestTrackerSurvivorScrollAndReform(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1))
|
||||
// Pinch in a bit.
|
||||
runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 80, 100, 10),
|
||||
pev(pointer.Drag, 1, 320, 100, 11))
|
||||
// Finger 0 lifts. Finger 1 survives.
|
||||
_, _, ok, _, _ := runFrame(&tr,
|
||||
pev(pointer.Release, 0, 80, 100, 20))
|
||||
if ok {
|
||||
t.Fatal("broken pair must not emit a factor")
|
||||
}
|
||||
// The survivor scrolls: 40px up over two frames -> +40, +25.
|
||||
_, _, _, _, scroll := runFrame(&tr,
|
||||
pev(pointer.Drag, 1, 320, 60, 30))
|
||||
if scroll != 40 {
|
||||
t.Fatalf("survivor scroll=%d want 40", scroll)
|
||||
}
|
||||
_, _, _, _, scroll = runFrame(&tr,
|
||||
pev(pointer.Drag, 1, 320, 35, 40))
|
||||
if scroll != 25 {
|
||||
t.Fatalf("survivor scroll=%d want 25", scroll)
|
||||
}
|
||||
// A second finger returns: CANDIDATE for a re-form; the pair re-forms
|
||||
// only when the new finger MOVES (two-mover rule — a palm resting on
|
||||
// the survivor is not a pinch).
|
||||
_, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Press, 2, 50, 35, 50))
|
||||
if ok || len(grabs) != 0 {
|
||||
t.Fatalf("candidate press must not re-form yet (ok=%v grabs=%v)", ok, grabs)
|
||||
}
|
||||
f, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 1, 320, 35, 60),
|
||||
pev(pointer.Drag, 2, 30, 35, 61)) // 20px from press: a real second finger
|
||||
if len(grabs) != 1 || grabs[0] != 2 {
|
||||
t.Fatalf("re-form grabs=%v want [2] (only the new finger)", grabs)
|
||||
}
|
||||
// Baseline = the distance at the candidate's PRESS (270 = 320-50); the
|
||||
// spread to 290 by re-form time is owed, not lost.
|
||||
if !ok || !approx(float64(f), 290.0/270) {
|
||||
t.Fatalf("post-reform f=%v ok=%v want %v", f, ok, 290/270)
|
||||
}
|
||||
// Survivor lifts: fully idle again.
|
||||
_, _, ok, _, scroll = runFrame(&tr,
|
||||
pev(pointer.Release, 1, 320, 35, 70))
|
||||
if ok || scroll != 0 {
|
||||
t.Fatal("idle state must emit nothing")
|
||||
}
|
||||
}
|
||||
|
||||
// Device regression "dramatic zoom before the pinch starts": a NEW pinch
|
||||
// must never emit a factor on its formation frame, even if the previous
|
||||
// pinch ended at a very different distance (stale baseline).
|
||||
func TestTrackerFreshBaselineEachPinch(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
// Pinch A at ~200px.
|
||||
runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1))
|
||||
runFrame(&tr,
|
||||
pev(pointer.Release, 0, 100, 100, 10),
|
||||
pev(pointer.Release, 1, 300, 100, 11))
|
||||
// Pinch B starts 500px apart (the fingers landed far apart). The old
|
||||
// code would have emitted 500/200 = 2.5x on the first frame.
|
||||
_, _, ok, _, _ := runFrame(&tr,
|
||||
pev(pointer.Press, 0, 0, 100, 100),
|
||||
pev(pointer.Press, 1, 500, 100, 101))
|
||||
if ok {
|
||||
t.Fatal("second pinch's formation frame must not emit a factor (stale baseline)")
|
||||
}
|
||||
f, _, ok, _, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 0, -20, 100, 110),
|
||||
pev(pointer.Drag, 1, 520, 100, 111))
|
||||
if !ok || !approx(float64(f), 540.0/500) {
|
||||
t.Fatalf("f=%v ok=%v want %v (baseline = this pinch's own start)", f, ok, 540/500)
|
||||
}
|
||||
}
|
||||
|
||||
// A third finger landing DURING an active pinch is ignored (palm rest): the
|
||||
// pair is fixed, its distance is untouched, and no extra grabs are issued.
|
||||
func TestTrackerExtraFingerDuringPinchIgnored(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1))
|
||||
// Palm lands and sits.
|
||||
_, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Press, 4, 700, 900, 10))
|
||||
if ok || len(grabs) != 0 {
|
||||
t.Fatalf("extra finger during pinch: ok=%v grabs=%v", ok, grabs)
|
||||
}
|
||||
// Palm drifts a bit; the two pinch fingers move (forming the pair); the
|
||||
// palm is not in the pair and its distance is untouched.
|
||||
f, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 4, 710, 910, 20), // 14px: a mover, but its distance to the fingers changes little
|
||||
pev(pointer.Drag, 0, 80, 100, 21),
|
||||
pev(pointer.Drag, 1, 320, 100, 22))
|
||||
if len(grabs) != 2 {
|
||||
t.Fatalf("pinch fingers must form the pair, grabs=%v", grabs)
|
||||
}
|
||||
if !ok || !approx(float64(f), 240.0/200) {
|
||||
t.Fatalf("f=%v ok=%v want %v", f, ok, 240/200)
|
||||
}
|
||||
// Palm lifts: still nothing.
|
||||
_, _, ok, _, _ = runFrame(&tr,
|
||||
pev(pointer.Release, 4, 750, 950, 30))
|
||||
if ok {
|
||||
t.Fatal("palm release must not emit a factor")
|
||||
}
|
||||
}
|
||||
|
||||
// A global cancel (app switch) breaks the pair with no survivor.
|
||||
func TestTrackerCancelBreaksPair(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1))
|
||||
runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 80, 100, 10),
|
||||
pev(pointer.Drag, 1, 320, 100, 11))
|
||||
// App switch: cancels for both pointers.
|
||||
_, _, ok, _, scroll := runFrame(&tr,
|
||||
pev(pointer.Cancel, 0, 0, 0, 20),
|
||||
pev(pointer.Cancel, 1, 0, 0, 21))
|
||||
if ok || scroll != 0 {
|
||||
t.Fatal("cancel must break the pair cleanly")
|
||||
}
|
||||
// The cancelled drags must not resurrect anything.
|
||||
_, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 70, 100, 30))
|
||||
if ok || len(grabs) != 0 {
|
||||
t.Fatal("post-cancel drags must be inert")
|
||||
}
|
||||
}
|
||||
|
||||
// A finger that leaves the region pre-pinch is no longer a candidate:
|
||||
// press-leave-press must not form a pair.
|
||||
func TestTrackerLeaveCancelsCandidate(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0))
|
||||
runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 500, 1500, 10), // leaves the region
|
||||
pev(pointer.Leave, 0, 500, 1500, 11))
|
||||
_, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Press, 1, 300, 100, 20))
|
||||
if ok || len(grabs) != 0 {
|
||||
t.Fatalf("leaving finger must not pair with a new one (ok=%v grabs=%v)", ok, grabs)
|
||||
}
|
||||
}
|
||||
|
||||
// A pair broken by one finger lifting, then the survivor lifting too: the
|
||||
// state must be fully idle (a later single-finger scroll emits nothing).
|
||||
func TestTrackerFullyIdleAfterSurvivorLift(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1))
|
||||
runFrame(&tr,
|
||||
pev(pointer.Release, 0, 100, 100, 10))
|
||||
runFrame(&tr,
|
||||
pev(pointer.Drag, 1, 300, 60, 20)) // survivor scrolls
|
||||
runFrame(&tr,
|
||||
pev(pointer.Release, 1, 300, 60, 30))
|
||||
for i := 0; i < 5; i++ {
|
||||
_, _, ok, grabs, scroll := runFrame(&tr,
|
||||
pev(pointer.Drag, 1, 300, float32(60-i*20), time.Duration(40+i*10)))
|
||||
if ok || len(grabs) != 0 || scroll != 0 {
|
||||
t.Fatalf("frame %d: not idle (ok=%v grabs=%v scroll=%v)", i, ok, grabs, scroll)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The sanity clamp: a factor outside 0.1..10 (teleporting pair) is dropped
|
||||
// and the baseline advances, so one bad frame cannot jump the font.
|
||||
func TestTrackerSanityClamp(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1))
|
||||
// Teleport: finger 0 jumps 3000px (ID-reuse noise) while finger 1 moves
|
||||
// 20px: both are movers, the pair forms, distance 200 -> 2800, factor
|
||||
// 14x — must be dropped, baseline advanced.
|
||||
f, _, ok, _, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 3100, 100, ms(10)),
|
||||
pev(pointer.Drag, 1, 320, 100, ms(11)))
|
||||
if ok {
|
||||
t.Fatalf("implausible factor emitted: %v", f)
|
||||
}
|
||||
// Next frame is sane relative to the NEW baseline (2800px).
|
||||
f, _, ok, _, _ = runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 3050, 100, ms(20)),
|
||||
pev(pointer.Drag, 1, 325, 100, ms(21)))
|
||||
if !ok || !approx(float64(f), 2725.0/2780) {
|
||||
t.Fatalf("f=%v ok=%v want %v", f, ok, 2725.0/2780)
|
||||
}
|
||||
}
|
||||
|
||||
// A slow frame: the whole pinch (formation + spread + both releases) can
|
||||
// arrive in ONE frame's drain (the Android driver replays historical samples
|
||||
// and a low frame rate batches events). The factors must still be owed:
|
||||
// the formation-frame movement relative to the formation distance, and the
|
||||
// final movement settled when the pair breaks.
|
||||
func TestTrackerSlowFrameFullPinch(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
// Frame 1: both Presses (200px apart) and drags out to 300px, all in
|
||||
// one drain. The pair forms at 200 and moved to 300 this frame:
|
||||
// factor 300/200 owed.
|
||||
f, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1),
|
||||
pev(pointer.Drag, 0, 50, 100, 2),
|
||||
pev(pointer.Drag, 1, 350, 100, 3))
|
||||
if !ok || !approx(float64(f), 300.0/200) {
|
||||
t.Fatalf("f1=%v ok=%v want %v (formation-frame movement is owed)", f, ok, 300.0/200)
|
||||
}
|
||||
if len(grabs) != 2 {
|
||||
t.Fatalf("grabs=%v want 2", grabs)
|
||||
}
|
||||
// Frame 2: spread to 400px, then both fingers lift — same drain. The
|
||||
// 400/300 factor must be settled at the break, not lost.
|
||||
f, _, ok, _, _ = runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 0, 100, 10),
|
||||
pev(pointer.Drag, 1, 400, 100, 11),
|
||||
pev(pointer.Release, 0, 0, 100, 12),
|
||||
pev(pointer.Release, 1, 400, 100, 13))
|
||||
if !ok || !approx(float64(f), 400.0/300) {
|
||||
t.Fatalf("f2=%v ok=%v want %v (break settles the owed factor)", f, ok, 400.0/300)
|
||||
}
|
||||
// Fully idle after the break.
|
||||
if _, _, ok, _, scroll := runFrame(&tr,
|
||||
pev(pointer.Drag, 1, 400, 80, 20)); ok || scroll != 0 {
|
||||
t.Fatal("tracker must be idle after both releases")
|
||||
}
|
||||
}
|
||||
|
||||
// A stationary pair that forms and then breaks without moving owes nothing.
|
||||
func TestTrackerSlowFrameNoMovement(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
if _, _, ok, _, _ := runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1),
|
||||
pev(pointer.Release, 0, 100, 100, 2),
|
||||
pev(pointer.Release, 1, 300, 100, 3)); ok {
|
||||
t.Fatal("stationary pair that breaks must not emit a factor")
|
||||
}
|
||||
}
|
||||
|
||||
// The worst case (a ~1fps emulator frame): the ENTIRE pinch — formation,
|
||||
// spread, and both releases — lands in one frame's drain. The owed factor
|
||||
// is settled at the break against the formation distance.
|
||||
func TestTrackerBornAndDeadInOneFrame(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
f, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Press, 0, 100, 100, 0),
|
||||
pev(pointer.Press, 1, 300, 100, 1),
|
||||
pev(pointer.Drag, 0, 0, 100, 2),
|
||||
pev(pointer.Drag, 1, 400, 100, 3),
|
||||
pev(pointer.Release, 0, 0, 100, 4),
|
||||
pev(pointer.Release, 1, 400, 100, 5))
|
||||
if !ok || !approx(float64(f), 400.0/200) {
|
||||
t.Fatalf("f=%v ok=%v want %v", f, ok, 400.0/200)
|
||||
}
|
||||
if len(grabs) != 2 {
|
||||
t.Fatalf("grabs=%v want 2", grabs)
|
||||
}
|
||||
if _, _, ok, _, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 1, 400, 80, 10)); ok {
|
||||
t.Fatal("idle tracker must not emit")
|
||||
}
|
||||
}
|
||||
|
||||
// Palm-first three-finger: the palm edge is resting and still, then the two
|
||||
// pinch fingers land (all within the freshness window). The pair must be the
|
||||
// two NEWEST fingers — the pinch pair — not the palm.
|
||||
func TestTrackerPalmFirstThreeFingers(t *testing.T) {
|
||||
var tr pinchTracker
|
||||
// Palm rests at t=0 (still), pinch fingers land 80/120ms later.
|
||||
// Pending: no pair yet (movement decides).
|
||||
_, _, _, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Press, 3, 720, 400, 0),
|
||||
pev(pointer.Press, 0, 570, 1200, ms(80)),
|
||||
pev(pointer.Press, 1, 870, 1200, ms(120)))
|
||||
if len(grabs) != 0 {
|
||||
t.Fatalf("pending: no grabs yet, got %v", grabs)
|
||||
}
|
||||
// The pinch spreads 300 -> 596 while the palm stays put: the two MOVERS
|
||||
// (the pinch fingers) form the pair, and the factor must track the
|
||||
// pinch pair — not the palm.
|
||||
f, _, ok, grabs, _ := runFrame(&tr,
|
||||
pev(pointer.Drag, 0, 422, 1200, ms(200)),
|
||||
pev(pointer.Drag, 1, 1018, 1200, ms(201)),
|
||||
pev(pointer.Drag, 3, 720, 400, ms(202))) // palm still
|
||||
if len(grabs) != 2 || (grabs[0] != 0 && grabs[0] != 1) || (grabs[1] != 0 && grabs[1] != 1) {
|
||||
t.Fatalf("pair must be the two pinch fingers, grabs=%v", grabs)
|
||||
}
|
||||
if !ok || !approx(float64(f), 596.0/300) {
|
||||
t.Fatalf("f=%v ok=%v want %v (palm must not enter the distance)", f, ok, 596.0/300)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,470 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/io/pointer"
|
||||
)
|
||||
|
||||
// pinchFreshWindow is how recently BOTH pair fingers must have pressed for a
|
||||
// pinch to start. A finger that has been resting (palm edge, pinky parked on
|
||||
// the screen) is not a pinch candidate; without this window a resting third
|
||||
// finger could be paired with a new one and the font would follow a
|
||||
// single-finger scroll.
|
||||
const pinchFreshWindow = 300 * time.Millisecond
|
||||
|
||||
// pinchMoveEps is how far (window px, from the press position) a pending
|
||||
// finger must travel to count as "moving" for pair formation. It filters
|
||||
// touch jitter while still registering an intentional pinch start quickly.
|
||||
const pinchMoveEps = 10.0
|
||||
|
||||
// Pair formation requires TWO MOVING fingers, and nothing else. A pinch is
|
||||
// two fingers moving apart (or together); a scroll is one finger moving;
|
||||
// a resting palm is a finger that never moves. No static rule about which
|
||||
// finger is "the palm" (first? last? still?) survives both palm-first and
|
||||
// palm-last hand lands — movement is the only signal that works for both.
|
||||
// While 2-3 fresh fingers are down the tracker is "pending": it forms the
|
||||
// pair the moment two of them have moved more than pinchMoveEps from where
|
||||
// they pressed (the pair = those two). A lone mover is a scroll, never a
|
||||
// pinch, and cannot form a pair; the pending state cancels on any release
|
||||
// that leaves fewer than two fingers.
|
||||
|
||||
// pinchTracker is the pure state machine for the editor's two-finger pinch
|
||||
// (font scaling). It is independent of the Gio input router so the whole
|
||||
// gesture — including the failure modes found on device (pair switching,
|
||||
// stale baselines, scroll-grab starvation) — is unit-testable with synthetic
|
||||
// pointer-event sequences.
|
||||
//
|
||||
// Design: the pair is EXPLICIT and stable. When two fresh fingers are down
|
||||
// inside the region, they are named as the pair and the adapter issues
|
||||
// pointer.GrabCmds for both (exclusive event delivery: releases always
|
||||
// arrive, even off-clip, and scroll/click are dropped with a Cancel). The
|
||||
// per-frame factor is current pair distance / previous frame's pair
|
||||
// distance. Neither the pair's composition nor its baseline is ever
|
||||
// re-derived from whatever pointers happen to be present — that re-derivation
|
||||
// (two lowest IDs + stale prevDist) is what made single-finger scrolls scale
|
||||
// the font on the phone.
|
||||
type pinchTracker struct {
|
||||
// Pre-pinch ("pending"): fresh fingers down in the region, no pair yet.
|
||||
// The pair forms when TWO of them have moved more than pinchMoveEps
|
||||
// from where they pressed (see the comment above the constants).
|
||||
observed map[pointer.ID]f32.Point
|
||||
observedAt map[pointer.ID]time.Duration
|
||||
// Position each pending finger had when it PRESSED: the formation
|
||||
// baseline (formDist) is the press distance, so a pinch that has
|
||||
// already spread by the time the pair starts owes that spread; and the
|
||||
// displacement from here is the "mover" measurement.
|
||||
pressPos map[pointer.ID]f32.Point
|
||||
// Pending fingers that released in the CURRENT frame's drain, held lazy
|
||||
// until factor() (which forms the pair — and may break it again — at
|
||||
// the fingers' final positions). A normal active-pair release breaks
|
||||
// immediately in step(); this map is only for releases that arrive
|
||||
// while the pair is still pending.
|
||||
released map[pointer.ID]bool
|
||||
// Reform candidate: after a pair broke, the survivor is still grabbed
|
||||
// and a NEW finger has landed. The pair re-forms (survivor + new) only
|
||||
// when the new finger moves (pinchMoveEps) — the same two-mover rule;
|
||||
// a palm landing on the survivor is not a pinch. reformPos is where
|
||||
// the candidate PRESSED (the mover reference).
|
||||
reformID pointer.ID
|
||||
reformPos f32.Point
|
||||
reformBase float32 // pair distance at the candidate's press
|
||||
reformHave bool
|
||||
|
||||
// The active (grabbed) pair.
|
||||
pair [2]pointer.ID
|
||||
pos [2]f32.Point
|
||||
on bool
|
||||
prevDist float32
|
||||
// The pair's distance at the moment of (re)formation: the distance of
|
||||
// the two press positions. The Android driver replays
|
||||
// historical samples, so drags of the forming pair can land in the same
|
||||
// frame as the pair's formation; movement relative to the formation
|
||||
// distance is real and owed.
|
||||
formDist float32
|
||||
// Set on the pair's formation frame (start or re-form): the baseline
|
||||
// is being established, so factor() emits nothing unless the pair
|
||||
// moved from its formation positions this frame.
|
||||
fresh bool
|
||||
// The pair broke (a finger lifted) during the current frame's drain.
|
||||
// factor() runs AFTER the drain, so on a slow frame the pair's drags
|
||||
// and the breaking release arrive together and the frame's factor
|
||||
// would be lost (on==false by the time factor() runs). The factor is
|
||||
// settled here instead.
|
||||
brokeFactor float32
|
||||
brokeMid f32.Point
|
||||
|
||||
// A pair finger was released while the other is still down (still
|
||||
// grabbed by the probe; Gio v0.10 has no release-grab). The survivor's
|
||||
// drags are forwarded as scroll so the finger is not dead.
|
||||
survOn bool
|
||||
survID pointer.ID
|
||||
survPos f32.Point
|
||||
// Accumulated survivor scroll for the current frame (window px,
|
||||
// gesture.Scroll convention: positive = content scrolls up).
|
||||
survScroll int
|
||||
}
|
||||
|
||||
// pinchStep is the outcome of one pointer event.
|
||||
type pinchStep struct {
|
||||
// Grabs the adapter must issue (pointer.GrabCmd per ID). Only the
|
||||
// survivor re-form path emits grabs per event; initial pair formation
|
||||
// is decided in factor() (after the whole frame's events).
|
||||
grabs []pointer.ID
|
||||
}
|
||||
|
||||
// step feeds one pointer event into the tracker. The per-frame factor is not
|
||||
// part of the step result: the adapter calls factor() once after draining
|
||||
// the frame's events, because the Android driver replays historical samples
|
||||
// (several drags per frame) and the font must see one factor per frame.
|
||||
func (t *pinchTracker) step(pe pointer.Event) pinchStep {
|
||||
var s pinchStep
|
||||
id := pe.PointerID
|
||||
switch pe.Kind {
|
||||
case pointer.Press:
|
||||
switch {
|
||||
case t.on:
|
||||
// Extra finger during an active pinch (palm rest, third
|
||||
// finger): ignore — the pair is fixed.
|
||||
case t.survOn && t.reformHave && id == t.reformID:
|
||||
// The reform candidate lifted before moving: not a pinch.
|
||||
t.reformHave = false
|
||||
case t.survOn:
|
||||
// A new finger lands while the survivor is down: CANDIDATE for
|
||||
// a re-form; it must MOVE to become the pair (the same two-mover
|
||||
// rule — a palm resting on the survivor is not a pinch). The
|
||||
// formation baseline is the distance at this PRESS (any spread
|
||||
// by re-form time is owed).
|
||||
t.reformID = id
|
||||
t.reformPos = pe.Position
|
||||
t.reformBase = pairDist(t.survPos, pe.Position)
|
||||
t.reformHave = true
|
||||
default:
|
||||
if t.observed == nil {
|
||||
t.observed = make(map[pointer.ID]f32.Point)
|
||||
t.observedAt = make(map[pointer.ID]time.Duration)
|
||||
t.pressPos = make(map[pointer.ID]f32.Point)
|
||||
}
|
||||
t.observed[id] = pe.Position
|
||||
t.observedAt[id] = pe.Time
|
||||
t.pressPos[id] = pe.Position
|
||||
// Prune stale fingers (resting palm/pinky); a pruned palm is
|
||||
// out of the game entirely.
|
||||
t.pruneStale(pe.Time)
|
||||
// (Two or more fingers down is the PENDING state; the pair
|
||||
// forms only when two of them are movers, decided in factor().)
|
||||
}
|
||||
case pointer.Drag:
|
||||
switch {
|
||||
case t.on:
|
||||
if i := t.pairIndex(id); i >= 0 {
|
||||
t.pos[i] = pe.Position
|
||||
}
|
||||
case t.survOn && id == t.survID:
|
||||
t.survScroll += int(t.survPos.Y - pe.Position.Y)
|
||||
t.survPos = pe.Position
|
||||
case t.survOn && t.reformHave && id == t.reformID:
|
||||
// The reform candidate moves: if it clears the epsilon from
|
||||
// where it pressed it is a real second finger — re-form the
|
||||
// pair (survivor + new).
|
||||
dx, dy := pe.Position.X-t.reformPos.X, pe.Position.Y-t.reformPos.Y
|
||||
if dx*dx+dy*dy > pinchMoveEps*pinchMoveEps {
|
||||
t.pair = [2]pointer.ID{t.survID, id}
|
||||
t.pos = [2]f32.Point{t.survPos, pe.Position}
|
||||
t.survOn = false
|
||||
t.reformHave = false
|
||||
t.on = true
|
||||
t.fresh = true
|
||||
t.formDist = t.reformBase
|
||||
s.grabs = []pointer.ID{id} // survivor already grabbed
|
||||
}
|
||||
default:
|
||||
if _, ok := t.pressPos[id]; ok {
|
||||
t.observed[id] = pe.Position
|
||||
// Formation is NOT decided here: a per-event decision would
|
||||
// lock in the first mover pair seen (e.g. (finger, drifting
|
||||
// palm)) before the second pinch finger's drag lands in the
|
||||
// same drain. factor() decides, after the full frame.
|
||||
}
|
||||
}
|
||||
case pointer.Release, pointer.Cancel:
|
||||
switch {
|
||||
case t.on:
|
||||
if i := t.pairIndex(id); i >= 0 {
|
||||
t.breakPair(i)
|
||||
}
|
||||
case t.survOn && id == t.survID:
|
||||
// The survivor lifts too: fully idle (the candidate, if any,
|
||||
// was a lone finger — never a pair).
|
||||
t.survOn = false
|
||||
t.reformHave = false
|
||||
default:
|
||||
// Pending finger goes up: held LAZY until factor() — on a slow
|
||||
// frame the whole pinch (drags AND releases) can land in one
|
||||
// drain, and the pair must form at the fingers' final positions
|
||||
// before the break settles the owed factor.
|
||||
if _, ok := t.observed[id]; ok {
|
||||
t.markReleased(id)
|
||||
}
|
||||
}
|
||||
case pointer.Leave:
|
||||
// Only meaningful pre-pinch: a finger that leaves the editor
|
||||
// region is no longer a pinch candidate. (A grabbed pair finger
|
||||
// keeps delivering through the grab; its Leave is ignored.)
|
||||
if t.survOn && t.reformHave && id == t.reformID {
|
||||
t.reformHave = false
|
||||
} else if t.observed != nil {
|
||||
if _, ok := t.observed[id]; ok {
|
||||
t.markReleased(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// breakPair settles the frame's owed factor (if any) and breaks the pair.
|
||||
// The surviving finger stays grabbed by the probe (no release-grab in v0.10)
|
||||
// and becomes the survivor — UNLESS it too released in this same drain
|
||||
// (slow frame: a born-and-dead pair), in which case the tracker goes fully
|
||||
// idle. Settling here (before factor() runs) is what keeps the factor on a
|
||||
// frame where the drags and the breaking release arrive together.
|
||||
func (t *pinchTracker) breakPair(i int) {
|
||||
base := t.prevDist
|
||||
if base == 0 && t.fresh {
|
||||
base = t.formDist
|
||||
}
|
||||
if d := pairDist(t.pos[0], t.pos[1]); d > 0 && base > 0 && d != base {
|
||||
if f := d / base; f >= 0.1 && f <= 10 {
|
||||
t.brokeFactor = f
|
||||
t.brokeMid = pairMid(t.pos[0], t.pos[1])
|
||||
}
|
||||
}
|
||||
t.on = false
|
||||
t.prevDist = 0
|
||||
other := 1 - i
|
||||
if !t.released[t.pair[other]] {
|
||||
t.survOn = true
|
||||
t.survID = t.pair[other]
|
||||
t.survPos = t.pos[other]
|
||||
}
|
||||
}
|
||||
|
||||
// markReleased lazily records a pending finger's release (see the Release
|
||||
// case). Cleared by factor() after it has formed (and possibly broken) the
|
||||
// pair.
|
||||
func (t *pinchTracker) markReleased(id pointer.ID) {
|
||||
if t.released == nil {
|
||||
t.released = make(map[pointer.ID]bool)
|
||||
}
|
||||
t.released[id] = true
|
||||
}
|
||||
|
||||
// dropObserved removes a pending finger for good.
|
||||
func (t *pinchTracker) dropObserved(id pointer.ID) {
|
||||
if t.observed == nil {
|
||||
return
|
||||
}
|
||||
delete(t.observed, id)
|
||||
delete(t.observedAt, id)
|
||||
delete(t.pressPos, id)
|
||||
delete(t.released, id)
|
||||
}
|
||||
|
||||
// movers returns the pending fingers that moved more than pinchMoveEps from
|
||||
// where they pressed, most-displaced first.
|
||||
func (t *pinchTracker) movers() []pointer.ID {
|
||||
type dm struct {
|
||||
id pointer.ID
|
||||
d float64
|
||||
}
|
||||
var list []dm
|
||||
for id, pp := range t.pressPos {
|
||||
dx := float64(t.observed[id].X - pp.X)
|
||||
dy := float64(t.observed[id].Y - pp.Y)
|
||||
if d := math.Sqrt(dx*dx + dy*dy); d > pinchMoveEps {
|
||||
list = append(list, dm{id, d})
|
||||
}
|
||||
}
|
||||
for i := 0; i < len(list); i++ {
|
||||
for j := i + 1; j < len(list); j++ {
|
||||
if list[j].d > list[i].d {
|
||||
list[i], list[j] = list[j], list[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
out := make([]pointer.ID, 0, len(list))
|
||||
for _, e := range list {
|
||||
out = append(out, e.id)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// tryFormPair forms the pair from the pending fingers if at least TWO are
|
||||
// movers (two moving fingers = a pinch; one = a scroll, never a pair). Of
|
||||
// the mover pairs, the one whose DISTANCE changed most wins: a pinch's pair
|
||||
// distance changes, while a drifting palm's distance to a finger changes
|
||||
// little. A mover pair whose distance does not change (fingers moving in
|
||||
// unison) is a slide, not a pinch. Returns the IDs to grab, or nil when no
|
||||
// pair forms.
|
||||
func (t *pinchTracker) tryFormPairAny() []pointer.ID {
|
||||
m := t.movers()
|
||||
if len(m) < 2 {
|
||||
return nil
|
||||
}
|
||||
bestA, bestB := m[0], m[1]
|
||||
bestChange := -1.0
|
||||
for i := 0; i < len(m); i++ {
|
||||
for j := i + 1; j < len(m); j++ {
|
||||
a, b := m[i], m[j]
|
||||
now := float64(pairDist(t.observed[a], t.observed[b]))
|
||||
press := float64(pairDist(t.pressPos[a], t.pressPos[b]))
|
||||
if chg := math.Abs(now - press); chg > bestChange {
|
||||
bestChange = chg
|
||||
bestA, bestB = a, b
|
||||
}
|
||||
}
|
||||
}
|
||||
if bestChange <= pinchMoveEps {
|
||||
return nil // unison movement: a slide, not a pinch
|
||||
}
|
||||
a, b := bestA, bestB
|
||||
t.pair = [2]pointer.ID{a, b}
|
||||
t.pos = [2]f32.Point{t.observed[a], t.observed[b]}
|
||||
t.on = true
|
||||
t.fresh = true
|
||||
// Baseline = the PRESS distance of the pair: any spread that happened
|
||||
// before the pair started is owed, not lost.
|
||||
t.formDist = pairDist(t.pressPos[a], t.pressPos[b])
|
||||
t.observed = nil
|
||||
t.observedAt = nil
|
||||
t.pressPos = nil
|
||||
return []pointer.ID{a, b}
|
||||
}
|
||||
|
||||
// pairIndex returns 0/1 if id is a member of the active pair, else -1.
|
||||
func (t *pinchTracker) pairIndex(id pointer.ID) int {
|
||||
if !t.on {
|
||||
return -1
|
||||
}
|
||||
if t.pair[0] == id {
|
||||
return 0
|
||||
}
|
||||
if t.pair[1] == id {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// pruneStale drops observed fingers that pressed more than
|
||||
// pinchFreshWindow before now.
|
||||
func (t *pinchTracker) pruneStale(now time.Duration) {
|
||||
for id, at := range t.observedAt {
|
||||
if now-at > pinchFreshWindow {
|
||||
delete(t.observed, id)
|
||||
delete(t.observedAt, id)
|
||||
delete(t.pressPos, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// factor returns the current frame's relative pinch scale and the pair
|
||||
// midpoint (window px), or ok=false when no factor applies (no active pair,
|
||||
// first frame of a pinch, or an implausible jump). It must be called once
|
||||
// per frame, after all of the frame's events have been stepped.
|
||||
func (t *pinchTracker) factor() (f float32, mid f32.Point, ok bool, grabs []pointer.ID) {
|
||||
// Pending pair formation is decided HERE, after the whole frame's
|
||||
// events (not per event): a per-event decision would lock in the first
|
||||
// mover pair seen — e.g. (finger, drifting palm) — before the second
|
||||
// pinch finger's drag lands in the same drain.
|
||||
if !t.on && !t.survOn && t.observed != nil {
|
||||
if g := t.tryFormPairAny(); g != nil {
|
||||
grabs = g
|
||||
// Slow frame: a pair member may have released in this same
|
||||
// drain (the release arrived while the pair was still pending):
|
||||
// break it now at the fingers' final positions.
|
||||
for i, id := range t.pair {
|
||||
if t.released[id] {
|
||||
t.breakPair(i)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Pending fingers that released this frame are dropped for good.
|
||||
if t.released != nil {
|
||||
for id := range t.released {
|
||||
t.dropObserved(id)
|
||||
}
|
||||
t.released = nil
|
||||
}
|
||||
if !t.on {
|
||||
// The pair broke during this frame's drain: emit the settled
|
||||
// factor (on a slow frame the drags and the breaking release can
|
||||
// arrive together, and factor() runs only after the drain).
|
||||
if t.brokeFactor > 0 {
|
||||
f, mid, ok = t.brokeFactor, t.brokeMid, true
|
||||
t.brokeFactor = 0
|
||||
t.brokeMid = f32.Point{}
|
||||
}
|
||||
return f, mid, ok, grabs
|
||||
}
|
||||
d := pairDist(t.pos[0], t.pos[1])
|
||||
if t.fresh {
|
||||
// Formation frame: establish the baseline. Emit only if the pair
|
||||
// already moved from its formation positions this frame (the
|
||||
// Android driver replays historical samples, so drags can batch
|
||||
// with the formation Press).
|
||||
t.fresh = false
|
||||
t.prevDist = d
|
||||
if t.formDist > 0 && d != t.formDist {
|
||||
f = d / t.formDist
|
||||
if f < 0.1 || f > 10 {
|
||||
return 0, f32.Point{}, false, grabs
|
||||
}
|
||||
return f, pairMid(t.pos[0], t.pos[1]), true, grabs
|
||||
}
|
||||
return 0, f32.Point{}, false, grabs
|
||||
}
|
||||
if t.prevDist > 0 && d > 0 && d != t.prevDist {
|
||||
// No-movement guard: a stationary pair emits nothing (an f=1.0
|
||||
// factor would churn the font pin's re-layout for no change).
|
||||
f = d / t.prevDist
|
||||
// Sanitize: a real pinch moves millimeters between frames; a
|
||||
// factor this far off 1 is noise, not a finger.
|
||||
if f < 0.1 || f > 10 {
|
||||
t.prevDist = d
|
||||
return 0, f32.Point{}, false, grabs
|
||||
}
|
||||
t.prevDist = d
|
||||
return f, pairMid(t.pos[0], t.pos[1]), true, grabs
|
||||
}
|
||||
t.prevDist = d
|
||||
return 0, f32.Point{}, false, grabs
|
||||
}
|
||||
|
||||
// survivorScroll returns and resets the frame's accumulated survivor-finger
|
||||
// scroll (window px, gesture.Scroll convention: positive = content scrolls
|
||||
// up).
|
||||
func (t *pinchTracker) survivorScroll() int {
|
||||
d := t.survScroll
|
||||
t.survScroll = 0
|
||||
return d
|
||||
}
|
||||
|
||||
// reset clears all state (the editor left the screen).
|
||||
func (t *pinchTracker) reset() {
|
||||
*t = pinchTracker{}
|
||||
}
|
||||
|
||||
// pairDist is the distance between two pair points (window px).
|
||||
func pairDist(a, b f32.Point) float32 {
|
||||
dx, dy := a.X-b.X, a.Y-b.Y
|
||||
return float32(math.Sqrt(float64(dx*dx + dy*dy)))
|
||||
}
|
||||
|
||||
// pairMid is the midpoint of two pair points (window px).
|
||||
func pairMid(a, b f32.Point) f32.Point {
|
||||
return f32.Point{X: (a.X + b.X) / 2, Y: (a.Y + b.Y) / 2}
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
package ui
|
||||
|
||||
// Definitive host test: run the app's REAL Renderer.Draw op stream (a
|
||||
// realistic editor frame: status bar, editor TextField, bottom bar) through
|
||||
// the real input.Router with the app's per-frame protocol (drain before
|
||||
// commit), queue a touch press inside the editor text, and check which tags
|
||||
// receive it: the scroll tag (known-good on the phone) as control, plus the
|
||||
// pressProbe/pinchProbe tags (dead on the phone).
|
||||
|
||||
import (
|
||||
"image"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/font/gofont"
|
||||
"gioui.org/io/event"
|
||||
"gioui.org/io/input"
|
||||
"gioui.org/io/pointer"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
)
|
||||
|
||||
func TestRealDrawOpsProbeHit(t *testing.T) {
|
||||
shp := text.NewShaper(text.WithCollection(gofont.Collection()))
|
||||
r := New(Theme{FontSize: 14}, shp)
|
||||
noop := func(any) {}
|
||||
|
||||
const (
|
||||
wW = 411
|
||||
wH = 914
|
||||
)
|
||||
editorRegion := Region{X: 10, Y: 52, W: wW - 20, H: 700}
|
||||
editor := NewTextField("editor_text", "hello world\nsecond line\nthird line", editorRegion, true, editorRegion.W, 0, 5, -1, -1, []Interaction{
|
||||
{Gesture: Scroll, Handler: noop},
|
||||
{Gesture: Tap, Handler: noop},
|
||||
{Gesture: Pinch, Handler: noop},
|
||||
})
|
||||
editor.Focused = true
|
||||
editor.ShowIMESeq = 7
|
||||
statusBar := NewContainer(Region{X: 0, Y: 0, W: wW, H: 52}, Color{R: 240, G: 240, B: 240, A: 255}, []Element{
|
||||
NewLabel("←", 16, Region{X: 5, Y: 5, W: 40, H: 42}, AlignStart, "back_id", nil),
|
||||
NewLabel("/storage/emulated/0/Notes/test.txt", 14, Region{X: 50, Y: 5, W: 300, H: 42}, AlignStart, "path_id", nil),
|
||||
})
|
||||
bottomBar := NewContainer(Region{X: 0, Y: wH - 40, W: wW, H: 40}, Color{R: 240, G: 240, B: 240, A: 255}, []Element{
|
||||
NewLabel("Saved", 14, Region{X: 5, Y: wH - 35, W: 100, H: 30}, AlignStart, "saved_id", nil),
|
||||
})
|
||||
elems := []Element{statusBar, editor, bottomBar}
|
||||
|
||||
m := unit.Metric{PxPerDp: 1, PxPerSp: 1}
|
||||
gtxFor := func(ops *op.Ops) layout.Context {
|
||||
return layout.Context{
|
||||
Ops: ops,
|
||||
Metric: m,
|
||||
Constraints: layout.Constraints{Min: image.Point{}, Max: image.Point{X: wW, Y: wH}},
|
||||
}
|
||||
}
|
||||
|
||||
var rtr input.Router
|
||||
probeK := pointer.Press | pointer.Drag | pointer.Release | pointer.Cancel
|
||||
pinchF := pointer.Filter{Target: event.Tag(r.pinchProbe), Kinds: probeK}
|
||||
pressF := pointer.Filter{Target: event.Tag(r.pressProbe), Kinds: probeK}
|
||||
|
||||
// Frame 1: record the real app ops and commit.
|
||||
{
|
||||
var ops op.Ops
|
||||
r.Draw(gtxFor(&ops), elems, 1.0)
|
||||
if _, ok := r.scrolls["editor_text"]; !ok {
|
||||
t.Fatal("scroll reg not created for editor_text")
|
||||
}
|
||||
scroll := r.scrolls["editor_text"].scroll
|
||||
t.Logf("scroll tag = %p", scroll)
|
||||
// Pre-merge filters (app calls Event every frame; first call here).
|
||||
rtr.Source().Event(pinchF)
|
||||
rtr.Source().Event(pressF)
|
||||
rtr.Source().Event(pointer.Filter{Target: scroll, Kinds: probeK})
|
||||
rtr.Frame(&ops)
|
||||
}
|
||||
|
||||
// A touch press lands in the editor text region.
|
||||
pos := f32.Pt(100, 100)
|
||||
rtr.Queue(pointer.Event{Kind: pointer.Press, Source: pointer.Touch, Position: pos})
|
||||
t.Logf("queued press at %v", pos)
|
||||
|
||||
// Frame 2: draw again, then drain (app protocol: consume before commit).
|
||||
{
|
||||
var ops op.Ops
|
||||
r.Draw(gtxFor(&ops), elems, 1.0)
|
||||
drain := func(name string, f pointer.Filter) {
|
||||
for {
|
||||
e, ok := rtr.Source().Event(f)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if pe, ok := e.(pointer.Event); ok {
|
||||
t.Logf("frame2: %-12s got kind=%v pos=%v", name, pe.Kind, pe.Position)
|
||||
}
|
||||
}
|
||||
}
|
||||
drain("pressProbe", pressF)
|
||||
drain("pinchProbe", pinchF)
|
||||
drain("scroll", pointer.Filter{Target: r.scrolls["editor_text"].scroll, Kinds: probeK})
|
||||
rtr.Frame(&ops)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRealDrawPinchGrabLifecycle runs the REAL app frame loop (draw ops ->
|
||||
// CheckGestures -> commit) through the real input.Router with a real
|
||||
// two-finger pinch and verifies the grab semantics that fix the on-device
|
||||
// jank: the pair is tracked exclusively (the probe keeps getting drags even
|
||||
// off-clip, scroll gets nothing), a factor is emitted per moved frame, a
|
||||
// release breaks the pair, and the survivor finger's drags come out as plain
|
||||
// scroll (forwarded), so the finger is not dead after a pinch.
|
||||
func TestRealDrawPinchGrabLifecycle(t *testing.T) {
|
||||
shp := text.NewShaper(text.WithCollection(gofont.Collection()))
|
||||
r := New(Theme{FontSize: 14}, shp)
|
||||
|
||||
var pinchEvents []any
|
||||
var scrollEvents []any
|
||||
editor := NewTextField("editor_text", "hello world\nsecond line\nthird line",
|
||||
Region{X: 10, Y: 52, W: 391, H: 700}, true, 391, 0, 5, -1, -1, []Interaction{
|
||||
{Gesture: Scroll, Handler: func(d any) { scrollEvents = append(scrollEvents, d) }},
|
||||
{Gesture: Tap, Handler: func(any) {}},
|
||||
{Gesture: Pinch, Handler: func(d any) { pinchEvents = append(pinchEvents, d) }},
|
||||
})
|
||||
editor.Focused = true
|
||||
elems := []Element{editor}
|
||||
|
||||
m := unit.Metric{PxPerDp: 1, PxPerSp: 1}
|
||||
gtxFor := func(ops *op.Ops) layout.Context {
|
||||
return layout.Context{
|
||||
Ops: ops,
|
||||
Metric: m,
|
||||
Constraints: layout.Constraints{Min: image.Point{}, Max: image.Point{X: 411, Y: 914}},
|
||||
}
|
||||
}
|
||||
var rtr input.Router
|
||||
var prevOps op.Ops
|
||||
havePrev := false
|
||||
// ptEv builds a router-queueable pointer event. "Drag" is written as
|
||||
// Move: the router only accepts Press/Move/Release/Cancel/Scroll and
|
||||
// converts a pressed pointer's Move into a Drag before delivery.
|
||||
ptEv := func(kind pointer.Kind, id pointer.ID, x, y float32, at time.Duration) pointer.Event {
|
||||
if kind == pointer.Drag {
|
||||
kind = pointer.Move
|
||||
}
|
||||
return pointer.Event{Kind: kind, Source: pointer.Touch, PointerID: id, Position: f32.Point{X: x, Y: y}, Time: at}
|
||||
}
|
||||
// runFrame mirrors the app's loop: commit the previous frame's ops
|
||||
// (w.Event), queue this frame's pointer events, draw, CheckGestures.
|
||||
// It also observes what the SCROLL tag receives this frame (drained
|
||||
// before CheckGestures so the observation is lossless).
|
||||
runFrame := func(evts ...pointer.Event) (events []InputEvent, scrollKinds []pointer.Kind) {
|
||||
if havePrev {
|
||||
rtr.Frame(&prevOps)
|
||||
}
|
||||
for _, e := range evts {
|
||||
rtr.Queue(e)
|
||||
}
|
||||
var ops op.Ops
|
||||
r.Draw(gtxFor(&ops), elems, 1.0)
|
||||
for {
|
||||
e, ok := rtr.Source().Event(pointer.Filter{
|
||||
Target: r.scrolls["editor_text"].scroll,
|
||||
Kinds: pointer.Press | pointer.Drag | pointer.Release | pointer.Cancel,
|
||||
})
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if pe, ok := e.(pointer.Event); ok {
|
||||
scrollKinds = append(scrollKinds, pe.Kind)
|
||||
}
|
||||
}
|
||||
events = r.CheckGestures(rtr.Source(), m)
|
||||
// The app's main loop dispatches each event to its logic handler;
|
||||
// mirror that so the capture handlers see them.
|
||||
for _, e := range events {
|
||||
e.Handler(e.Data)
|
||||
}
|
||||
prevOps, havePrev = ops, true
|
||||
return events, scrollKinds
|
||||
}
|
||||
|
||||
// Setup frame: register the ops.
|
||||
runFrame()
|
||||
|
||||
hasKind := func(kinds []pointer.Kind, k pointer.Kind) bool {
|
||||
for _, x := range kinds {
|
||||
if x == k {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Two fingers press 200px apart inside the editor text. Pending: no
|
||||
// pair yet (the pair forms when BOTH move), no pinch events, no grabs.
|
||||
_, scrollKinds := runFrame(
|
||||
ptEv(pointer.Press, 0, 100, 100, time.Millisecond),
|
||||
ptEv(pointer.Press, 1, 300, 100, 2*time.Millisecond))
|
||||
if len(pinchEvents) != 0 {
|
||||
t.Fatalf("press frame emitted a pinch event: %v", pinchEvents)
|
||||
}
|
||||
if hasKind(scrollKinds, pointer.Drag) {
|
||||
t.Fatalf("scroll saw a drag on the press frame: %v", scrollKinds)
|
||||
}
|
||||
|
||||
// The pair spreads 200 -> 240: this is the FORMATION frame — both
|
||||
// fingers moved (the two-mover rule), the pair forms and the grabs are
|
||||
// issued. One factor (1.2) is owed against the press distance. The
|
||||
// pair's drags of THIS frame still reach scroll (the grabs commit on
|
||||
// the next frame): a one-frame leak bounded by the scroll slop — the
|
||||
// cost of not grabbing on press (which would kill single-finger
|
||||
// scrolls). From the NEXT frame on, scroll must see nothing of the pair.
|
||||
runFrame(
|
||||
ptEv(pointer.Drag, 0, 80, 100, 10*time.Millisecond),
|
||||
ptEv(pointer.Drag, 1, 320, 100, 11*time.Millisecond))
|
||||
if len(pinchEvents) != 1 {
|
||||
t.Fatalf("formation frame: pinch events=%d want 1 (%v)", len(pinchEvents), pinchEvents)
|
||||
}
|
||||
fpe, ok := pinchEvents[0].(FontPinchEvent)
|
||||
if !ok || fpe.Scale < 1.19 || fpe.Scale > 1.21 {
|
||||
t.Fatalf("scale=%v want ~1.2", pinchEvents[0])
|
||||
}
|
||||
|
||||
// The pair keeps spreading: from here the grabs are active and SCROLL
|
||||
// sees no drag of the pair (a Cancel for the dropped press may arrive).
|
||||
_, scrollKinds = runFrame(
|
||||
ptEv(pointer.Drag, 0, 50, 100, 15*time.Millisecond),
|
||||
ptEv(pointer.Drag, 1, 350, 100, 16*time.Millisecond))
|
||||
if len(pinchEvents) != 2 {
|
||||
t.Fatalf("mid-pinch frame: pinch events=%d want 2 (%v)", len(pinchEvents), pinchEvents)
|
||||
}
|
||||
if hasKind(scrollKinds, pointer.Drag) {
|
||||
t.Fatalf("scroll saw the pair's drags after formation: %v (grab failed)", scrollKinds)
|
||||
}
|
||||
|
||||
// Finger 0 drags FAR OUTSIDE the editor region: the grab keeps it
|
||||
// delivering to the probe (no stale pointer, no lost release).
|
||||
_, scrollKinds = runFrame(
|
||||
ptEv(pointer.Drag, 0, 2, 890, 20*time.Millisecond), // off-clip
|
||||
ptEv(pointer.Drag, 1, 330, 100, 21*time.Millisecond))
|
||||
if len(pinchEvents) != 3 {
|
||||
t.Fatalf("off-clip frame: pinch events=%d want 3 (pair must survive off-clip)", len(pinchEvents))
|
||||
}
|
||||
if hasKind(scrollKinds, pointer.Drag) {
|
||||
t.Fatalf("scroll saw the off-clip drag: %v", scrollKinds)
|
||||
}
|
||||
|
||||
// Finger 0 lifts (off-clip): the release still arrives via the grab.
|
||||
// The pair breaks; no pinch event.
|
||||
before := len(pinchEvents)
|
||||
runFrame(ptEv(pointer.Release, 0, 2, 890, 30*time.Millisecond))
|
||||
if len(pinchEvents) != before {
|
||||
t.Fatal("broken pair emitted a pinch event")
|
||||
}
|
||||
|
||||
// The survivor (finger 1) scrolls 30px up: forwarded as a plain scroll
|
||||
// delta to the editor's scroll handler — the finger is not dead.
|
||||
scrollBefore := len(scrollEvents)
|
||||
runFrame(ptEv(pointer.Drag, 1, 330, 70, 40*time.Millisecond))
|
||||
if len(scrollEvents) != scrollBefore+1 {
|
||||
t.Fatalf("survivor scroll not forwarded: events=%d want %d", len(scrollEvents), scrollBefore+1)
|
||||
}
|
||||
if d, ok := scrollEvents[scrollBefore].(int); !ok || d != 30 {
|
||||
t.Fatalf("survivor delta=%v want 30 (px, scroll-up positive)", scrollEvents[scrollBefore])
|
||||
}
|
||||
|
||||
// A second finger returns: candidate for a re-form (no event on the
|
||||
// press); it must MOVE to become the pair (two-mover rule).
|
||||
runFrame(ptEv(pointer.Press, 2, 50, 70, 50*time.Millisecond))
|
||||
before = len(pinchEvents)
|
||||
runFrame(
|
||||
ptEv(pointer.Drag, 1, 340, 70, 60*time.Millisecond),
|
||||
ptEv(pointer.Drag, 2, 30, 70, 61*time.Millisecond)) // 20px from press
|
||||
if len(pinchEvents) != before+1 {
|
||||
t.Fatalf("re-formed pair: pinch events=%d want %d", len(pinchEvents), before+1)
|
||||
}
|
||||
}
|
||||
+446
-13
@@ -24,6 +24,7 @@ import (
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget"
|
||||
"golang.org/x/image/math/fixed"
|
||||
"log"
|
||||
)
|
||||
|
||||
// maxInt32 is a large value used as MaxWidth for single-line text layout.
|
||||
@@ -62,6 +63,17 @@ type scrollReg struct {
|
||||
handler func(any)
|
||||
}
|
||||
|
||||
// Probe tags for the raw-pointer probes (long-press, pinch). Each probe
|
||||
// needs its OWN named type: the unnamed fieldless struct{} is a single
|
||||
// canonical Go type, so distinct `struct{}` fields are the SAME value.
|
||||
// Gio's router keys handlers by the tag value, so two `struct{}` probes
|
||||
// collapse into one handler and whichever probe drains first (the press
|
||||
// probe, which is consumed before the pinch probe) consumes every event,
|
||||
// starving the other — this is why pinch received nothing on device while
|
||||
// long-press appeared to work.
|
||||
type pressProbeTag struct{}
|
||||
type pinchProbeTag struct{}
|
||||
|
||||
// Renderer consumes a slice of elements and draws them.
|
||||
//
|
||||
// The Renderer is owned by the main goroutine. It is the home of any state
|
||||
@@ -80,6 +92,17 @@ type Renderer struct {
|
||||
lastLineY Dp // last line baseline offset from text origin, in Dp (derived from GlyphLayout)
|
||||
glyphLayout GlyphLayout // captured per-glyph layout from last drawWrappedText
|
||||
|
||||
// ZeroWheelScroll, set by main on a frame whose window size shrank, makes
|
||||
// CheckGestures drain any pointer.Scroll events queued for the editor's
|
||||
// scroll gesture before consuming gestures. Gio's window calls RevealFocus
|
||||
// on any frame the viewport shrinks (e.g. the IME opening under
|
||||
// adjustResize) and synthesizes a pointer.Scroll nudge to bring the focused
|
||||
// field's (stale, pre-resize) bounds into view; consumed by gesture.Scroll
|
||||
// that nudge shifts the editor content. The drain kills only the
|
||||
// synthesized event: finger scroll (pointer.Drag) and the flinger are
|
||||
// untouched, and normal frames consume pointer.Scroll as before.
|
||||
ZeroWheelScroll bool
|
||||
|
||||
// IME dedup (main-owned, persistent across frames). Re-pushing an unchanged
|
||||
// snippet or selection every frame resets the IME's composition and caret,
|
||||
// which desyncs fast commits; push only on change, as widget.Editor does in
|
||||
@@ -88,10 +111,55 @@ type Renderer struct {
|
||||
lastIMEField string
|
||||
lastWasFocused bool
|
||||
lastSnippet key.Snippet
|
||||
lastSelStart int // window-relative rune index of last-pushed selection start; -1 = no selection
|
||||
lastSelCaret int // window-relative rune index of last-pushed selection end/caret
|
||||
|
||||
lastSelStart int // absolute rune index of last-pushed selection start; -1 = no selection
|
||||
lastSelCaret int // absolute rune index of last-pushed selection end/caret
|
||||
lastIMEShowSeq uint64 // last ShowIMESeq value that issued SoftKeyboardCmd{Show:true}
|
||||
|
||||
// IME model (main-owned, persistent across frames). The renderer keeps a
|
||||
// local copy of the snippet the IME currently holds and updates it in
|
||||
// place as IME edits arrive (ApplyIMEEdit / ApplyIMEKey, called from the
|
||||
// key-event drain), so the snippet pushed each frame (FlushIME) is
|
||||
// byte-identical to the IME's own post-commit snippet. Gioui calls
|
||||
// imm.restartInput() whenever the pushed snippet differs from the IME's
|
||||
// internal state (app/os_android.go EditorStateChanged); a restart makes
|
||||
// the keyboard (Gboard) re-derive its auto-cap state from scratch, which
|
||||
// is the root cause of the random mid-word capitals. The model is flushed
|
||||
// AFTER the key events of the frame were drained and BEFORE e.Frame
|
||||
// flushes the ops, so the commit frame pushes the post-edit text instead
|
||||
// of the pre-edit frame text (which is what desynced the old push-from-
|
||||
// Draw path and restarted the IME on every keystroke).
|
||||
|
||||
// Caret geometry drawn during the most recent frame, in view-local
|
||||
// pixels (imeCaretPos is the caret/baseline intersection, per key.Caret).
|
||||
// FlushIME attaches it to the pushed SelectionCmd: gioui only calls
|
||||
// GioView.updateCaret (-> imm.updateCursorAnchorInfo, the update that
|
||||
// actually reaches the IME and makes Gboard re-evaluate its auto-cap
|
||||
// state) when the SelectionCmd's Caret changes. A zero caret never
|
||||
// changes, so Gboard never saw a caret move and kept its stale
|
||||
// auto-shift after the caret jumped (e.g. from a line start, where
|
||||
// caps are legitimately on, to mid-word).
|
||||
// lastIMECaretPos is the caret last shipped in a SelectionCmd.
|
||||
imeCaretPos f32.Point
|
||||
imeCaretAscent float32
|
||||
imeCaretDescent float32
|
||||
lastIMECaretPos f32.Point
|
||||
|
||||
// imeHold: a commit was applied to the pushed model (see
|
||||
// ApplyIMECommitToModel) but the logic's post-commit frame has not been
|
||||
// drawn yet. The frame being drained is pre-commit (it was built before
|
||||
// the logic processed the commit); its snippet would clobber the model
|
||||
// with the stale text, regressing gioui's editor state and causing a
|
||||
// restart per keystroke. The hold is armed with the drained frame's
|
||||
// EditSeq: while the hold is active, FlushIME skips a frame whose
|
||||
// EditSeq is at most the armed one (the drained frame, and any no-op
|
||||
// frame repeating the seq) and resumes on the next higher one. The
|
||||
// logic's HandleIMECommit markDirties the buffer, so the post-commit
|
||||
// frame always advances it.
|
||||
imeHoldActive bool
|
||||
imeHoldEditSeq uint64
|
||||
imeHoldFrames int
|
||||
|
||||
// FocusCmd dedup (main-owned, persistent across frames). key.FocusCmd is
|
||||
// issued ONLY on a focus transition, never per frame: even a no-op FocusCmd
|
||||
// (same focus) takes the router's "immediate command" path, which re-queues
|
||||
@@ -114,12 +182,40 @@ type Renderer struct {
|
||||
// scroll/drag then, not a press). longPressID gates the long-press to the
|
||||
// editor's click reg (browser rows etc. don't long-press). ppLast is in
|
||||
// f32.Point because pointer.Event.Position is window-space f32.
|
||||
pressProbe struct{}
|
||||
pressProbe pressProbeTag
|
||||
ppLast f32.Point
|
||||
ppActive bool
|
||||
ppMoved bool
|
||||
longPressID string
|
||||
|
||||
// Pinch-to-change-font-size (editor text region). Gio v0.10 has no
|
||||
// two-finger pinch primitive: pinchProbe is a raw event tag inside the
|
||||
// editor clip, and pinchT (pinch_tracker.go) is the gesture's state
|
||||
// machine. When two fresh fingers are down the tracker names them as an
|
||||
// EXPLICIT pair and the adapter grabs both (pointer.GrabCmd): exclusive
|
||||
// event delivery (releases always arrive, even off-clip; scroll/click
|
||||
// are dropped with a Cancel, which also stops the first finger dragging
|
||||
// the text mid-pinch). The per-frame factor is the pair-distance ratio
|
||||
// (FontPinchEvent to pinchHandler). The pair is never re-derived from
|
||||
// whatever pointers happen to be present: that re-derivation (two
|
||||
// lowest IDs) let a resting third finger pair with a live one and made
|
||||
// single-finger scrolls scale the font on the phone. When one pair
|
||||
// finger lifts, the survivor stays grabbed (v0.10 has no release-grab)
|
||||
// and its drags are forwarded as scroll via pinchScrollHandler. State
|
||||
// is dropped when the editor leaves the screen (see Draw).
|
||||
pinchProbe pinchProbeTag
|
||||
pinchT pinchTracker
|
||||
pinchHandler func(any)
|
||||
pinchScrollHandler func(any)
|
||||
pinchProbeOn bool
|
||||
|
||||
// appFontScale is the app-local font-size multiplier (pinch zoom;
|
||||
// 1.0 = default, 0 = not set yet). The main goroutine feeds it from the
|
||||
// frame's snapshot via SetAppFontScale before Draw; drawWrappedText
|
||||
// multiplies the editor font size by it. The system user font scale is
|
||||
// separate and already folded into gtx.Metric/gtx.Sp.
|
||||
appFontScale float32
|
||||
|
||||
// Selection / caret drag handles (0 = start, 1 = end, 2 = body, 3 = caret
|
||||
// handle). Registered clipped in drawWrappedText only while a selection or
|
||||
// caret handle is visible; a gesture.Drag grabs the pointer once movement
|
||||
@@ -155,6 +251,17 @@ type Renderer struct {
|
||||
gestureExclusions [][4]int
|
||||
}
|
||||
|
||||
// SetAppFontScale sets the app-local font-size multiplier for subsequent
|
||||
// draw passes (1.0 = default; <= 0 is treated as 1). Main-goroutine-only;
|
||||
// call before Draw (see appFontScale).
|
||||
func (r *Renderer) SetAppFontScale(v float32) {
|
||||
if v > 0 {
|
||||
r.appFontScale = v
|
||||
} else {
|
||||
r.appFontScale = 1
|
||||
}
|
||||
}
|
||||
|
||||
// GestureExclusions returns the handle grab boxes collected for the last
|
||||
// frame (see gestureExclusions).
|
||||
func (r *Renderer) GestureExclusions() [][4]int { return r.gestureExclusions }
|
||||
@@ -172,6 +279,199 @@ func (r *Renderer) pointInHandleBox(p image.Point) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ApplyIMECommitToModel applies a drained IME commit to the pushed snippet
|
||||
// model and re-pushes it IMMEDIATELY, on the drain pass. This is required
|
||||
// because gioui's EditEvent callback applies the same commit to its own
|
||||
// window state (w.imeState) directly, bypassing the op queue; the op-queue
|
||||
// state then lags w.imeState by exactly this commit, and gioui's
|
||||
// per-frame comparison (updateState) sees the mismatch as a SNIPPET
|
||||
// REGRESSION — it rolls the state back and sends a restartInput with the
|
||||
// PRE-commit text on every single keystroke. Gboard resets its per-word
|
||||
// input session on that restart (and it never queries the app for
|
||||
// context), which is the source of the random mid-word capitalization.
|
||||
// Pushing the post-commit snippet here makes the op queue match w.imeState
|
||||
// before the next frame: the comparison is a no-op, no restart is sent,
|
||||
// and Gboard's session (auto-shift, composition) survives the keystroke.
|
||||
// The logic goroutine applies the same commit to the buffer asynchronously;
|
||||
// when its frame arrives, FlushIME sees the snippet unchanged (already
|
||||
// pushed) and does nothing. If the logic SNAPS the commit to a different
|
||||
// position (drift guard), the post-commit frame's snippet differs from the
|
||||
// model and one resyncing restart is pushed then — correct.
|
||||
// frameEditSeq is the EditSeq of the frame the commit was drained from
|
||||
// (the pre-commit frame); it arms the hold (see the imeHold field).
|
||||
func (r *Renderer) ApplyIMECommitToModel(gtx layout.Context, ev key.EditEvent, frameEditSeq uint64) {
|
||||
sn := r.lastSnippet
|
||||
local := ev.Range.Start - sn.Range.Start
|
||||
total := utf8.RuneCountInString(sn.Text)
|
||||
if local < 0 || local > total || ev.Range.End-sn.Range.Start > total {
|
||||
return // commit outside the pushed window; the next frame resyncs
|
||||
}
|
||||
// Replace [local, local+repl) runes with ev.Text (repl is the commit's
|
||||
// own replaced span — 0 for a plain insertion).
|
||||
rs := []rune(sn.Text)
|
||||
repl := ev.Range.End - ev.Range.Start
|
||||
newRs := make([]rune, 0, len(rs)+len([]rune(ev.Text)))
|
||||
newRs = append(newRs, rs[:local]...)
|
||||
newRs = append(newRs, []rune(ev.Text)...)
|
||||
if local+repl < len(rs) {
|
||||
newRs = append(newRs, rs[local+repl:]...)
|
||||
}
|
||||
newSnip := key.Snippet{
|
||||
Range: key.Range{
|
||||
Start: sn.Range.Start,
|
||||
End: sn.Range.End + len([]rune(ev.Text)) - repl,
|
||||
},
|
||||
Text: string(newRs),
|
||||
}
|
||||
r.lastSnippet = newSnip
|
||||
gtx.Execute(key.SnippetCmd{Tag: "editor_text", Snippet: newSnip})
|
||||
// The caret is at the end of the inserted text: push the selection so
|
||||
// the op queue's selection matches w.imeState (the EditEvent's Replace
|
||||
// moved it there). The px caret position is the current one (the next
|
||||
// frame corrects it); gioui's updateCaret is cosmetic for the IME.
|
||||
// Hold the pre-commit frame(s) out of FlushIME until the post-commit
|
||||
// frame arrives (they would clobber the model just pushed). Arm with
|
||||
// the drained frame's own EditSeq: it is the seq of the last
|
||||
// pre-commit frame, and the post-commit frame's seq is strictly higher
|
||||
// (the logic's HandleIMECommit markDirties the buffer).
|
||||
r.imeHoldActive = true
|
||||
r.imeHoldEditSeq = frameEditSeq
|
||||
r.imeHoldFrames = 0
|
||||
// The post-commit caret is at the END of the inserted text, which
|
||||
// starts at Range.Start: for an insertion (Start == End) that is
|
||||
// Range.End + len, but for a replacement (a swipe/autocorrect over a
|
||||
// selection) it is Range.Start + len — Range.End + len would land
|
||||
// (End-Start) runes past the inserted text and desynchronize the IME's
|
||||
// caret from ours (observed on the phone: a 4-rune replacement left
|
||||
// every subsequent commit 4 runes ahead of the caret).
|
||||
caret := ev.Range.Start + utf8.RuneCountInString(ev.Text)
|
||||
log.Printf("IME PUSH commit-range=[%d,%d) text=%q -> sel caret=%d", ev.Range.Start, ev.Range.End, ev.Text, caret)
|
||||
r.lastSelStart, r.lastSelCaret = -1, caret
|
||||
r.lastIMECaretPos = r.imeCaretPos
|
||||
gtx.Execute(key.SelectionCmd{
|
||||
Tag: "editor_text",
|
||||
Range: key.Range{Start: caret, End: caret},
|
||||
Caret: key.Caret{
|
||||
Pos: r.imeCaretPos,
|
||||
Ascent: r.imeCaretAscent,
|
||||
Descent: r.imeCaretDescent,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Renderer) FlushIME(gtx layout.Context, tf TextField) {
|
||||
// An IME commit is in flight (see ApplyIMECommitToModel): a pre-commit
|
||||
// frame must not push its stale snippet and regress gioui's state.
|
||||
if r.imeHoldActive {
|
||||
if tf.EditSeq <= r.imeHoldEditSeq {
|
||||
// Bounded so a commit whose post-commit frame never arrives
|
||||
// (logic stall) cannot stick the hold: after a few frames
|
||||
// resume normal flushing.
|
||||
if r.imeHoldFrames++; r.imeHoldFrames > 8 {
|
||||
r.imeHoldActive = false
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
r.imeHoldActive = false
|
||||
}
|
||||
// The snippet is the hysteresis window around the caret (see
|
||||
// State.computeIMESnippetWindow), shipped precomputed by the logic:
|
||||
// stable across render-window moves (keyboard show/hide, tap
|
||||
// re-centering, scrolls), so those events do NOT re-push the snippet —
|
||||
// a re-push reaches Gboard as imm.restartInput, which starts a fresh
|
||||
// input session that capitalizes the first committed character even
|
||||
// mid-word. It re-anchors only when the caret leaves the window
|
||||
// margins (far tap, fling past the window, file switch), and on edits
|
||||
// (whose text ApplyIMECommitToModel has already pushed, so the frame's
|
||||
// copy dedupes away).
|
||||
snippet := key.Snippet{
|
||||
Range: key.Range{
|
||||
Start: tf.IMESnippetStartRune,
|
||||
End: tf.IMESnippetEndRune,
|
||||
},
|
||||
Text: tf.IMESnippetText,
|
||||
}
|
||||
if tf.IMEForceResync && utf8.RuneCountInString(tf.IMESnippetText) > 2 {
|
||||
// The IME's local text has desynchronized (several consecutive
|
||||
// drift-snapped or empty commits — see EditorState.IMEForceResync)
|
||||
// and it keeps re-sending the same fix in a loop. Pushing the
|
||||
// window trimmed by one rune forces a snippet change (and thus a
|
||||
// restartInput): the IME re-fetches the real text and selection
|
||||
// around the caret and its model converges. The next frame pushes
|
||||
// the full window again (one more restart), then everything is
|
||||
// quiet again.
|
||||
rs := []rune(tf.IMESnippetText)
|
||||
snippet = key.Snippet{
|
||||
Range: key.Range{
|
||||
Start: tf.IMESnippetStartRune + 1,
|
||||
End: tf.IMESnippetEndRune,
|
||||
},
|
||||
Text: string(rs[1:]),
|
||||
}
|
||||
}
|
||||
if snippet != r.lastSnippet {
|
||||
r.lastSnippet = snippet
|
||||
// A snippet change resets the IME's selection: force the selection
|
||||
// re-push below so the caret is re-anchored in the new text in the
|
||||
// same frame. (A drained IME commit is applied to the model BEFORE
|
||||
// this runs — see ApplyIMECommitToModel — so a normal keystroke
|
||||
// never reaches this branch; it is the re-anchoring path: scroll,
|
||||
// tap, file switch, snapped commit, external edit.)
|
||||
log.Printf("IME PUSH snippet=[%d,%d) len=%d (restart)", snippet.Range.Start, snippet.Range.End, utf8.RuneCountInString(snippet.Text))
|
||||
r.lastSelStart, r.lastSelCaret = -1, -1
|
||||
gtx.Execute(key.SnippetCmd{Tag: tf.id, Snippet: snippet})
|
||||
}
|
||||
// Push the caret/selection (deduped) in absolute file runes, the same
|
||||
// coordinate space as the snippet (see widget.Editor's updateIMEState).
|
||||
var selStart, selEnd int
|
||||
if tf.IMESelStartRune >= 0 && tf.IMESelEndRune > tf.IMESelStartRune {
|
||||
selStart, selEnd = tf.IMESelStartRune, tf.IMESelEndRune
|
||||
} else {
|
||||
selStart = -1
|
||||
selEnd = tf.IMECaretRune
|
||||
}
|
||||
selectionJumped := selStart != r.lastSelStart || selEnd != r.lastSelCaret
|
||||
caretMoved := r.imeCaretPos != r.lastIMECaretPos
|
||||
if selectionJumped || caretMoved {
|
||||
// Skip while a selection/caret handle drag is in flight (see
|
||||
// Renderer.selDragActive): a mid-gesture updateSelection makes the
|
||||
// IME's selection jump and aborts the gesture. key.SelDragActive is
|
||||
// stale here (renderer-owned), so use the renderer's own flag.
|
||||
if !r.selDragActive {
|
||||
r.lastSelStart = selStart
|
||||
r.lastSelCaret = selEnd
|
||||
r.lastIMECaretPos = r.imeCaretPos
|
||||
log.Printf("IME PUSH sel=[%d,%d)", selStart, selEnd)
|
||||
rng := key.Range{Start: selStart, End: selEnd}
|
||||
if selStart < 0 {
|
||||
rng = key.Range{Start: selEnd, End: selEnd}
|
||||
}
|
||||
// The SelectionCmd must reach the IME on every caret jump: it is
|
||||
// the signal (GioView.updateSelection -> imm.updateSelection,
|
||||
// delivered as onUpdateSelection) that makes Gboard re-derive
|
||||
// its auto-shift state from the new position — mid-word: off;
|
||||
// line/sentence start: on. It must NOT be shadowed by a snippet
|
||||
// push in the same frame: a snippet change makes gioui's
|
||||
// EditorStateChanged take the restartInput branch instead (which
|
||||
// Gboard does not use to re-derive caps), so a caret jump with an
|
||||
// unchanged snippet is exactly the case that works.
|
||||
// The Caret field carries the real position (view-local px, Pos
|
||||
// on the baseline) so gioui also fires updateCaret
|
||||
// (imm.updateCursorAnchorInfo) for the jump.
|
||||
gtx.Execute(key.SelectionCmd{
|
||||
Tag: tf.id,
|
||||
Range: rng,
|
||||
Caret: key.Caret{
|
||||
Pos: r.imeCaretPos,
|
||||
Ascent: r.imeCaretAscent,
|
||||
Descent: r.imeCaretDescent,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// New creates a new Renderer.
|
||||
func New(th Theme, shp *text.Shaper) *Renderer {
|
||||
r := &Renderer{
|
||||
@@ -238,6 +538,12 @@ func (r *Renderer) toDp(px Px) Dp {
|
||||
func (r *Renderer) Draw(gtx layout.Context, elems []Element, scale float32) {
|
||||
r.scale = scale
|
||||
r.focusSeenThisFrame = false // per-frame reset for FocusCmd dedup
|
||||
if !r.pinchProbeOn {
|
||||
// No editor text in the previous frame: drop all pinch state so a
|
||||
// later pinch starts clean.
|
||||
r.pinchT.reset()
|
||||
}
|
||||
r.pinchProbeOn = false
|
||||
// Gio sets constraints to layout.Exact(windowSize), so Min==Max. Use (0,0) as Min.
|
||||
winW := gtx.Constraints.Max.X
|
||||
winH := gtx.Constraints.Max.Y
|
||||
@@ -343,6 +649,12 @@ func (r *Renderer) CheckGestures(q input.Source, m unit.Metric) []InputEvent {
|
||||
// Long-press motion probe first: it must see the press/drag events before
|
||||
// the click loop decides about a long press.
|
||||
r.consumePressProbe(q)
|
||||
// Pinch probe: drain the raw pointer events, grab/release the pair,
|
||||
// and emit the frame's scale factor and any survivor-finger scroll.
|
||||
// Runs before the click/scroll loops so the pinch is applied in the
|
||||
// same input batch that carried the finger moves, and the pair's
|
||||
// grabs are queued ahead of any competing scroll grab.
|
||||
events = append(events, r.consumePinchProbe(q)...)
|
||||
for id, reg := range r.clicks {
|
||||
// Drain every queued event for this gesture in this frame.
|
||||
// gesture.Click returns one event per Update call, but on Android a
|
||||
@@ -467,9 +779,25 @@ func (r *Renderer) CheckGestures(q input.Source, m unit.Metric) []InputEvent {
|
||||
// gesture.Scroll.Update returns scroll delta in pixels.
|
||||
// ScrollY range: Min = -scrollOffset (remaining above), Max = large (content height unknown yet).
|
||||
// With Min==Max==0, clampSplit consumes zero scroll.
|
||||
// Update runs unconditionally (it keeps the gesture's flinger state
|
||||
// healthy). Emission is suppressed while a pinch owns the pair: the
|
||||
// pair is grabbed (scroll is dropped from its path) once the grabs
|
||||
// commit, and this guard covers the one-frame window before that.
|
||||
if r.ZeroWheelScroll {
|
||||
// RevealFocus (see ZeroWheelScroll) queued a synthetic
|
||||
// pointer.Scroll on this shrink frame; consume it here so the
|
||||
// gesture never sees it. Scroll-range clamping is no use: the
|
||||
// router UNIONs ranges across frames and the historical max
|
||||
// can never shrink back to zero.
|
||||
for {
|
||||
if _, ok := q.Event(pointer.Filter{Target: reg.scroll, Kinds: pointer.Scroll}); !ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
delta := reg.scroll.Update(m, q, time.Now(), gesture.Vertical,
|
||||
pointer.ScrollRange{}, pointer.ScrollRange{Min: -(1 << 30), Max: 1 << 30})
|
||||
if delta != 0 {
|
||||
if delta != 0 && !r.pinchT.on {
|
||||
events = append(events, InputEvent{
|
||||
Handler: reg.handler,
|
||||
Data: delta,
|
||||
@@ -480,11 +808,63 @@ func (r *Renderer) CheckGestures(q input.Source, m unit.Metric) []InputEvent {
|
||||
return events
|
||||
}
|
||||
|
||||
// consumePinchProbe drains the pinch probe's raw pointer events into the
|
||||
// tracker, issues the pair's grabs, and returns this frame's events: the
|
||||
// scale factor (when the active pair moved) and the survivor finger's
|
||||
// forwarded scroll (when a pair broke with one finger still down).
|
||||
func (r *Renderer) consumePinchProbe(q input.Source) []InputEvent {
|
||||
var events []InputEvent
|
||||
for {
|
||||
evt, ok := q.Event(pointer.Filter{Target: r.pinchProbe, Kinds: pointer.Press | pointer.Drag | pointer.Release | pointer.Cancel | pointer.Leave})
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
pe, ok := evt.(pointer.Event)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if s := r.pinchT.step(pe); len(s.grabs) > 0 {
|
||||
for _, id := range s.grabs {
|
||||
q.Execute(pointer.GrabCmd{Tag: r.pinchProbe, ID: id})
|
||||
}
|
||||
}
|
||||
}
|
||||
// The tracker may have formed the pair during factor() (after the full
|
||||
// frame's events); issue those grabs now. Either way they commit
|
||||
// before any scroll grab queued later in this frame (FIFO command
|
||||
// queue), so the pair wins the race even if a finger is already past
|
||||
// the scroll slop.
|
||||
f, mid, ok, grabs := r.pinchT.factor()
|
||||
for _, id := range grabs {
|
||||
q.Execute(pointer.GrabCmd{Tag: r.pinchProbe, ID: id})
|
||||
}
|
||||
if ok && r.pinchHandler != nil {
|
||||
events = append(events, InputEvent{
|
||||
Handler: r.pinchHandler,
|
||||
Data: FontPinchEvent{
|
||||
Scale: f,
|
||||
Center: Point{X: r.toDp(Px(mid.X)), Y: r.toDp(Px(mid.Y))},
|
||||
},
|
||||
})
|
||||
}
|
||||
if d := r.pinchT.survivorScroll(); d != 0 && r.pinchScrollHandler != nil {
|
||||
events = append(events, InputEvent{
|
||||
Handler: r.pinchScrollHandler,
|
||||
Data: d,
|
||||
})
|
||||
}
|
||||
return events
|
||||
}
|
||||
|
||||
// consumePressProbe drains the raw pointer events of the long-press probe
|
||||
// and updates ppLast/ppMoved. It runs before the click loop each frame.
|
||||
// Leave ends the pending press: a finger that drifts off the editor region
|
||||
// must not keep a long press armed (its release would never come to the
|
||||
// probe if it was grabbed by scroll, so without this the timer could fire
|
||||
// for a finger that is long gone).
|
||||
func (r *Renderer) consumePressProbe(q input.Source) {
|
||||
for {
|
||||
evt, ok := q.Event(pointer.Filter{Target: r.pressProbe, Kinds: pointer.Press | pointer.Drag | pointer.Release | pointer.Cancel})
|
||||
evt, ok := q.Event(pointer.Filter{Target: r.pressProbe, Kinds: pointer.Press | pointer.Drag | pointer.Release | pointer.Cancel | pointer.Leave})
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -505,7 +885,7 @@ func (r *Renderer) consumePressProbe(q input.Source) {
|
||||
}
|
||||
}
|
||||
r.ppLast = pe.Position
|
||||
case pointer.Release, pointer.Cancel:
|
||||
case pointer.Release, pointer.Cancel, pointer.Leave:
|
||||
r.ppMoved = false
|
||||
r.ppActive = false
|
||||
}
|
||||
@@ -595,6 +975,18 @@ func (r *Renderer) drawElement(gtx layout.Context, e Element) {
|
||||
// clipped in drawWrappedText where the handle geometry is known.
|
||||
r.registerInteraction(interactive.ID(), interaction, gtx)
|
||||
}
|
||||
if interaction.Gesture == Pinch {
|
||||
// The renderer owns the probe (raw two-pointer geometry);
|
||||
// this records the logic handlers. The scroll handler is
|
||||
// the survivor-finger's forwarding target: after a pinch
|
||||
// breaks with one finger still down, that finger stays
|
||||
// grabbed by the probe (v0.10 has no release-grab), so its
|
||||
// drags are emitted as plain scroll deltas.
|
||||
r.pinchHandler = interaction.Handler
|
||||
if reg, ok := r.scrolls[interactive.ID()]; ok {
|
||||
r.pinchScrollHandler = reg.handler
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
e.Draw(gtx, r)
|
||||
@@ -783,19 +1175,28 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
|
||||
}
|
||||
r.gestureExclusions = nil // rebuilt from this frame's handle boxes
|
||||
|
||||
// App-local font-size multiplier (pinch zoom; SetAppFontScale keeps it
|
||||
// > 0). It multiplies the sp font size directly, so the value is a
|
||||
// continuous float — no rounding to whole points anywhere.
|
||||
appScale := r.appFontScale
|
||||
if appScale <= 0 {
|
||||
appScale = 1
|
||||
}
|
||||
size := float32(r.theme.FontSize) * appScale
|
||||
|
||||
// Fixed line height based on font size, not glyph metrics.
|
||||
lineHeightSp := unit.Sp(float32(r.theme.FontSize) * LineHeightScale)
|
||||
lineHeightSp := unit.Sp(size * LineHeightScale)
|
||||
// User font-size setting (sp per dp). The shaper draws baselines at
|
||||
// Sp(...) physical px, so the RENDERED line pitch in density-dp is
|
||||
// lineHeightSp × fontScale. Every dp-space value below (line height,
|
||||
// ascent) uses the scaled form so caret/handles/highlight follow the
|
||||
// drawn glyphs; the logic side tracks the same factor via
|
||||
// EffectiveLineHeight (ScaleEvent.FontScale).
|
||||
// EffectiveLineHeight (ScaleEvent.FontScale × app font scale).
|
||||
fontScale := float32(1)
|
||||
if gtx.Metric.PxPerDp > 0 && gtx.Metric.PxPerSp > 0 {
|
||||
fontScale = gtx.Metric.PxPerSp / gtx.Metric.PxPerDp
|
||||
}
|
||||
ascent := Dp(float32(r.theme.FontSize) * fontScale)
|
||||
ascent := Dp(size * fontScale)
|
||||
lineH := Dp(float32(lineHeightSp) * fontScale)
|
||||
// Wrap disabled: shape with unlimited width so lines extend past the
|
||||
// region (clipped by textClip below) instead of wrapping.
|
||||
@@ -804,7 +1205,7 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
|
||||
maxWidthPx = int(r.toPx(wrapWidth))
|
||||
}
|
||||
params := text.Parameters{
|
||||
PxPerEm: fixed.I(gtx.Sp(r.theme.FontSize)),
|
||||
PxPerEm: fixed.I(gtx.Sp(unit.Sp(size))),
|
||||
MinWidth: 0,
|
||||
MaxWidth: maxWidthPx,
|
||||
MaxLines: 0, // unlimited - wrap at MaxWidth
|
||||
@@ -875,10 +1276,28 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
|
||||
flushLine()
|
||||
if g.Flags&text.FlagLineBreak != 0 {
|
||||
lineCount++
|
||||
// Record the next visual line's start ONLY when a real line
|
||||
// follows the break: a real "\n" cluster (zero width, one
|
||||
// rune — including the final one of a newline-terminated
|
||||
// file, whose trailing empty line is real) or a soft wrap
|
||||
// with content after it. The shaper ALSO flags the last
|
||||
// visible glyph of a final line without a trailing newline,
|
||||
// and the synthetic end-of-text glyph after a trailing
|
||||
// newline: both sit at len(str) with no line after them.
|
||||
// Recording them creates a phantom visual line: taps below
|
||||
// the content map onto it (harmless — it returns the EOF
|
||||
// byte), but CaretPoint resolves the caret through it and
|
||||
// draws the caret on that phantom line — one line BELOW
|
||||
// where the typed text lands, so the letter appeared a line
|
||||
// above the cursor.
|
||||
isRealNewline := g.Advance == 0 && g.Runes > 0
|
||||
softWrap := g.Advance != 0 && byteOffset < len(str)
|
||||
if isRealNewline || softWrap {
|
||||
layout.VisualLineStarts = append(layout.VisualLineStarts, byteOffset)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(line) > 0 {
|
||||
flushLine()
|
||||
lineCount++
|
||||
@@ -933,9 +1352,19 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
|
||||
// byte and len(str) is the window's last insertion point.
|
||||
if focused && cursorPos >= 0 && cursorPos <= len(str) {
|
||||
// Determine cursor position from `layout` and `cursorPos`
|
||||
cursorX, cursorY := caretPoint(cursorPos)
|
||||
cursorX = reg.X + cursorX
|
||||
cursorY = reg.Y - scrollOffset + cursorY - ascent
|
||||
caretX, caretY := caretPoint(cursorPos) // y is the line's baseline, region-relative
|
||||
cursorX := reg.X + caretX
|
||||
cursorY := reg.Y - scrollOffset + caretY - ascent
|
||||
|
||||
// Save the caret geometry (view-local px; Pos on the baseline) for
|
||||
// FlushIME's SelectionCmd so the IME is told where the caret moved.
|
||||
// See imeCaretPos.
|
||||
r.imeCaretPos = f32.Point{
|
||||
X: float32(r.toPx(cursorX)),
|
||||
Y: float32(r.toPx(reg.Y - scrollOffset + caretY)),
|
||||
}
|
||||
r.imeCaretAscent = float32(r.toPx(ascent))
|
||||
r.imeCaretDescent = float32(r.toPx(lineH - ascent))
|
||||
|
||||
// Draw the cursor (thin vertical bar)
|
||||
cursorRegion := Region{
|
||||
@@ -952,6 +1381,10 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
|
||||
// inside the editor region.
|
||||
r.selDragsOn = [4]bool{}
|
||||
event.Op(gtx.Ops, r.pressProbe)
|
||||
// Pinch probe: same clip as the press probe, so a pinch only registers
|
||||
// when both fingers' presses/moves land in the editor text region.
|
||||
event.Op(gtx.Ops, r.pinchProbe)
|
||||
r.pinchProbeOn = true
|
||||
r.longPressID = "editor_text"
|
||||
if r.selDragHandler != nil && (selStart >= 0 && selEnd > selStart || caretDrag) {
|
||||
// handleAt mirrors the cursor computation above: window-relative byte
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"image"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gioui.org/font/gofont"
|
||||
"gioui.org/gesture"
|
||||
"gioui.org/io/event"
|
||||
"gioui.org/io/input"
|
||||
"gioui.org/io/key"
|
||||
"gioui.org/io/pointer"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
)
|
||||
|
||||
// TestRevealFocusDrainTermination reproduces the IME-open scenario at the
|
||||
// router level: the focused editor field has registered (stale, taller)
|
||||
// bounds, the window's viewport shrinks, and the framework calls
|
||||
// Router.RevealFocus which synthesizes a pointer.Scroll for the focused
|
||||
// field's scroll handler. The app's shrink-frame fix (Renderer
|
||||
// ZeroWheelScroll) drains pointer.Scroll events for the gesture's tag before
|
||||
// gesture.Scroll.Update consumes them. This test verifies:
|
||||
// 1. RevealFocus really does queue a scroll event for the gesture tag,
|
||||
// 2. the drain loop terminates (bounded iteration count),
|
||||
// 3. after the drain, gesture.Scroll.Update returns 0 (content does not
|
||||
// move), and
|
||||
// 4. on a normal (non-shrink) frame the drain is not needed and the
|
||||
// gesture consumes whatever scroll it would have consumed anyway.
|
||||
func TestRevealFocusDrainTermination(t *testing.T) {
|
||||
shp := text.NewShaper(text.WithCollection(gofont.Collection()))
|
||||
r := New(Theme{FontSize: 14}, shp)
|
||||
noop := func(any) {}
|
||||
|
||||
const (
|
||||
wW = 411
|
||||
wH = 914
|
||||
)
|
||||
editorRegion := Region{X: 10, Y: 52, W: wW - 20, H: wH - 92}
|
||||
// KeyDown is required: it is what makes drawElement record the
|
||||
// event.Op reference for the tag, without which the key queue clears
|
||||
// the FocusCmd target ("tag has no event.Op references").
|
||||
editor := NewTextField("editor_text", "hello world\nsecond line\nthird line", editorRegion, true, editorRegion.W, 0, 5, -1, -1, []Interaction{
|
||||
{Gesture: Scroll, Handler: noop},
|
||||
{Gesture: KeyDown, Handler: noop},
|
||||
})
|
||||
editor.Focused = true
|
||||
elems := []Element{editor}
|
||||
|
||||
m := unit.Metric{PxPerDp: 1, PxPerSp: 1}
|
||||
|
||||
var rtr input.Router
|
||||
gtxFor := func(ops *op.Ops) layout.Context {
|
||||
return layout.Context{
|
||||
Ops: ops,
|
||||
Metric: m,
|
||||
Source: rtr.Source(),
|
||||
Constraints: layout.Constraints{Min: image.Point{}, Max: image.Point{X: wW, Y: wH}},
|
||||
}
|
||||
}
|
||||
// The app consumes a key.FocusFilter for the focused field every frame
|
||||
// (main.go); that is what marks the handler focusable so keyQueue.Frame
|
||||
// keeps the focus across frames.
|
||||
drainFocus := func(q input.Source) {
|
||||
for {
|
||||
if _, ok := q.Event(key.FocusFilter{Target: event.Tag("editor_text")}); !ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Frame 1: register handlers, focus the field (Draw issues the FocusCmd).
|
||||
{
|
||||
var ops op.Ops
|
||||
gtx := gtxFor(&ops)
|
||||
r.Draw(gtx, elems, 1.0)
|
||||
drainFocus(gtx.Source)
|
||||
rtr.Frame(&ops)
|
||||
}
|
||||
scroll := r.scrolls["editor_text"].scroll
|
||||
t.Logf("focused(editor_text) after frame1: %v", rtr.Source().Focused(event.Tag("editor_text")))
|
||||
|
||||
// Frame 2: the app consumes gestures every frame; gesture.Scroll.Update
|
||||
// registers the Scroll kind in the handler's filter, which Router.Deliver
|
||||
// (used by RevealFocus/ScrollFocus) requires to match.
|
||||
{
|
||||
var ops op.Ops
|
||||
gtx := gtxFor(&ops)
|
||||
r.Draw(gtx, elems, 1.0)
|
||||
drainFocus(gtx.Source)
|
||||
_ = scroll.Update(m, gtx.Source, time.Now(), gesture.Vertical,
|
||||
pointer.ScrollRange{}, pointer.ScrollRange{Min: -(1 << 30), Max: 1 << 30})
|
||||
rtr.Frame(&ops)
|
||||
}
|
||||
|
||||
// Simulate the keyboard opening: the framework shrinks the viewport and
|
||||
// calls RevealFocus with the new (smaller) viewport.
|
||||
shrunk := image.Rectangle{Min: image.Point{}, Max: image.Point{X: wW, Y: wH - 400}}
|
||||
rtr.RevealFocus(shrunk)
|
||||
|
||||
// The app's shrink-frame fix: drain pointer.Scroll for the gesture tag.
|
||||
q := rtr.Source()
|
||||
drained := 0
|
||||
for {
|
||||
evt, ok := q.Event(pointer.Filter{Target: scroll, Kinds: pointer.Scroll})
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
drained++
|
||||
if pe, ok := evt.(pointer.Event); ok {
|
||||
t.Logf("drained synthetic scroll: %+v", pe.Scroll)
|
||||
}
|
||||
if drained > 10 {
|
||||
t.Fatalf("drain loop did not terminate within 10 iterations")
|
||||
}
|
||||
}
|
||||
if drained == 0 {
|
||||
t.Fatalf("RevealFocus did not queue a scroll event for the scroll gesture tag")
|
||||
}
|
||||
|
||||
// The gesture must now see no scroll (content stays put).
|
||||
var ops op.Ops
|
||||
r.Draw(gtxFor(&ops), elems, 1.0)
|
||||
delta := scroll.Update(m, q, time.Now(), gesture.Vertical,
|
||||
pointer.ScrollRange{}, pointer.ScrollRange{Min: -(1 << 30), Max: 1 << 30})
|
||||
if delta != 0 {
|
||||
t.Fatalf("gesture consumed %d px of scroll after drain; content would shift", delta)
|
||||
}
|
||||
rtr.Frame(&ops)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gioui.org/font/gofont"
|
||||
"gioui.org/text"
|
||||
)
|
||||
|
||||
// Regression test for the pinch-on-device bug: the probes were declared as
|
||||
// `struct{}` fields. The unnamed fieldless struct{} is a single canonical Go
|
||||
// type, so all three probes were the SAME tag value and Gio's router merged
|
||||
// them into one handler — the press probe (drained first) consumed every
|
||||
// event and the pinch probe was starved. Each probe now has its own named
|
||||
// type and must be a distinct map key.
|
||||
func TestProbeTagIdentity(t *testing.T) {
|
||||
r := New(Theme{FontSize: 14}, text.NewShaper(text.WithCollection(gofont.Collection())))
|
||||
m := map[interface{}]int{}
|
||||
m[r.pressProbe] = 1
|
||||
m[r.pinchProbe] = 2
|
||||
t.Logf("distinct probe tag keys: %d", len(m))
|
||||
if len(m) != 2 {
|
||||
t.Fatalf("probe tags must be distinct values, got %d distinct keys", len(m))
|
||||
}
|
||||
var a, b interface{} = r.pressProbe, r.pinchProbe
|
||||
if a == b {
|
||||
t.Fatal("pressProbe and pinchProbe must not be equal interface values")
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,11 @@ type LayoutFeedback struct {
|
||||
WindowStartByte int // absolute byte offset of the window's first byte
|
||||
WindowStartLine int // logical line the window starts at (-1: none)
|
||||
EditSeq uint64 // editor content-edit counter at frame time
|
||||
// ScrollOffset is the editor scroll offset (Dp) the frame this layout
|
||||
// was shaped from carried. The logic needs it to express layout positions
|
||||
// (window-relative) in content coordinates: the window top is the
|
||||
// shaped scroll's sub-line remainder above the region top.
|
||||
ScrollOffset Dp
|
||||
}
|
||||
|
||||
type GlyphLayout struct {
|
||||
|
||||
+24
-1
@@ -363,8 +363,31 @@ new2 = ".method public onCreate(Landroid/os/Bundle;)V\n .locals 3"
|
||||
if old2 not in t:
|
||||
sys.exit("ERROR: GioActivity onCreate .locals anchor not found")
|
||||
t = t.replace(old2, new2)
|
||||
# 3. onStop hook: persist the session snapshot the moment the activity goes
|
||||
# away (recents-wipe, app switch). The OS provides no user-space hook for
|
||||
# the process kill that follows, so this is the last reliable flush. Go
|
||||
# side: pad_flush_session (impl_android.go) -> Logic.FlushSession.
|
||||
old3 = """.method public onStop()V
|
||||
.locals 1
|
||||
|
||||
.line 47
|
||||
iget-object v0, p0, Lorg/gioui/GioActivity;->view:Lorg/gioui/GioView;"""
|
||||
new3 = """.method public static native padFlushSession()V
|
||||
.end method
|
||||
|
||||
.method public onStop()V
|
||||
.locals 1
|
||||
|
||||
# Pad: persist the session snapshot before the activity goes away.
|
||||
invoke-static {}, Lorg/gioui/GioActivity;->padFlushSession()V
|
||||
|
||||
.line 47
|
||||
iget-object v0, p0, Lorg/gioui/GioActivity;->view:Lorg/gioui/GioView;"""
|
||||
if old3 not in t:
|
||||
sys.exit("ERROR: GioActivity onStop anchor not found")
|
||||
t = t.replace(old3, new3)
|
||||
p.write_text(t)
|
||||
print("patched: GioActivity IME insets wiring")
|
||||
print("patched: GioActivity IME insets wiring + onStop session flush")
|
||||
PYEOF
|
||||
|
||||
echo "=== apktool rebuild ==="
|
||||
|
||||
+30
-1
@@ -31,8 +31,14 @@ command -v apksigner >/dev/null 2>&1 || die "apksigner not found (need $ANDROID_
|
||||
command -v adb >/dev/null 2>&1 || die "adb not found (need $ANDROID_HOME/platform-tools)"
|
||||
[ -f "$HOME/.android/debug.keystore" ] || die "debug keystore missing: $HOME/.android/debug.keystore"
|
||||
|
||||
# SKIP_CHECK=1 skips the static checks (scripts/release.sh already runs them
|
||||
# as gate 1); a standalone build always runs them.
|
||||
if [ "${SKIP_CHECK:-0}" = "1" ]; then
|
||||
echo "=== static checks (skipped: SKIP_CHECK=1) ==="
|
||||
else
|
||||
echo "=== static checks (go vet + staticcheck) ==="
|
||||
"$REPO/scripts/check.sh"
|
||||
fi
|
||||
|
||||
APKTOOL="$ANDROID_HOME/tools/apktool.jar"
|
||||
if [ ! -f "$APKTOOL" ]; then
|
||||
@@ -356,8 +362,31 @@ new2 = ".method public onCreate(Landroid/os/Bundle;)V\n .locals 3"
|
||||
if old2 not in t:
|
||||
sys.exit("ERROR: GioActivity onCreate .locals anchor not found")
|
||||
t = t.replace(old2, new2)
|
||||
# 3. onStop hook: persist the session snapshot the moment the activity goes
|
||||
# away (recents-wipe, app switch). The OS provides no user-space hook for
|
||||
# the process kill that follows, so this is the last reliable flush. Go
|
||||
# side: pad_flush_session (impl_android.go) -> Logic.FlushSession.
|
||||
old3 = """.method public onStop()V
|
||||
.locals 1
|
||||
|
||||
.line 47
|
||||
iget-object v0, p0, Lorg/gioui/GioActivity;->view:Lorg/gioui/GioView;"""
|
||||
new3 = """.method public static native padFlushSession()V
|
||||
.end method
|
||||
|
||||
.method public onStop()V
|
||||
.locals 1
|
||||
|
||||
# Pad: persist the session snapshot before the activity goes away.
|
||||
invoke-static {}, Lorg/gioui/GioActivity;->padFlushSession()V
|
||||
|
||||
.line 47
|
||||
iget-object v0, p0, Lorg/gioui/GioActivity;->view:Lorg/gioui/GioView;"""
|
||||
if old3 not in t:
|
||||
sys.exit("ERROR: GioActivity onStop anchor not found")
|
||||
t = t.replace(old3, new3)
|
||||
p.write_text(t)
|
||||
print("patched: GioActivity IME insets wiring")
|
||||
print("patched: GioActivity IME insets wiring + onStop session flush")
|
||||
PYEOF
|
||||
|
||||
echo "=== apktool rebuild ==="
|
||||
|
||||
+9
-4
@@ -16,7 +16,11 @@
|
||||
# # first tap right after launch/open is
|
||||
# # sometimes swallowed — re-tap.
|
||||
# scripts/emu.sh type TEXT # type into the focused field (spaces ok)
|
||||
# scripts/emu.sh cmd <top|bottom|frac F|dp N> # one-shot editor debug command
|
||||
# scripts/emu.sh cmd <top|bottom|frac F|dp N|pinch F|fontsize F>
|
||||
# # one-shot editor debug command
|
||||
# # (pinch F = relative app font scale
|
||||
# # anchored at the region center;
|
||||
# # fontsize F = absolute, top-anchored)
|
||||
# scripts/emu.sh perf on|off # enable/disable the in-app profiler
|
||||
# scripts/emu.sh perf pull [FILE] # pull logic_frames.csv (default ./logic_frames.csv)
|
||||
# scripts/emu.sh push FILE # push FILE -> /storage/emulated/0/Notes/
|
||||
@@ -30,7 +34,8 @@
|
||||
# the CSV is truncated on app relaunch, so pull it before restarting the
|
||||
# app if you are accumulating data.
|
||||
# - One-shot editor debug commands are read from /storage/emulated/0/PadPerf/cmd
|
||||
# (scroll: top | bottom | "frac 0.5" | "dp 1234").
|
||||
# (scroll: top | bottom | "frac 0.5" | "dp 1234"; app font scale:
|
||||
# "pinch 1.1" relative | "fontsize 1.5" absolute).
|
||||
# - Test files go under /storage/emulated/0/Notes/.
|
||||
# - The emulator OOMs above ~2.5 GB RSS on this VM; kill it if it wedges.
|
||||
#
|
||||
@@ -138,7 +143,7 @@ cmd_up() {
|
||||
kill -9 "$qp" 2>/dev/null || true
|
||||
sleep 2
|
||||
fi
|
||||
start_emulator
|
||||
start_emulator "$@"
|
||||
local rc=0
|
||||
wait_device_online "$ONLINE_TIMEOUT" || rc=$?
|
||||
if [ "$rc" -ne 0 ]; then
|
||||
@@ -163,7 +168,7 @@ cmd_up() {
|
||||
sleep 1
|
||||
j=$((j + 1))
|
||||
done
|
||||
start_emulator -no-snapshot-load
|
||||
start_emulator -no-snapshot-load "$@"
|
||||
# Note: full BOOT_TIMEOUT here — a cold boot legitimately takes far
|
||||
# longer than the ONLINE_TIMEOUT used for the first (snapshot) attempt.
|
||||
wait_device_online "$BOOT_TIMEOUT" || { tail -n 5 "$EMU_LOG" >&2; die "device never came online (cold boot); see $EMU_LOG"; }
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate cmd/pad/appicon.png, the Pad app icon.
|
||||
|
||||
Design: a soft, light cream pillow on a dark ink field, with "P_" (the
|
||||
underscore standing in for the text cursor) embroidered on it in slate
|
||||
thread. gogio picks appicon.png up automatically from the main package
|
||||
(cmd/pad) and builds the Android mipmap/adaptive-icon set and the iOS
|
||||
icon set from it.
|
||||
|
||||
Usage: scripts/make_icon.py
|
||||
Requires: python3 with PIL.
|
||||
"""
|
||||
import pathlib
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFilter, ImageFont
|
||||
|
||||
SIZE = 1024
|
||||
INK = (32, 38, 50) # background
|
||||
PILLOW = (243, 231, 211) # base fabric
|
||||
PILLOW_LIT = (252, 246, 234)
|
||||
PILLOW_RIM = (206, 187, 158)
|
||||
THREAD = (92, 104, 134) # embroidered slate thread
|
||||
THREAD_DARK = (58, 67, 92)
|
||||
|
||||
FONT = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf"
|
||||
|
||||
|
||||
def rrect_mask(w, h, r):
|
||||
m = Image.new("L", (w, h), 0)
|
||||
ImageDraw.Draw(m).rounded_rectangle([0, 0, w - 1, h - 1], radius=r, fill=255)
|
||||
return m
|
||||
|
||||
|
||||
def main() -> None:
|
||||
img = Image.new("RGB", (SIZE, SIZE), INK)
|
||||
|
||||
# --- pillow geometry (kept inside the adaptive-icon safe zone) ---
|
||||
pw, ph, pr = 672, 612, 195 # pillow size + corner radius
|
||||
px0, py0 = (SIZE - pw) // 2, (SIZE - ph) // 2 + 6
|
||||
|
||||
# --- soft drop shadow (slightly below, so light comes from above) ---
|
||||
shadow = Image.new("L", (SIZE, SIZE), 0)
|
||||
d = ImageDraw.Draw(shadow)
|
||||
d.rounded_rectangle([px0 + 16, py0 + 30, px0 + pw - 16, py0 + ph + 36],
|
||||
radius=pr + 10, fill=120)
|
||||
shadow = shadow.filter(ImageFilter.GaussianBlur(42))
|
||||
img = Image.composite(Image.new("RGB", (SIZE, SIZE), (18, 21, 29)), img,
|
||||
shadow)
|
||||
|
||||
# --- pillow body: plump radial shading ---
|
||||
base = Image.new("RGB", (SIZE, SIZE), INK)
|
||||
base.paste(Image.new("RGB", (pw, ph), PILLOW), (px0, py0))
|
||||
sharp = rrect_mask(pw, ph, pr)
|
||||
inner = sharp.filter(ImageFilter.GaussianBlur(pr * 0.8))
|
||||
lit = Image.new("RGB", (pw, ph), PILLOW_LIT)
|
||||
rim = Image.new("RGB", (pw, ph), PILLOW_RIM)
|
||||
base.paste(lit, (px0, py0), inner) # bright center
|
||||
rim_mask = Image.composite(Image.new("L", (pw, ph), 0), sharp,
|
||||
inner) # edge only
|
||||
rim_mask = rim_mask.point(lambda v: min(255, v * 2))
|
||||
base.paste(rim, (px0, py0), rim_mask) # shaded edge
|
||||
|
||||
# top sheen: an out-of-focus light patch
|
||||
sheen = Image.new("L", (SIZE, SIZE), 0)
|
||||
d = ImageDraw.Draw(sheen)
|
||||
d.ellipse([px0 + 110, py0 + 30, px0 + pw - 110, py0 + ph // 2 + 40],
|
||||
fill=80)
|
||||
sheen = sheen.filter(ImageFilter.GaussianBlur(75))
|
||||
base = Image.composite(Image.new("RGB", (SIZE, SIZE), PILLOW_LIT),
|
||||
base, sheen)
|
||||
|
||||
# corner dimples: soft darkening where a puffed pillow pinches
|
||||
for cx, cy in [
|
||||
(px0 + pr - 20, py0 + pr - 20), (px0 + pw - pr + 20, py0 + pr - 20),
|
||||
(px0 + pr - 20, py0 + ph - pr + 20),
|
||||
(px0 + pw - pr + 20, py0 + ph - pr + 20),
|
||||
]:
|
||||
dip = Image.new("L", (SIZE, SIZE), 0)
|
||||
ImageDraw.Draw(dip).ellipse([cx - 70, cy - 70, cx + 70, cy + 70],
|
||||
fill=55)
|
||||
dip = dip.filter(ImageFilter.GaussianBlur(30))
|
||||
base = Image.composite(
|
||||
Image.blend(base, Image.new("RGB", (SIZE, SIZE),
|
||||
(214, 196, 168)), 0.5),
|
||||
base, dip)
|
||||
|
||||
# --- dashed seam line, like a quilted pillow ---
|
||||
# Sample the rounded-rect path finely, then draw dash segments along it.
|
||||
m = 44 # margin from the pillow edge
|
||||
r = pr - m # corner radius of the seam
|
||||
import math as _m
|
||||
# Corners clockwise: center, angle start -> end (screen coords, y down).
|
||||
# Each arc is followed by the straight side to the next arc's start.
|
||||
arcs = [
|
||||
(m + r, m + r, _m.pi, 1.5 * _m.pi), # top-left
|
||||
(pw - m - r, m + r, 1.5 * _m.pi, 2 * _m.pi), # top-right
|
||||
(pw - m - r, ph - m - r, 0, 0.5 * _m.pi), # bottom-right
|
||||
(m + r, ph - m - r, 0.5 * _m.pi, _m.pi), # bottom-left
|
||||
]
|
||||
samples = []
|
||||
for i, (cx, cy, a0, a1) in enumerate(arcs):
|
||||
nx, ny, na0, _ = arcs[(i + 1) % 4]
|
||||
n = max(2, int(abs(a1 - a0) * r // 3))
|
||||
for k in range(n + 1):
|
||||
a = a0 + (a1 - a0) * k / n
|
||||
samples.append((cx + r * _m.cos(a), cy + r * _m.sin(a)))
|
||||
# straight side from this arc's end to the next arc's start
|
||||
end = (nx + r * _m.cos(na0), ny + r * _m.sin(na0))
|
||||
last = samples[-1]
|
||||
n = max(2, int(_m.hypot(end[0] - last[0], end[1] - last[1]) // 3))
|
||||
for k in range(1, n + 1):
|
||||
samples.append((last[0] + (end[0] - last[0]) * k / n,
|
||||
last[1] + (end[1] - last[1]) * k / n))
|
||||
# cumulative arc length
|
||||
cum = [0.0]
|
||||
for i in range(1, len(samples)):
|
||||
cum.append(cum[-1] + _m.hypot(samples[i][0] - samples[i - 1][0],
|
||||
samples[i][1] - samples[i - 1][1]))
|
||||
total = cum[-1]
|
||||
dash, gap = 26.0, 18.0
|
||||
seam_ring = Image.new("L", (pw, ph), 0)
|
||||
d = ImageDraw.Draw(seam_ring)
|
||||
s0 = 0.0
|
||||
while s0 < total:
|
||||
s1 = min(s0 + dash, total)
|
||||
pts = [(x, y) for (x, y), c in zip(samples, cum) if s0 <= c <= s1]
|
||||
if len(pts) > 1:
|
||||
d.line(pts, fill=255, width=9, joint="curve")
|
||||
s0 += dash + gap
|
||||
seam_ring = seam_ring.filter(ImageFilter.GaussianBlur(1.2))
|
||||
seam = seam_ring
|
||||
seam_full = Image.new("L", (SIZE, SIZE), 0)
|
||||
seam_full.paste(seam, (px0, py0))
|
||||
seam_col = Image.new("RGB", (SIZE, SIZE), (203, 183, 154))
|
||||
base = Image.composite(seam_col, base, seam_full)
|
||||
|
||||
# composite pillow onto background via its sharp mask
|
||||
full_sharp = Image.new("L", (SIZE, SIZE), 0)
|
||||
full_sharp.paste(sharp, (px0, py0))
|
||||
img.paste(base, (0, 0), full_sharp)
|
||||
|
||||
# --- embroidered "P_" ---
|
||||
# Center the CAP of the P on the pillow (optical center); the
|
||||
# underscore hangs below the baseline like a cursor on the next line.
|
||||
font = ImageFont.truetype(FONT, 300)
|
||||
# Drawing "P" at (x, y) puts its cap top at y + pb[1]; center the cap
|
||||
# on the pillow, keep the whole "P_" string on the same baseline.
|
||||
probe = ImageDraw.Draw(img)
|
||||
pb = probe.textbbox((0, 0), "P", font=font)
|
||||
cb_h = pb[3] - pb[1] # cap height
|
||||
ty = SIZE // 2 - cb_h // 2 - pb[1]
|
||||
tb = probe.textbbox((0, 0), "P_", font=font)
|
||||
tw, th = tb[2] - tb[0], tb[3] - tb[1]
|
||||
tx = (SIZE - tw) // 2 - tb[0]
|
||||
|
||||
# stitch texture: fine diagonal thread lines, tiled
|
||||
tile = 6
|
||||
pat = Image.new("L", (tile, tile), 160)
|
||||
ImageDraw.Draw(pat).line([(0, tile - 1), (tile - 1, 0)], fill=255,
|
||||
width=1)
|
||||
pat = pat.filter(ImageFilter.GaussianBlur(0.5))
|
||||
mw, mh = tw + 40, tb[3] + 80
|
||||
stitch = pat.resize((mw, mh), Image.NEAREST)
|
||||
|
||||
# text mask in local coords
|
||||
# local (0,0) == the absolute drawing origin (tx, ty); paste happens at
|
||||
# (tx-20, ty-20), so text goes at (20 - tb[0], 20).
|
||||
mask = Image.new("L", (mw, mh), 0)
|
||||
ImageDraw.Draw(mask).text((20 - tb[0], 20), "P_", font=font,
|
||||
fill=255)
|
||||
|
||||
# drop shadow of the stitches on the fabric (thread sits on top of it)
|
||||
sh = mask.filter(ImageFilter.GaussianBlur(5))
|
||||
sh_full = Image.new("L", (SIZE, SIZE), 0)
|
||||
sh_full.paste(sh, (tx - 20, ty - 20 + 7))
|
||||
img = Image.composite(Image.new("RGB", (SIZE, SIZE), (182, 164, 140)),
|
||||
img, sh_full)
|
||||
|
||||
# thread color modulated by the stitch pattern (subtle per-stitch depth)
|
||||
stitch_full = Image.new("L", (SIZE, SIZE), 0)
|
||||
stitch_full.paste(stitch, (tx - 20, ty - 20))
|
||||
mod = Image.composite(Image.new("RGB", (SIZE, SIZE), THREAD_DARK),
|
||||
Image.new("RGB", (SIZE, SIZE), THREAD),
|
||||
stitch_full)
|
||||
mask_full = Image.new("L", (SIZE, SIZE), 0)
|
||||
mask_full.paste(mask, (tx - 20, ty - 20))
|
||||
img = Image.composite(mod, img, mask_full)
|
||||
|
||||
out = pathlib.Path(__file__).resolve().parent.parent / "cmd" / "pad" / "appicon.png"
|
||||
img.save(out)
|
||||
print(f"wrote {out} ({SIZE}x{SIZE})")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+62
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env bash
|
||||
# Pad release: run the release gates, build the phone APK, and install it to
|
||||
# every connected device.
|
||||
#
|
||||
# The full process, policies, and failure handling are documented in
|
||||
# doc/release.md — this script is the executable form of that document.
|
||||
#
|
||||
# Gates (ALL must pass; any failure aborts before the install):
|
||||
# 1. static checks: go vet + staticcheck (scripts/check.sh)
|
||||
# 2. go test -count=1 ./... (includes TestNoFramesWhileIdle)
|
||||
# 3. frame-regression profile on the EMULATOR (scripts/profile_emulator.sh)
|
||||
#
|
||||
# Install policy (doc/release.md):
|
||||
# - The built APK is installed to EVERY connected device — emulators and
|
||||
# phones. Pushing a release build to the developer's phone is the
|
||||
# DEFAULT, expected behavior.
|
||||
# - This script installs ONLY. It never profiles or tests a phone.
|
||||
# On-device profiling/testing (e.g. scripts/profile_emulator.sh -s
|
||||
# <phone serial>) is diagnostic and requires explicit developer approval
|
||||
# per run.
|
||||
#
|
||||
# Usage:
|
||||
# ./scripts/release.sh # gates + build + install to all devices
|
||||
# ./scripts/release.sh --no-install
|
||||
set -euo pipefail
|
||||
|
||||
REPO=$(cd "$(dirname "$0")/.." && pwd)
|
||||
INSTALL=1
|
||||
[ "${1:-}" = "--no-install" ] && INSTALL=0
|
||||
|
||||
if [ -n "$(git -C "$REPO" status --porcelain 2>/dev/null)" ]; then
|
||||
echo "NOTE: working tree is dirty — the gates run on the current (possibly"
|
||||
echo " uncommitted) state. Commit the release work before shipping."
|
||||
fi
|
||||
|
||||
echo "=== gate 1/3: static checks ==="
|
||||
"$REPO/scripts/check.sh"
|
||||
|
||||
echo "=== gate 2/3: go test (includes TestNoFramesWhileIdle) ==="
|
||||
(cd "$REPO" && go test -count=1 ./...)
|
||||
|
||||
echo "=== gate 3/3: frame-regression profile (emulator only) ==="
|
||||
"$REPO/scripts/profile_emulator.sh"
|
||||
|
||||
echo "=== build phone APK ==="
|
||||
SKIP_CHECK=1 "$REPO/scripts/build_phone.sh" --no-install
|
||||
|
||||
if [ "$INSTALL" = "1" ]; then
|
||||
echo "=== install to all connected devices ==="
|
||||
serials=$(adb devices | awk 'NR>1 && $2=="device" {print $1}')
|
||||
if [ -z "$serials" ]; then
|
||||
echo "no connected devices — APK built at cmd/pad/pad-phone.apk;"
|
||||
echo "install later with: adb -s <serial> install -r cmd/pad/pad-phone.apk"
|
||||
else
|
||||
for s in $serials; do
|
||||
echo "-- installing on $s"
|
||||
adb -s "$s" install -r "$REPO/cmd/pad/pad-phone.apk"
|
||||
done
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "=== RELEASE COMPLETE ==="
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="touch.inject">
|
||||
|
||||
<uses-permission android:name="android.permission.INJECT_EVENTS"/>
|
||||
|
||||
<application>
|
||||
<service android:name=".Injector" android:exported="true"/>
|
||||
<receiver android:name=".Injector$ScriptReceiver" android:exported="true"/>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -0,0 +1,223 @@
|
||||
package touch.inject;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.hardware.input.InputManager;
|
||||
import android.os.IBinder;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Multi-touch gesture injector for the Pad emulator.
|
||||
*
|
||||
* Runs as a privileged system app (/system/priv-app) so that the
|
||||
* signature|privileged INJECT_EVENTS permission is granted; it then injects
|
||||
* real MotionEvents through InputManager — the exact path a physical touch
|
||||
* screen takes — which is what the adb `input` applet cannot do (it has no
|
||||
* multi-touch support).
|
||||
*
|
||||
* Usage:
|
||||
* adb shell am startservice -n touch.inject/.Injector -e script "SCRIPT"
|
||||
*
|
||||
* SCRIPT is a whitespace-separated sequence of:
|
||||
* down <finger> <x> <y> finger (1-based) touches at (x,y) px
|
||||
* move <finger> <x> <y> finger moves to (x,y) px
|
||||
* up <finger> finger lifts
|
||||
* wait <ms> pause
|
||||
*
|
||||
* Progress is logged to logcat under the "TouchInject" tag.
|
||||
*/
|
||||
public class Injector extends Service {
|
||||
public static final String TAG = "TouchInject";
|
||||
|
||||
/** Shell-triggerable entry point: works even while the app is stopped
|
||||
* (the shell can broadcast to an explicit component).
|
||||
* KNOWN FLAKE: the process is "cached" while the thread runs and the
|
||||
* 1.5GB emulator OOM-killed it once mid-script (during a 200ms wait),
|
||||
* losing the final UP. Service routing is NOT an alternative — Android
|
||||
* 12+ blocks background startService from a receiver, and the AVD's
|
||||
* locked bootloader blocks the system-app escalation. The harness
|
||||
* therefore verifies "=== done" in logcat after every script and
|
||||
* re-runs on failure; scripts should stay short. */
|
||||
public static class ScriptReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String script = intent.getStringExtra("script");
|
||||
if (script == null) {
|
||||
return;
|
||||
}
|
||||
InputManager im = (InputManager)
|
||||
context.getSystemService(Context.INPUT_SERVICE);
|
||||
new Thread(() -> execute(im, script), "TouchInject").start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
String script = intent != null ? intent.getStringExtra("script") : null;
|
||||
if (script == null) {
|
||||
stopSelf();
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
InputManager im = (InputManager) getSystemService(INPUT_SERVICE);
|
||||
new Thread(() -> execute(im, script), "TouchInject").start();
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void execute(InputManager im, String script) {
|
||||
Log.i(TAG, "=== script: " + script);
|
||||
// finger number (1-based) -> [x, y]; insertion order = pointer index.
|
||||
LinkedHashMap<Integer, float[]> fingers = new LinkedHashMap<>();
|
||||
long downTime = 0;
|
||||
String[] toks = script.split("\\s+");
|
||||
int i = 0;
|
||||
while (i < toks.length) {
|
||||
String cmd = toks[i++];
|
||||
try {
|
||||
switch (cmd) {
|
||||
case "down": {
|
||||
int f = Integer.parseInt(toks[i++]);
|
||||
float x = Float.parseFloat(toks[i++]);
|
||||
float y = Float.parseFloat(toks[i++]);
|
||||
int action = fingers.isEmpty()
|
||||
? MotionEvent.ACTION_DOWN
|
||||
: MotionEvent.ACTION_POINTER_DOWN;
|
||||
int idx = fingers.size();
|
||||
fingers.put(f, new float[]{x, y});
|
||||
inject(im, action, idx, fingers, downTime);
|
||||
if (downTime == 0) {
|
||||
downTime = SystemClock.uptimeMillis();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "move": {
|
||||
int f = Integer.parseInt(toks[i++]);
|
||||
float x = Float.parseFloat(toks[i++]);
|
||||
float y = Float.parseFloat(toks[i++]);
|
||||
float[] p = fingers.get(f);
|
||||
if (p == null) {
|
||||
Log.w(TAG, "move of unknown finger " + f + " (skipped)");
|
||||
break;
|
||||
}
|
||||
p[0] = x;
|
||||
p[1] = y;
|
||||
// Generic ACTION_MOVE for all moves (1 or N pointers):
|
||||
// InputFlinger accepts it and GioView treats every
|
||||
// pointer as a MOVE.
|
||||
inject(im, MotionEvent.ACTION_MOVE, 0, fingers, downTime);
|
||||
break;
|
||||
}
|
||||
case "up": {
|
||||
int f = Integer.parseInt(toks[i++]);
|
||||
if (!fingers.containsKey(f)) {
|
||||
Log.w(TAG, "up of unknown finger " + f + " (skipped)");
|
||||
break;
|
||||
}
|
||||
boolean last = fingers.size() == 1;
|
||||
int action = last
|
||||
? MotionEvent.ACTION_UP
|
||||
: MotionEvent.ACTION_POINTER_UP;
|
||||
int idx = indexOf(fingers, f);
|
||||
// The lifted pointer must still be part of the event.
|
||||
inject(im, action, idx, fingers, downTime);
|
||||
fingers.remove(f);
|
||||
if (fingers.isEmpty()) {
|
||||
downTime = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "wait": {
|
||||
int ms = Integer.parseInt(toks[i++]);
|
||||
Thread.sleep(ms);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
Log.e(TAG, "unknown command: " + cmd);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "script error at '" + cmd + "': " + e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Log.i(TAG, "=== done");
|
||||
}
|
||||
|
||||
private static int indexOf(LinkedHashMap<Integer, float[]> m, int f) {
|
||||
int idx = 0;
|
||||
for (Integer k : m.keySet()) {
|
||||
if (k == f) {
|
||||
return idx;
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT. The SDK stub jar omits the
|
||||
// constant (and injectInputEvent itself), so both go through reflection;
|
||||
// the method is a public API at runtime on the device.
|
||||
private static final int INJECT_WAIT_FOR_RESULT = 1;
|
||||
|
||||
private static void inject(InputManager im, int action, int actionIndex,
|
||||
Map<Integer, float[]> fingers, long downTime) {
|
||||
int n = fingers.size();
|
||||
int[] ids = new int[n];
|
||||
MotionEvent.PointerCoords[] coords = new MotionEvent.PointerCoords[n];
|
||||
int j = 0;
|
||||
for (Map.Entry<Integer, float[]> e : fingers.entrySet()) {
|
||||
ids[j] = e.getKey() - 1; // pointer id (0-based)
|
||||
MotionEvent.PointerCoords c = new MotionEvent.PointerCoords();
|
||||
c.x = e.getValue()[0];
|
||||
c.y = e.getValue()[1];
|
||||
c.pressure = 1f;
|
||||
c.size = 1f;
|
||||
// TOOL_TYPE_FINGER. The compile-time android.jar lacks
|
||||
// PointerCoords.setToolType, but the device runtime (API 24+)
|
||||
// has it, so call it reflectively.
|
||||
try {
|
||||
java.lang.reflect.Method stt = MotionEvent.PointerCoords.class
|
||||
.getMethod("setToolType", int.class);
|
||||
stt.invoke(c, 1);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
coords[j] = c;
|
||||
j++;
|
||||
}
|
||||
long now = SystemClock.uptimeMillis();
|
||||
int fullAction = action == MotionEvent.ACTION_MOVE
|
||||
? action
|
||||
: action | (actionIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT);
|
||||
MotionEvent ev = MotionEvent.obtain(downTime, now, fullAction, n, ids, coords,
|
||||
0 /*edgeFlags*/, 1f /*xPrecision*/, 1f /*yPrecision*/,
|
||||
0 /*metaState*/, 0 /*deviceId*/,
|
||||
0x4000003 /*SOURCE_TOUCH|SOURCE_CLASS_MASK*/, 0 /*displayId*/);
|
||||
boolean ok = doInject(im, ev);
|
||||
Log.i(TAG, String.format("inject action=%d idx=%d n=%d ids=%s ok=%b",
|
||||
action, actionIndex, n, java.util.Arrays.toString(ids), ok));
|
||||
ev.recycle();
|
||||
}
|
||||
|
||||
private static boolean doInject(InputManager im, MotionEvent ev) {
|
||||
try {
|
||||
java.lang.reflect.Method m = InputManager.class.getMethod(
|
||||
"injectInputEvent", android.view.InputEvent.class, int.class);
|
||||
Object r = m.invoke(im, ev, INJECT_WAIT_FOR_RESULT);
|
||||
return r instanceof Boolean && (Boolean) r;
|
||||
} catch (Exception e) {
|
||||
Throwable c = e.getCause() != null ? e.getCause() : e;
|
||||
Log.e(TAG, "injectInputEvent failed: " + c);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Executable
+48
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env bash
|
||||
# Builds touchinject.apk (a privileged multi-touch gesture injector) from
|
||||
# Injector.java using the local Android SDK. The APK must be installed under
|
||||
# /system/priv-app for the INJECT_EVENTS (signature|privileged) permission to
|
||||
# be granted — see the bottom of this file.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
SDK=/home/gmp/android-sdk
|
||||
BT="$SDK/build-tools/35.0.0"
|
||||
PLAT="$SDK/platforms/android-35/android.jar"
|
||||
|
||||
rm -rf build
|
||||
mkdir -p build/classes
|
||||
javac -nowarn -source 1.8 -target 1.8 -classpath "$PLAT" -d build/classes Injector.java
|
||||
"$BT/d8" --release --min-api 24 --lib "$PLAT" --output build build/classes/touch/inject/*.class
|
||||
# aapt2 resolves the android: namespace against the framework resource
|
||||
# table, which is shipped inside android.jar (resources.arsc).
|
||||
"$BT/aapt2" link --manifest AndroidManifest.xml -I "$PLAT" \
|
||||
--min-sdk-version 24 --target-sdk-version 35 -o build/base.apk
|
||||
cp build/base.apk build/touchinject-unsigned.apk
|
||||
jar uf build/touchinject-unsigned.apk -C build classes.dex
|
||||
|
||||
if [ ! -f build/ts.jks ]; then
|
||||
keytool -genkeypair -keystore build/ts.jks -storepass android -keypass android \
|
||||
-alias ts -dname "CN=TouchInject, OU=Dev" -keyalg RSA -keysize 2048 -validity 10000
|
||||
fi
|
||||
"$BT/apksigner" sign --ks build/ts.jks --ks-pass pass:android --key-pass pass:android \
|
||||
--out touchinject.apk build/touchinject-unsigned.apk
|
||||
"$BT/apksigner" verify --print-certs touchinject.apk | head -1
|
||||
echo "=== DONE: $(pwd)/touchinject.apk"
|
||||
|
||||
# --- Install as a privileged system app (emulator, rooted) ---------------
|
||||
# SER=emulator-5554
|
||||
# adb -s $SER root && adb -s $SER remount
|
||||
# adb -s $SER shell mkdir -p /system/priv-app/TouchInject
|
||||
# adb -s $SER push touchinject.apk /system/priv-app/TouchInject/TouchInject.apk
|
||||
# adb -s $SER shell chmod 644 /system/priv-app/TouchInject/TouchInject.apk
|
||||
# adb -s $SER reboot # permission grant is evaluated at install/boot
|
||||
# adb -s $SER wait-for-device && adb -s $SER shell 'while [ -z $(getprop sys.boot_completed) ]; do sleep 1; done'
|
||||
# adb -s $SER shell dumpsys package touch.inject | grep -A2 INJECT_EVENTS # granted=true
|
||||
#
|
||||
# --- Inject a gesture ------------------------------------------------------
|
||||
# adb -s $SER shell am startservice -n touch.inject/.Injector \
|
||||
# -e script "down 1 400 1000 wait 100 down 2 700 1200 wait 100 \
|
||||
# move 1 380 980 move 2 720 1220 wait 50 \
|
||||
# move 1 340 940 move 2 760 1260 wait 50 up 1 up 2"
|
||||
# adb -s $SER logcat -d -s TouchInject
|
||||
Executable
+128
@@ -0,0 +1,128 @@
|
||||
#!/usr/bin/env bash
|
||||
# Pinch e2e test harness for the Pad emulator (aosp_atd AVD).
|
||||
#
|
||||
# The platform-signed touch.inject app (real MotionEvents through
|
||||
# InputManager — the only multi-touch path that works in the emulator)
|
||||
# drives the renderer's pinch probe; each gesture below is ONE script in
|
||||
# ONE process (the injector's finger map is per-process).
|
||||
#
|
||||
# KNOWN FLAKES (both verified, both handled below):
|
||||
# 1. The injector process is "cached" while its script thread runs and the
|
||||
# 1.5 GB emulator OOM-kills it mid-script occasionally — the script
|
||||
# never reaches "=== done". ti() checks logcat for the done line and
|
||||
# re-runs (a re-run starts a fresh gesture; a stuck pointer from a
|
||||
# half-run is replaced by the next ACTION_DOWN).
|
||||
# 2. The app is on-demand-rendering at ~1 fps: a burst of moves within
|
||||
# one frame's drain does not scroll (single-frame delta pattern).
|
||||
# Scroll tests therefore space the moves >= 40 ms apart — which is
|
||||
# also what a real finger produces over several frames.
|
||||
#
|
||||
# READING RESULTS: AppFontScale in the session file is the reliable pinch
|
||||
# signal. Scroll in the session is NOT (the restore re-derives it from the
|
||||
# pinned line anchor); for scroll tests compare the before/after
|
||||
# screenshots instead.
|
||||
SER=emulator-5554
|
||||
export ANDROID_SERIAL=$SER
|
||||
|
||||
ti() {
|
||||
local script="$1"
|
||||
for attempt in 1 2 3; do
|
||||
adb logcat -c 2>/dev/null
|
||||
adb shell 'am broadcast -n "touch.inject/.Injector$ScriptReceiver" --es script "'"$script"'"' >/dev/null 2>&1
|
||||
sleep 4
|
||||
if adb shell logcat -d 2>/dev/null | grep -aq "TouchInject: === done"; then
|
||||
return 0
|
||||
fi
|
||||
echo " (injection incomplete, attempt $attempt — retrying)"
|
||||
done
|
||||
echo " !! injection failed after 3 attempts"
|
||||
return 1
|
||||
}
|
||||
# ensure Pad is in the foreground (the ATD launcher intermittently holds
|
||||
# focus after a cold start; a backgrounded app receives no touch)
|
||||
fg() {
|
||||
for i in 1 2 3 4 5; do
|
||||
FOC=$(adb shell 'dumpsys window | grep mCurrentFocus')
|
||||
case "$FOC" in *pad.pad*) return 0;; esac
|
||||
adb shell am start -n pad.pad/org.gioui.GioActivity >/dev/null 2>&1
|
||||
sleep 8
|
||||
done
|
||||
echo " !! Pad never gained focus: $FOC"
|
||||
return 1
|
||||
}
|
||||
dbg() { adb shell "echo '$1' > /sdcard/PadPerf/cmd"; sleep 1.5; }
|
||||
shot() { adb exec-out screencap -p > "$1"; }
|
||||
# force-stop (flushes the session), print it, relaunch + reopen the file
|
||||
measure() {
|
||||
adb shell am force-stop pad.pad
|
||||
sleep 4
|
||||
echo "session: $(adb shell cat /sdcard/Pad/session.json 2>/dev/null)"
|
||||
adb shell am start -n pad.pad/org.gioui.GioActivity >/dev/null 2>&1
|
||||
sleep 4
|
||||
}
|
||||
|
||||
case "$1" in
|
||||
t1) # single-finger scroll must NEVER change the font (device regression)
|
||||
echo "=== T1: single-finger scroll (font must not change) ==="
|
||||
fg || exit 1
|
||||
shot /tmp/t1_before.png
|
||||
S="down 1 720 1400 wait 80"
|
||||
for y in 1320 1240 1160 1080 1000 920 840 760 680 600 520 440 360 280 200; do
|
||||
S="$S move 1 720 $y wait 40"
|
||||
done
|
||||
S="$S wait 100 up 1"
|
||||
ti "$S"
|
||||
sleep 2
|
||||
shot /tmp/t1_after.png # compare to before: content moved, same font size
|
||||
measure
|
||||
;;
|
||||
t2) # two-finger pinch-out: font must scale, center anchored
|
||||
echo "=== T2: two-finger pinch-out (font must grow) ==="
|
||||
fg || exit 1
|
||||
shot /tmp/t2_before.png
|
||||
S="down 1 570 1200 wait 40 down 2 870 1200 wait 60"
|
||||
for i in 1 2 3 4 5 6 7 8; do
|
||||
x1=$((570 - i*37)); x2=$((870 + i*37))
|
||||
S="$S move 1 $x1 1200 move 2 $x2 1200 wait 50"
|
||||
done
|
||||
S="$S wait 150 up 1 up 2"
|
||||
ti "$S"
|
||||
sleep 1
|
||||
shot /tmp/t2_after.png
|
||||
measure
|
||||
;;
|
||||
t3) # palm-first: the resting finger lands FIRST, the pinch pair is the
|
||||
# two that move — the palm must never enter the distance
|
||||
echo "=== T3: palm-first three fingers (pinch pair = the two movers) ==="
|
||||
fg || exit 1
|
||||
shot /tmp/t3_before.png
|
||||
S="down 1 720 400 wait 100 down 2 570 1200 wait 60 down 3 870 1200 wait 150"
|
||||
for i in 1 2 3 4 5 6 7 8; do
|
||||
x2=$((570 - i*37)); x3=$((870 + i*37))
|
||||
S="$S move 2 $x2 1200 move 3 $x3 1200 wait 50"
|
||||
done
|
||||
S="$S wait 150 up 2 up 3 wait 50 up 1"
|
||||
ti "$S"
|
||||
sleep 1
|
||||
shot /tmp/t3_after.png
|
||||
measure
|
||||
;;
|
||||
t4) # lift one finger mid-pinch: survivor must SCROLL, not zoom
|
||||
echo "=== T4: lift finger mid-pinch (survivor scrolls, no more zoom) ==="
|
||||
fg || exit 1
|
||||
shot /tmp/t4_before.png
|
||||
S="down 1 570 1200 wait 40 down 2 870 1200 wait 60 move 1 533 1200 move 2 907 1200 wait 80 up 2 wait 80"
|
||||
for y in 1280 1360 1440 1520 1600 1680 1760 1840 1920 2000; do
|
||||
S="$S move 1 533 $y wait 40" # survivor (finger 1) keeps its own x
|
||||
done
|
||||
S="$S wait 100 up 1"
|
||||
ti "$S"
|
||||
sleep 1
|
||||
shot /tmp/t4_after.png # compare to before: content scrolled, font frozen
|
||||
measure
|
||||
;;
|
||||
reset) # back to a known state (one command per file — no pipes)
|
||||
dbg "fontsize 1"; dbg "top"
|
||||
;;
|
||||
*) echo "usage: $0 t1|t2|t3|t4|reset" ;;
|
||||
esac
|
||||
Reference in New Issue
Block a user