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 text <=> input.text; in property enabled <=> input.enabled; in property alignment <=> input.horizontal-alignment; in property input-type <=> input.input-type; in property ink: Theme.on-surface; in property label; in property placeholder; out property 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 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 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); } } }