diff options
| author | ruki <[email protected]> | 2018-01-25 22:40:10 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-01-25 10:01:05 +0800 |
| commit | 0c2c2d7b393d63caceea975eca697dc8d6665a1c (patch) | |
| tree | ee13d1f8493b66ee5c35d010bb1bf4165735f8ee | |
| parent | 231320f8fc52bd03ab88f048a6e0efa027346c83 (diff) | |
on select item for menuconf
| -rw-r--r-- | tests/ui/mconfdialog.lua | 12 | ||||
| -rw-r--r-- | xmake/core/ui/action.lua | 1 | ||||
| -rw-r--r-- | xmake/core/ui/event.lua | 2 | ||||
| -rw-r--r-- | xmake/core/ui/mconfdialog.lua | 16 | ||||
| -rw-r--r-- | xmake/core/ui/menuconf.lua | 36 |
5 files changed, 54 insertions, 13 deletions
diff --git a/tests/ui/mconfdialog.lua b/tests/ui/mconfdialog.lua index a9f1e71b8..43cccd624 100644 --- a/tests/ui/mconfdialog.lua +++ b/tests/ui/mconfdialog.lua @@ -49,12 +49,12 @@ function demo:init() 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:insert({default = false, description = "boolean config item"}) - menuconf:insert({default = true, new = true, description = {"boolean config item2", "more"}}) - menuconf:insert({kind = "number", value = 6, default = 10, description = "number config item"}) - menuconf:insert({value = "x86_64", description = "string config item"}) - menuconf:insert({kind = "menu", description = "menu config item"}) - menuconf:insert({kind = "choice", value = 3, values = {1, 5, 6, 7}, description = "choice config item"}) + 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"}) end) mconfdialog:action_set(action.ac_on_save, function (v) log:print("save") end) self:insert(mconfdialog) diff --git a/xmake/core/ui/action.lua b/xmake/core/ui/action.lua index 2fcbbdf97..431a047f2 100644 --- a/xmake/core/ui/action.lua +++ b/xmake/core/ui/action.lua @@ -43,6 +43,7 @@ end -- register action enums action:register("ac_max", "ac_on_text_changed", + "ac_on_selected", "ac_on_enter", "ac_on_load", "ac_on_save", diff --git a/xmake/core/ui/event.lua b/xmake/core/ui/event.lua index 1e9060f30..bf32abe22 100644 --- a/xmake/core/ui/event.lua +++ b/xmake/core/ui/event.lua @@ -70,7 +70,7 @@ end event:register("ev_max", "ev_keyboard", "ev_mouse", "ev_command", "ev_text", "ev_idle") -- register command event types (ev_command) -event:register("cm_max", "cm_quit", "cm_exit") +event:register("cm_max", "cm_quit", "cm_exit", "cm_enter") -- define keyboard event -- diff --git a/xmake/core/ui/mconfdialog.lua b/xmake/core/ui/mconfdialog.lua index 392836f8e..2afe71ea4 100644 --- a/xmake/core/ui/mconfdialog.lua +++ b/xmake/core/ui/mconfdialog.lua @@ -47,7 +47,7 @@ Pressing <Y> includes, <N> excludes. Enter <Esc> to go back or exit, <?> for Hel ]]) -- init buttons - self:button_add("select", "< Select >", function (v, e) end) + self:button_add("select", "< Select >", function (v, e) self:menuconf():event_on(event.command {"cm_enter"}) end) self:button_add("exit", "< Exit >", function (v, e) self:quit() end) self:button_add("help", "< Help >", function (v, e) end) self:button_add("save", "< Save >", function (v, e) self:action_on(action.ac_on_save) end) @@ -56,6 +56,14 @@ Pressing <Y> includes, <N> excludes. Enter <Esc> to go back or exit, <?> for Hel -- insert menu config self:box():panel():insert(self:menuconf()) + + -- disable to select to box (disable Tab switch and only response to buttons) + self:box():option_set("selectable", false) + + -- 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") + end) end -- get menu config @@ -63,6 +71,7 @@ function mconfdialog:menuconf() if not self._MENUCONF then local bounds = self:box():panel():bounds() self._MENUCONF = menuconf:new("mconfdialog.menuconf", rect:new(math.floor(bounds:width() / 3), 0, bounds:width(), bounds:height())) + self._MENUCONF:state_set("focused", true) -- we can select and highlight selected item end return self._MENUCONF end @@ -76,6 +85,11 @@ function mconfdialog:event_on(e) self:action_on(action.ac_on_load) self._LOADED = true end + -- select config + elseif e.type == event.ev_keyboard then + if e.key_name == "Down" or e.key_name == "Up" or e.key_name == " " then + return self:menuconf():event_on(e) + end end -- TODO diff --git a/xmake/core/ui/menuconf.lua b/xmake/core/ui/menuconf.lua index db34e4b99..6bf34f81e 100644 --- a/xmake/core/ui/menuconf.lua +++ b/xmake/core/ui/menuconf.lua @@ -28,6 +28,7 @@ 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") @@ -53,7 +54,13 @@ function menuconf:event_on(e) 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 @@ -81,16 +88,16 @@ end -- menu config -- - {name = "...", kind = "menu", description = "menu config item", configs = {...}} -- -function menuconf:insert(config) +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:_text(config)) + local item = button:new("menuconf.config." .. self:count(), rect:new(0, self:count(), self:width(), 1), self:_config_text(config)) -- attach this config item:extra_set("config", config) -- insert this config item - panel.insert(self, item) + self:insert(item) -- select the first item self:select(self:first()) @@ -100,7 +107,7 @@ function menuconf:insert(config) end -- load configs -function menuconf:load(configs) +function menuconf:configs_load(configs) -- clear the views first self:clear() @@ -117,8 +124,18 @@ function menuconf:configs() return self._CONFIGS 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 + -- get text from the given config -function menuconf:_text(config) +function menuconf:_config_text(config) -- get text (first line in description) local text = config.description or "" @@ -151,5 +168,14 @@ function menuconf:_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 + -- return module return menuconf |
