| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | import { ChildIndicator, NameLabel, ResettingLineEdit, SecondaryContent } from "./basics.slint" ; |
| 5 | |
| 6 | import { Api, PropertyValue } from "../../api.slint" ; |
| 7 | import { EditorSpaceSettings } from "../../components/styling.slint" ; |
| 8 | |
| 9 | import { CheckBox } from "std-widgets.slint" ; |
| 10 | |
| 11 | export component StringWidget inherits GridLayout { |
| 12 | // FIXME: @ogoffart says the plural support is not working at this time, so |
| 13 | // we do not offer it in the UI for the time being... |
| 14 | |
| 15 | in property <bool> enabled; |
| 16 | in property <string> property-name; |
| 17 | in property <PropertyValue> property-value; |
| 18 | in property <bool> has-code-action; |
| 19 | in property <bool> has-reset-action; |
| 20 | in property <bool> is-translatable: true; |
| 21 | |
| 22 | out property <length> i-am-a-hack-remove-me: 0px; |
| 23 | |
| 24 | property <bool> open: false; |
| 25 | |
| 26 | private property <bool> is-translated; |
| 27 | private property <string> tr-context-value; |
| 28 | |
| 29 | callback update-display-string(value: string); |
| 30 | |
| 31 | callback code-action(); |
| 32 | callback reset-action(); |
| 33 | |
| 34 | callback test-string-binding(text: string, is-translated: bool) -> bool; |
| 35 | callback set-string-binding(text: string, is-translated: bool); |
| 36 | |
| 37 | function tsb() -> bool { |
| 38 | return test-string-binding(Api.string-to-code(text-rle.text, self.is-translated, self.tr-context-value, "" , "" ), self.is-translated); |
| 39 | } |
| 40 | function ssb() { |
| 41 | update-display-string("\"\{text-rle.text}\"" ); |
| 42 | set-string-binding(Api.string-to-code(text-rle.text, self.is-translated, self.tr-context-value, "" , "" ), self.is-translated); |
| 43 | } |
| 44 | |
| 45 | function apply-value() { |
| 46 | text_rle.default-text = property-value.value-string; |
| 47 | self.is-translated = root.property-value.is-translatable; |
| 48 | self.tr-context-value = root.property-value.tr-context; |
| 49 | } |
| 50 | |
| 51 | init => { |
| 52 | self.i-am-a-hack-remove-me = self.preferred-height; |
| 53 | |
| 54 | apply-value(); |
| 55 | } |
| 56 | |
| 57 | private property <bool> child-focus: false; |
| 58 | private property <bool> has-focus: text_rle.has-focus || self.child-focus; |
| 59 | |
| 60 | changed has-focus => { |
| 61 | if !has-focus { |
| 62 | apply-value(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | changed property-value => { |
| 67 | if !has-focus { |
| 68 | apply-value(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | spacing-vertical: EditorSpaceSettings.default-spacing; |
| 73 | |
| 74 | Row { |
| 75 | NameLabel { |
| 76 | col: 1; |
| 77 | property-name: root.property-name; |
| 78 | property-value: root.property-value; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | Row { |
| 83 | childIndicator := ChildIndicator { |
| 84 | visible: root.is-translatable; |
| 85 | horizontal-stretch: 0; |
| 86 | control-hover: text_rle.has-focus; |
| 87 | } |
| 88 | |
| 89 | content := HorizontalLayout { |
| 90 | spacing: EditorSpaceSettings.default-spacing; |
| 91 | |
| 92 | text_rle := ResettingLineEdit { |
| 93 | enabled: root.enabled; |
| 94 | edited(text) => { |
| 95 | self.can-compile = root.tsb(); |
| 96 | } |
| 97 | accepted(text) => { |
| 98 | root.ssb(); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | Row { |
| 105 | sub := SecondaryContent { |
| 106 | col: 1; |
| 107 | |
| 108 | enabled: root.enabled; |
| 109 | open: childIndicator.open && root.is-translatable; |
| 110 | |
| 111 | has-code-action <=> root.has-code-action; |
| 112 | has-reset-action <=> root.has-reset-action; |
| 113 | |
| 114 | code-action() => { |
| 115 | root.code-action(); |
| 116 | } |
| 117 | reset-action => { |
| 118 | root.reset-action(); |
| 119 | } |
| 120 | |
| 121 | VerticalLayout { |
| 122 | spacing: EditorSpaceSettings.default-spacing; |
| 123 | tr-cb := CheckBox { |
| 124 | checked: root.is-translated; |
| 125 | toggled => { |
| 126 | root.is-translated = self.checked; |
| 127 | root.ssb(); |
| 128 | } |
| 129 | enabled: root.enabled; |
| 130 | text: "Translatable" ; |
| 131 | } |
| 132 | |
| 133 | HorizontalLayout { |
| 134 | spacing: EditorSpaceSettings.default-spacing; |
| 135 | |
| 136 | Text { |
| 137 | vertical-alignment: center; |
| 138 | horizontal-alignment: right; |
| 139 | text: "Context" ; |
| 140 | } |
| 141 | |
| 142 | tr-context := ResettingLineEdit { |
| 143 | enabled: root.enabled && tr-cb.checked; |
| 144 | default-text: root.tr-context-value; |
| 145 | edited(text) => { |
| 146 | root.tr-context-value = text; |
| 147 | self.can-compile = root.tsb(); |
| 148 | } |
| 149 | accepted(text) => { |
| 150 | root.tr-context-value = text; |
| 151 | root.ssb(); |
| 152 | } |
| 153 | changed has-focus => { |
| 154 | root.child-focus = self.has-focus; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |