Debian Codegraph @

Nasal/canvas/gui/widgets/Label.nas

raw
1 # SPDX-FileCopyrightText: (C) 2022 James Turner <[email protected]>
2 # SPDX-License-Identifier: GPL-2.0-or-later
3
4 gui.widgets.Label = {
5 new: func(parent, style = nil, cfg = nil)
6 {
7 style = style or canvas.style;
8 cfg = Config.new(cfg);
9 var m = gui.Widget.new(gui.widgets.Label, cfg);
10 m._color = nil;
11 m._focus_policy = m.NoFocus;
12 m._setView( style.createWidget(parent, "label", m._cfg) );
13
14 m.setText(m._cfg.get("text", ""));
15 m.setTextAlign(m._cfg.get("text-align", "left"));
16 if (var color = m._cfg.get("color")) {
17 m.setColor(color);
18 }
19
20 return m;
21 },
22 setColor: func(color) {
23 var type = typeof(color);
24 if (color == nil or type == "scalar") {
25 me._color = color;
26 } elsif (type == "vector") {
27 me._color = canvas._getColor(color);
28 } else {
29 die("canvas.gui.widgets.Label.setColor: 'color' is of unsupported type '" ~ type ~ "'");
30 }
31 if (me._view != nil) {
32 me._view.setColor(me, me._color);
33 }
34 },
35 setTextAlign: func(align) {
36 me._text_align = align;
37 if (me._view != nil) {
38 me._view.setSize(me, me._size[0], me._size[1]);
39 }
40 },
41 setText: func(text)
42 {
43 if (me._view != nil) {
44 me._view.setText(me, text);
45 }
46 return me;
47 },
48 setImage: func(img)
49 {
50 if (me._view != nil) {
51 me._view.setImage(me, img);
52 }
53 return me;
54 },
55 setBackground: func(bg)
56 {
57 if (me._view != nil) {
58 me._view.setBackground(me, bg);
59 }
60 return me;
61 }
62 };