summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2018-01-16 00:25:44 +0800
committerruki <[email protected]>2018-01-15 20:34:26 +0800
commita36785d37fa91a238a3c5358f0dadb2f8de473d3 (patch)
tree767b3102892042cf7aac784bdf699732680dee6e
parenta5e736899e441c3c93e8bbece028ed16b98dbb1c (diff)
select and draw focused view
-rw-r--r--xmake/core/ui/button.lua28
-rw-r--r--xmake/core/ui/panel.lua41
-rw-r--r--xmake/core/ui/program.lua21
-rw-r--r--xmake/core/ui/view.lua7
4 files changed, 60 insertions, 37 deletions
diff --git a/xmake/core/ui/button.lua b/xmake/core/ui/button.lua
index 3d788b560..482dcef9a 100644
--- a/xmake/core/ui/button.lua
+++ b/xmake/core/ui/button.lua
@@ -45,7 +45,7 @@ function button:init(name, bounds, text, action)
self:cursor_show(true)
-- init action
- self:action_set("on_enter", action)
+ self:action_set("keyboard.Enter", action)
end
-- draw button
@@ -85,21 +85,27 @@ function button:event_on(e)
-- enter this button?
if e.type == event.ev_keyboard then
- if e.key_name == "Enter" then
- local on_enter = self:action("on_enter")
- if on_enter then
- if type(on_enter) == "string" then
- -- send command
- self:application():send(on_enter)
- elseif type(on_enter) == "function" then
- -- do enter script
- on_enter(self)
- end
+ local action = self:action("keyboard." .. e.key_name)
+ if action then
+ if type(action) == "string" then
+ -- send command
+ self:application():send(action)
+ elseif type(action) == "function" then
+ -- do action script
+ action(self, e)
end
return true
end
end
end
+-- set state
+function button:state_set(name, enable)
+ if name == "focused" and self:state(name) ~= enable then
+ self:invalidate()
+ end
+ return view.state_set(self, name, enable)
+end
+
-- return module
return button
diff --git a/xmake/core/ui/panel.lua b/xmake/core/ui/panel.lua
index f163ee483..17a35531e 100644
--- a/xmake/core/ui/panel.lua
+++ b/xmake/core/ui/panel.lua
@@ -177,18 +177,15 @@ function panel:select(v)
-- update the new selected view
if v then
- -- modify view order and mark this view as top
- if v:option("top_select") then
- self._VIEWS:remove(v)
- self._VIEWS:push(v)
- end
-
-- select and focus this view
v:state_set('selected', true)
if self:state("focused") then
v:state_set('focused', true)
end
end
+
+ -- ok
+ return v
end
-- select the next view
@@ -206,8 +203,7 @@ function panel:select_next(start)
local next = self:next(current)
while next and next ~= current do
if next:option("selectable") and next:state("visible") then
- self:select(next)
- break
+ return self:select(next)
end
next = self:next(next)
end
@@ -228,8 +224,7 @@ function panel:select_prev(start)
local prev = self:prev(current)
while prev and prev ~= current do
if prev:option("selectable") and prev:state("visible") then
- self:select(prev)
- break
+ return self:select(prev)
end
prev = self:prev(prev)
end
@@ -237,18 +232,24 @@ end
-- on event
function panel:event_on(e)
-
- -- is empty views?
- if self:empty() then
- return
- end
-
- -- send event to all child views
- for v in self:views() do
- if v:event_on(e) then
- return true
+
+ -- select view?
+ if e.type == event.ev_keyboard then
+ if e.key_name == "Right" or e.key_name == "Tab" then
+ return self:select_next()
+ elseif e.key_name == "Left" then
+ return self:select_prev()
end
+ end
+end
+
+-- set state
+function panel:state_set(name, enable)
+ view.state_set(self, name, enable)
+ if name == "focused" and self:current() then
+ self:current():state_set(name, enable)
end
+ return self
end
-- draw panel
diff --git a/xmake/core/ui/program.lua b/xmake/core/ui/program.lua
index 0fe44049b..82ada0bcf 100644
--- a/xmake/core/ui/program.lua
+++ b/xmake/core/ui/program.lua
@@ -80,6 +80,10 @@ function program:init(name)
-- init panel
panel.init(self, name, rect {0, 0, curses.columns(), curses.lines()})
+
+ -- init state
+ self:state_set("focused", true)
+ self:state_set("selected", true)
end
-- exit program
@@ -143,9 +147,22 @@ end
-- on event
function program:event_on(e)
- if panel.event_on(self, e) then
- return true
+
+ -- get the top focused view
+ local focused_view = self
+ while focused_view:type() == "panel" and focused_view:current() do
+ focused_view = focused_view:current()
end
+
+ -- do event for focused views
+ while focused_view and focused_view ~= self do
+ if focused_view:event_on(e) then
+ return true
+ end
+ focused_view = focused_view:parent()
+ end
+
+ -- quit program?
if e.type == event.ev_keyboard and (e.key_name == "Esc" or e.key_name == "CtrlC") then
self:quit()
return true
diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua
index a507ac483..d01682f91 100644
--- a/xmake/core/ui/view.lua
+++ b/xmake/core/ui/view.lua
@@ -60,8 +60,8 @@ function view:init(name, bounds)
state.visible = true -- view visibility
state.cursor_visible = false -- cursor visibility
state.block_cursor = false -- block cursor
- state.selected = false -- current selected window inside panel
- state.focused = false -- true if parent is also focused
+ 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
@@ -70,14 +70,13 @@ function view:init(name, bounds)
-- init options
local options = object()
options.selectable = false -- true if window can be selected
- options.top_select = false -- if true, selecting window will bring it to front
self._OPTIONS = options
-- init attributes
self._ATTRS = object()
-- init actions
- self._ACTIONS = object()
+ self._ACTIONS = object()
-- init name
self._NAME = name