resize dialog
This commit is contained in:
@@ -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",
|
||||
|
@@ -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
|
||||
|
@@ -32,6 +32,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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user