summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-01-26 22:18:08 +0800
committerruki <[email protected]>2018-01-26 07:48:52 +0800
commit019c81967d55a77950d0249d6d5674bbdb193796 (patch)
tree7939fcaca646d5c61367eade1ec263a730f14312
parent7d775a7dc5dc064c629e63a7e06249cebbfc8e11 (diff)
improve string input dialog in mconfdialog
-rw-r--r--xmake/core/ui/mconfdialog.lua43
-rw-r--r--xmake/core/ui/menuconf.lua52
-rw-r--r--xmake/core/ui/view.lua4
3 files changed, 75 insertions, 24 deletions
diff --git a/xmake/core/ui/mconfdialog.lua b/xmake/core/ui/mconfdialog.lua
index a3e3f2652..8a6869862 100644
--- a/xmake/core/ui/mconfdialog.lua
+++ b/xmake/core/ui/mconfdialog.lua
@@ -23,14 +23,15 @@
--
-- load modules
-local log = require("ui/log")
-local rect = require("ui/rect")
-local event = require("ui/event")
-local action = require("ui/action")
-local curses = require("ui/curses")
-local window = require("ui/window")
-local menuconf = require("ui/menuconf")
-local boxdialog = require("ui/boxdialog")
+local log = require("ui/log")
+local rect = require("ui/rect")
+local event = require("ui/event")
+local action = require("ui/action")
+local curses = require("ui/curses")
+local window = require("ui/window")
+local menuconf = require("ui/menuconf")
+local boxdialog = require("ui/boxdialog")
+local inputdialog = require("ui/inputdialog")
-- define module
local mconfdialog = mconfdialog or boxdialog()
@@ -60,10 +61,32 @@ Pressing <Y> includes, <N> excludes. Enter <Esc> to go back or exit, <?> for Hel
-- 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)
+ -- init input dialog
+ local dialog_input = inputdialog:new("dialog.input", rect {0, 0, math.min(80, self:width()), math.min(8, self:height())}, "input dialog")
+ dialog_input:background_set(self:frame():background())
+ dialog_input:frame():background_set("cyan")
+ dialog_input:button_add("ok", "< Ok >", function (v)
+ local config = dialog_input:extra("config")
+ if config.kind == "string" then
+ config.value = dialog_input:textedit():text()
+ end
+ dialog_input:quit()
+ end)
+ dialog_input:button_add("help", "< Help >", function (v)
-- TODO
end)
+ dialog_input:button_select("ok")
+
+ -- on selected
+ self:menuconf():action_set(action.ac_on_selected, function (v, config)
+ if config.kind == "string" then
+ dialog_input:extra_set("config", config)
+ dialog_input:title():text_set(config:prompt())
+ dialog_input:textedit():text_set(config.value)
+ dialog_input:text():text_set("Please enter a string value. Use the <TAB> key to move from the input fields to buttons below it.")
+ self:insert(dialog_input, {centerx = true, centery = true})
+ end
+ end)
end
-- get menu config
diff --git a/xmake/core/ui/menuconf.lua b/xmake/core/ui/menuconf.lua
index ca4c772d6..370f59247 100644
--- a/xmake/core/ui/menuconf.lua
+++ b/xmake/core/ui/menuconf.lua
@@ -93,6 +93,9 @@ function menuconf:_do_insert(config)
-- attach this config
item:extra_set("config", config)
+ -- attach this view
+ config._view = item
+
-- insert this config item
self:insert(item)
end
@@ -108,19 +111,17 @@ function menuconf:_do_select()
-- get value
local value = config.value
- if value == nil then
- value = config.default
- end
-- select the boolean config
if config.kind == "boolean" then
- config.new = false
config.value = not value
- item:text_set(tostring(config))
end
-- do action: on selected
- self:action_on(action.ac_on_selected)
+ self:action_on(action.ac_on_selected, config)
+
+ -- clear new state
+ config.new = false
end
-- init config object
@@ -147,22 +148,49 @@ end
-- menu config
-- - {name = "...", kind = "menu", description = "menu config item", configs = {...}}
--
-local config = config or object {new = true}
+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}
--- to string
-function config:__tostring()
+-- 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
- if value == nil then
- value = self.default
- end
-- update text
if self.kind == "boolean" or (not self.kind and type(value) == "boolean") then -- boolean config?
diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua
index b49e53d66..f409fbd29 100644
--- a/xmake/core/ui/view.lua
+++ b/xmake/core/ui/view.lua
@@ -365,7 +365,7 @@ function view:action_set(name, on_action)
end
-- do action
-function view:action_on(name)
+function view:action_on(name, ...)
local on_action = self:action(name)
if on_action then
if type(on_action) == "string" then
@@ -375,7 +375,7 @@ function view:action_on(name)
end
elseif type(on_action) == "function" then
-- do action script
- on_action(self)
+ on_action(self, ...)
end
end
end