summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-01-12 00:39:22 +0800
committerruki <[email protected]>2018-01-11 21:56:13 +0800
commitb8276d35af753721b99e893d92485c99e2de111f (patch)
tree26f3228f129e671622e10e784aaa00ce85239dde
parent3d4fcf847c7a7f365f84d4c4064597c66e124d4c (diff)
add buttons to dialog
-rw-r--r--tests/ui/dialog.lua13
-rw-r--r--xmake/core/ui/button.lua2
-rw-r--r--xmake/core/ui/dialog.lua50
-rw-r--r--xmake/core/ui/event.lua2
-rw-r--r--xmake/core/ui/panel.lua13
-rw-r--r--xmake/core/ui/rect.lua11
6 files changed, 85 insertions, 6 deletions
diff --git a/tests/ui/dialog.lua b/tests/ui/dialog.lua
index 7700eae8c..bf4f0ca0a 100644
--- a/tests/ui/dialog.lua
+++ b/tests/ui/dialog.lua
@@ -44,10 +44,19 @@ function demo:init()
self:background_set("blue")
-- init main dialog
--- self:insert(dialog:new("dialog.main", rect {1, 1, self:width() - 1, self:height() - 1}, "main dialog"))
+ local dialog_main = dialog:new("dialog.main", rect {1, 1, self:width() - 1, self:height() - 1}, "main dialog")
+ dialog_main:button_add("ok", "< OK >", "cm_ok")
+ dialog_main:button_add("cancel", "< Cancel >", "cm_cancel")
+ dialog_main:button_add("help", "< Help >", "cm_help")
+ dialog_main:button_add("quit", "< Quit >", "cm_quit")
+ self:insert(dialog_main)
-- init hello dialog
- self:insert(dialog:new("dialog.hello", rect {0, 0, self:width() / 2, self:height() / 4}), {centerx = true, centery = true})
+ local dialog_hello = dialog:new("dialog.hello", rect {0, 0, self:width() / 2, self:height() / 4}):background_set(dialog_main:frame():background())
+ dialog_hello:frame():background_set("green")
+ dialog_hello:button_add("yes", "< Yes >", "cm_yes")
+ dialog_hello:button_add("no", "< No >", "cm_no")
+ self:insert(dialog_hello, {centerx = true, centery = true})
end
-- main entry
diff --git a/xmake/core/ui/button.lua b/xmake/core/ui/button.lua
index 0464c8617..b78c0bc11 100644
--- a/xmake/core/ui/button.lua
+++ b/xmake/core/ui/button.lua
@@ -31,7 +31,7 @@ local curses = require("ui/curses")
local button = button or label()
-- init button
-function button:init(name, bounds, text)
+function button:init(name, bounds, text, command)
-- init label
label.init(self, name, bounds, text)
diff --git a/xmake/core/ui/dialog.lua b/xmake/core/ui/dialog.lua
index 6a48046a1..f978bc56f 100644
--- a/xmake/core/ui/dialog.lua
+++ b/xmake/core/ui/dialog.lua
@@ -40,13 +40,59 @@ function dialog:init(name, bounds, title)
-- init window
window.init(self, name, bounds, title, true)
+ -- insert buttons
+ self:panel():insert(self:buttons())
+
-- init buttons
- local button_yes = button:new("dialog.button.yes", rect:new(0, self:panel():height() - 1, 7, 1), "< Yes >")
- self:panel():insert(button_yes, {centerx = true})
+-- local button_yes = button:new("dialog.button.yes", rect:new(0, self:panel():height() - 1, 7, 1), "< Yes >")
+-- self:panel():insert(button_yes, {centerx = true})
-- local button_no = button:new("dialog.button.no", rect:new(0, self:panel():height() - 1, 7, 1), "< No >")
-- self:panel():insert(button_no, {centerx = true})
end
+-- get buttons
+function dialog:buttons()
+ if not self._BUTTONS then
+ self._BUTTONS = panel:new("dialog.buttons", rect:new(0, self:panel():height() - 1, self:panel():width(), 1))
+ end
+ return self._BUTTONS
+end
+
+-- get button from the given button name
+function dialog:button(name)
+ return self:buttons():view(name)
+end
+
+-- add button
+function dialog:button_add(name, text, command)
+
+ -- init button
+ local btn = button:new(name, rect:new(0, 0, #text, 1), text, command)
+
+ -- insert button
+ 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 / 6)
+ 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)
+ index = index + 1
+ end
+
+ -- invalidate
+ self:invalidate()
+
+ -- ok
+ return btn
+end
+
-- return module
return dialog
diff --git a/xmake/core/ui/event.lua b/xmake/core/ui/event.lua
index a8d57f61c..411cf9e40 100644
--- a/xmake/core/ui/event.lua
+++ b/xmake/core/ui/event.lua
@@ -70,7 +70,7 @@ end
event:register("ev_max", "ev_keyboard", "ev_mouse", "ev_command", "ev_idle")
-- register command event types (ev_command)
-event:register("cm_max", "cm_quit")
+event:register("cm_max", "cm_quit", "cm_ok", "cm_cancel", "cm_yes", "cm_no", "cm_help")
-- define keyboard event
--
diff --git a/xmake/core/ui/panel.lua b/xmake/core/ui/panel.lua
index d9a6f8c4c..6576c0b24 100644
--- a/xmake/core/ui/panel.lua
+++ b/xmake/core/ui/panel.lua
@@ -48,6 +48,9 @@ function panel:init(name, bounds)
-- init child views
self._VIEWS = dlist()
+
+ -- init views cache
+ self._VIEWS_CACHE = {}
end
-- get all child views
@@ -85,11 +88,17 @@ function panel:current()
return self._CURRENT
end
+-- get view from the given name
+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()), "%s has been in this panel!", v)
-- this view has been inserted into this panel? remove it first
if v:parent() == self then
@@ -111,6 +120,9 @@ function panel:insert(v, opt)
-- insert this view
self._VIEWS:push(v)
+ -- cache this view
+ self._VIEWS_CACHE[v:name()] = v
+
-- set it's parent view
v:parent_set(self)
@@ -131,6 +143,7 @@ function panel:remove(v)
-- remove view
self._VIEWS:remove(v)
+ self._VIEWS_CACHE[v:name()] = nil
-- select next view
if self:current() == v then
diff --git a/xmake/core/ui/rect.lua b/xmake/core/ui/rect.lua
index 623dc8ee6..c10d54377 100644
--- a/xmake/core/ui/rect.lua
+++ b/xmake/core/ui/rect.lua
@@ -54,6 +54,17 @@ function rect:move(dx, dy)
return self
end
+-- move rect to the given position
+function rect:move2(x, y)
+ local w = self.ex - self.sx
+ local h = self.ey - self.sy
+ self.sx = x
+ self.sy = y
+ self.ex = x + w
+ self.ey = y + h
+ return self
+end
+
-- move top right corner of the rect
function rect:moves(dx, dy)
self.sx = self.sx + dx