Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TextEditBase #5236

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 0 additions & 75 deletions internal/compiler/widgets/common/common.slint
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,6 @@

import { StyleMetrics, Palette, ScrollView, Palette } from "std-widgets-impl.slint";

export component TextEdit inherits ScrollView {
in property <TextWrap> wrap <=> i-text-input.wrap;
in property horizontal-alignment <=> i-text-input.horizontal-alignment;
in property read-only <=> i-text-input.read-only;
in property <length> font-size <=> i-text-input.font-size;
in-out property <string> text <=> i-text-input.text;

accessible-role: AccessibleRole.text-input;
accessible-value <=> text;

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;
has-focus: i-text-input.has-focus;
enabled <=> i-text-input.enabled;
horizontal-stretch: 1;
vertical-stretch: 1;
viewport-width: root.wrap == TextWrap.word-wrap ? root.visible-width : max(root.visible-width, i-text-input.preferred-width);
viewport-height: max(self.visible-height, i-text-input.preferred-height);

Rectangle {
background: root.enabled ? StyleMetrics.textedit-background : StyleMetrics.textedit-background-disabled;
}

i-text-input := TextInput {
enabled: true;
color: self.enabled ? StyleMetrics.textedit-text-color : StyleMetrics.textedit-text-color-disabled;
single-line: false;
wrap: word-wrap;
selection-background-color: Palette.selection-background;
selection-foreground-color: Palette.selection-foreground;

edited => {
root.edited(self.text);
}

cursor-position-changed(cpos) => {
if (cpos.x + root.viewport-x < StyleMetrics.layout-padding) {
root.viewport-x = min(0px, max(parent.visible-width - self.width, - cpos.x + StyleMetrics.layout-padding ));
} else if (cpos.x + root.viewport-x > parent.visible-width - StyleMetrics.layout-padding) {
root.viewport-x = min(0px, max(parent.visible-width - self.width, parent.visible-width - cpos.x - StyleMetrics.layout-padding ));
}
if (cpos.y + root.viewport-y < StyleMetrics.layout-padding) {
root.viewport-y = min(0px, max(parent.visible-height - self.height, - cpos.y + StyleMetrics.layout-padding ));
} else if (cpos.y + root.viewport-y > parent.visible-height - StyleMetrics.layout-padding - 20px) {
// FIXME: font-height hardcoded to 20px
root.viewport-y = min(0px, max(parent.visible-height - self.height, parent.visible-height - cpos.y - StyleMetrics.layout-padding - 20px));
}
}
}
}
export component AboutSlint {
preferred-height: 100%;
preferred-width: 100%;
Expand Down
92 changes: 92 additions & 0 deletions internal/compiler/widgets/common/textedit-base.slint
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.2 OR LicenseRef-Slint-commercial

import { ScrollView } from "std-widgets-impl.slint";

export component TextEditBase inherits Rectangle {
in property <length> scroll-view-padding;

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;
in-out property <bool> has-focus: text-input.has-focus;
out property <length> visible-width <=> scroll-view.visible-width;
out property <length> visible-height <=> scroll-view.visible-height;
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 <brush> foreground <=> text-input.color;
in property <int> font-weight <=> text-input.font-weight;
in property <brush> selection-background-color;
in property <brush> selection-foreground-color;

callback edited(/* text */ string);

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;

scroll-view := ScrollView {
x: root.scroll-view-padding;
y: root.scroll-view-padding;
width: parent.width - 2 * root.scroll-view-padding;
height: parent.height - 2 * root.scroll-view-padding;
viewport-width: root.wrap == TextWrap.word-wrap ? self.visible-width : max(self.visible-width, text-input.preferred-width);
viewport-height: max(self.visible-height, text-input.preferred-height);

text-input := TextInput {
enabled: true;
single-line: false;
wrap: word-wrap;
selection-background-color: root.selection-background-color;
selection-foreground-color: root.selection-foreground-color;

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

@children
}
92 changes: 30 additions & 62 deletions internal/compiler/widgets/cosmic-base/textedit.slint
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,75 @@

import { CosmicFontSettings, CosmicPalette } from "styling.slint";
import { ScrollView } from "scrollview.slint";
import { TextEditBase } from "../common/textedit-base.slint";

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;
in-out property <bool> has-focus: text-input.has-focus;
out property <length> visible-width <=> scroll-view.visible-width;
out property <length> visible-height <=> scroll-view.visible-height;
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 <TextWrap> wrap <=> base.wrap;
in property <TextHorizontalAlignment> horizontal-alignment <=> base.horizontal-alignment;
in property <bool> read-only <=> base.read-only;
in property <length> font-size <=> base.font-size;
in property <bool> enabled <=> base.enabled;
in-out property <bool> has-focus: base.has-focus;
out property <length> visible-width <=> base.visible-width;
out property <length> visible-height <=> base.visible-height;
in-out property <string> text <=> base.text;
in-out property <length> viewport-x <=> base.viewport-x;
in-out property <length> viewport-y <=> base.viewport-y;
in-out property <length> viewport-width <=> base.viewport-width;
in-out property <length> viewport-height <=> base.viewport-height;

callback edited(/* text */ string);
accessible-role: AccessibleRole.text-input;
accessible-value <=> text;

public function set-selection-offsets(start: int, end: int) {
text-input.set-selection-offsets(start, end);
base.set-selection-offsets(start, end);
}

public function select-all() {
text-input.select-all();
base.select-all();
}

public function clear-selection() {
text-input.clear-selection();
base.clear-selection();
}

public function cut() {
text-input.cut();
base.cut();
}

public function copy() {
text-input.copy();
base.copy();
}

public function paste() {
text-input.paste();
base.paste();
}

forward-focus: text-input;
forward-focus: base;
horizontal-stretch: 1;
vertical-stretch: 1;

states [
disabled when !root.enabled : {
disabled when !root.enabled: {
root.opacity: 0.5;
}
]

background := Rectangle {
base := TextEditBase {
width: 100%;
height: 100%;
border-radius: 8px;
background: CosmicPalette.control-background;
border-width: 1px;
border-color: CosmicPalette.control-divider;

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, text-input.preferred-width);
viewport-height: max(self.visible-height, text-input.preferred-height);

text-input := TextInput {
enabled: true;
color: CosmicPalette.foreground;
font-size: CosmicFontSettings.body.font-size;
font-weight: CosmicFontSettings.body.font-weight;
selection-background-color: CosmicPalette.selection-background;
selection-foreground-color: CosmicPalette.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));
}
}
}
}

if (root.has-focus && root.enabled) : Rectangle {
scroll-view-padding: 12px;
foreground: CosmicPalette.foreground;
font-size: CosmicFontSettings.body.font-size;
font-weight: CosmicFontSettings.body.font-weight;
selection-background-color: CosmicPalette.selection-background;
selection-foreground-color: CosmicPalette.selection-foreground;
if root.has-focus && root.enabled: Rectangle {
width: parent.width + 2px;
height: parent.height + 2px;
border-radius: parent.border-radius + 2px;
Expand Down
2 changes: 1 addition & 1 deletion internal/compiler/widgets/cupertino-base/textedit.slint
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CupertinoFontSettings, CupertinoPalette } from "styling.slint";
import { ScrollBar } from "scrollview.slint";
import { FocusBorder } from "components.slint";

// FIXME: After auto-hiding of scrollbars is implemented, remove and use `ScrollView` from `scrollview.slint`
// FIXME: After auto-hiding of scrollbars is implemented, use TextEditBase
component ScrollView {
in property <bool> enabled: true;
out property <length> visible-width <=> i-flickable.width;
Expand Down
Loading
Loading