Tunal/crates/tunal/ui/components/scroll-input.slint
2026-11-25 15:01:09 -06:00

61 lines
2.3 KiB
Text

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);
}
}
}