From db81539967db330f749541356e98f1a6402e840d Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 5 May 2020 00:39:55 +0800 Subject: [PATCH] resize program --- src/ltui/panel.lua | 42 ++++++++-------- src/ltui/program.lua | 27 ++++++---- tests/dialog.lua | 116 +++++++++++++++++++++++++++++-------------- 3 files changed, 118 insertions(+), 67 deletions(-) diff --git a/src/ltui/panel.lua b/src/ltui/panel.lua index ba771fb..04aa422 100644 --- a/src/ltui/panel.lua +++ b/src/ltui/panel.lua @@ -89,17 +89,8 @@ function panel:view(name) return self._VIEWS_CACHE[name] end --- insert view -function panel:insert(v, opt) - - -- check - assert(not v:parent() or v:parent() == self) - assert(not self:view(v:name()), v:name() .. " has been in this panel!") - - -- this view has been inserted into this panel? remove it first - if v:parent() == self then - self:remove(v) - end +-- center view +function panel:center(v, opt) -- center this view if centerx or centery are set local bounds = v:bounds() @@ -117,6 +108,22 @@ function panel:insert(v, opt) bounds:move(org.x - bounds.sx, org.y - bounds.sy) v:invalidate(true) end +end + +-- insert view +function panel:insert(v, opt) + + -- check + assert(not v:parent() or v:parent() == self) + assert(not self:view(v:name()), v:name() .. " has been in this panel!") + + -- this view has been inserted into this panel? remove it first + if v:parent() == self then + self:remove(v) + end + + -- center this view if centerx or centery are set + self:center(v, opt) -- insert this view self._VIEWS:push(v) @@ -315,18 +322,13 @@ end -- resize panel function panel:resize() - -- resize panel? - local resize = self:state("resize") - if resize then - view.resize(self) - end + -- resize panel + view.resize(self) -- resize all child views for v in self:views() do - if resize then - v:state_set("resize", true) - end - if v:state("visible") and (v:state("resize") or v:type() == "panel") then + v:state_set("resize", true) + if v:state("visible") then v:resize() end end diff --git a/src/ltui/program.lua b/src/ltui/program.lua index 946e6f2..e8f3bf1 100644 --- a/src/ltui/program.lua +++ b/src/ltui/program.lua @@ -18,11 +18,6 @@ -- @file program.lua -- ---[[ Console User Interface (cui) ]----------------------------------------- -Author: Tiago Dionizio (tiago.dionizio AT gmail.com) -$Id: program.lua 18 2007-06-21 20:43:52Z tngd $ ---------------------------------------------------------------------------]] - -- load modules local log = require("ltui/base/log") local rect = require("ltui/rect") @@ -162,10 +157,22 @@ function program:event_on(e) focused_view = parent end + -- do event + if e.type == event.ev_keyboard then + -- resize? + if e.key_name == "Resize" then + self:bounds_set(rect {0, 0, curses.columns(), curses.lines()}) + return true + -- refresh? + elseif e.key_name == "Refresh" then + self:invalidate() + return true + -- ctrl+c? quit program + elseif e.key_name == "CtrlC" then + self:send("cm_exit") + return true + end -- quit program? - if e.type == event.ev_keyboard and e.key_name == "CtrlC" then - self:send("cm_exit") - return true elseif event.is_command(e, "cm_exit") then self:quit() return true @@ -220,7 +227,9 @@ function program:loop(argv) end -- resize views - self:resize() + if self:state("resize") then + self:resize() + end -- draw views self:draw() diff --git a/tests/dialog.lua b/tests/dialog.lua index a6c18a4..7bf149b 100644 --- a/tests/dialog.lua +++ b/tests/dialog.lua @@ -46,49 +46,89 @@ function demo:init() -- init background self:background_set("blue") - -- read help content - local helptext = nil - local file = io.open("./LICENSE.md", 'r') - if file then - helptext = file:read("*a") - file:close() - end - - -- init help dialog - local dialog_help = textdialog:new("dialog.help", rect {1, 1, self:width() - 1, self:height() - 1}, "README") - if helptext then - dialog_help:text():text_set(helptext) - end - dialog_help:button_add("exit", "< Exit >", function (v) self:remove(dialog_help) end) - -- init main dialog - local dialog_main = boxdialog:new("dialog.main", rect {1, 1, self:width() - 1, self:height() - 1}, "main dialog") - dialog_main:text():text_set("The project focuses on making development and building easier and provides many features (.e.g package, install, plugin, macro, action, option, task ...), so that any developer can quickly pick it up and enjoy the productivity boost when developing and building project.") - dialog_main:button_add("tips", "< Tips >", function (v) self:view("dialog.tips"):show(true, {focused = true}) end) - dialog_main:button_add("input", "< Input >", function (v) self:view("dialog.input"):show(true, {focused = true}) end) - dialog_main:button_add("help", "< Help >", function (v) self:insert(dialog_help) end) - dialog_main:button_add("quit", "< Quit >", "cm_quit") - self:insert(dialog_main) + self:insert(self:dialog_main()) -- init input dialog - local dialog_input = inputdialog:new("dialog.input", rect {0, 0, 50, 8}):background_set(dialog_main:frame():background()) - dialog_input:frame():background_set("cyan") - dialog_input:text():text_set("please input text:"):textattr_set("red") - dialog_input:button_add("no", "< No >", function (v) dialog_input:show(false) end) - dialog_input:button_add("yes", "< Yes >", function (v) - dialog_main:text():text_set(dialog_input:textedit():text()) - dialog_input:show(false) - end) - self:insert(dialog_input, {centerx = true, centery = true}) - dialog_input:show(false) + self:insert(self:dialog_input(), {centerx = true, centery = true}) -- init tips dialog - local dialog_tips = textdialog:new("dialog.tips", rect {0, 0, 50, 8}):background_set(dialog_main:frame():background()) - dialog_tips:frame():background_set("cyan") - dialog_tips:text():text_set("hello ltui! (https://tboox.org)\nA cross-platform terminal ui library based on Lua"):textattr_set("red") - dialog_tips:button_add("yes", "< Yes >", function (v) dialog_tips:show(false) end) - dialog_tips:button_add("no", "< No >", function (v) dialog_tips:show(false) end) - self:insert(dialog_tips, {centerx = true, centery = true}) + self:insert(self:dialog_tips(), {centerx = true, centery = true}) +end + +-- main dialog +function demo:dialog_main() + local dialog_main = self._DIALOG_MAIN + if not dialog_main then + dialog_main = boxdialog:new("dialog.main", rect {1, 1, self:width() - 1, self:height() - 1}, "main dialog") + dialog_main:text():text_set("The project focuses on making development and building easier and provides many features (.e.g package, install, plugin, macro, action, option, task ...), so that any developer can quickly pick it up and enjoy the productivity boost when developing and building project.") + dialog_main:button_add("tips", "< Tips >", function (v) self:view("dialog.tips"):show(true, {focused = true}) end) + dialog_main:button_add("input", "< Input >", function (v) self:view("dialog.input"):show(true, {focused = true}) end) + dialog_main:button_add("help", "< Help >", function (v) self:insert(self:dialog_help()) end) + dialog_main:button_add("quit", "< Quit >", "cm_quit") + self._DIALOG_MAIN = dialog_main + end + return dialog_main +end + +-- help dialog +function demo:dialog_help() + local dialog_help = self._DIALOG_HELP + if not dialog_help then + dialog_help = textdialog:new("dialog.help", rect {1, 1, self:width() - 1, self:height() - 1}, "README") + local helptext = nil + local file = io.open("./LICENSE.md", 'r') + if file then + helptext = file:read("*a") + file:close() + end + if helptext then + dialog_help:text():text_set(helptext) + end + dialog_help:button_add("exit", "< Exit >", function (v) self:remove(dialog_help) end) + self._DIALOG_HELP = dialog_help + end + return dialog_help +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, 50, 8}):background_set(self:dialog_main():frame():background()) + dialog_input:frame():background_set("cyan") + dialog_input:text():text_set("please input text:"):textattr_set("red") + dialog_input:button_add("no", "< No >", function (v) dialog_input:show(false) end) + dialog_input:button_add("yes", "< Yes >", function (v) + self:dialog_main():text():text_set(dialog_input:textedit():text()) + dialog_input:show(false) + end) + dialog_input:show(false) + self._DIALOG_INPUT = dialog_input + end + return dialog_input +end + +-- tips dialog +function demo:dialog_tips() + local dialog_tips = self._DIALOG_TIPS + if not dialog_tips then + dialog_tips = textdialog:new("dialog.tips", rect {0, 0, 50, 8}):background_set(self:dialog_main():frame():background()) + dialog_tips:frame():background_set("cyan") + dialog_tips:text():text_set("hello ltui! (https://tboox.org)\nA cross-platform terminal ui library based on Lua"):textattr_set("red") + dialog_tips:button_add("yes", "< Yes >", function (v) dialog_tips:show(false) end) + dialog_tips:button_add("no", "< No >", function (v) dialog_tips:show(false) end) + self._DIALOG_TIPS = dialog_tips + end + return dialog_tips +end + +-- resize dialog +function demo:resize() + self:dialog_main():bounds_set(rect {1, 1, self:width() - 1, self:height() - 1}) + self:center(self:dialog_input(), {centerx = true, centery = true}) + self:center(self:dialog_tips(), {centerx = true, centery = true}) + application.resize(self) end -- run demo