// Copyright © SixtyFPS GmbH // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial import { CosmicFontSettings, CosmicPalette } from "styling.slint"; import { ScrollView } from "scrollview.slint"; export component TextEdit { in property wrap <=> text-input.wrap; in property horizontal-alignment <=> text-input.horizontal-alignment; in property read-only <=> text-input.read-only; in property font-size <=> text-input.font-size; in property enabled <=> text-input.enabled; in-out property has-focus: text-input.has-focus; out property visible-width <=> scroll-view.visible-width; out property visible-height <=> scroll-view.visible-height; in-out property text <=> text-input.text; in-out property viewport-x <=> scroll-view.viewport-x; in-out property viewport-y <=> scroll-view.viewport-y; in-out property viewport-width <=> scroll-view.viewport-width; in-out property viewport-height <=> scroll-view.viewport-height; 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; horizontal-stretch: 1; vertical-stretch: 1; states [ disabled when !root.enabled : { root.opacity: 0.5; } ] background := Rectangle { 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 { width: parent.width + 2px; height: parent.height + 2px; border-radius: parent.border-radius + 2px; border-color: CosmicPalette.state-focus; border-width: 1px; } } }