slint/internal/compiler/widgets/fluent-base/textedit.slint
Brandon Fowler 3e8940660b
Add set-selection-offsets function to TextInput, TextEdit, and LineEdit (#4197)
The function accepts two arguments that specify the start and the end of the text to select.

Fixes #4164
2024-01-06 11:12:53 +01:00

119 lines
4.7 KiB
Text

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
import { FluentFontSettings, FluentPalette } from "styling.slint";
import { ScrollView } from "scrollview.slint";
export component TextEdit {
in property <TextWrap> wrap <=> i-text-input.wrap;
in property <TextHorizontalAlignment> horizontal-alignment <=> i-text-input.horizontal-alignment;
in property <bool> read-only <=> i-text-input.read-only;
in property <length> font-size <=> i-text-input.font-size;
in property <bool> enabled <=> i-text-input.enabled;
in-out property <bool> has-focus: i-text-input.has-focus;
out property <length> visible-width <=> i-scroll-view.visible-width;
out property <length> visible-height <=> i-scroll-view.visible-height;
in-out property <string> text <=> i-text-input.text;
in-out property <length> viewport-x <=> i-scroll-view.viewport-x;
in-out property <length> viewport-y <=> i-scroll-view.viewport-y;
in-out property <length> viewport-width <=> i-scroll-view.viewport-width;
in-out property <length> viewport-height <=> i-scroll-view.viewport-height;
callback edited(/* text */ string);
public function set-selection-offsets(start: int, end: int) {
i-text-input.set-selection-offsets(start, end);
}
public function select-all() {
i-text-input.select-all();
}
public function clear-selection() {
i-text-input.clear-selection();
}
public function cut() {
i-text-input.cut();
}
public function copy() {
i-text-input.copy();
}
public function paste() {
i-text-input.paste();
}
forward-focus: i-text-input;
horizontal-stretch: 1;
vertical-stretch: 1;
states [
disabled when !root.enabled : {
i-background.background: FluentPalette.control-disabled;
i-background.border-color: FluentPalette.border;
i-text-input.color: FluentPalette.text-disabled;
i-text-input.selection-foreground-color: FluentPalette.text-accent-foreground-disabled;
}
focused when root.has-focus : {
i-background.background: FluentPalette.control-input-active;
i-background.border-color: FluentPalette.border;
i-focus-border.background: FluentPalette.accent-background;
}
]
i-background := Rectangle {
width: 100%;
height: 100%;
border-radius: 4px;
background: FluentPalette.control-background;
border-width: 1px;
border-color: FluentPalette.text-control-border;
i-scroll-view := ScrollView {
x: 12px;
y: 12px;
width: parent.width - 24px;
height: parent.height - 24px;
viewport-width: root.wrap == TextWrap.word-wrap ? self.visible-width : max(self.visible-width, i-text-input.preferred-width);
viewport-height: max(self.visible-height, i-text-input.preferred-height);
i-text-input := TextInput {
enabled: true;
color: FluentPalette.foreground;
font-size: FluentFontSettings.body.font-size;
font-weight: FluentFontSettings.body.font-weight;
selection-background-color: FluentPalette.selection-background;
selection-foreground-color: FluentPalette.accent-foreground;
single-line: false;
wrap: word-wrap;
edited => {
root.edited(self.text);
}
cursor-position-changed(cpos) => {
if (cpos.x + root.viewport-x < 12px) {
root.viewport-x = min(0px, max(parent.visible-width - self.width, - cpos.x + 12px ));
} else if (cpos.x + root.viewport-x > parent.visible-width - 12px) {
root.viewport-x = min(0px, max(parent.visible-width - self.width, parent.visible-width - cpos.x - 12px ));
}
if (cpos.y + root.viewport-y < 12px) {
root.viewport-y = min(0px, max(parent.visible-height - self.height, - cpos.y + 12px ));
} else if (cpos.y + root.viewport-y > parent.visible-height - 12px - 20px) {
// FIXME: font-height hardcoded to 20px
root.viewport-y = min(0px, max(parent.visible-height - self.height, parent.visible-height - cpos.y - 12px - 20px));
}
}
}
}
i-focus-border := Rectangle {
x: parent.border-radius;
y: parent.height - self.height;
width: parent.width - 2 * parent.border-radius;
height: 2px;
}
}
}