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
4import { ChildIndicator, NameLabel, ResettingLineEdit } from "./basics.slint";
5
6import { PropertyValue } from "../../api.slint";
7import { EditorSpaceSettings } from "../../components/styling.slint";
8
9import { TextEdit } from "std-widgets.slint";
10
11export component JsonWidget inherits GridLayout {
12 in property <bool> enabled;
13 in property <string> property-name;
14 in property <PropertyValue> property-value;
15
16 property <bool> can-compile: true;
17 property <length> border: 3px;
18
19 callback set-code-binding(text: string) -> bool;
20
21 spacing-vertical: EditorSpaceSettings.default-spacing;
22
23 function apply-value() {
24 edit.text = root.property-value.code;
25 }
26
27 init => {
28 apply-value();
29 }
30
31 private property <bool> has-focus: edit.has-focus;
32
33 changed has-focus => {
34 if !has-focus {
35 apply-value();
36 }
37 }
38
39 changed property-value => {
40 if !has-focus {
41 apply-value();
42 }
43 }
44
45 Row {
46 NameLabel {
47 col: 1;
48
49 property-name: root.property-name;
50 property-value: root.property-value;
51 }
52 }
53
54 Row {
55 childIndicator := ChildIndicator {
56 horizontal-stretch: 0;
57 visible: false;
58 }
59
60 Rectangle {
61 VerticalLayout {
62 edit := TextEdit {
63 min-height: 200px;
64
65 enabled: root.enabled;
66
67 edited(text) => {
68 root.can-compile = root.set-code-binding(text);
69 }
70
71 changed has-focus => {
72 self.text = root.property-value.code;
73 root.can-compile = true;
74 }
75 }
76 }
77
78 Rectangle {
79 visible: !root.can-compile;
80
81 background: Colors.red.transparentize(0.94);
82 x: edit.x + root.border;
83 y: edit.y + root.border;
84 width: edit.width - 2 * root.border;
85 height: edit.height - 2 * root.border;
86
87 border-radius: root.border;
88 }
89 }
90 }
91}
92
93