From a5e736899e441c3c93e8bbece028ed16b98dbb1c Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 13 Jan 2018 22:30:39 +0800 Subject: add action to button and view --- xmake/core/ui/button.lua | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'xmake/core/ui/button.lua') 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 -- cgit v1.3.1