fields: one-line inputs scroll to keep the cursor in view

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-25 15:01:09 -06:00
commit b31ef87235
5 changed files with 81 additions and 31 deletions

View file

@ -1,5 +1,5 @@
import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { ScrollInput } from "scroll-input.slint";
// A name edited in place of its label: a framed text input that takes the focus when it
// appears, with the first `select-end` bytes (the name without its extension) selected.
@ -25,7 +25,7 @@ export component RenameField inherits Rectangle {
}
}
min-height: input.preferred-height + 2 * Theme.space-1;
min-height: input.min-height + 2 * Theme.space-1;
background: Theme.surface;
border-radius: Theme.corner-xs;
border-width: Theme.focus-ring-width;
@ -49,20 +49,13 @@ export component RenameField inherits Rectangle {
reject
}
input := TextInput {
input := ScrollInput {
x: Theme.space-2;
width: parent.width - 2 * Theme.space-2;
height: parent.height;
vertical-alignment: center;
text: root.text;
color: Theme.on-surface;
selection-background-color: Theme.primary-container;
selection-foreground-color: Theme.on-primary-container;
font-size: Theme.body-medium.size;
font-weight: Theme.body-medium.weight;
horizontal-alignment: root.alignment;
single-line: true;
accessible-label: "Name";
alignment: root.alignment;
label: "Name";
accepted => {
root.finish(true);
}

View file

@ -0,0 +1,61 @@
import { Theme } from "../theme.slint";
// One line of editable text that scrolls so the cursor stays in view. Draws no frame.
export component ScrollInput inherits Rectangle {
in-out property <string> text <=> input.text;
in property <bool> enabled <=> input.enabled;
in property <TextHorizontalAlignment> alignment <=> input.horizontal-alignment;
in property <InputType> input-type <=> input.input-type;
in property <color> ink: Theme.on-surface;
in property <string> label;
in property <string> placeholder;
out property <bool> has-focus: input.has-focus;
callback edited(text: string);
callback accepted(text: string);
public function select-all() {
input.select-all();
}
public function set-selection-offsets(anchor: int, focus: int) {
input.set-selection-offsets(anchor, focus);
}
// Room kept between the cursor and the edge it scrolls at.
property <length> margin: min(Theme.space-4, self.width / 4);
// A clipped input of no width can't take the focus.
min-width: 1px;
min-height: input.preferred-height;
clip: true;
forward-focus: input;
input := TextInput {
property <length> shift;
x: min(0px, max(parent.width - self.width, self.shift));
// The whole text, so a short one can still be centred and clicked anywhere.
width: max(parent.width, self.preferred-width + self.text-cursor-width);
height: parent.height;
single-line: true;
vertical-alignment: center;
color: root.ink;
selection-background-color: Theme.primary-container;
selection-foreground-color: Theme.on-primary-container;
font-size: Theme.body-medium.size;
font-weight: Theme.body-medium.weight;
accessible-label: root.label;
accessible-placeholder-text: root.placeholder;
cursor-position-changed(at) => {
if at.x + self.x < root.margin {
self.shift = min(0px, root.margin - at.x);
} else if at.x + self.x > root.width - root.margin - self.text-cursor-width {
self.shift = root.width - root.margin - self.text-cursor-width - at.x;
}
}
edited => {
root.edited(self.text);
}
accepted => {
root.accepted(self.text);
}
}
}

View file

@ -1,3 +1,4 @@
import { ScrollInput } from "scroll-input.slint";
import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Icons } from "icon.slint";
@ -110,20 +111,14 @@ export component TextField {
overflow: elide;
}
input := TextInput {
input := ScrollInput {
width: 100%;
text <=> root.text;
accessible-label: root.label;
accessible-placeholder-text: root.placeholder;
label: root.label;
placeholder: root.placeholder;
enabled: root.enabled;
single-line: true;
input-type: root.secret ? InputType.password : InputType.text;
color: root.enabled ? Theme.on-surface : root.disabled;
selection-background-color: Theme.primary-container;
selection-foreground-color: Theme.on-primary-container;
font-size: Theme.body-medium.size;
font-weight: Theme.body-medium.weight;
vertical-alignment: center;
ink: root.enabled ? Theme.on-surface : root.disabled;
edited => {
root.edited(self.text);
}

View file

@ -2,6 +2,7 @@ import { Theme } from "../theme.slint";
import { Motion } from "../motion.slint";
import { Icon, Icons } from "../components/icon.slint";
import { StateLayer } from "../components/state-layer.slint";
import { ScrollInput } from "../components/scroll-input.slint";
import { DropTarget, DropZone } from "../components/drop-menu.slint";
// One segment of the path: the root (glyph and name) or a folder.
@ -227,15 +228,10 @@ export component Breadcrumb inherits Rectangle {
reject
}
input := TextInput {
single-line: true;
color: Theme.on-surface;
selection-background-color: Theme.primary-container;
selection-foreground-color: Theme.on-primary-container;
font-size: Theme.body-medium.size;
font-weight: Theme.body-medium.weight;
vertical-alignment: center;
accessible-label: "Location";
input := ScrollInput {
width: 100%;
height: 100%;
label: "Location";
edited => {
root.highlight = -1;
root.edited(self.text);

View file

@ -313,5 +313,10 @@ One entry per session, newest last, ≤ 15 lines: done, verified how, not done,
- Verified: clippy on tunal + tunal-devices, release build; the user's USB stick opened, unmounted and mounted again under Xvfb.
- Not done: Safely remove not run (needs a replug); inner disks, cards and discs not tried; locked (LUKS) drives are not listed; no test run.
## Text fields scroll (user bug)
- Done: `components/scroll-input.slint` (one-line input that keeps the cursor in view) in the rename field, every text field and the location bar; Slint's bare `TextInput` doesn't scroll.
- Verified: release build; rename of a long name in the grid and the location bar under Xvfb.
- Not done: no test run; password fields and arrow / Home / End keys not tried.
## Later
-