resize dialog

This commit is contained in:
ruki
2020-05-05 23:44:53 +08:00
parent 1dc02e058d
commit 60f41eec71
7 changed files with 82 additions and 27 deletions

View File

@@ -40,6 +40,7 @@ end
action:register("ac_max", action:register("ac_max",
"ac_on_text_changed", "ac_on_text_changed",
"ac_on_selected", "ac_on_selected",
"ac_on_resized",
"ac_on_enter", "ac_on_enter",
"ac_on_load", "ac_on_load",
"ac_on_save", "ac_on_save",

View File

@@ -72,8 +72,8 @@ end
-- on resize -- on resize
function boxdialog:on_resize() function boxdialog:on_resize()
textdialog.on_resize(self)
self:box():bounds_set(rect{0, 3, self:panel():width(), self:panel():height() - 1}) self:box():bounds_set(rect{0, 3, self:panel():width(), self:panel():height() - 1})
textdialog.on_resize(self)
end end
-- return module -- return module

View File

@@ -32,6 +32,25 @@ local curses = require("ltui/curses")
-- define module -- define module
local dialog = dialog or window() 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 -- init dialog
function dialog:init(name, bounds, title) function dialog:init(name, bounds, title)
@@ -40,6 +59,10 @@ function dialog:init(name, bounds, title)
-- insert buttons -- insert buttons
self:panel():insert(self: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 end
-- get buttons -- get buttons
@@ -65,19 +88,7 @@ function dialog:button_add(name, text, command)
self:buttons():insert(btn) self:buttons():insert(btn)
-- update the position of all buttons -- update the position of all buttons
local index = 1 self:_update_buttons_layout()
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
-- invalidate -- invalidate
self:invalidate() self:invalidate()
@@ -113,7 +124,6 @@ end
-- on resize -- on resize
function dialog:on_resize() function dialog:on_resize()
window.on_resize(self) window.on_resize(self)
self:buttons():bounds_set(rect:new(0, self:panel():height() - 1, self:panel():width(), 1))
end end
-- return module -- return module

View File

@@ -59,6 +59,11 @@ function inputdialog:init(name, bounds, title)
end end
end 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 end
-- get textedit -- get textedit
@@ -69,5 +74,10 @@ function inputdialog:textedit()
return self._TEXTEDIT return self._TEXTEDIT
end end
-- on resize
function inputdialog:on_resize()
textdialog.on_resize(self)
end
-- return module -- return module
return inputdialog return inputdialog

View File

@@ -25,6 +25,7 @@ local event = require("ltui/event")
local dialog = require("ltui/dialog") local dialog = require("ltui/dialog")
local curses = require("ltui/curses") local curses = require("ltui/curses")
local textarea = require("ltui/textarea") local textarea = require("ltui/textarea")
local action = require("ltui/action")
-- define module -- define module
local textdialog = textdialog or dialog() local textdialog = textdialog or dialog()
@@ -40,6 +41,11 @@ function textdialog:init(name, bounds, title)
-- select buttons by default -- select buttons by default
self:panel():select(self:buttons()) 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 end
-- get text -- get text
@@ -67,7 +73,6 @@ end
-- on resize -- on resize
function textdialog:on_resize() function textdialog:on_resize()
dialog.on_resize(self) dialog.on_resize(self)
self:text():bounds_set(rect:new(0, 0, self:panel():width(), self:panel():height() - 1))
end end
-- return module -- return module

View File

@@ -20,11 +20,13 @@
-- load modules -- load modules
local log = require("ltui/base/log") local log = require("ltui/base/log")
local table = require("ltui/base/table")
local rect = require("ltui/rect") local rect = require("ltui/rect")
local point = require("ltui/point") local point = require("ltui/point")
local object = require("ltui/object") local object = require("ltui/object")
local canvas = require("ltui/canvas") local canvas = require("ltui/canvas")
local curses = require("ltui/curses") local curses = require("ltui/curses")
local action = require("ltui/action")
-- define module -- define module
local view = view or object() local view = view or object()
@@ -232,6 +234,9 @@ function view:on_resize()
-- clear mark -- clear mark
self:state_set("resize", false) self:state_set("resize", false)
-- do action
self:action_on(action.ac_on_resized)
end end
-- show view? -- show view?
@@ -349,20 +354,21 @@ function view:extra_set(name, value)
return self return self
end end
-- get action
function view:action(name)
return self._ACTIONS[name]
end
-- set action -- set action
function view:action_set(name, on_action) function view:action_set(name, on_action)
self._ACTIONS[name] = on_action self._ACTIONS[name] = on_action
return self return self
end 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 -- do action
function view:action_on(name, ...) function view:action_on(name, ...)
local on_action = self:action(name) local on_action = self._ACTIONS[name]
if on_action then if on_action then
if type(on_action) == "string" then if type(on_action) == "string" then
-- send command -- send command
@@ -372,6 +378,13 @@ function view:action_on(name, ...)
elseif type(on_action) == "function" then elseif type(on_action) == "function" then
-- do action script -- do action script
return on_action(self, ...) 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 end
end end

View File

@@ -45,11 +45,27 @@ function demo:init()
self:background_set("blue") self:background_set("blue")
-- init input dialog -- init input dialog
local dialog_input = inputdialog:new("dialog.input", rect {0, 0, 50, 8}) 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:text():text_set("please input text:")
dialog_input:button_add("no", "< No >", function (v) dialog_input:quit() end) dialog_input:button_add("no", "< No >", function (v) dialog_input:quit() end)
dialog_input:button_add("yes", "< Yes >", 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._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 end
-- run demo -- run demo