summaryrefslogtreecommitdiff
path: root/xmake/core/ui/menuconf.lua
blob: cf68a1450fc53c68bce2fde88133962d8234dffa (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
--!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        menuconf.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 menuconf = menuconf or panel()

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

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

    -- init configs
    self._CONFIGS = {}
end

-- on event
function menuconf:event_on(e)
 
    -- select config
    local back = false
    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
        elseif e.key_name:lower() == "y" then
            self:_do_include(true)
            return true
        elseif e.key_name:lower() == "n" then
            self:_do_include(false)
            return true
        elseif e.key_name == "Esc" then
            back = true
        end
    elseif e.type == event.ev_command then
        if e.command == "cm_enter" then
            self:_do_select()
            return true
        elseif e.command == "cm_back" then
            back = true
        end
    end  

    -- back?
    if back then
        -- load the previous menu configs
        local configs_prev = self._CONFIGS._PREV
        if configs_prev then
            self._CONFIGS._PREV = configs_prev._PREV
            self:load(configs_prev)
            return true
        end
    end
end

-- load configs
function menuconf:load(configs)

    -- clear the views first
    self:clear()

    -- detach the previous config and view
    local configs_prev = self._CONFIGS._PREV
    if configs_prev then
        for _, config in ipairs(configs_prev) do
            config._view = nil
        end
    end

    -- insert configs
    self._CONFIGS = configs
    for _, config in ipairs(configs) do
        if self:count() < self:height() then
            self:_do_insert(config)
        end
    end

    -- select the first item
    self:select(self:first())

    -- invalidate
    self:invalidate()
end

-- do insert a config item
function menuconf:_do_insert(config)

    -- init a config item view
    local item = button:new("menuconf.config." .. self:count(), rect:new(0, self:count(), self:width(), 1), tostring(config))

    -- attach this config
    item:extra_set("config", config)

    -- attach this view
    config._view = item

    -- insert this config item
    self:insert(item)
end

-- do select the current config
function menuconf:_do_select()

    -- get the current item
    local item = self:current()
    
    -- get the current config
    local config = item:extra("config")

    -- clear new state    
    config.new = false

    -- do action: on selected
    if self:action_on(action.ac_on_selected, config) then
        return 
    end

    -- select the boolean config 
    if config.kind == "boolean" then
        config.value = not config.value
    -- show sub-menu configs
    elseif config.kind == "menu" and config.configs and #config.configs > 0 then
        local configs_prev = self._CONFIGS
        self:load(config.configs)
        self._CONFIGS._PREV = configs_prev
    end
end

-- do include
function menuconf:_do_include(enabled)

    -- get the current item
    local item = self:current()
    
    -- get the current config
    local config = item:extra("config")

    -- clear new state    
    config.new = false

    -- select the boolean config 
    if config.kind == "boolean" then
        config.value = enabled
    end
end

-- init config object
--
-- kind
--  - {kind = "number/boolean/string/choice/menu"}
--
-- description
--  - {description = "config item description"}
--  - {description = {"config item description", "line2", "line3", "more description ..."}}
--
-- boolean config
--  - {name = "...", kind = "boolean", value = true, default = true, description = "boolean config item", new = true/false}
--
-- number config
--  - {name = "...", kind = "number", value = 10, default = 0, description = "number config item", new = true/false}
--
-- string config
--  - {name = "...", kind = "string", value = "xmake", default = "", description = "string config item", new = true/false}
--
-- choice config (value is index)
--  - {name = "...", kind = "choice", value = 1, default = 1, description = "choice config item", values = {2, 2, 3, 4, 5}}
--
-- menu config
--  - {name = "...", kind = "menu", description = "menu config item", configs = {...}}
--
local config = config or object{new = true, 
                                __index = function (tbl, key)
                                    if key == "value" then
                                        local val = rawget(tbl, "_value")
                                        if val == nil then
                                            val = rawget(tbl, "default")
                                        end
                                        return val
                                    end
                                    return rawget(tbl, key)
                                end,
                                __newindex = function (tbl, key, val)
                                    if key == "value" then
                                        key = "_value"
                                    end
                                    rawset(tbl, key, val)
                                    if key == "_value" then
                                        local v = rawget(tbl, "_view") -- update the config item text in view
                                        if v then
                                            v:text_set(tostring(tbl))
                                        end
                                    end
                                end} 

-- the prompt info
function config:prompt()

    -- get text (first line in description)
    local text = self.description or ""
    if type(text) == "table" then
        text = text[1] or ""
    end
    return text
end

-- to string
function config:__tostring()

    -- get text (first line in description)
    local text = self:prompt()

    -- get value
    local value = self.value
    
    -- update text
    if self.kind == "boolean" or (not self.kind and type(value) == "boolean") then -- boolean config?
        text = (value and "[*] " or "[ ] ") .. text
    elseif self.kind == "number" or (not self.kind and type(value) == "number") then -- number config?
        text = "    " .. text .. " (" .. tostring(value or 0) .. ")" 
    elseif self.kind == "string" or (not self.kind and type(value) == "string") then -- string config?
        text = "    " .. text .. " (" .. tostring(value or "") .. ")"
    elseif self.kind == "choice" then -- choice config?
        if self.values and #self.values > 0 then
            text = "    " .. text .. " (" .. tostring(self.values[value or 1]) .. ")" .. "  --->"
        else
            text = "    " .. text .. " ()  ----"
        end
    elseif self.kind == "menu" then -- menu config?
        text = "    " .. text .. (self.configs and #self.configs > 0 and "  --->" or "  ----")
    end

    -- new config?
    if self.new and self.kind ~= "choice" and self.kind ~= "menu" then
        text = text .. " (NEW)"
    end

    -- ok
    return text
end

-- save config objects
menuconf.config  = menuconf.config or config
menuconf.menu    = menuconf.menu or config { kind = "menu", configs = {} }
menuconf.number  = menuconf.number or config { kind = "number", default = 0 }
menuconf.string  = menuconf.string or config { kind = "string", default = "" }
menuconf.choice  = menuconf.choice or config { kind = "choice", default = 1, values = {} }
menuconf.boolean = menuconf.boolean or config { kind = "boolean", default = false }

-- return module
return menuconf