Debian Codegraph @

Nasal/canvas/ErrorNotification.nas

raw
1 var ErrorNotification =
2 {
3 SHOW_TIME: 10.0,
4 SLICE: 17,
5 MARGIN: 10,
6
7 new: func
8 {
9 var m = {
10 parents: [ErrorNotification, PropertyElement.new(["/sim/gui/canvas", "window"], 'error-popup')],
11 _title: "",
12 };
13
14 m.setInt("size[0]", 500);
15 m.setInt("size[1]", 400);
16 m.setBool("visible", 0);
17 m.setInt("z-index", gui.STACK_INDEX["always-on-top"]);
18
19 m._hideTimer = maketimer(m.SHOW_TIME, m, ErrorNotification._hideTimeout);
20 m._hideTimer.singleShot = 1;
21 m._reportIndex = 0;
22
23 return m;
24 },
25
26 # Destructor
27 del: func
28 {
29 me.parents[1].del();
30 if( me["_canvas"] != nil )
31 me._canvas.del();
32 },
33
34 _createCanvas: func()
35 {
36 var size = [
37 me.get("size[0]"),
38 me.get("size[1]")
39 ];
40
41 me._canvas = new({
42 size: [2 * size[0], 2 * size[1]],
43 view: size,
44 placement: {
45 type: "window",
46 id: "error-popup"
47 },
48 name: "Error Notification"
49 });
50
51 me.set("capture-events", 1);
52 me.set("fill", "rgba(255,255,255,0.8)");
53
54 # transparent background
55 me._canvas.setColorBackground(0.0, 0.0, 0.0, 0.0);
56
57 var root = me._canvas.createGroup();
58 me._root = root;
59
60 me._frame =
61 root.createChild("image", "background")
62 .set("src", "gui/images/tooltip.png")
63 .set("slice", me.SLICE ~ " fill")
64 .setSize(size);
65
66 me._warningIcon =
67 root.createChild("image", "warning-icon")
68 .set("src", "gui/images/warning-icon.png")
69 .setTranslation(me.SLICE, me.SLICE);
70
71 var iconWidth = me._warningIcon.get("size[0]");
72
73 me._text =
74 root.createChild("text", "error-description")
75 .setText(translate("error-popup-heading", "sys"))
76 .setAlignment("left-top")
77 .setFontSize(14)
78 .setFont("LiberationFonts/LiberationSans-Bold.ttf")
79 .setColor(1,1,1)
80 .setDrawMode(Text.TEXT)
81 .setTranslation(me.SLICE + iconWidth + me.MARGIN, me.SLICE);
82
83 me._canvas.addEventListener("mousedown", func me.clicked());
84
85 return me._canvas;
86 },
87
88 clicked: func()
89 {
90 # always hide on click
91 me.hideNow();
92
93 # only show the report if we are a notification, not
94 # if we're a simple message
95 if (me._reportIndex >= 0) {
96 fgcommand("show-error-report", props.Node.new({ "index": me._reportIndex})); # should we show the current one?
97 }
98 },
99
100 updateText: func(msg = nil)
101 {
102 if (msg == nil) {
103 msg = getprop("/sim/error-report/display/category");
104 var clickForMoreMsg = translate("error-popup-click-for-more", "sys");
105 me._text.setText(msg ~ "\n\n" ~ clickForMoreMsg);
106 } else {
107 me._text.setText(msg);
108 }
109 me._updateBounds();
110 },
111
112 _updateBounds: func
113 {
114 # the width of everything except the text
115 var extraWidth = me._warningIcon.get("size[0]") + me.MARGIN + (2 * me.SLICE);
116 var maxTextWidth = me.get("size[0]") - extraWidth;
117
118 me._text.setMaxWidth(maxTextWidth);
119
120 # compute the bounds
121 var text_bb = me._text.update().getBoundingBox();
122 var width = text_bb[2];
123 var height = text_bb[3];
124
125 if( width > maxTextWidth )
126 width = maxTextWidth;
127
128 me._width = width + extraWidth;
129 me._height = height + 2 * me.SLICE;
130 me._frame.setSize(me._width, me._height)
131 .update();
132
133 me._updatePosition();
134 },
135
136 _updatePosition: func
137 {
138 var INSET = 50;
139 var y = INSET;
140 var x = getprop('/sim/startup/xsize') - (me._width + INSET);
141
142 me.setInt("x", x);
143 me.setInt("y", y);
144 },
145
146 show: func(index)
147 {
148 me._reportIndex = index;
149 me._hideTimer.stop();
150 me.setBool("visible", 1);
151
152 if (me._reportIndex >= 0) {
153 # auto hide for notifications but not for messages
154 me._hideTimer.start();
155 }
156 },
157
158 hideNow: func()
159 {
160 me._hideTimer.stop();
161 me._hideTimeout();
162 },
163
164 _hideTimeout: func()
165 {
166 me.setBool("visible", 0);
167
168 # if the popup auto-hides, dismiss the report as well, or we will
169 # never process any future report
170 if (me._reportIndex >= 0) {
171 fgcommand("dismiss-error-report");
172 }
173 }
174 };
175
176 var errorNotificationCanvas = nil;
177
178 var showErrorNotification = func(node)
179 {
180 if (errorNotificationCanvas == nil) {
181 # create instance
182 errorNotificationCanvas = canvas.ErrorNotification.new();
183 errorNotificationCanvas._createCanvas();
184 }
185
186 errorNotificationCanvas.updateText();
187 var reportIndex = node.getNode("index").getValue();
188 errorNotificationCanvas.show(reportIndex);
189 }
190
191 var showErrorMessage = func(node)
192 {
193 if (errorNotificationCanvas == nil) {
194 # create instance
195 errorNotificationCanvas = canvas.ErrorNotification.new();
196 errorNotificationCanvas._createCanvas();
197 }
198
199 var msg = node.getNode("message").getValue();
200 errorNotificationCanvas.updateText(msg);
201 errorNotificationCanvas.show(-1);
202 }
203
204 addcommand("show-error-notification-popup", showErrorNotification);
205 addcommand("show-error-message-popup", showErrorMessage);
206
207 # called from unload() in api.nas
208 var unloadErrorNotification = func
209 {
210 removecommand("show-error-notification-popup");
211 if (errorNotificationCanvas) {
212 errorNotificationCanvas.del();
213 errorNotificationCanvas = nil;
214 }
215 }