diff --git a/src/ltui/action.lua b/src/ltui/action.lua index 3a785a0..961d280 100644 --- a/src/ltui/action.lua +++ b/src/ltui/action.lua @@ -40,6 +40,7 @@ end action:register("ac_max", "ac_on_text_changed", "ac_on_selected", + "ac_on_resized", "ac_on_enter", "ac_on_load", "ac_on_save", diff --git a/src/ltui/boxdialog.lua b/src/ltui/boxdialog.lua index 5952b4d..8b6c404 100644 --- a/src/ltui/boxdialog.lua +++ b/src/ltui/boxdialog.lua @@ -72,8 +72,8 @@ end -- on resize function boxdialog:on_resize() - textdialog.on_resize(self) self:box():bounds_set(rect{0, 3, self:panel():width(), self:panel():height() - 1}) + textdialog.on_resize(self) end -- return module diff --git a/src/ltui/dialog.lua b/src/ltui/dialog.lua index 2604d97..9104c00 100644 --- a/src/ltui/dialog.lua +++ b/src/ltui/dialog.lua @@ -31,6 +31,25 @@ local curses = require("ltui/curses") -- define module local dialog = dialog or window() + +-- update the position of all buttons +function dialog:_update_buttons_layout() + + -- update the position of all buttons + local index = 1 + local width = self:buttons():width() + local count = self:buttons():count() + local padding = math.floor(width / 8) + for v in self:buttons():views() do + local x = padding + index * math.floor((width - padding * 2) / (count + 1)) - math.floor(v:width() / 2) + if x + v:width() > width then + x = math.max(0, width - v:width()) + end + v:bounds():move2(x, 0) + v:invalidate(true) + index = index + 1 + end +end -- init dialog function dialog:init(name, bounds, title) @@ -40,6 +59,10 @@ function dialog:init(name, bounds, title) -- insert buttons self:panel():insert(self:buttons()) + self:panel():action_add(action.ac_on_resized, function (v) + self:buttons():bounds_set(rect:new(0, v:height() - 1, v:width(), 1)) + self:_update_buttons_layout() + end) end -- get buttons @@ -65,19 +88,7 @@ function dialog:button_add(name, text, command) self:buttons():insert(btn) -- update the position of all buttons - local index = 1 - local width = self:buttons():width() - local count = self:buttons():count() - local padding = math.floor(width / 8) - for v in self:buttons():views() do - local x = padding + index * math.floor((width - padding * 2) / (count + 1)) - math.floor(v:width() / 2) - if x + v:width() > width then - x = math.max(0, width - v:width()) - end - v:bounds():move2(x, 0) - v:invalidate(true) - index = index + 1 - end + self:_update_buttons_layout() -- invalidate self:invalidate() @@ -113,7 +124,6 @@ end -- on resize function dialog:on_resize() window.on_resize(self) - self:buttons():bounds_set(rect:new(0, self:panel():height() - 1, self:panel():width(), 1)) end -- return module diff --git a/src/ltui/inputdialog.lua b/src/ltui/inputdialog.lua index 49ea367..48cedac 100644 --- a/src/ltui/inputdialog.lua +++ b/src/ltui/inputdialog.lua @@ -59,6 +59,11 @@ function inputdialog:init(name, bounds, title) end end end) + + -- on resize for panel + self:panel():action_add(action.ac_on_resized, function (v) + self:textedit():bounds_set(rect{0, 1, v:width(), v:height() - 1}) + end) end -- get textedit @@ -69,5 +74,10 @@ function inputdialog:textedit() return self._TEXTEDIT end +-- on resize +function inputdialog:on_resize() + textdialog.on_resize(self) +end + -- return module return inputdialog diff --git a/src/ltui/textdialog.lua b/src/ltui/textdialog.lua index 48bb335..3f31093 100644 --- a/src/ltui/textdialog.lua +++ b/src/ltui/textdialog.lua @@ -25,6 +25,7 @@ local event = require("ltui/event") local dialog = require("ltui/dialog") local curses = require("ltui/curses") local textarea = require("ltui/textarea") +local action = require("ltui/action") -- define module local textdialog = textdialog or dialog() @@ -40,6 +41,11 @@ function textdialog:init(name, bounds, title) -- select buttons by default self:panel():select(self:buttons()) + + -- on resize for panel + self:panel():action_add(action.ac_on_resized, function (v) + self:text():bounds_set(rect:new(0, 0, v:width(), v:height() - 1)) + end) end -- get text @@ -67,7 +73,6 @@ end -- on resize function textdialog:on_resize() dialog.on_resize(self) - self:text():bounds_set(rect:new(0, 0, self:panel():width(), self:panel():height() - 1)) end -- return module diff --git a/src/ltui/view.lua b/src/ltui/view.lua index e9cf943..416ae34 100644 --- a/src/ltui/view.lua +++ b/src/ltui/view.lua @@ -20,11 +20,13 @@ -- load modules local log = require("ltui/base/log") +local table = require("ltui/base/table") local rect = require("ltui/rect") local point = require("ltui/point") local object = require("ltui/object") local canvas = require("ltui/canvas") local curses = require("ltui/curses") +local action = require("ltui/action") -- define module local view = view or object() @@ -232,6 +234,9 @@ function view:on_resize() -- clear mark self:state_set("resize", false) + + -- do action + self:action_on(action.ac_on_resized) end -- show view? @@ -349,20 +354,21 @@ function view:extra_set(name, value) return self end --- get action -function view:action(name) - return self._ACTIONS[name] -end - -- set action function view:action_set(name, on_action) self._ACTIONS[name] = on_action return self end +-- add action +function view:action_add(name, on_action) + self._ACTIONS[name] = table.join(table.wrap(self._ACTIONS[name]), on_action) + return self +end + -- do action function view:action_on(name, ...) - local on_action = self:action(name) + local on_action = self._ACTIONS[name] if on_action then if type(on_action) == "string" then -- send command @@ -372,6 +378,13 @@ function view:action_on(name, ...) elseif type(on_action) == "function" then -- do action script return on_action(self, ...) + elseif type(on_action) == "table" then + for _, on_action_val in ipairs(on_action) do + -- we cannot uses the return value of action for multi-actions + if type(on_action_val) == "function" then + on_action_val(self, ...) + end + end end end end diff --git a/tests/inputdialog.lua b/tests/inputdialog.lua index f772628..83c3a3e 100644 --- a/tests/inputdialog.lua +++ b/tests/inputdialog.lua @@ -45,11 +45,27 @@ function demo:init() self:background_set("blue") -- init input dialog - local dialog_input = inputdialog:new("dialog.input", rect {0, 0, 50, 8}) - dialog_input:text():text_set("please input text:") - dialog_input:button_add("no", "< No >", function (v) dialog_input:quit() end) - dialog_input:button_add("yes", "< Yes >", function (v) dialog_input:quit() end) - self:insert(dialog_input, {centerx = true, centery = true}) + self:insert(self:dialog_input(), {centerx = true, centery = true}) +end + +-- input dialog +function demo:dialog_input() + local dialog_input = self._DIALOG_INPUT + if not dialog_input then + dialog_input = inputdialog:new("dialog.input", rect{0, 0, math.floor(self:width() / 2), math.floor(self:height() / 3)}) + dialog_input:text():text_set("please input text:") + dialog_input:button_add("no", "< No >", function (v) dialog_input:quit() end) + dialog_input:button_add("yes", "< Yes >", function (v) dialog_input:quit() end) + self._DIALOG_INPUT = dialog_input + end + return dialog_input +end + +-- on resize +function demo:on_resize() + self:dialog_input():bounds_set(rect{0, 0, math.floor(self:width() / 2), math.floor(self:height() / 3)}) + self:center(self:dialog_input(), {centerx = true, centery = true}) + application.on_resize(self) end -- run demo