fix codestyle for mouse

This commit is contained in:
ruki
2020-11-20 22:48:28 +08:00
parent be0060b465
commit 078b76500c
6 changed files with 49 additions and 82 deletions

View File

@@ -28,7 +28,6 @@ local program = require("ltui/program")
local desktop = require("ltui/desktop")
local menubar = require("ltui/menubar")
local statusbar = require("ltui/statusbar")
local action = require("ltui/action")
-- define module
local application = application or program()
@@ -86,15 +85,7 @@ end
-- on event
function application:on_event(e)
if (not program.on_event(self, e)) and curses.KEY_MOUSE then
-- mouse events
if e.type == event.ev_mouse and (
e.btn_name == "BUTTON1_CLICKED" or
e.btn_name == "BUTTON1_DOUBLE_CLICKED") then
self:action_on(action.ac_on_clicked, e.x, e.y)
end
end
program.on_event(self, e)
end
-- on resize

View File

@@ -65,32 +65,23 @@ function boxdialog:init(name, bounds, title)
self:box():bounds_set(rect{0, self._TEXT_EY, v:width(), v:height() - 1})
end)
if curses.KEY_MOUSE then
-- on click for frame
self:frame():action_set(action.ac_on_clicked, function (v, x, y)
-- set click action
self:frame():action_set(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
-- return if not mouseable
if not v:option("mouseable") then
return
end
-- 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(action.ac_on_clicked, x, y) and
(not box:option("selectable")) and
box:bounds():contains(px, py) then
-- bypass panel
-- if coordinates don't match any view try box
if panel:option("mouseable") then
if panel:action_on(action.ac_on_clicked, x, y) then
return true
elseif box:option("mouseable") and not box:option("selectable") and box:bounds():contains(px, py) then
return box:action_on(action.ac_on_clicked, px, py)
end
end)
end
end
end)
end
-- get box

View File

@@ -41,28 +41,16 @@ function button:init(name, bounds, text, on_action)
-- show cursor
self:cursor_show(true)
-- init action
-- init actions
self:action_set(action.ac_on_enter, on_action)
if curses.KEY_MOUSE then
-- mark as mouseable
self:option_set("mouseable", true)
-- set click action
self:action_set(action.ac_on_clicked, function (v)
-- return if not mouseable
if not v:option("mouseable") then
return
end
if v:parent()._do_select then
return v:parent():_do_select()
end
return v:action_on(action.ac_on_enter)
end)
end
self:action_set(action.ac_on_clicked, function (v)
-- FIXME
if v:parent()._do_select then
v:parent():_do_select()
end
v:action_on(action.ac_on_enter)
return true
end)
end
-- draw button

View File

@@ -49,40 +49,30 @@ function panel:init(name, bounds)
-- init views cache
self._VIEWS_CACHE = {}
if curses.KEY_MOUSE then
-- on click action
self:action_set(action.ac_on_clicked, function (v, x, y)
-- mark as mouseable
self:option_set("mouseable", true)
-- get relative coordinates
x, y = x - v:bounds().sx, y - v:bounds().sy
-- set click action
self:action_set(action.ac_on_clicked, function (v, x, y)
-- try focused first
local current = v:current()
if current and current:option("mouseable") and current:bounds():contains(x, y) then
return current:action_on(action.ac_on_clicked, x, y)
end
-- return if not clickable
if not v:option("mouseable") then
return
end
-- 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(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(action.ac_on_clicked, x, y)
local p = v:last()
while p do
if p:option('selectable') and p:bounds():contains(x, y) then
v:select(p)
if p:option("mouseable") then
p:action_on(action.ac_on_clicked, x, y)
end
p = v:prev(p)
return true
end
-- return true if does not match any selectable view
return true
end)
end
p = v:prev(p)
end
end)
end
-- get all child views

View File

@@ -25,6 +25,7 @@ local point = require("ltui/point")
local panel = require("ltui/panel")
local event = require("ltui/event")
local curses = require("ltui/curses")
local action = require("ltui/action")
-- define module
local program = program or panel()
@@ -187,6 +188,11 @@ function program:on_event(e)
elseif event.is_command(e, "cm_exit") then
self:quit()
return true
-- mouse events
elseif e.type == event.ev_mouse and self:option("mouseable") then
if e.btn_name == "BUTTON1_CLICKED" or e.btn_name == "BUTTON1_DOUBLE_CLICKED" then
self:action_on(action.ac_on_clicked, e.x, e.y)
end
end
end

View File

@@ -68,6 +68,7 @@ function view:init(name, bounds)
-- init options
local options = object()
options.selectable = false -- true if window can be selected
options.mouseable = curses.KEY_MOUSE and true or false -- enable it by default
self._OPTIONS = options
-- init attributes