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
|
--!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-2020, TBOOX Open Source Group.
--
-- @author ruki
-- @file choicebox.lua
--
-- load modules
local log = require("ui/log")
local view = require("ui/view")
local rect = require("ui/rect")
local panel = require("ui/panel")
local event = require("ui/event")
local action = require("ui/action")
local curses = require("ui/curses")
local button = require("ui/button")
local object = require("ui/object")
-- define module
local choicebox = choicebox or panel()
-- init choicebox
function choicebox:init(name, bounds)
-- init panel
panel.init(self, name, bounds)
-- init values
self._VALUES = {}
end
-- on event
function choicebox:on_event(e)
-- select config
if e.type == event.ev_keyboard then
if e.key_name == "Down" then
return self:select_next()
elseif e.key_name == "Up" then
return self:select_prev()
elseif e.key_name == "Enter" or e.key_name == " " then
self:_do_select()
return true
end
elseif e.type == event.ev_command and e.command == "cm_enter" then
self:_do_select()
return true
end
end
-- load values
function choicebox:load(values, selected)
-- clear the views first
self:clear()
-- insert values
self._VALUES = values
for idx, value in ipairs(values) do
self:_do_insert(value, idx, idx == selected)
end
-- select the first item
self:select(self:first())
-- invalidate
self:invalidate()
end
-- do insert a value item
function choicebox:_do_insert(value, index, selected)
-- init text
local text = (selected and "(X) " or "( ) ") .. tostring(value)
-- init a value item view
local item = button:new("choicebox.value." .. self:count(),
rect:new(0, self:count(), self:width(), 1),
text,
function (v, e)
self:_do_select()
end)
-- attach this index
item:extra_set("index", index)
-- insert this config item
self:insert(item)
end
-- do select the current config
function choicebox:_do_select()
-- clear selected text
for v in self:views() do
local text = v:text()
if text and text:startswith("(X) ") then
local i = v:extra("index")
if i then
local t = self._VALUES[i]
v:text_set("( ) " .. tostring(t))
end
end
end
-- get the current item
local item = self:current()
-- get the current index
local index = item:extra("index")
-- get the current value
local value = self._VALUES[index]
-- do action: on selected
self:action_on(action.ac_on_selected, index, value)
-- update text
item:text_set("(X) " .. tostring(value))
end
-- return module
return choicebox
|