diff options
| author | ruki <[email protected]> | 2018-01-23 22:19:11 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-01-23 07:55:11 +0800 |
| commit | 2db3a5f053faebb08c3e65e5b097d0de4064ac1e (patch) | |
| tree | 460a7b2fb2d10f510a3bc712f2bd60f89e626abf | |
| parent | ac234fd5eb079d9e21485ac7d4b06c2c30d2db2a (diff) | |
add action enums to ui
| -rw-r--r-- | tests/ui/mconfdialog.lua | 3 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/ui/action.lua | 26 | ||||
| -rw-r--r-- | xmake/core/ui/action.lua | 50 | ||||
| -rw-r--r-- | xmake/core/ui/boxdialog.lua | 3 | ||||
| -rw-r--r-- | xmake/core/ui/button.lua | 23 | ||||
| -rw-r--r-- | xmake/core/ui/dialog.lua | 7 | ||||
| -rw-r--r-- | xmake/core/ui/inputdialog.lua | 3 | ||||
| -rw-r--r-- | xmake/core/ui/label.lua | 7 | ||||
| -rw-r--r-- | xmake/core/ui/view.lua | 4 |
9 files changed, 105 insertions, 21 deletions
diff --git a/tests/ui/mconfdialog.lua b/tests/ui/mconfdialog.lua index 9208b7099..5a52047e5 100644 --- a/tests/ui/mconfdialog.lua +++ b/tests/ui/mconfdialog.lua @@ -28,6 +28,7 @@ import("core.ui.rect") import("core.ui.view") import("core.ui.label") import("core.ui.event") +import("core.ui.action") import("core.ui.mconfdialog") import("core.ui.application") @@ -45,7 +46,7 @@ function demo:init() -- init menu config dialog local mconfdialog = mconfdialog:new("mconfdialog.main", rect {1, 1, self:width() - 1, self:height() - 1}, "menu config") - mconfdialog:action_set("command.exit", function (v, e) self:quit() end) + mconfdialog:action_set(action.ac_on_exit, function (v, e) self:quit() end) self:insert(mconfdialog) end diff --git a/xmake/core/sandbox/modules/import/core/ui/action.lua b/xmake/core/sandbox/modules/import/core/ui/action.lua new file mode 100644 index 000000000..707d06405 --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/ui/action.lua @@ -0,0 +1,26 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015 - 2018, TBOOX Open Source Group. +-- +-- @author ruki +-- @file action.lua +-- + +-- return module: action +return require("ui/action") diff --git a/xmake/core/ui/action.lua b/xmake/core/ui/action.lua new file mode 100644 index 000000000..11f320ec2 --- /dev/null +++ b/xmake/core/ui/action.lua @@ -0,0 +1,50 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015 - 2018, TBOOX Open Source Group. +-- +-- @author ruki +-- @file action.lua +-- + +-- load modules +local log = require("ui/log") +local object = require("ui/object") + +-- define module +local action = action or object { } + +-- register action types +function action:register(tag, ...) + local base = self[tag] or 0 + local enums = {...} + local n = #enums + for i = 1, n do + self[enums[i]] = i + base + end + self[tag] = base + n +end + +-- register action enums +action:register("ac_max", + "ac_on_text_changed", + "ac_on_enter", + "ac_on_exit") + +-- return module +return action diff --git a/xmake/core/ui/boxdialog.lua b/xmake/core/ui/boxdialog.lua index ed5a48b9d..0ed780e9e 100644 --- a/xmake/core/ui/boxdialog.lua +++ b/xmake/core/ui/boxdialog.lua @@ -25,6 +25,7 @@ -- load modules local log = require("ui/log") local rect = require("ui/rect") +local action = require("ui/action") local curses = require("ui/curses") local window = require("ui/window") local textdialog = require("ui/textdialog") @@ -48,7 +49,7 @@ function boxdialog:init(name, bounds, title) self:text():option_set("progress", false) -- text changed - self:text():action_set("text.changed", function (v, e) + self:text():action_set(action.ac_on_text_changed, function (v, e) if e.text then local lines = #self:text():splitext(e.text) if lines > 0 and lines < self:height() then diff --git a/xmake/core/ui/button.lua b/xmake/core/ui/button.lua index 6d6d19996..9c6e398f9 100644 --- a/xmake/core/ui/button.lua +++ b/xmake/core/ui/button.lua @@ -27,13 +27,14 @@ local log = require("ui/log") local view = require("ui/view") local event = require("ui/event") local label = require("ui/label") +local action = require("ui/action") local curses = require("ui/curses") -- define module local button = button or label() -- init button -function button:init(name, bounds, text, action) +function button:init(name, bounds, text, on_action) -- init label label.init(self, name, bounds, text) @@ -45,7 +46,7 @@ function button:init(name, bounds, text, action) self:cursor_show(true) -- init action - self:action_set("keyboard.Enter", action) + self:action_set(action.ac_on_enter, on_action) end -- draw button @@ -85,14 +86,16 @@ function button:event_on(e) -- enter this button? if e.type == event.ev_keyboard then - local action = self:action("keyboard." .. e.key_name) - if action then - if type(action) == "string" then - -- send command - self:application():send(action) - elseif type(action) == "function" then - -- do action script - action(self, e) + if e.key_name == "Enter" then + local on_action = self:action(action.ac_on_enter) + if on_action then + if type(on_action) == "string" then + -- send command + self:application():send(on_action) + elseif type(on_action) == "function" then + -- do action script + on_action(self, e) + end end return true end diff --git a/xmake/core/ui/dialog.lua b/xmake/core/ui/dialog.lua index 440f9219a..fdd692f6d 100644 --- a/xmake/core/ui/dialog.lua +++ b/xmake/core/ui/dialog.lua @@ -28,6 +28,7 @@ local rect = require("ui/rect") local event = require("ui/event") local label = require("ui/label") local panel = require("ui/panel") +local action = require("ui/action") local button = require("ui/button") local window = require("ui/window") local curses = require("ui/curses") @@ -99,9 +100,9 @@ end function dialog:quit() local parent = self:parent() if parent then - local action = self:action("command.exit") - if action then - action(self, event.command {"cm_exit"}) + local on_action = self:action(action.ac_on_exit) + if on_action then + on_action(self, event.command {"cm_exit"}) end parent:remove(self) end diff --git a/xmake/core/ui/inputdialog.lua b/xmake/core/ui/inputdialog.lua index d58095292..5a2a271ff 100644 --- a/xmake/core/ui/inputdialog.lua +++ b/xmake/core/ui/inputdialog.lua @@ -27,6 +27,7 @@ local log = require("ui/log") local rect = require("ui/rect") local view = require("ui/view") local event = require("ui/event") +local action = require("ui/action") local curses = require("ui/curses") local window = require("ui/window") local textedit = require("ui/textedit") @@ -51,7 +52,7 @@ function inputdialog:init(name, bounds, title) self:text():option_set("progress", false) -- text changed - self:text():action_set("text.changed", function (v, e) + self:text():action_set(action.ac_on_text_changed, function (v, e) if e.text then local lines = #self:text():splitext(e.text) if lines > 0 and lines < self:height() then diff --git a/xmake/core/ui/label.lua b/xmake/core/ui/label.lua index 87df818fc..f39358368 100644 --- a/xmake/core/ui/label.lua +++ b/xmake/core/ui/label.lua @@ -26,6 +26,7 @@ local log = require("ui/log") local view = require("ui/view") local event = require("ui/event") +local action = require("ui/action") local curses = require("ui/curses") -- define module @@ -69,9 +70,9 @@ end function label:text_set(text) text = text or "" if self._TEXT ~= text then - local action = self:action("text.changed") - if action then - action(self, event.text {text}) + local on_action = self:action(action.ac_on_text_changed) + if on_action then + on_action(self, event.text {text}) end end self._TEXT = text diff --git a/xmake/core/ui/view.lua b/xmake/core/ui/view.lua index ef5c5d64a..23a3faa87 100644 --- a/xmake/core/ui/view.lua +++ b/xmake/core/ui/view.lua @@ -345,8 +345,8 @@ function view:action(name) end -- set action -function view:action_set(name, action) - self._ACTIONS[name] = action +function view:action_set(name, on_action) + self._ACTIONS[name] = on_action return self end |
