summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-12-23 00:39:37 +0800
committerruki <[email protected]>2017-12-22 21:57:44 +0800
commit9e2b084be73bc0b966c2c43f73403703d64b7228 (patch)
tree5ab95a1343897bde53c6b3f0fedccd6420df7328
parent0f825ce5a225f7dfba8e46e44de82c50df0b2e35 (diff)
handle cm_quit for application
-rw-r--r--tests/ui/hello.lua2
-rw-r--r--xmake/core/ui/event.lua13
-rw-r--r--xmake/core/ui/program.lua15
3 files changed, 30 insertions, 0 deletions
diff --git a/tests/ui/hello.lua b/tests/ui/hello.lua
index fb75384a8..4d3dcbc84 100644
--- a/tests/ui/hello.lua
+++ b/tests/ui/hello.lua
@@ -52,6 +52,8 @@ function hello:event_on(e)
self:menubar():show(not self:menubar():state("visible"))
elseif e.key_name == "d" then
self:desktop():show(not self:desktop():state("visible"))
+ elseif e.key_name == "q" then
+ self:quit()
end
end
end
diff --git a/xmake/core/ui/event.lua b/xmake/core/ui/event.lua
index 76d013fef..6fab1c4f4 100644
--- a/xmake/core/ui/event.lua
+++ b/xmake/core/ui/event.lua
@@ -44,6 +44,16 @@ function event:register(tag, ...)
self[tag] = base + n
end
+-- is key?
+function event:is_key(key_name)
+ return self.type == event.ev_keyboard and self.key_name == key_name
+end
+
+-- is command event: cm_xxx?
+function event:is_command(command)
+ return self.type == event.ev_command and self.command == command
+end
+
-- 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")
@@ -58,6 +68,9 @@ event:register("cm_max", "cm_quit")
--
event.keyboard = object {_init = { "key_code", "key_name", "key_meta" }, type = event.ev_keyboard}
+-- define command event
+event.command = object {_init = { "command", "extra" }, type = event.ev_command}
+
-- define idle event
event.idle = object {_init = {}, type = event.ev_idle}
diff --git a/xmake/core/ui/program.lua b/xmake/core/ui/program.lua
index e934e8e54..a79d2af51 100644
--- a/xmake/core/ui/program.lua
+++ b/xmake/core/ui/program.lua
@@ -150,6 +150,16 @@ function program:event_put(e)
table.insert(self._EVENT_QUEUE, e)
end
+-- send command
+function program:send(command, extra)
+ self:event_put(event.command {command, extra})
+end
+
+-- quit program
+function program:quit()
+ self:send("cm_quit")
+end
+
-- run program loop
function program:loop(argv)
@@ -173,6 +183,11 @@ function program:loop(argv)
self:event_on(event.idle())
end
+ -- quit?
+ if e and event.is_command(e, "cm_quit") then
+ break
+ end
+
-- draw views
self:draw()