Debian Codegraph @

Nasal/canvas/gui/dialogs/WidgetsFactoryDialog.nas

raw
1 var WidgetsFactoryDialog = {
2 new: func {
3 var m = {
4 parents: [WidgetsFactoryDialog],
5 window: canvas.Window.new([500, 500], "dialog")
6 };
7
8 m.window.setBool("resize", 1);
9
10 m.root = m.window.getCanvas(1)
11 .set("background", style.getColor("bg_color"))
12 .createGroup();
13 m.vbox = VBoxLayout.new();
14 m.window.setLayout(m.vbox);
15
16 m.menubar = canvas.gui.widgets.MenuBar.new(m.root, canvas.style, {});
17
18 m.fileMenu = m.menubar.createMenu("File");
19 m.fileMenu.createItem(text: "Quit", cb: func m.del(), shortcut: "<Ctrl>+Q");
20
21 m.tabsMenu = m.menubar.createMenu("Tabs");
22 m.tabsMenu.createItem(text: "Select first tab", cb: func m.tabs.setCurrentTab("tab-1"));
23 m.tabsMenu.createItem(text: "Select second tab", cb: func m.tabs.setCurrentTab("tab-2"));
24
25 m.widgetsMenu = m.menubar.createMenu("Widgets");
26 m.widgetsMenu.createItem(text: "Benchmark label", cb: func {
27 m.benchmark_widget(canvas.gui.widgets.Label, func(w, i) {
28 w.setText("Label " ~ i);
29 });
30 });
31 m.widgetsMenu.createItem(text: "Benchmark slider", cb: func {
32 m.benchmark_widget(widget: canvas.gui.widgets.Slider, proc_func: func(w, i) {
33 w.setValue(i);
34 }, cfg: {
35 "value-position": canvas.gui.widgets.Slider.ValuePosition.Below,
36 "value-style": canvas.gui.widgets.Slider.ValueStyle.Moving,
37 "ticks-position": gui.widgets.Slider.TicksPosition.Below,
38 });
39 });
40 m.widgetsMenu.createItem(text: "Benchmark radio button", cb: func {
41 m.benchmark_radio_button(func(w, i) {
42 w.setText("Radio button " ~ i);
43 });
44 });
45 m.vbox.addItem(m.menubar);
46
47 m.tabs = gui.widgets.TabWidget.new(m.root, style, {"tabs-closeable": 1});
48 m.tabsContent = m.tabs.getContent();
49 m.vbox.addItem(m.tabs);
50
51 m.tab_1 = VBoxLayout.new();
52 m.tabs.addTab("tab-1", "Tab 1", m.tab_1);
53 m.label = gui.widgets.Label.new(m.tabsContent, style, {})
54 .setText("A label")
55 .setBackground("#ffaaaa");
56 m.tab_1.addItem(m.label);
57
58 var r = gui.widgets.HorizontalRule.new(m.tabsContent, style, {});
59 r.setText("Checkboxes!");
60 m.tab_1.addItem(r);
61
62 m.checkbox_left = gui.widgets.CheckBox.new(m.tabsContent, style, {})
63 .setText("Wanna check something ?");
64 m.tab_1.addItem(m.checkbox_left);
65 m.checkbox_right = gui.widgets.CheckBox.new(m.tabsContent, style, {"label-position": "left"})
66 .setText("Checkbox with text on the left side");
67 m.tab_1.addItem(m.checkbox_right);
68 m.property_checkbox = gui.widgets.PropertyCheckBox.new(m.tabsContent, style, {
69 "node": props.globals.getNode("/controls/lighting/nav-lights"),
70 })
71 .setText("Nav lights");
72 m.tab_1.addItem(m.property_checkbox);
73
74 var r2 = gui.widgets.HorizontalRule.new(m.tabsContent, style, {});
75 m.tab_1.addItem(r2);
76
77 m.radio_label = gui.widgets.Label.new(m.tabsContent, style, {})
78 .setText("Selected radio button: none");
79 m.tab_1.addItem(m.radio_label);
80 m.radio1 = gui.widgets.RadioButton.new(m.tabsContent)
81 .setText("Radio button 1");
82 m.radio1.listen("group-checked-radio-changed", func(e) {
83 var checkedRadio = m.radio1.getRadioButtonsGroup().getCheckedRadio();
84 m.radio_label.setText("Selected radio button: " ~ (checkedRadio != nil ? checkedRadio._text : "none"));
85 });
86
87 m.tab_1.addItem(m.radio1);
88 m.radio2 = gui.widgets.RadioButton.new(parent: m.tabsContent, cfg: {"parent-radio": m.radio1})
89 .setText("Radio button 2");
90 m.tab_1.addItem(m.radio2);
91 m.radio3 = gui.widgets.RadioButton.new(parent: m.tabsContent, cfg: {"parent-radio": m.radio1})
92 .setText("Radio button 3");
93 m.tab_1.addItem(m.radio3);
94 m.radio4 = gui.widgets.RadioButton.new(parent: m.tabsContent, cfg: {"parent-radio": m.radio1})
95 .setText("Radio button 4");
96 m.tab_1.addItem(m.radio4);
97
98 m.tab_2 = HBoxLayout.new();
99 m.tabs.addTab("tab-2", "Tab 2", m.tab_2);
100
101 m.button_box = VBoxLayout.new();
102 m.tab_2.addItem(m.button_box);
103
104 m.button = gui.widgets.Button.new(m.tabsContent, style, {})
105 .setText("A button")
106 .setFixedSize(128, 30)
107 .listen("clicked", func {
108 InputDialog.getText("You clicked the button …", "Enter some text:", func (button, text) {
109 MessageBox.information("You clicked the button …", "… and entered '" ~ (text != nil ? text : "nothing") ~ "' !");
110 });
111 });
112 m.button_box.addItem(m.button);
113 m.image = gui.widgets.Label.new(m.tabsContent, style, {})
114 .setImage("Textures/Splash1.png")
115 .setVisible(0)
116 .setFixedSize(128, 128);
117
118 m.button_box.addItem(m.image);
119 m.image._view._root.addEventListener("mousedown", func (e) {
120 logprint(LOG_INFO, "Image was clicked at:" ~ e.localX ~ "," ~ e.localY);
121 logprint(LOG_INFO, "Client pos:" ~ e.clientX ~ "," ~ e.clientY);
122 logprint(LOG_INFO, "Screen pos:" ~ e.screenX ~ "," ~ e.screenY);
123
124 var img = m.image._view._root;
125 var localPos = [e.localX, e.localY];
126 var canvasPos = img.localToCanvas(localPos);
127
128 logprint(LOG_INFO, "computed canvasPos pos:" ~ canvasPos[0] ~ "," ~ canvasPos[1]);
129
130 var screenPos = m.window.toScreenPosition(canvasPos);
131 logprint(LOG_INFO, "computed screen pos:" ~ screenPos[0] ~ "," ~ screenPos[1]);
132 });
133
134 m.checkable_button = gui.widgets.Button.new(m.tabsContent, style, {})
135 .setCheckable(1)
136 .setChecked(0)
137 .setText("Checkable button")
138 .setFixedSize(128, 30)
139 .listen("toggled", func (e) {
140 m.image.setVisible(int(e.detail.checked));
141 });
142 m.button_box.addItem(m.checkable_button);
143
144 m.upsize_button = gui.widgets.Button.new(m.tabsContent, style, {})
145 .setText("Upsize window")
146 .setFixedSize(128, 30)
147 .listen("clicked", func {
148 var s = m.window.getSize();
149 m.window.setSize(s[0] + 100, s[1] + 100);
150 });
151 m.button_box.addItem(m.upsize_button);
152
153 m.downsize_button = gui.widgets.Button.new(m.tabsContent, style, {})
154 .setText("Downsize window")
155 .setFixedSize(128, 30)
156 .listen("clicked", func {
157 var s = m.window.getSize();
158 m.window.setSize(s[0] - 100, s[1] - 100);
159 });
160 m.button_box.addItem(m.downsize_button);
161 m.combo1 = gui.widgets.ComboBox.new(m.tabsContent, style, {});
162 m.combo1.addMenuItem("Apples", 0);
163 m.combo1.addMenuItem("Pears", 1);
164 m.combo1.addMenuItem("Lemons", 2);
165 m.combo1.addMenuItem("Oranges", 3);
166 m.combo1.setFixedSize(128, 30);
167 m.button_box.addItem(m.combo1);
168
169 m.switch_box = HBoxLayout.new();
170 m.button_box.addItem(m.switch_box);
171
172 m.switch_label = gui.widgets.Label.new(m.tabsContent, style, {})
173 .setText("Switch state: on");
174 m.switch_box.addItem(m.switch_label);
175 m.switch = gui.widgets.Switch.new(m.tabsContent)
176 .setChecked(1)
177 .listen("toggled", func(e) {
178 m.switch_label.setText("Switch state: " ~ (e.detail.checked ? "on" : "off"));
179 });
180 m.switch_box.addItem(m.switch);
181
182 m.list_box = VBoxLayout.new();
183 m.tab_2.addItem(m.list_box);
184
185 m.list = gui.widgets.List.new(m.tabsContent);
186 for (var i = 0; i < 30; i += 1) {
187 m.list.createItem("Item " ~ i);
188 }
189 m.list.listen("selection-changed", func {
190 m.list_selection_label.setText("Selected items: " ~ (string.join(", ", std.map(func(item) item._text, m.list.getSelectedItems())) or "none"));
191 });
192 m.list.setSizeHint([m.list._MAX_SIZE, m.list._MAX_SIZE]);
193 m.list_box.addItem(m.list);
194
195 m.list_selection_label = gui.widgets.Label.new(m.tabsContent, canvas.style, {})
196 .setText("Selected items: none");
197 m.list_selection_label.setAlignment(canvas.AlignBottom);
198 m.list_box.addItem(m.list_selection_label);
199
200 m.benchmark_tab = VBoxLayout.new();
201 m.tabs.addTab("benchmark", "Benchmark", m.benchmark_tab);
202 m.benchmark_tab_scroll = canvas.gui.widgets.ScrollArea.new(m.tabsContent, canvas.style, {});
203 m.benchmark_tab_scroll.setSizeHint([m.list._MAX_SIZE, m.list._MAX_SIZE]);
204 m.benchmark_tab_scroll_layout = VBoxLayout.new();
205 m.benchmark_tab_scroll_layout.setSpacing(0);
206 m.benchmark_tab_scroll.setLayout(m.benchmark_tab_scroll_layout);
207 m.benchmark_tab.addItem(m.benchmark_tab_scroll);
208 m.benchmark_statistics = canvas.gui.widgets.Label.new(m.tabsContent, canvas.style, {});
209 m.benchmark_statistics.setAlignment(canvas.AlignBottom);
210 m.benchmark_tab.addItem(m.benchmark_statistics);
211
212 m.numericControlsTab = VBoxLayout.new();
213 m.tabs.addTab("numeric-controls", "Numeric controls", m.numericControlsTab);
214 m.slider = gui.widgets.Slider.new(m.tabsContent, style, {
215 "max-value" : 100,
216 "page-size" : 20,
217 "tick-step" : 10,
218 "value-style": gui.widgets.Slider.ValueStyle.Moving,
219 "value-position": gui.widgets.Slider.ValuePosition.Above,
220 "ticks-position": gui.widgets.Slider.TicksPosition.Above,
221 })
222 .setValue(42);
223 m.numericControlsTab.addItem(m.slider);
224
225 m.dialBox = HBoxLayout.new();
226 m.dialBox.setContentsMargin(10);
227 m.numericControlsTab.addItem(m.dialBox);
228
229 m.dial = gui.widgets.Dial.new(m.tabsContent, style, {
230 "min-value": 5,
231 "max-value": 50,
232 "step-size": 0.5,
233 "page-size": 5,
234 "tick-step": 2,
235 "show-value": 0,
236 "show-ticks": 0,
237 "value": 14,
238 "value-format": "%.1f",
239 "wrap": 0,
240 });
241 m.dialBox.addItem(m.dial);
242
243 m.dialOptionsBox =VBoxLayout.new();
244 m.dialBox.addItem(m.dialOptionsBox);
245 m.dialShowValueCheckBox = gui.widgets.CheckBox.new(m.tabsContent, canvas.style, {})
246 .setText("Show value")
247 .listen("toggled", func(e) {
248 m.dial.setShowValue(e.detail.checked);
249 });
250 m.dialShowValueCheckBox.setAlignment(canvas.AlignTop);
251 m.dialOptionsBox.addItem(m.dialShowValueCheckBox);
252 m.dialShowTicksCheckBox = gui.widgets.CheckBox.new(m.tabsContent, canvas.style, {})
253 .setText("Show ticks")
254 .listen("toggled", func(e) {
255 m.dial.setShowTicks(e.detail.checked);
256 });
257 m.dialShowTicksCheckBox.setAlignment(canvas.AlignTop);
258 m.dialOptionsBox.addItem(m.dialShowTicksCheckBox);
259 m.dialWrapCheckBox = gui.widgets.CheckBox.new(m.tabsContent, canvas.style, {})
260 .setText("Wrap value")
261 .listen("toggled", func(e) {
262 m.dial.setWrap(e.detail.checked);
263 });
264 m.dialWrapCheckBox.setAlignment(canvas.AlignTop);
265 m.dialOptionsBox.addItem(m.dialWrapCheckBox);
266
267
268 return m;
269 },
270
271 benchmark_widget: func(widget, proc_func=nil, amount=50, cfg=nil) {
272 cfg = cfg or {};
273 var start = systime();
274 me.benchmark_tab_scroll_layout.clear();
275 for (var i = 0; i < amount; i += 1) {
276 var w = widget.new(me.benchmark_tab_scroll.getContent(), canvas.style, cfg);
277 if (proc_func != nil) {
278 proc_func(w, i);
279 }
280 me.benchmark_tab_scroll_layout.addItem(w);
281 }
282 var time = systime() - start;
283 me.benchmark_statistics.setText("Took " ~ time ~ " seconds to add " ~ amount ~ " widgets.");
284 },
285
286 benchmark_radio_button: func(proc_func=nil, amount=50, cfg= nil) {
287 cfg = cfg or {};
288 var start = systime();
289 me.benchmark_tab_scroll_layout.clear();
290 var r = canvas.gui.widgets.RadioButton.new(me.benchmark_tab_scroll.getContent());
291 cfg["parentRadio"] = r;
292 for (var i = 1; i < amount; i += 1) {
293 var w = canvas.gui.widgets.RadioButton.new(me.benchmark_tab_scroll.getContent(), canvas.style, cfg);
294 if (proc_func != nil) {
295 proc_func(w, i);
296 }
297 me.benchmark_tab_scroll_layout.addItem(w);
298 }
299 var time = systime() - start;
300 me.benchmark_statistics.setText("Took " ~ time ~ " seconds to add " ~ amount ~ " widgets.");
301 },
302
303 del: func {
304 me.property_checkbox.del();
305 me.window.del();
306 }
307 };
308