slint/internal/compiler/widgets/common/lineedit-base.slint
Olivier Goffart 017448893a
Fix LineEdit with right alignment (#4016)
* Fix LineEdit with right alignement

 - Align the placeholder the same way
 - Make sure the cursor is visible to the right (it is drawn outside of
   the TextInput, so some size need to be accounted for

(Discussed in https://github.com/slint-ui/slint/discussions/3996 )
2023-11-27 18:52:54 +01:00

83 lines
No EOL
3 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
export component LineEditBase inherits Rectangle {
in-out property <string> placeholder-text;
in-out property <length> font-size <=> i-text-input.font-size;
in-out property <string> text <=> i-text-input.text;
in-out property <brush> placeholder-color;
in-out property <bool> enabled <=> i-text-input.enabled;
in-out property <bool> has-focus: i-text-input.has-focus;
in-out property <InputType> input-type <=> i-text-input.input-type;
in-out property <TextHorizontalAlignment> horizontal-alignment <=> i-text-input.horizontal-alignment;
in-out property <bool> read-only <=> i-text-input.read-only;
in property <int> font-weight <=> i-text-input.font-weight;
in property <brush> text-color;
in property <color> selection-background-color <=> i-text-input.selection-background-color;
in property <color> selection-foreground-color <=> i-text-input.selection-foreground-color;
in property <length> margin;
callback accepted( /* text */ string);
callback edited(/* text */ string);
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();
}
min-height: i-text-input.preferred-height;
min-width: max(50px, i-placeholder.min-width);
clip: true;
forward-focus: i-text-input;
i-placeholder := Text {
width: 100%;
height: 100%;
vertical-alignment: center;
text: (root.text == "" && i-text-input.preedit-text == "") ? root.placeholder-text : "";
font-size: i-text-input.font-size;
font-italic: i-text-input.font-italic;
font-weight: i-text-input.font-weight;
font-family: i-text-input.font-family;
color: root.placeholder-color;
horizontal-alignment: root.horizontal-alignment;
}
i-text-input := TextInput {
property <length> computed-x;
x: min(0px, max(parent.width - self.width - self.text-cursor-width, self.computed-x));
width: max(parent.width - self.text-cursor-width, self.preferred-width);
height: 100%;
vertical-alignment: center;
single-line: true;
color: root.text-color;
cursor-position-changed(cpos) => {
if (cpos.x + self.computed_x < root.margin) {
self.computed_x = - cpos.x + root.margin;
} else if (cpos.x + self.computed_x > parent.width - root.margin - self.text-cursor-width) {
self.computed_x = parent.width - cpos.x - root.margin - self.text-cursor-width;
}
}
accepted => { root.accepted(self.text); }
edited => { root.edited(self.text); }
}
}