slint/internal/compiler/widgets/cupertino/textedit.slint
Olivier Goffart 686f5e43e2 Widget style: simplify -light/-dark handling
Instead of having all style duplicated and re-using a base, we just hack
into the funciton that queries the dark/light theme based on the style
suffix known at compile time.

This removes one of the problem that happens when trying to work on the
widget style with the extension, as it relies on include path hacks
2024-08-20 16:55:15 +02:00

190 lines
7 KiB
Text

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
import { CupertinoFontSettings, CupertinoPalette } from "styling.slint";
import { ScrollBar } from "scrollview.slint";
import { FocusBorder } from "components.slint";
// FIXME: After auto-hiding of scrollbars is implemented, use TextEditBase
component ScrollView {
in property <bool> enabled: true;
out property <length> visible-width <=> flickable.width;
out property <length> visible-height <=> flickable.height;
in-out property <length> viewport-width <=> flickable.viewport-width;
in-out property <length> viewport-height <=> flickable.viewport-height;
in-out property <length> viewport-x <=> flickable.viewport-x;
in-out property <length> viewport-y <=> flickable.viewport-y;
// FIXME: remove. This property is currently set by the ListView and is used by the native style to draw the scrollbar differently when it has focus
in-out property <bool> has-focus;
min-height: 50px;
min-width: 50px;
horizontal-stretch: 1;
vertical-stretch: 1;
preferred-height: 100%;
preferred-width: 100%;
flickable := Flickable {
x: 2px;
y: 2px;
interactive: false;
viewport-y <=> vertical-bar.value;
viewport-x <=> horizontal-bar.value;
width: parent.width - 16px;
height: 100%;
@children
}
vertical-bar := ScrollBar {
enabled: root.enabled;
x: parent.width - self.width;
y: 0;
width: self.has-hover ? 20px : 12px;
height: horizontal-bar.visible ? parent.height - horizontal-bar.height : parent.height;
horizontal: false;
maximum: flickable.viewport-height - flickable.height;
page-size: flickable.height;
visible: flickable.viewport-height > flickable.height;
}
horizontal-bar := ScrollBar {
enabled: root.enabled;
width: vertical-bar.visible ? parent.width - vertical-bar.width : parent.width;
height: self.has-hover ? 20px : 12px;
y: parent.height - self.height;
x: 0;
horizontal: true;
maximum: flickable.viewport-width - flickable.width;
page-size: flickable.width;
visible: flickable.viewport-width > flickable.width;
}
}
export component TextEdit {
in property <TextWrap> wrap <=> text-input.wrap;
in property <TextHorizontalAlignment> horizontal-alignment <=> text-input.horizontal-alignment;
in property <bool> read-only <=> text-input.read-only;
in property <length> font-size <=> text-input.font-size;
in property <bool> enabled <=> text-input.enabled;
out property <length> visible-width <=> scroll-view.visible-width;
out property <length> visible-height <=> scroll-view.visible-height;
in-out property <bool> has-focus: text-input.has-focus;
in-out property <string> text <=> text-input.text;
in-out property <length> viewport-x <=> scroll-view.viewport-x;
in-out property <length> viewport-y <=> scroll-view.viewport-y;
in-out property <length> viewport-width <=> scroll-view.viewport-width;
in-out property <length> viewport-height <=> scroll-view.viewport-height;
in property <string> placeholder-text;
callback edited(/* text */ string);
accessible-role: AccessibleRole.text-input;
accessible-value <=> text;
accessible-placeholder-text: text == "" ? placeholder-text : "";
public function set-selection-offsets(start: int,end: int){
text-input.set-selection-offsets(start, end);
}
public function select-all(){
text-input.select-all();
}
public function clear-selection(){
text-input.clear-selection();
}
public function cut(){
text-input.cut();
}
public function copy(){
text-input.copy();
}
public function paste(){
text-input.paste();
}
forward-focus: text-input;
horizontal-stretch: 1;
vertical-stretch: 1;
states [
disabled when !root.enabled: {
text-input.color: CupertinoPalette.foreground-secondary;
background.background: CupertinoPalette.tertiary-control-background;
}
focused when root.has-focus: {
background.background: CupertinoPalette.control-background;
}
]
FocusBorder {
x: (parent.width - self.width) / 2;
y: (parent.height - self.height) / 2;
width: parent.width + 6px;
height: parent.height + 6px;
has-focus: root.has-focus;
}
background := Rectangle {
background: CupertinoPalette.alternate-background;
border-color: CupertinoPalette.border;
border-width: 1px;
}
scroll-view := ScrollView {
x: 8px;
y: 8px;
width: parent.width - 16px;
height: parent.height - 16px;
viewport-width: root.wrap == TextWrap.no-wrap ? max(self.visible-width, text-input.preferred-width) : self.visible-width;
viewport-height: max(self.visible-height, text-input.preferred-height);
text-input := TextInput {
enabled: true;
color: CupertinoPalette.foreground;
font-size: CupertinoFontSettings.body.font-size;
font-weight: CupertinoFontSettings.body.font-weight;
selection-background-color: CupertinoPalette.selection-background;
selection-foreground-color: self.color;
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));
}
}
}
}
placeholder := Text {
x: scroll-view.x;
y: scroll-view.y;
width: scroll-view.width;
vertical-alignment: top;
text: (root.text == "" && text-input.preedit-text == "") ? root.placeholder-text : "";
font-size: text-input.font-size;
font-italic: text-input.font-italic;
font-weight: text-input.font-weight;
font-family: text-input.font-family;
color: CupertinoPalette.foreground-secondary;
overflow: elide;
// the label is set on the TextEdit itself
accessible-role: none;
}
}