diff options
| author | ruki <[email protected]> | 2018-01-26 22:36:35 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-01-26 09:39:35 +0800 |
| commit | 19e82986fb67b09726905788b53c8ad8ad05c263 (patch) | |
| tree | aa421ba9cceeff0e343c29952104f6fc84e64ddc | |
| parent | ba6283f21c9318c886dabf5a4b87df3a7a1b6ce8 (diff) | |
add choice box and dialog
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/ui/choicebox.lua | 26 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/ui/choicedialog.lua | 26 | ||||
| -rw-r--r-- | xmake/core/ui/choicebox.lua | 120 | ||||
| -rw-r--r-- | xmake/core/ui/choicedialog.lua | 91 | ||||
| -rw-r--r-- | xmake/core/ui/mconfdialog.lua | 42 |
5 files changed, 292 insertions, 13 deletions
diff --git a/xmake/core/sandbox/modules/import/core/ui/choicebox.lua b/xmake/core/sandbox/modules/import/core/ui/choicebox.lua new file mode 100644 index 000000000..45f0bb881 --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/ui/choicebox.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 choicebox.lua +-- + +-- return module: choicebox +return require("ui/choicebox") diff --git a/xmake/core/sandbox/modules/import/core/ui/choicedialog.lua b/xmake/core/sandbox/modules/import/core/ui/choicedialog.lua new file mode 100644 index 000000000..bc8999d8f --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/ui/choicedialog.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 choicedialog.lua +-- + +-- return module: choicedialog +return require("ui/choicedialog") diff --git a/xmake/core/ui/choicebox.lua b/xmake/core/ui/choicebox.lua new file mode 100644 index 000000000..2d0f32b81 --- /dev/null +++ b/xmake/core/ui/choicebox.lua @@ -0,0 +1,120 @@ +--!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 choicebox.lua +-- + +-- load modules +local log = require("ui/log") +local view = require("ui/view") +local rect = require("ui/rect") +local panel = require("ui/panel") +local event = require("ui/event") +local action = require("ui/action") +local curses = require("ui/curses") +local button = require("ui/button") +local object = require("ui/object") + +-- define module +local choicebox = choicebox or panel() + +-- init choicebox +function choicebox:init(name, bounds) + + -- init panel + panel.init(self, name, bounds) + + -- init values + self._VALUES = {} +end + +-- on event +function choicebox:event_on(e) + + -- select config + if e.type == event.ev_keyboard then + if e.key_name == "Down" then + return self:select_next() + elseif e.key_name == "Up" then + return self:select_prev() + elseif e.key_name == "Enter" or e.key_name == " " then + self:_do_select() + return true + end + elseif e.type == event.ev_command and e.command == "cm_enter" then + self:_do_select() + return true + end +end + +-- load values +function choicebox:load(values, selected) + + -- clear the views first + self:clear() + + -- insert values + self._VALUES = values + for idx, value in ipairs(values) do + self:_do_insert(value, idx == selected) + end + + -- select the first item + self:select(self:first()) + + -- invalidate + self:invalidate() +end + +-- do insert a value item +function choicebox:_do_insert(value, selected) + + -- init text + local text = (selected and "(X) " or "( ) ") .. tostring(value) + + -- init a value item view + local item = button:new("choicebox.value." .. self:count(), rect:new(0, self:count(), self:width(), 1), text) + + -- attach this value + item:extra_set("value", value) + + -- insert this config item + self:insert(item) +end + +-- do select the current config +function choicebox:_do_select() + + -- get the current item + local item = self:current() + + -- get the current value + local value = item:extra("value") + + -- do action: on selected + self:action_on(action.ac_on_selected, value) + + -- update text + item:text_set("(X) " .. tostring(value)) +end + +-- return module +return choicebox diff --git a/xmake/core/ui/choicedialog.lua b/xmake/core/ui/choicedialog.lua new file mode 100644 index 000000000..580345487 --- /dev/null +++ b/xmake/core/ui/choicedialog.lua @@ -0,0 +1,91 @@ +--!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 choicedialog.lua +-- + +-- load modules +local log = require("ui/log") +local rect = require("ui/rect") +local event = require("ui/event") +local action = require("ui/action") +local curses = require("ui/curses") +local window = require("ui/window") +local choicebox = require("ui/choicebox") +local boxdialog = require("ui/boxdialog") + +-- define module +local choicedialog = choicedialog or boxdialog() + +-- init dialog +function choicedialog:init(name, bounds, title) + + -- init window + boxdialog.init(self, name, bounds, title) + + -- init text + self:text():text_set("Use the arrow keys to navigate this window or press the hotkey of the item you wish to select followed by the <SPACEBAR>. Press <?> for additional information about this") + + -- init buttons + self:button_add("select", "< Select >", function (v, e) + self:choicebox():event_on(event.command {"cm_enter"}) + self:quit() + end) + self:button_add("help", "< Help >", function (v, e) end) + self:buttons():select(self:button("select")) + + -- insert choice box + self:box():panel():insert(self:choicebox()) + + -- disable to select to box (disable Tab switch and only response to buttons) + self:box():option_set("selectable", false) +end + +-- get choice box +function choicedialog:choicebox() + if not self._CHOICEBOX then + local bounds = self:box():panel():bounds() + self._CHOICEBOX = choicebox:new("choicedialog.choicebox", rect:new(math.floor(bounds:width() / 3), 0, bounds:width(), bounds:height())) + self._CHOICEBOX:state_set("focused", true) -- we can select and highlight selected item + end + return self._CHOICEBOX +end + +-- on event +function choicedialog:event_on(e) + + -- load values first + if e.type == event.ev_idle then + if not self._LOADED then + self:action_on(action.ac_on_load) + self._LOADED = true + end + -- select value + elseif e.type == event.ev_keyboard then + if e.key_name == "Down" or e.key_name == "Up" or e.key_name == " " then + return self:choicebox():event_on(e) + end + end + return boxdialog.event_on(self, e) +end + +-- return module +return choicedialog diff --git a/xmake/core/ui/mconfdialog.lua b/xmake/core/ui/mconfdialog.lua index b02eb17ea..7f05aa5ba 100644 --- a/xmake/core/ui/mconfdialog.lua +++ b/xmake/core/ui/mconfdialog.lua @@ -23,15 +23,16 @@ -- -- load modules -local log = require("ui/log") -local rect = require("ui/rect") -local event = require("ui/event") -local action = require("ui/action") -local curses = require("ui/curses") -local window = require("ui/window") -local menuconf = require("ui/menuconf") -local boxdialog = require("ui/boxdialog") -local inputdialog = require("ui/inputdialog") +local log = require("ui/log") +local rect = require("ui/rect") +local event = require("ui/event") +local action = require("ui/action") +local curses = require("ui/curses") +local window = require("ui/window") +local menuconf = require("ui/menuconf") +local boxdialog = require("ui/boxdialog") +local inputdialog = require("ui/inputdialog") +local choicedialog = require("ui/choicedialog") -- define module local mconfdialog = mconfdialog or boxdialog() @@ -62,7 +63,7 @@ Pressing <Y> includes, <N> excludes. Enter <Esc> to go back or exit, <?> for Hel self:box():option_set("selectable", false) -- init input dialog - local dialog_input = inputdialog:new("dialog.input", rect {0, 0, math.min(80, self:width()), math.min(8, self:height())}, "input dialog") + local dialog_input = inputdialog:new("mconfdialog.input", rect {0, 0, math.min(80, self:width()), math.min(8, self:height())}, "input dialog") dialog_input:background_set(self:frame():background()) dialog_input:frame():background_set("cyan") dialog_input:textedit():option_set("multiline", false) @@ -71,7 +72,10 @@ Pressing <Y> includes, <N> excludes. Enter <Esc> to go back or exit, <?> for Hel if config.kind == "string" then config.value = dialog_input:textedit():text() elseif config.kind == "number" then - config.value = tonumber(dialog_input:textedit():text()) + local value = tonumber(dialog_input:textedit():text()) + if value ~= nil then + config.value = value + end end dialog_input:quit() end) @@ -80,8 +84,16 @@ Pressing <Y> includes, <N> excludes. Enter <Esc> to go back or exit, <?> for Hel end) dialog_input:button_select("ok") + -- init choice dialog + local dialog_choice = choicedialog:new("mconfdialog.choice", rect {0, 0, math.min(80, self:width()), math.min(16, self:height())}, "input dialog") + dialog_choice:background_set(self:frame():background()) + dialog_choice:frame():background_set("cyan") + dialog_choice:box():frame():background_set("cyan") + -- on selected self:menuconf():action_set(action.ac_on_selected, function (v, config) + + -- show input dialog if config.kind == "string" or config.kind == "number" then dialog_input:extra_set("config", config) dialog_input:title():text_set(config:prompt()) @@ -92,6 +104,12 @@ Pressing <Y> includes, <N> excludes. Enter <Esc> to go back or exit, <?> for Hel dialog_input:text():text_set("Please enter a decimal value. Fractions will not be accepted. Use the <TAB> key to move from the input field to the buttons below it.") end self:insert(dialog_input, {centerx = true, centery = true}) + + -- show choice dialog + elseif config.kind == "choice" then + dialog_choice:title():text_set(config:prompt()) + dialog_choice:choicebox():load(config.values, 1) + self:insert(dialog_choice, {centerx = true, centery = true}) end end) end @@ -121,8 +139,6 @@ function mconfdialog:event_on(e) return self:menuconf():event_on(e) end end - - -- TODO return boxdialog.event_on(self, e) end |
