diff options
| author | ruki <[email protected]> | 2018-01-13 22:30:39 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-01-13 22:30:39 +0800 |
| commit | a5e736899e441c3c93e8bbece028ed16b98dbb1c (patch) | |
| tree | aa52fc5af6647cd0e53274a9a27a6f80840f09e5 | |
| parent | bb8a982842b1a447b3da54538a8b245888ccbd86 (diff) | |
add action to button and view
| -rw-r--r-- | xmake/core/ui/application.lua | 3 | ||||
| -rw-r--r-- | xmake/core/ui/button.lua | 32 | ||||
| -rw-r--r-- | xmake/core/ui/panel.lua | 3 | ||||
| -rw-r--r-- | xmake/core/ui/view.lua | 29 |
4 files changed, 53 insertions, 14 deletions
diff --git a/xmake/core/ui/application.lua b/xmake/core/ui/application.lua index aca6a2297..000294c64 100644 --- a/xmake/core/ui/application.lua +++ b/xmake/core/ui/application.lua @@ -49,9 +49,6 @@ function application:init(name) -- init program program.init(self, name) - -- save application - self:application_set(self) - -- trace log:print("<application: %s>: init ok", name) end diff --git a/xmake/core/ui/button.lua b/xmake/core/ui/button.lua index abdf63d44..3d788b560 100644 --- a/xmake/core/ui/button.lua +++ b/xmake/core/ui/button.lua @@ -25,6 +25,7 @@ -- load modules local log = require("ui/log") local view = require("ui/view") +local event = require("ui/event") local label = require("ui/label") local curses = require("ui/curses") @@ -32,7 +33,7 @@ local curses = require("ui/curses") local button = button or label() -- init button -function button:init(name, bounds, text, command) +function button:init(name, bounds, text, action) -- init label label.init(self, name, bounds, text) @@ -42,6 +43,9 @@ function button:init(name, bounds, text, command) -- show cursor self:cursor_show(true) + + -- init action + self:action_set("on_enter", action) end -- draw button @@ -71,5 +75,31 @@ function button:draw() self:canvas():attr(textattr):move(0, 0):puts(str) end +-- on event +function button:event_on(e) + + -- selected? + if not self:state("selected") then + return + end + + -- 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 + end + return true + end + end +end + -- return module return button diff --git a/xmake/core/ui/panel.lua b/xmake/core/ui/panel.lua index 417f1d725..f163ee483 100644 --- a/xmake/core/ui/panel.lua +++ b/xmake/core/ui/panel.lua @@ -126,9 +126,6 @@ function panel:insert(v, opt) -- set it's parent view v:parent_set(self) - -- set application - v:application_set(self:application()) - -- select this view if v:option("selectable") then self:select(v) diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua index 70d4149ec..a507ac483 100644 --- a/xmake/core/ui/view.lua +++ b/xmake/core/ui/view.lua @@ -74,8 +74,10 @@ function view:init(name, bounds) self._OPTIONS = options -- init attributes - local attrs = object() - self._ATTRS = attrs + self._ATTRS = object() + + -- init actions + self._ACTIONS = object() -- init name self._NAME = name @@ -154,14 +156,16 @@ end -- get the application function view:application() + if not self._APPLICATION then + local app = self + while app:parent() do + app = app:parent() + end + self._APPLICATION = app + end return self._APPLICATION end --- set the application -function view:application_set(app) - self._APPLICATION = app -end - -- get the view window function view:window() return self._WINDOW @@ -319,6 +323,17 @@ function view:attr_set(name, value) return self end +-- get action +function view:action(name) + return self._ACTIONS[name] +end + +-- set action +function view:action_set(name, action) + self._ACTIONS[name] = action + return self +end + -- get cursor position function view:cursor() return self._CURSOR |
