diff options
| author | ruki <[email protected]> | 2017-12-20 23:20:43 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-12-20 14:04:19 +0800 |
| commit | bf0ea773b29ca4a85430dcb5ab024ed27a8eeb0c (patch) | |
| tree | f21820ed08d80213c1049b50e1b99b07bc19f627 | |
| parent | 5924ef4adbe4b084c465bc9604254be60d147074 (diff) | |
fix on event for ui
| -rw-r--r-- | tests/ui/hello.lua | 13 | ||||
| -rw-r--r-- | xmake/core/ui/application.lua | 11 | ||||
| -rw-r--r-- | xmake/core/ui/event.lua | 7 | ||||
| -rw-r--r-- | xmake/core/ui/group.lua | 25 | ||||
| -rw-r--r-- | xmake/core/ui/menubar.lua | 11 | ||||
| -rw-r--r-- | xmake/core/ui/statusbar.lua | 28 | ||||
| -rw-r--r-- | xmake/core/ui/view.lua | 29 |
7 files changed, 91 insertions, 33 deletions
diff --git a/tests/ui/hello.lua b/tests/ui/hello.lua index 21bcd1b96..a069d015c 100644 --- a/tests/ui/hello.lua +++ b/tests/ui/hello.lua @@ -23,6 +23,9 @@ -- -- imports +import("core.ui.log") +import("core.ui.view") +import("core.ui.event") import("core.ui.application") -- the hello application @@ -35,7 +38,15 @@ function hello:init() application.init(self, "hello") -- init title - self:menubar():title_set("xmake") + self:menubar():title_set("Menu Bar (Hello)") +end + +-- on event +function hello:event_on(e) + view.event_on(self, e) + if e.type == event.ev_keyboard then + self:statusbar():info_set(e.key_name) + end end -- main entry diff --git a/xmake/core/ui/application.lua b/xmake/core/ui/application.lua index 9401728be..874864826 100644 --- a/xmake/core/ui/application.lua +++ b/xmake/core/ui/application.lua @@ -31,6 +31,7 @@ $Id: application.lua 18 2007-06-21 20:43:52Z tngd $ local os = require("base/os") local log = require("ui/log") local rect = require("ui/rect") +local event = require("ui/event") local curses = require("ui/curses") local program = require("ui/program") local desktop = require("ui/desktop") @@ -61,6 +62,9 @@ function application:init(name) self:insert(self:menubar()) self:insert(self:desktop()) + -- register event type + self:event_register(event.ev_keyboard) + -- trace log:print("<application: %s>: init ok", name) end @@ -99,10 +103,9 @@ function application:statusbar() return self._STATUSBAR end --- do event -function application:do_event(e) - program.do_event(self, e) - -- TODO +-- on event +function application:event_on(e) + program.event_on(self, e) end -- run application diff --git a/xmake/core/ui/event.lua b/xmake/core/ui/event.lua index 82beb7959..76d013fef 100644 --- a/xmake/core/ui/event.lua +++ b/xmake/core/ui/event.lua @@ -44,8 +44,11 @@ function event:register(tag, ...) self[tag] = base + n end --- register event types, event.ev_keyboard = 1, event.ev_mouse = 2, ... , event.ev_idle = 5, event.ev_max = 5 -event:register("ev_max", "ev_keyboard", "ev_mouse", "ev_command", "ev_broadcast", "ev_idle") +-- register event types, event.ev_keyboard = 1, event.ev_mouse = 2, ... , event.ev_idle = 4, event.ev_max = 4 +event:register("ev_max", "ev_keyboard", "ev_mouse", "ev_command", "ev_idle") + +-- register command event types (ev_command) +event:register("cm_max", "cm_quit") -- define keyboard event -- diff --git a/xmake/core/ui/group.lua b/xmake/core/ui/group.lua index a088ef7af..06f2a1864 100644 --- a/xmake/core/ui/group.lua +++ b/xmake/core/ui/group.lua @@ -183,9 +183,9 @@ function group:select(v) -- undo the current view first if self:state("focused") then - current:state_set('focused', false) + current:state_set("focused", false) end - current:state_set('selected', false) + current:state_set("selected", false) end -- update the current selected view @@ -241,9 +241,20 @@ function group:select_next(forward, start) end end --- do event -function group:do_event(e) - -- TODO +-- on event +function group: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_need(e) then + v:event_on(e) + end + end end -- execute group @@ -266,11 +277,11 @@ function group:execute() -- do event if e then - self:do_event(e) + self:event_on(e) sleep = false else -- do idle event - app:do_event(event.idle()) + app:event_on(event.idle()) end -- wait some time, 50ms diff --git a/xmake/core/ui/menubar.lua b/xmake/core/ui/menubar.lua index 1c770d64c..af0e2b593 100644 --- a/xmake/core/ui/menubar.lua +++ b/xmake/core/ui/menubar.lua @@ -42,7 +42,10 @@ function menubar:init(name, bounds) view.init(self, name, bounds) -- init title - self._TITLE = "" + self._TITLE = "Menu Bar" + + -- init color + self:attr_set("color", curses.color_pair("red", "white")) end -- exit menubar @@ -57,9 +60,9 @@ function menubar:draw() c:move(1, 0):write(self:title()) end --- do event -function menubar:do_event(e) - view.do_event(self, e) +-- en event +function menubar:event_on(e) + view.event_on(self, e) end -- get title diff --git a/xmake/core/ui/statusbar.lua b/xmake/core/ui/statusbar.lua index 564ac4a15..0c00c730c 100644 --- a/xmake/core/ui/statusbar.lua +++ b/xmake/core/ui/statusbar.lua @@ -28,15 +28,22 @@ $Id: statusbar.lua 18 2007-06-21 20:43:52Z tngd $ --------------------------------------------------------------------------]] -- load modules +local log = require("ui/log") local view = require("ui/view") +local event = require("ui/event") local curses = require("ui/curses") -- define module local statusbar = statusbar or view() -- init statusbar -function statusbar:init(name, bounds) +function statusbar:init(name, bounds, commands) + + -- init view view.init(self, name, bounds) + + -- init info + self:info_set("") end -- exit statusbar @@ -57,11 +64,24 @@ function statusbar:draw() if x < self:width() then c:attr(color):write(string.rep(' ', self:width() - x)) end + + -- draw status info + c:move(1, 0):write(self:info()) +end + +-- on event +function statusbar:event_on(e) + view.event_on(self, e) +end + +-- get status info +function statusbar:info() + return self._INFO end --- do event -function statusbar:do_event(e) - view.do_event(self, e) +-- set status info +function statusbar:info_set(info) + self._INFO = info or "" end -- return module diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua index 6efb3c525..5c32f59e2 100644 --- a/xmake/core/ui/view.lua +++ b/xmake/core/ui/view.lua @@ -40,13 +40,13 @@ local view = view or object() local screen_lock = 0 -- new view instance -function view:new(name, bounds) +function view:new(name, bounds, ...) -- create instance self = self() -- init view - self:init(name, bounds) + self:init(name, bounds, ...) -- done return self @@ -61,23 +61,16 @@ function view:init(name, bounds) -- init state local state = object() state.visible = false -- view visibility - state.cursor_visible = false -- cursor visibility - state.block_cursor = false -- block cursor state.selected = false -- current selected window inside group state.focused = false -- true if parent is also focused - state.disabled = false -- view disabled - state.modal = false -- is modal view? self._STATE = state -- 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 - options.pre_event = false -- receive event before focused window - options.post_event = false -- receive event after focused window options.centerx = false -- center horizontaly when inserting in parent options.centery = false -- center verticaly when inserting in parent - options.validate = false -- validate self._OPTIONS = options -- init attributes @@ -85,6 +78,10 @@ function view:init(name, bounds) attrs.color = curses.color_pair("blue", "white") self._ATTRS = attrs + -- init needed events type + local events = object() + self._EVENTS = events + -- init name self._NAME = name @@ -228,8 +225,8 @@ function view:show(visible) end end --- do event (abstract) -function view:do_event(e) +-- on event (abstract) +function view:event_on(e) end -- get the current event @@ -242,6 +239,16 @@ function view:event_put(e) return self:parent() and self:parent():event_put(e) end +-- need this event? +function view:event_need(e) + return self._EVENTS[e.type] +end + +-- register an event type +function view:event_register(etype) + self._EVENTS[etype] = true +end + -- get state function view:state(name) return self._STATE[name] |
