1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
-- @file view.lua
--
-- load modules
local table = require("base/table")
local log = require("ui/log")
local rect = require("ui/rect")
local point = require("ui/point")
local object = require("ui/object")
local canvas = require("ui/canvas")
local curses = require("ui/curses")
local action = require("ui/action")
-- define module
local view = view or object()
-- new view instance
function view:new(name, bounds, ...)
-- create instance
self = self()
-- init view
self:init(name, bounds, ...)
-- done
return self
end
-- init view
function view:init(name, bounds)
-- check
assert(name and type(bounds) == 'table')
-- init type
self._TYPE = "view"
-- init state
local state = object()
state.visible = true -- view visibility
state.cursor_visible = false -- cursor visibility
state.block_cursor = false -- block cursor
state.selected = false -- is selected?
state.focused = false -- is focused?
state.redraw = true -- need redraw
state.on_refresh = true -- need refresh
state.on_resize = true -- need resize
self._STATE = state
-- init options
local options = object()
options.selectable = false -- true if window can be selected
options.mouseable = false -- false by default
self._OPTIONS = options
-- init attributes
self._ATTRS = object()
-- init actions
self._ACTIONS = object()
-- init extras
self._EXTRAS = object()
-- init name
self._NAME = name
-- init cursor
self._CURSOR = point{0, 0}
-- init bounds and window
self:bounds_set(bounds)
end
-- exit view
function view:exit()
-- close window
if self._WINDOW then
self._WINDOW:close()
self._WINDOW = nil
end
end
-- get view name
function view:name()
return self._NAME
end
-- get view bounds
function view:bounds()
return self._BOUNDS
end
-- set window bounds
function view:bounds_set(bounds)
if bounds and self:bounds() ~= bounds then
self._BOUNDS = bounds()
self:invalidate(true)
end
end
-- get view width
function view:width()
return self:bounds():width()
end
-- get view height
function view:height()
return self:bounds():height()
end
-- get view size
function view:size()
return self:bounds():size()
end
-- get the parent view
function view:parent()
return self._PARENT
end
-- set the parent view
function view:parent_set(parent)
self._PARENT = parent
end
-- get the application
function view:application()
if not self._APPLICATION then
local app = self
while app:parent() do
app = app:parent()
end
self._APPLICATION = app
end
return self._APPLICATION
end
-- get the view window
function view:window()
if not self._WINDOW then
-- create window
self._WINDOW = curses.new_pad(self:height() > 0 and self:height() or 1, self:width() > 0 and self:width() or 1)
assert(self._WINDOW, "cannot create window!")
-- disable cursor
self._WINDOW:leaveok(true)
end
return self._WINDOW
end
-- get the view canvas
function view:canvas()
if not self._CANVAS then
self._CANVAS = canvas:new(self, self:window())
end
return self._CANVAS
end
-- draw view
function view:on_draw(transparent)
-- trace
log:print("%s: draw (transparent: %s) ..", self, tostring(transparent))
-- draw background
if not transparent then
local background = self:background()
if background then
background = curses.color_pair(background, background)
self:canvas():attr(background):move(0, 0):putchar(' ', self:width() * self:height())
else
self:canvas():clear()
end
end
-- clear mark
self:state_set("redraw", false)
self:_mark_refresh()
end
-- refresh view
function view:on_refresh()
-- refresh to the parent view
local parent = self:parent()
if parent and self:state("visible") then
-- clip bounds with the parent view
local bounds = self:bounds()
local r = bounds():intersect(rect{0, 0, parent:width(), parent:height()})
if not r:empty() then
-- trace
log:print("%s: refresh to %s(%d, %d, %d, %d) ..", self, parent:name(), r.sx, r.sy, r.ex, r.ey)
-- copy this view to parent view
self:window():copy(parent:window(), 0, 0, r.sy, r.sx, r.ey - 1, r.ex - 1)
end
end
end
-- resize bounds of inner child views (abstract)
function view:on_resize()
-- trace
log:print("%s: resize ..", self)
-- close the previous windows first
if self._WINDOW then
self._WINDOW:close()
self._WINDOW = nil
end
-- need renew canvas
self._CANVAS = nil
-- clear mark
self:state_set("resize", false)
-- do action
self:action_on(action.ac_on_resized)
end
-- show view?
--
-- .e.g
-- v:show(false)
-- v:show(true, {focused = true})
--
function view:show(visible, opt)
if self:state("visible") ~= visible then
local parent = self:parent()
if parent and parent:current() == self and not visible then
parent:select_next(nil, true)
elseif parent and visible and opt and opt.focused then
parent:select(self)
end
self:state_set("visible", visible)
self:invalidate()
end
end
-- invalidate view to redraw it
function view:invalidate(bounds)
if bounds then
self:_mark_resize()
end
self:_mark_redraw()
end
-- on event (abstract)
--
-- @return true: done and break dispatching, false/nil: continous to dispatch to other views
--
function view:on_event(e)
end
-- get the current event
function view:event()
return self:parent() and self:parent():event()
end
-- put an event to view
function view:put_event(e)
return self:parent() and self:parent():put_event(e)
end
-- get type
function view:type()
return self._TYPE
end
-- set type
function view:type_set(t)
self._TYPE = t or "view"
return self
end
-- get state
function view:state(name)
return self._STATE[name]
end
-- set state
function view:state_set(name, enable)
-- state not changed?
enable = enable or false
if self:state(name) == enable then
return self
end
-- change state
self._STATE[name] = enable
return self
end
-- get option
function view:option(name)
return self._OPTIONS[name]
end
-- set option
function view:option_set(name, enable)
-- state not changed?
enable = enable or false
if self:option(name) == enable then
return
end
-- set option
self._OPTIONS[name] = enable
end
-- get attribute
function view:attr(name)
return self._ATTRS[name]
end
-- set attribute
function view:attr_set(name, value)
self._ATTRS[name] = value
self:invalidate()
return self
end
-- get extra data
function view:extra(name)
return self._EXTRAS[name]
end
-- set extra data
function view:extra_set(name, value)
self._EXTRAS[name] = value
return self
end
-- set action
function view:action_set(name, on_action)
self._ACTIONS[name] = on_action
return self
end
-- add action
function view:action_add(name, on_action)
self._ACTIONS[name] = table.join(table.wrap(self._ACTIONS[name]), on_action)
return self
end
-- do action
function view:action_on(name, ...)
local on_action = self._ACTIONS[name]
if on_action then
if type(on_action) == "string" then
-- send command
if self:application() then
self:application():send(on_action)
end
elseif type(on_action) == "function" then
-- do action script
return on_action(self, ...)
elseif type(on_action) == "table" then
for _, on_action_val in ipairs(on_action) do
-- we cannot uses the return value of action for multi-actions
if type(on_action_val) == "function" then
on_action_val(self, ...)
end
end
end
end
end
-- get cursor position
function view:cursor()
return self._CURSOR
end
-- move cursor to the given position
function view:cursor_move(x, y)
self._CURSOR = point{ self:_limit(x, 0, self:width() - 1), self:_limit(y, 0, self:height() - 1) }
return self
end
-- show cursor?
function view:cursor_show(visible)
if self:state("cursor_visible") ~= visible then
self:state_set("cursor_visible", visible)
end
return self
end
-- get background
function view:background()
local background = self:attr("background")
if not background and self:parent() then
background = self:parent():background()
end
return background
end
-- set background, .e.g background_set("blue")
function view:background_set(color)
return self:attr_set("background", color)
end
-- limit value range
function view:_limit(value, minval, maxval)
return math.min(maxval, math.max(value, minval))
end
-- need resize view
function view:_mark_resize()
-- have been marked?
if self:state("resize") then
return
end
-- trace
log:print("%s: mark as resize", self)
-- need resize it
self:state_set("resize", true)
-- @note we need to trigger on_resize() of the root view and pass it to this subview
if self:parent() then
self:parent():invalidate(true)
end
end
-- need redraw view
function view:_mark_redraw()
-- have been marked?
if self:state("redraw") then
return
end
-- trace
log:print("%s: mark as redraw", self)
-- need redraw it
self:state_set("redraw", true)
-- need redraw it's parent view if this view is invisible
if not self:state("visible") and self:parent() then
self:parent():_mark_redraw()
end
end
-- need refresh view
function view:_mark_refresh()
-- have been marked?
if self:state("refresh") then
return
end
-- need refresh it
if self:state("visible") then
self:state_set("refresh", true)
end
-- need refresh it's parent view
if self:parent() then
self:parent():_mark_refresh()
end
end
-- tostring(view)
function view:__tostring()
return string.format("<view(%s) %s>", self:name(), tostring(self:bounds()))
end
-- return module
return view
|