diff options
| author | ruki <[email protected]> | 2018-01-26 00:41:59 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-01-25 22:11:59 +0800 |
| commit | 2a0884e05eea1f1c274d52d4922485513b97e3e2 (patch) | |
| tree | 550324018f824152017d1b929e4d5d84249a1d18 | |
| parent | d537a382ef3575f888c75cddc7930c911c67a600 (diff) | |
add menuconf config objects
| -rw-r--r-- | tests/ui/mconfdialog.lua | 16 | ||||
| -rw-r--r-- | xmake/core/ui/mconfdialog.lua | 2 | ||||
| -rw-r--r-- | xmake/core/ui/menuconf.lua | 131 |
3 files changed, 77 insertions, 72 deletions
diff --git a/tests/ui/mconfdialog.lua b/tests/ui/mconfdialog.lua index 43cccd624..2968dacaf 100644 --- a/tests/ui/mconfdialog.lua +++ b/tests/ui/mconfdialog.lua @@ -29,6 +29,7 @@ import("core.ui.view") import("core.ui.label") import("core.ui.event") import("core.ui.action") +import("core.ui.menuconf") import("core.ui.mconfdialog") import("core.ui.application") @@ -48,13 +49,14 @@ function demo:init() local mconfdialog = mconfdialog:new("mconfdialog.main", rect {1, 1, self:width() - 1, self:height() - 1}, "menu config") mconfdialog:action_set(action.ac_on_exit, function (v) self:quit() end) mconfdialog:action_set(action.ac_on_load, function (v) - local menuconf = v:menuconf() - menuconf:config_insert({default = false, description = "boolean config item"}) - menuconf:config_insert({default = true, new = true, description = {"boolean config item2", "more"}}) - menuconf:config_insert({kind = "number", value = 6, default = 10, description = "number config item"}) - menuconf:config_insert({value = "x86_64", description = "string config item"}) - menuconf:config_insert({kind = "menu", description = "menu config item"}) - menuconf:config_insert({kind = "choice", value = 3, values = {1, 5, 6, 7}, description = "choice config item"}) + local configs = {} + table.insert(configs, menuconf.boolean {description = "boolean config item"}) + table.insert(configs, menuconf.boolean {default = true, new = false, description = {"boolean config item2", "more"}}) + table.insert(configs, menuconf.number {value = 6, default = 10, description = "number config item"}) + table.insert(configs, menuconf.string {value = "x86_64", description = "string config item"}) + table.insert(configs, menuconf.menu {description = "menu config item"}) + table.insert(configs, menuconf.choice {value = 3, values = {1, 5, 6, 7}, description = "choice config item"}) + v:menuconf():load(configs) end) mconfdialog:action_set(action.ac_on_save, function (v) log:print("save") end) self:insert(mconfdialog) diff --git a/xmake/core/ui/mconfdialog.lua b/xmake/core/ui/mconfdialog.lua index 2afe71ea4..a3e3f2652 100644 --- a/xmake/core/ui/mconfdialog.lua +++ b/xmake/core/ui/mconfdialog.lua @@ -62,7 +62,7 @@ Pressing <Y> includes, <N> excludes. Enter <Esc> to go back or exit, <?> for Hel -- on selected self:menuconf():action_set(action.ac_on_selected, function (v) - log:print("on_selected: %s", v:config_current() and v:_config_text(v:config_current()) or "null") + -- TODO end) end diff --git a/xmake/core/ui/menuconf.lua b/xmake/core/ui/menuconf.lua index 24c8b2f1b..f749a23e6 100644 --- a/xmake/core/ui/menuconf.lua +++ b/xmake/core/ui/menuconf.lua @@ -31,6 +31,7 @@ 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() @@ -64,7 +65,54 @@ function menuconf:event_on(e) end end --- insert a config item +-- load configs +function menuconf:load(configs) + + -- clear the views first + self:clear() + + -- insert configs + self._CONFIGS = configs + for _, config in ipairs(configs) do + self:_do_insert(config) + 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) + + -- insert this config item + self:insert(item) +end + +-- do select the current config +function menuconf:_do_select() + + -- TODO + + -- get the current config + local config = self:current() + if self:current() then + config = self:current():extra("config") + end + + -- do action: on selected + self:action_on(action.ac_on_selected) +end + +-- init config object -- -- kind -- - {kind = "number/boolean/string/choice/menu"} @@ -88,79 +136,35 @@ end -- menu config -- - {name = "...", kind = "menu", description = "menu config item", configs = {...}} -- -function menuconf:config_insert(config) - - -- init a config item view - local item = button:new("menuconf.config." .. self:count(), rect:new(0, self:count(), self:width(), 1), self:_config_text(config)) +local config = config or object {new = true} - -- attach this config - item:extra_set("config", config) - - -- insert this config item - self:insert(item) - - -- select the first item - self:select(self:first()) - - -- invalidate - self:invalidate() -end - --- get the current config -function menuconf:config_current() - - -- get the current view item - local item = panel.current(self) - if item then - return item:extra("config") - end -end - --- load configs -function menuconf:configs_load(configs) - - -- clear the views first - self:clear() - - -- save configs - self._CONFIGS = configs - - -- invalidate - self:invalidate() -end - --- get configs -function menuconf:configs() - return self._CONFIGS -end - --- get text from the given config -function menuconf:_config_text(config) +-- to string +function config:__tostring() -- get text (first line in description) - local text = config.description or "" + local text = self.description or "" if type(text) == "table" then text = text[1] or "" end -- get value - local value = config.value or config.default + local value = self.value or self.default -- update text - if config.kind == "boolean" or (not config.kind and type(value) == "boolean") then -- boolean config? + if self.kind == "boolean" or (not self.kind and type(value) == "boolean") then -- boolean config? text = (value and "[*] " or "[ ] ") .. text - elseif config.kind == "number" or (not config.kind and type(value) == "number") then -- number config? + elseif self.kind == "number" or (not self.kind and type(value) == "number") then -- number config? text = "(" .. tostring(value or 0) .. ") " .. text - elseif config.kind == "string" or (not config.kind and type(value) == "string") then -- string config? + elseif self.kind == "string" or (not self.kind and type(value) == "string") then -- string config? text = "(" .. tostring(value or "") .. ") " .. text - elseif config.kind == "choice" then -- choice config? + elseif self.kind == "choice" then -- choice config? text = " " .. text .. " (" .. tostring(value or "") .. ")" .. " --->" - elseif config.kind == "menu" then -- menu config? + elseif self.kind == "menu" then -- menu config? text = " " .. text .. " --->" end -- new config? - if config.new then + if self.new and self.kind ~= "choice" and self.kind ~= "menu" then text = text .. " (NEW)" end @@ -168,14 +172,13 @@ function menuconf:_config_text(config) return text end --- do select the current config -function menuconf:_do_select() - - -- TODO - - -- do action: on selected - self:action_on(action.ac_on_selected) -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", values = {} } +menuconf.boolean = menuconf.boolean or config { kind = "boolean", default = false } -- return module return menuconf |
