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 /xmake/core/ui/button.lua | |
| parent | bb8a982842b1a447b3da54538a8b245888ccbd86 (diff) | |
add action to button and view
Diffstat (limited to 'xmake/core/ui/button.lua')
| -rw-r--r-- | xmake/core/ui/button.lua | 32 |
1 files changed, 31 insertions, 1 deletions
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 |
