Created
November 13, 2020 20:19
-
-
Save laelnasan/92129fecd1e231c6492cd3babd3a0f29 to your computer and use it in GitHub Desktop.
ltui customization file for mouse support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---A cross-platform terminal ui library based on Lua | |
-- | |
-- Licensed 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-2020, TBOOX Open Source Group. | |
-- | |
-- @author laelnasan | |
-- @file ltui.lua | |
-- | |
local custom = setmetatable({}, { | |
__index = function () return function (o) return o end end}) | |
-- define module | |
local ltui = ltui or {} | |
-- identify customization | |
ltui._CUSTOM_VERSION = 'mouse-support_v0.1' | |
-- customizations | |
function custom.action(ac) | |
-- action for mouse click | |
ac:register('ac_max', 'ac_on_clicked') | |
return ac | |
end | |
function custom.panel(panel) | |
local panel_init = panel.init | |
function panel:init(...) | |
-- init panel | |
panel_init(self, ...) | |
-- make panel selectable by mouse click | |
self:action_set(ltui.action.ac_on_clicked, function (v, x, y) | |
-- get relative coordinates | |
x, y = x - v:bounds().sx, y - v:bounds().sy | |
-- try focused first | |
if v:current() and v:current():bounds():contains(x, y) then | |
return v:current():action_on(ltui.action.ac_on_clicked, x, y) | |
end | |
local p = v:last() | |
while p do | |
if p:option('selectable') and p:bounds():contains(x, y) then | |
v:select(p) | |
return p:action_on(ltui.action.ac_on_clicked, x, y) | |
end | |
p = v:prev(p) | |
end | |
-- return true if it doesn't match any selectable view | |
return true | |
end) | |
end | |
end | |
function custom.boxdialog(boxdialog) | |
local boxdialog_init = boxdialog.init | |
function boxdialog:init(...) | |
-- init boxdialog | |
boxdialog_init(self, ...) | |
-- click selection of boxed elements | |
self:frame():action_set(ltui.action.ac_on_clicked, function (v, x, y) | |
-- get relative coordinates | |
x, y = x - v:bounds().sx, y - v:bounds().sy | |
local panel, box = v:parent():panel(), v:parent():box() | |
local px, py = x - panel:bounds().sx, y - panel:bounds().sy | |
-- if coordinates don't match any view try box | |
if panel:action_on(ltui.action.ac_on_clicked, x, y) and | |
(not box:option("selectable")) and | |
box:bounds():contains(px, py) then | |
-- bypass panel | |
return box:action_on(ltui.action.ac_on_clicked, px, py) | |
end | |
end) | |
end | |
return boxdialog | |
end | |
function custom.application(app) | |
-- application with mouse support | |
function app:on_event(e) | |
if not ltui.program.on_event(self, e) then | |
-- mouse events | |
if e.type == ltui.event.ev_mouse and ( | |
e.btn_name == "BUTTON1_CLICKED" or | |
e.btn_name == "BUTTON1_DOUBLE_CLICKED") then | |
self:action_on(ltui.action.ac_on_clicked, e.x, e.y) | |
end | |
end | |
end | |
return app | |
end | |
function custom.button(btn) | |
local btn_init = btn.init | |
function btn:init(...) | |
-- init button | |
btn_init(self, ...) | |
-- click acts as enter | |
self:action_set(ltui.action.ac_on_clicked, function (v) | |
if v:parent()._do_select then | |
return v:parent():_do_select() | |
end | |
return v:action_on(ltui.action.ac_on_enter) | |
end) | |
end | |
return btn | |
end | |
-- register (and modify) modules | |
-- this order respects heritance and tries to load in order of complexity | |
-- you may change the order as needed. | |
ltui.curses = custom.curses (require("ltui/curses")) | |
ltui.object = custom.object (require("ltui/object")) | |
ltui.event = custom.event (require("ltui/event")) | |
ltui.action = custom.action (require("ltui/action")) | |
ltui.point = custom.point (require("ltui/point")) | |
ltui.rect = custom.rect (require("ltui/rect")) | |
ltui.canvas = custom.canvas (require("ltui/canvas")) | |
ltui.view = custom.view (require("ltui/view")) | |
ltui.border = custom.border (require("ltui/border")) | |
ltui.label = custom.label (require("ltui/label")) | |
ltui.panel = custom.panel (require("ltui/panel")) | |
ltui.button = custom.button (require("ltui/button")) | |
ltui.textarea = custom.textarea (require("ltui/textarea")) | |
ltui.textedit = custom.textedit (require("ltui/textedit")) | |
ltui.desktop = custom.desktop (require("ltui/desktop")) | |
ltui.menuconf = custom.menuconf (require("ltui/menuconf")) | |
ltui.menubar = custom.menubar (require("ltui/menubar")) | |
ltui.statusbar = custom.statusbar (require("ltui/statusbar")) | |
ltui.choicebox = custom.choicebox (require("ltui/choicebox")) | |
ltui.window = custom.window (require("ltui/window")) | |
ltui.dialog = custom.dialog (require("ltui/dialog")) | |
ltui.textdialog = custom.textdialog (require("ltui/textdialog")) | |
ltui.inputdialog = custom.inputdialog (require("ltui/inputdialog")) | |
ltui.boxdialog = custom.boxdialog (require("ltui/boxdialog")) | |
ltui.choicedialog = custom.choicedialog(require("ltui/choicedialog")) | |
ltui.mconfdialog = custom.mconfdialog (require("ltui/mconfdialog")) | |
ltui.program = custom.program (require("ltui/program")) | |
ltui.application = custom.application (require("ltui/application")) | |
--[[ <<< other customizations may come here >>> ]] | |
-- return module | |
return ltui |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment