summaryrefslogtreecommitdiff
path: root/xmake/core/ui/panel.lua
blob: b403f339d3fce5cc61c2238b1735d70dccf7803d (plain)
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
--!A cross-platform build utility based on Lua
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements.  See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership.  The ASF licenses this file
-- to you 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 - 2018, TBOOX Open Source Group.
--
-- @author      ruki
-- @file        panel.lua
--

-- load modules
local log    = require("ui/log")
local view   = require("ui/view")
local rect   = require("ui/rect")
local event  = require("ui/event")
local point  = require("ui/point")
local curses = require("ui/curses")
local dlist  = require("base/dlist")

-- define module
local panel = panel or view()

-- init panel
function panel:init(name, bounds)

    -- init view
    view.init(self, name, bounds)

    -- mark as panel
    self:type_set("panel")

    -- mark as selectable
    self:option_set("selectable", true)

    -- init child views
    self._VIEWS = dlist()

    -- init views cache
    self._VIEWS_CACHE = {}
end

-- get all child views
function panel:views()
    return self._VIEWS:items()
end

-- get views count
function panel:count()
    return self._VIEWS:size()
end

-- is empty?
function panel:empty()
    return self._VIEWS:empty()
end

-- get the first view
function panel:first()
    return self._VIEWS:first()
end

-- get the next view
function panel:next(v)
    return self._VIEWS:next(v)
end

-- get the previous view
function panel:prev(v)
    return self._VIEWS:prev(v)
end

-- get the current selected child view
function panel:current()
    return self._CURRENT
end

-- get view from the given name
function panel:view(name)
    return self._VIEWS_CACHE[name]
end

-- insert view
function panel:insert(v, opt)

    -- check
    assert(not v:parent() or v:parent() == self)
    assert(not self:view(v:name()), v:name() .. " has been in this panel!")

    -- this view has been inserted into this panel? remove it first
    if v:parent() == self then
        self:remove(v)
    end

    -- center this view if centerx or centery are set
    local bounds = v:bounds()
    local center = false
    local org = point {bounds.sx, bounds.sy}
    if opt and opt.centerx then
        org.x = math.floor((self:width() - v:width()) / 2)
        center = true
    end
    if opt and opt.centery then
        org.y = math.floor((self:height() - v:height()) / 2)
        center = true
    end
    if center then
        bounds:move(org.x - bounds.sx, org.y - bounds.sy)
        v:invalidate(true)
    end

    -- insert this view
    self._VIEWS:push(v)

    -- cache this view
    self._VIEWS_CACHE[v:name()] = v

    -- set it's parent view
    v:parent_set(self)

    -- select this view
    if v:option("selectable") then
        self:select(v)
    end

    -- invalidate it
    self:invalidate()
end

-- remove view
function panel:remove(v)

    -- check
    assert(v:parent() == self)

    -- remove view
    self._VIEWS:remove(v)
    self._VIEWS_CACHE[v:name()] = nil

    -- clear parent
    v:parent_set(nil)

    -- select next view
    if self:current() == v then
        self:select_next(nil, true)
    end

    -- invalidate it
    self:invalidate()
end

-- clear views
function panel:clear()

    -- clear parents
    for v in self:views() do
        v:parent_set(nil)
    end

    -- clear views and cache
    self._VIEWS:clear()
    self._VIEWS_CACHE = {}

    -- reset the current view
    self._CURRENT = nil

    -- invalidate
    self:invalidate()
end

-- select the child view
function panel:select(v)

    -- check
    assert(v == nil or (v:parent() == self and v:option("selectable")))

    -- get the current selected view
    local current = self:current()
    if v == current then 
        return 
    end

    -- undo the previous selected view
    if current then

        -- undo the current view first
        if self:state("focused") then
            current:state_set("focused", false)
        end
        current:state_set("selected", false)
    end

    -- update the current selected view
    self._CURRENT = v

    -- update the new selected view
    if v then

        -- select and focus this view
        v:state_set("selected", true)
        if self:state("focused") then
            v:state_set("focused", true)
        end
    end

    -- ok
    return v
end

-- select the next view
function panel:select_next(start, reset)

    -- is empty?
    if self:empty() then
        return 
    end

    -- reset?
    if reset then
        self._CURRENT = nil
    end

    -- get current view
    local current = start or self:current()

    -- select the next view
    local next = self:next(current)
    while next ~= current do
        if next and next:option("selectable") and next:state("visible") then
            return self:select(next)
        end
        next = self:next(next)
    end
end

-- select the previous view
function panel:select_prev(start)

    -- is empty?
    if self:empty() then
        return 
    end

    -- reset?
    if reset then
        self._CURRENT = nil
    end

    -- get current view
    local current = start or self:current()

    -- select the previous view
    local prev = self:prev(current)
    while prev ~= current do
        if prev and prev:option("selectable") and prev:state("visible") then
            return self:select(prev)
        end
        prev = self:prev(prev)
    end
end

-- on event
function panel:event_on(e)
 
    -- select view?
    if e.type == event.ev_keyboard then
        if e.key_name == "Right" then
            return self:select_next()
        elseif e.key_name == "Left" then
            return self:select_prev()
        end
    end   
end

-- set state
function panel:state_set(name, enable)
    view.state_set(self, name, enable)
    if name == "focused" and self:current() then
        self:current():state_set(name, enable)
    end
    return self
end

-- draw panel 
function panel:draw(transparent)

    -- redraw panel?
    local redraw = self:state("redraw")

    -- draw panel background first
    if redraw then
        view.draw(self, transparent)
    end

    -- draw all child views
    for v in self:views() do
        if redraw then
            v:state_set("redraw", true)
        end
        if v:state("visible") and (v:state("redraw") or v:type() == "panel") then
            v:draw(transparent)
        end
    end
end

-- resize panel 
function panel:resize()

    -- resize panel?
    local resize = self:state("resize")
    if resize then
        view.resize(self)
    end

    -- resize all child views
    for v in self:views() do
        if resize then
            v:state_set("resize", true)
        end
        if v:state("visible") and (v:state("resize") or v:type() == "panel") then
            v:resize()
        end
    end
end

-- refresh panel
function panel:refresh()

    -- need not refresh? do not refresh it
    if not self:state("refresh") or not self:state("visible") then
        return 
    end

    -- refresh all child views
    for v in self:views() do
        if v:state("refresh") then
            v:refresh()
            v:state_set("refresh", false)
        end
    end

    -- refresh it
    view.refresh(self)

    -- clear mark
    self:state_set("refresh", false)
end

-- dump all views
function panel:dump()
    log:print("%s", self:_tostring(1))
end

-- tostring(panel, level)
function panel:_tostring(level)
    local str = ""
    if self.views then  
        str = str .. string.format("<%s %s>", self:name(), tostring(self:bounds()))
        if not self:empty() then
            str = str .. "\n"
        end
        for v in self:views() do  
            for l = 1, level do
                str = str .. "    "
            end
            str = str .. panel._tostring(v, level + 1) .. "\n"
        end  
    else
        str = tostring(self)
    end
    return str
end

-- tostring(panel)
function panel:__tostring()
    return string.format("<panel(%s) %s>", self:name(), tostring(self:bounds()))
end


-- return module
return panel