summaryrefslogtreecommitdiff
path: root/xmake/core/ui/view.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2020-05-07 22:45:25 +0800
committerruki <[email protected]>2020-05-07 10:32:31 +0800
commitca238c29d3e14b0c72787b47588b979356dd9410 (patch)
treeaccec9b30f7b057b9bfbf9cf9e5985d5060232db /xmake/core/ui/view.lua
parent5d22635894f2b19f49441e0671ac0eb1b1da9c17 (diff)
update ui
Diffstat (limited to 'xmake/core/ui/view.lua')
-rw-r--r--xmake/core/ui/view.lua76
1 files changed, 48 insertions, 28 deletions
diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua
index 2501918e7..fbe149e3b 100644
--- a/xmake/core/ui/view.lua
+++ b/xmake/core/ui/view.lua
@@ -19,12 +19,14 @@
--
-- load modules
+local table = require("base/table")
local log = require("ui/log")
local rect = require("ui/rect")
local point = require("ui/point")
local object = require("ui/object")
local canvas = require("ui/canvas")
local curses = require("ui/curses")
+local action = require("ui/action")
-- define module
local view = view or object()
@@ -59,8 +61,8 @@ function view:init(name, bounds)
state.selected = false -- is selected?
state.focused = false -- is focused?
state.redraw = true -- need redraw
- state.refresh = true -- need refresh
- state.resize = true -- need resize
+ state.on_refresh = true -- need refresh
+ state.on_resize = true -- need resize
self._STATE = state
-- init options
@@ -91,8 +93,8 @@ end
function view:exit()
-- close window
- if self:window() then
- self:window():close()
+ if self._WINDOW then
+ self._WINDOW:close()
self._WINDOW = nil
end
end
@@ -154,6 +156,15 @@ end
-- get the view window
function view:window()
+ if not self._WINDOW then
+
+ -- create window
+ self._WINDOW = curses.new_pad(self:height() > 0 and self:height() or 1, self:width() > 0 and self:width() or 1)
+ assert(self._WINDOW, "cannot create window!")
+
+ -- disable cursor
+ self._WINDOW:leaveok(true)
+ end
return self._WINDOW
end
@@ -166,10 +177,10 @@ function view:canvas()
end
-- draw view
-function view:draw(transparent)
+function view:on_draw(transparent)
-- trace
- log:print("%s: draw ..", self)
+ log:print("%s: draw (transparent: %s) ..", self, tostring(transparent))
-- draw background
if not transparent then
@@ -188,7 +199,7 @@ function view:draw(transparent)
end
-- refresh view
-function view:refresh()
+function view:on_refresh()
-- refresh to the parent view
local parent = self:parent()
@@ -209,34 +220,30 @@ function view:refresh()
end
-- resize bounds of inner child views (abstract)
-function view:resize()
+function view:on_resize()
-- trace
log:print("%s: resize ..", self)
-- close the previous windows first
- if self:window() then
- self:window():close()
+ if self._WINDOW then
+ self._WINDOW:close()
self._WINDOW = nil
end
-- need renew canvas
self._CANVAS = nil
- -- create a new window
- self._WINDOW = curses.new_pad(self:height() > 0 and self:height() or 1, self:width() > 0 and self:width() or 1)
- assert(self._WINDOW, "cannot create window!")
-
- -- disable cursor
- self:window():leaveok(true)
-
-- clear mark
self:state_set("resize", false)
+
+ -- do action
+ self:action_on(action.ac_on_resized)
end
-- show view?
--
--- e.g.
+-- .e.g
-- v:show(false)
-- v:show(true, {focused = true})
--
@@ -265,7 +272,7 @@ end
--
-- @return true: done and break dispatching, false/nil: continous to dispatch to other views
--
-function view:event_on(e)
+function view:on_event(e)
end
-- get the current event
@@ -274,8 +281,8 @@ function view:event()
end
-- put an event to view
-function view:event_put(e)
- return self:parent() and self:parent():event_put(e)
+function view:put_event(e)
+ return self:parent() and self:parent():put_event(e)
end
-- get type
@@ -349,20 +356,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 +380,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
@@ -404,7 +419,7 @@ function view:background()
return background
end
--- set background, e.g. background_set("blue")
+-- set background, .e.g background_set("blue")
function view:background_set(color)
return self:attr_set("background", color)
end
@@ -427,6 +442,11 @@ function view:_mark_resize()
-- need resize it
self:state_set("resize", true)
+
+ -- @note we need trigger on_resize() of the root view and pass it to this subview
+ if self:parent() then
+ self:parent():invalidate(true)
+ end
end
-- need redraw view