change property type dialog to use dropdown (has bug with popover autohide, but added...
[roobuilder] / src / Builder4 / PopoverProperty.bjs
1 {
2  "build_module" : "builder",
3  "gen_extended" : false,
4  "items" : [
5   {
6    "# Gtk.PositionType position" : "Gtk.PositionType.RIGHT",
7    "# JsRender.Node node" : "",
8    "# JsRender.NodeProp? original_prop" : "null",
9    "# JsRender.NodeProp? prop" : "null",
10    "# Xcls_MainWindow mainwindow" : "null",
11    "# string key_type" : "",
12    "# string old_keyname" : "",
13    "$ xns" : "Gtk",
14    "* ctor" : "new Gtk.Popover()",
15    "@ void success" : "(Project.Project pr, JsRender.JsRender file)",
16    "bool autohide" : true,
17    "bool done" : false,
18    "bool is_new" : false,
19    "id" : "PopoverProperty",
20    "items" : [
21     {
22      "$ xns" : "Gtk",
23      "* pack" : "set_child",
24      "Gtk.Orientation orientation" : "Gtk.Orientation.VERTICAL",
25      "bool homogeneous" : false,
26      "items" : [
27       {
28        "$ xns" : "Gtk",
29        "Gtk.Orientation orientation" : "Gtk.Orientation.HORIZONTAL",
30        "id" : "header",
31        "items" : [
32         {
33          "$ xns" : "Gtk",
34          "bool always_show_image" : true,
35          "bool hexpand" : true,
36          "id" : "cancelbtn",
37          "listeners" : {
38           "clicked" : [
39            "() => {",
40            "\t_this.prop = null;",
41            "\t_this.is_new = false;",
42            "\t_this.kname.el.set_text(\"Cancel\");",
43            "\t_this.el.hide();",
44            "\t",
45            "}"
46           ]
47          },
48          "string label" : "Cancel",
49          "xtype" : "Button"
50         },
51         {
52          "$ xns" : "Gtk",
53          "bool hexpand" : true,
54          "id" : "headertitle",
55          "string label" : "Add / Edit property",
56          "xtype" : "Label"
57         },
58         {
59          "$ xns" : "Gtk",
60          "bool always_show_image" : true,
61          "bool hexpand" : true,
62          "id" : "savebtn",
63          "listeners" : {
64           "clicked" : [
65            "() => {",
66            "\tif (!_this.is_new) {",
67            "\t\t_this.el.hide();",
68            "\t}",
69            "\t",
70            "\t// check if text is not empty..",
71            "\tif ( _this.kname.el.get_text().strip().length < 1) {",
72            "\t",
73            "\t\t// error should already be showing?",
74            "\t\treturn;",
75            "\t}",
76            "\t ",
77            "\t// since we can't add listeners?!?!?",
78            "\t// only check props.",
79            "\t// check if property already exists in node.\t",
80            "",
81            "",
82            "\tvar prop = new JsRender.NodeProp(",
83            "\t\t_this.kname.el.get_text().strip(),",
84            "\t\t_this.ptype.getValue(),",
85            "\t\t_this.ktype.el.get_text().strip(),",
86            "\t\t_this.prop.val",
87            "\t);",
88            "",
89            "\tif (_this.node.props.has_key(prop.to_index_key())) {",
90            "\t\t_this.error.setError(\"Property already exists\");",
91            "\t\treturn;\t",
92            "\t}",
93            "\t",
94            "\t",
95            "\t",
96            "\t_this.node.add_prop(prop);",
97            "\t// hide self",
98            "\t_this.prop = null; // skip checks..",
99            "\t_this.is_new = false;",
100            "\t_this.el.hide();",
101            " \t_this.mainwindow.windowstate.left_props.changed();",
102            "\t_this.mainwindow.windowstate.left_props.view.editProp(prop);",
103            "",
104            "\t",
105            "\t",
106            "}"
107           ]
108          },
109          "string label" : "Add Property",
110          "xtype" : "Button"
111         }
112        ],
113        "xtype" : "Box"
114       },
115       {
116        "$ visible" : true,
117        "$ xns" : "Gtk",
118        "Gtk.Align halign" : "Gtk.Align.START",
119        "Gtk.Justification justify" : "Gtk.Justification.LEFT",
120        "int margin_top" : 12,
121        "label" : "Property Type (eg. property or method)",
122        "x_options" : 4,
123        "xtype" : "Label"
124       },
125       {
126        "$ xns" : "Gtk",
127        "bool show_arrow" : true,
128        "id" : "ptype",
129        "items" : [
130         {
131          "$ strings" : "JsRender.NodePropType.get_pulldown_list()",
132          "$ xns" : "Gtk",
133          "* prop" : "model",
134          "xtype" : "StringList"
135         }
136        ],
137        "listeners" : {
138         "notify[\"selected\"]" : [
139          "() => {",
140          "",
141          "\t_this.el.grab_focus(); // stop prevent autohide breaking.",
142          " }"
143         ]
144        },
145        "xtype" : "DropDown",
146        "| JsRender.NodePropType getValue" : [
147         "() {",
148         "\tvar sl = this.el.model as Gtk.StringList;",
149         "\tvar str = sl.get_string(this.el.selected);",
150         "\treturn JsRender.NodePropType.nameToType(str);",
151         "}"
152        ],
153        "| void setValue" : [
154         "(JsRender.NodePropType ty) {",
155         "\tvar str = ty.to_name();",
156         "\tvar sl = this.el.model as Gtk.StringList;",
157         "\tfor(var i = 0; i < sl.get_n_items(); i++) {",
158         "\t\tif(sl.get_string(i) == str) {",
159         "\t\t\tthis.el.set_selected(i);",
160         "\t\t\tbreak;",
161         "\t\t}",
162         "\t}",
163         "\t",
164         "}"
165        ]
166       },
167       {
168        "$ visible" : true,
169        "$ xns" : "Gtk",
170        "Gtk.Align halign" : "Gtk.Align.START",
171        "Gtk.Justification justify" : "Gtk.Justification.LEFT",
172        "int margin_top" : 12,
173        "label" : "Type or Return Type",
174        "x_options" : 4,
175        "xtype" : "Label"
176       },
177       {
178        "$ visible" : true,
179        "$ xns" : "Gtk",
180        "id" : "ktype",
181        "xtype" : "Entry"
182       },
183       {
184        "$ visible" : true,
185        "$ xns" : "Gtk",
186        "Gtk.Align halign" : "Gtk.Align.START",
187        "Gtk.Justification justify" : "Gtk.Justification.LEFT",
188        "int margin_top" : 12,
189        "label" : "Name",
190        "tooltip_text" : "center, north, south, east, west",
191        "x_options" : 4,
192        "xtype" : "Label"
193       },
194       {
195        "$ visible" : true,
196        "$ xns" : "Gtk",
197        "id" : "kname",
198        "items" : [
199         {
200          "$ xns" : "Gtk",
201          "listeners" : {
202           "leave" : [
203            "( ) => {",
204            "",
205            "    _this.error.setError(\"\");",
206            "\tvar val = _this.kname.el.get_text().strip(); ",
207            "\tif (val.length < 1) {",
208            "\t\t_this.error.setError(\"Name can not be empty\");",
209            "\t}",
210            "",
211            "}",
212            ""
213           ]
214          },
215          "xtype" : "EventControllerFocus"
216         },
217         {
218          "$ xns" : "Gtk",
219          "listeners" : {
220           "key_released" : [
221            "(keyval, keycode, state) => {",
222            "",
223            "    _this.error.setError(\"\");",
224            "\tvar val = _this.kname.el.get_text().strip(); ",
225            "\tif (val.length < 1) {",
226            "\t\t_this.error.setError(\"Name can not be empty\");",
227            "\t}",
228            "",
229            "}",
230            ""
231           ]
232          },
233          "xtype" : "EventControllerKey"
234         }
235        ],
236        "xtype" : "Entry"
237       },
238       {
239        "$ visible" : true,
240        "$ xns" : "Gtk",
241        "Gtk.Align halign" : "Gtk.Align.START",
242        "Gtk.Justification justify" : "Gtk.Justification.LEFT",
243        "bool use_markup" : true,
244        "id" : "error",
245        "int margin_top" : 0,
246        "label" : "<span color=\"red\">Error Message</span>",
247        "tooltip_text" : "center, north, south, east, west",
248        "x_options" : 4,
249        "xtype" : "Label",
250        "| void setError" : [
251         "(string err)   {",
252         "\tif (err == \"\") {",
253         "\t\tthis.el.label = \"\";",
254         "\t} else {",
255         "",
256         "\t\t",
257         "\t\tthis.el.label = \"<span color=\\\"red\\\">\" + err + \"</span>\";",
258         "\t}",
259         "}",
260         ""
261        ]
262       }
263      ],
264      "xtype" : "Box"
265     }
266    ],
267    "listeners" : {
268     "closed" : [
269      "() => {",
270      "",
271      " \tGLib.debug(\"popover closed\");",
272      "\tif (_this.is_new) {",
273      "\t\t// dont allow hiding if we are creating a new one.",
274      "\t\t// on.hide will reshow it.",
275      "\t\treturn;",
276      "\t}",
277      "\tif (_this.prop == null) {",
278      "\t\t// hide and dont update.",
279      "\t\treturn;",
280      "\t}",
281      "\tif (this.kname.el.get_text().strip().length < 1) {",
282      "\t\treturn;",
283      "\t}",
284      "\t",
285      " ",
286      "         \t",
287      "         ",
288      "  \tthis.updateProp();",
289      "        ",
290      "\t ",
291      "",
292      "",
293      "  ",
294      "} "
295     ],
296     "hide" : [
297      "() => {",
298      "  \tGLib.debug(\"popover hidden\");",
299      "\tif (_this.is_new || this.kname.el.get_text().strip().length < 1) {",
300      "\t\t// dont allow hiding if we are creating a new one.",
301      "\t\tGLib.debug(\"prevent hiding as its new or text is empty\"); ",
302      "\t\tthis.el.show();",
303      "\t\treturn;",
304      "",
305      "\t}",
306      "\tif (this.original_prop != null && !this.prop.equals(this.original_prop)) {",
307      "\t\t// this is convoluted..",
308      "\t\t_this.mainwindow.windowstate.left_props.changed(); ",
309      "\t}",
310      "\t",
311      "\t",
312      "}"
313     ]
314    },
315    "xtype" : "Popover",
316    "| void show" : [
317     "(",
318     "\tGtk.Widget btn, ",
319     "\tJsRender.Node node, ",
320     "\tJsRender.NodeProp prop, ",
321     "\tint y,",
322     "\tbool is_new = false",
323     "\t ) ",
324     "{",
325     "\t",
326     "    this.original_prop = prop.dupe();",
327     "\tthis.is_new = is_new; ",
328     "\tvar pref = is_new ? \"Add \" : \"Modify \";",
329     "\tif (prop.ptype == JsRender.NodePropType.LISTENER) {",
330     "\t\tthis.headertitle.el.label = pref + \"Event Listener\"; // cant really happen yet?",
331     "\t} else {",
332     "\t\tthis.headertitle.el.label = pref + \"Property\";",
333     "\t}",
334     "\tthis.prop = prop;",
335     "\tthis.node = node;",
336     "\t",
337     "\t_this.kname.el.set_text(prop.name);",
338     "\t_this.ktype.el.set_text(prop.rtype);",
339     "\t",
340     " \t_this.ptype.setValue(prop.ptype);",
341     "\t// does node have this property...",
342     "",
343     "",
344     "\t_this.node = node;",
345     "\t//console.log('show all');",
346     "\t",
347     "\tGLib.debug(\"set parent = %s\", btn.get_type().name());",
348     "\tvar par = btn.get_parent();",
349     "\t",
350     "\tif (par == null) {",
351     "\t\tGLib.debug(\"parent of that is null - not showing\");",
352     "\t\treturn;",
353     "\t}",
354     "\tif (this.el.parent == null) {",
355     "\t\tthis.el.set_parent(btn);",
356     "\t}",
357     "\tvar  r = Gdk.Rectangle() {",
358     "\t\t\tx = btn.get_width(), // align left...",
359     "\t\t\ty = 0,",
360     "\t\t\twidth = 1,",
361     "\t\t\theight = 1",
362     "\t\t};",
363     "\t//Gtk.Allocation rect;",
364     "\t//btn.get_allocation(out rect);",
365     "    this.el.set_pointing_to(r);",
366     "    ",
367     "",
368     "\t ",
369     "\tif (y > -1) {",
370     "\t\t ",
371     "\t\t r = Gdk.Rectangle() {",
372     "\t\t\tx = btn.get_width(), // align left...",
373     "\t\t\ty = y,",
374     "\t\t\twidth = 1,",
375     "\t\t\theight = 1",
376     "\t\t};",
377     "\t\tthis.el.set_pointing_to( r);",
378     "\t}",
379     "\t",
380     "\t",
381     "",
382     "\t//this.el.set_position(Gtk.PositionType.TOP);",
383     "",
384     "\t// window + header?",
385     "\t GLib.debug(\"SHOWALL - POPIP\\n\");",
386     "\t",
387     "\tthis.kname.el.grab_focus();",
388     "\tthis.savebtn.el.set_label(\"Save\");",
389     "\tthis.cancelbtn.el.visible = false;",
390     "\tif (this.is_new) {",
391     "\t\tthis.savebtn.el.set_label(\"Add Property\");",
392     "\t\tthis.cancelbtn.el.visible = true;",
393     "\t}",
394     "\tthis.error.setError(\"\");",
395     "\tthis.el.show();",
396     "\t//this.success = c.success;",
397     " ",
398     "}"
399    ],
400    "| void updateProp" : [
401     "() {",
402     " \tGLib.debug(\"updateProp called\");",
403     "",
404     "\t",
405     "\t",
406     "\t_this.prop.name = this.kname.el.get_text().strip();",
407     "\t_this.prop.ptype = this.ptype.getValue();",
408     "\t_this.prop.rtype = this.ktype.el.get_text().strip();",
409     "\t",
410     "\t  ",
411     "}",
412     ""
413    ]
414   }
415  ],
416  "name" : "PopoverProperty"
417 }