Debian Codegraph @

Nasal/canvas/gui/Popup.nas

raw
1 # SPDX-FileCopyrightText: (C) 2022 Frederic Croix <[email protected]>
2 # SPDX-License-Identifier: GPL-2.0-or-later
3
4 gui.Popup = {
5 __used_ids: [],
6 # Constructor
7 #
8 # @param size ([width, height])
9 new: func(size_, id = nil, parent = nil, take_focus = 0) {
10 if (id == nil) {
11 logprint(LOG_WARN, "Creating gui.Popup without ID");
12 }
13
14
15 var ghost = _newWindowGhost(id);
16 var m = {
17 parents: [gui.Popup, PropertyElement, ghost],
18 _ghost: ghost,
19 _node: props.wrapNode(ghost._node_ghost),
20 _focused: 0,
21 _widgets: [],
22 _parent: parent,
23 _canvas: nil,
24 _frame_width: 1,
25 _take_focus: take_focus,
26 };
27
28 m.setInt("content-size[0]", size_[0]);
29 m.setInt("content-size[1]", size_[1]);
30 m._updateDecoration();
31
32 m.setFocus();
33
34 # arg = [child, listener_node, mode, is_child_event]
35 setlistener(m._node, func m._propCallback(arg[0], arg[2]), 0, 2);
36
37 return m;
38 },
39 # Destructor
40 del: func {
41 me.clearFocus();
42
43 if (me["_canvas"] != nil) {
44 var placements = me._canvas._node.getChildren("placement");
45 # Do not remove canvas if other placements exist
46 if (size(placements) > 1) {
47 foreach (var p; placements) {
48 if (p.getValue("type") == "window" and p.getValue("id") == me.get("id")) {
49 p.remove();
50 }
51 }
52 } else {
53 me._canvas.del();
54 }
55 me._canvas = nil;
56 }
57 if (me._node != nil) {
58 me._node.remove();
59 me._node = nil;
60 }
61 },
62 # Create the canvas to be used for this Window
63 #
64 # @return The new canvas
65 createCanvas: func() {
66 var size = [
67 me.get("content-size[0]"),
68 me.get("content-size[1]")
69 ];
70
71 me._canvas = new({
72 size: [size[0], size[1]],
73 view: size,
74 placement: {
75 type: "window",
76 id: me.get("id")
77 },
78
79 # Standard alpha blending
80 "blend-source-rgb": "src-alpha",
81 "blend-destination-rgb": "one-minus-src-alpha",
82
83 # Just keep current alpha (TODO allow using rgb textures instead of rgba?)
84 "blend-source-alpha": "zero",
85 "blend-destination-alpha": "one"
86 });
87
88 me._canvas._focused_widget = nil;
89 me._canvas.data("focused", me._focused);
90
91 return me._canvas;
92 },
93 # Set an existing canvas to be used for this Window
94 setCanvas: func(canvas_) {
95 if (ghosttype(canvas_) != "Canvas") {
96 return debug.warn("Not a Canvas");
97 }
98
99 canvas_.addPlacement({type: "window", "id": me.get("id")});
100 me['_canvas'] = canvas_;
101
102 canvas_._focused_widget = nil;
103 canvas_.data("focused", me._focused);
104
105 return me;
106 },
107 # Get the displayed canvas
108 getCanvas: func(create = 0) {
109 if (me['_canvas'] == nil and create) {
110 me.createCanvas();
111 }
112
113 return me['_canvas'];
114 },
115 setLayout: func(l) {
116 if (me['_canvas'] == nil) {
117 me.createCanvas();
118 }
119
120 me._canvas.update(); # Ensure placement is applied
121 me._ghost.setLayout(l);
122 return me;
123 },
124 #
125 setFocus: func {
126 if (gui.focused_window != nil and me._take_focus) {
127 gui.focused_window.clearFocus();
128 gui.focused_window = me;
129 }
130
131 # me.onFocusIn();
132 me._focused = 1;
133 me._onStateChange();
134 return me;
135 },
136 #
137 clearFocus: func {
138 # me.onFocusOut();
139 me._focused = 0;
140 me._onStateChange();
141 if (me._take_focus) {
142 if (gui.focused_window == me) {
143 gui.focused_window = nil;
144 }
145 if (me._parent != nil and contains(gui.open_popups, me._parent)) {
146 me._parent.setFocus();
147 }
148 }
149 return me;
150 },
151 setPosition: func {
152 if (size(arg) == 1) {
153 var arg = arg[0];
154 }
155 var (x, y) = arg;
156
157 me.setInt("tf/t[0]", x);
158 me.setInt("tf/t[1]", y);
159 return me;
160 },
161 setSize: func {
162 if (size(arg) == 1) {
163 var arg = arg[0];
164 }
165 var (w, h) = arg;
166
167 me.set("content-size[0]", w);
168 me.set("content-size[1]", h);
169
170 if (me.onResize != nil) {
171 me.onResize();
172 }
173
174 return me;
175 },
176 getSize: func {
177 var w = me.get("content-size[0]");
178 var h = me.get("content-size[1]");
179 return [w,h];
180 },
181 # Raise to top of window stack
182 raise: func() {
183 # on writing the z-index the window always is moved to the top of all other
184 # windows with the same z-index.
185 me.setInt("z-index", me.get("z-index", gui.STACK_INDEX["always-on-top"]));
186
187 me.setFocus();
188 },
189 hide: func(parents = 0) {
190 me.clearFocus();
191 me._ghost.hide();
192 for (var i = 0; i < size(gui.open_popups); i += 1) {
193 if (gui.open_popups[i] == me) {
194 gui.open_popups = subvec(gui.open_popups, 0, i) ~ subvec(gui.open_popups, i);
195 break;
196 }
197 }
198 if (me._parent != nil) {
199 me._parent.setFocus();
200 }
201 },
202 show: func() {
203 me._ghost.show();
204 me.raise();
205 if (me._canvas != nil) {
206 me._canvas.update();
207 }
208 for (var i = 0; i < size(gui.open_popups); i += 1) {
209 gui.open_popups[i].clearFocus();
210 }
211 append(gui.open_popups, me);
212 },
213 # Hide / show the window based on whether it's currently visible
214 toggle: func() {
215 if (me.isVisible()) {
216 me.hide();
217 } else {
218 me.show();
219 me.raise();
220 }
221 },
222 onResize: func() {
223 if (me['_canvas'] == nil) {
224 return;
225 }
226
227 for(var i = 0; i < 2; i += 1) {
228 var size = me.get("content-size[" ~ i ~ "]");
229 me._canvas.set("size[" ~ i ~ "]", size);
230 me._canvas.set("view[" ~ i ~ "]", size);
231 }
232 },
233 # protected:
234 _onStateChange: func {
235 var event = canvas.CustomEvent.new("wm.focus-" ~ (me._focused ? "in" : "out"));
236
237 if (me.getCanvas() != nil) {
238 me.getCanvas().data("focused", me._focused).dispatchEvent(event);
239 }
240 },
241 # private:
242 #mode 0 = value changed, +-1 add/remove node
243 _propCallback: func(child, mode) {
244 if (!me._node.equals(child.getParent())) {
245 return;
246 }
247 var name = child.getName();
248
249 # support for CSS like position: absolute; with right and/or bottom margin
250 if (name == "right") {
251 me._handlePositionAbsolute(child, mode, name, 0);
252 } elsif (name == "bottom") {
253 me._handlePositionAbsolute(child, mode, name, 1);
254 }
255
256 if (mode == 0) {
257 if (name == "size") {
258 me._resizeDecoration();
259 }
260 }
261 },
262 _handlePositionAbsolute: func(child, mode, name, index) {
263 # mode
264 # -1 child removed
265 # 0 value changed
266 # 1 child added
267
268 if (mode == 0) {
269 me._updatePos(index, name);
270 } elsif (mode == 1) {
271 me["_listener_" ~ name] = [
272 setlistener(
273 "/sim/gui/canvas/size[" ~ index ~ "]",
274 func me._updatePos(index, name)
275 ),
276 setlistener(
277 me._node.getNode("content-size[" ~ index ~ "]"),
278 func me._updatePos(index, name)
279 )
280 ];
281 } elsif (mode == -1) {
282 for (var i = 0; i < 2; i += 1) {
283 removelistener(me["_listener_" ~ name][i]);
284 }
285 }
286 },
287 _updatePos: func(index, name) {
288 me.setInt(
289 "tf/t[" ~ index ~ "]",
290 getprop("/sim/gui/canvas/size[" ~ index ~ "]") - me.get(name) - me.get("content-size[" ~ index ~ "]")
291 );
292 },
293 getCanvasDecoration: func() {
294 return wrapCanvas(me._getCanvasDecoration());
295 },
296 _updateDecoration: func() {
297 me.set("decoration-border", "1 1 1");
298 me.set("shadow-inset", 5);
299 me.set("shadow-radius", 2);
300 me.setBool("update", 1);
301
302 var canvas_deco = me.getCanvasDecoration();
303 canvas_deco.addEventListener("mousedown", func me.raise());
304 canvas_deco.set("blend-source-rgb", "src-alpha");
305 canvas_deco.set("blend-destination-rgb", "one-minus-src-alpha");
306 canvas_deco.set("blend-source-alpha", "one");
307 canvas_deco.set("blend-destination-alpha", "one");
308
309 var group_deco = canvas_deco.getGroup("decoration");
310 me._frame = group_deco.createChild("path");
311 me._frame.set("fill", "none");
312 me._frame.set("stroke", "#888888");
313 me._frame.set("stroke-width", me._frame_width);
314
315 me._resizeDecoration();
316 me._onStateChange();
317 },
318 _resizeDecoration: func() {
319 me._frame.reset()
320 .rect(0, 0, me.get("size[0]"), me.get("size[1]"));
321 }
322 };
323
324