Add View dragging (#1402)

This commit is contained in:
Jan 2023-04-07 19:15:50 +02:00 committed by GitHub
parent 6619a2e1ea
commit 34ba4dbee6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 0 deletions

View File

@ -1289,6 +1289,12 @@ function core.on_event(type, ...)
if not core.root_view:on_mouse_wheel(...) then
did_keymap = keymap.on_mouse_wheel(...)
end
elseif type == "touchpressed" then
core.root_view:on_touch_pressed(...)
elseif type == "touchreleased" then
core.root_view:on_touch_released(...)
elseif type == "touchmoved" then
core.root_view:on_touch_moved(...)
elseif type == "resized" then
core.window_mode = system.get_window_mode()
elseif type == "minimized" or type == "maximized" or type == "restored" then

View File

@ -60,6 +60,13 @@ function Node:on_mouse_left()
end
end
function Node:on_touch_moved(...)
if self.type == "leaf" then
self.active_view:on_touch_moved(...)
else
self:propagate("on_touch_moved", ...)
end
end
function Node:consume(node)
for k, _ in pairs(self) do self[k] = nil end

View File

@ -334,6 +334,49 @@ function RootView:on_text_input(...)
core.active_view:on_text_input(...)
end
function RootView:on_touch_pressed(x, y, ...)
self.touched_node = self.root_node:get_child_overlapping_point(x, y)
end
function RootView:on_touch_released(x, y, ...)
self.touched_node = nil
end
function RootView:on_touch_moved(x, y, dx, dy, ...)
if not self.touched_node then return end
if core.active_view == core.nag_view then
core.active_view:on_touch_moved(x, y, dx, dy, ...)
return
end
if self.dragged_divider then
local node = self.dragged_divider
if node.type == "hsplit" then
x = common.clamp(x, 0, self.root_node.size.x * 0.95)
resize_child_node(node, "x", x, dx)
elseif node.type == "vsplit" then
y = common.clamp(y, 0, self.root_node.size.y * 0.95)
resize_child_node(node, "y", y, dy)
end
node.divider = common.clamp(node.divider, 0.01, 0.99)
return
end
local dn = self.dragged_node
if dn and not dn.dragging then
-- start dragging only after enough movement
dn.dragging = common.distance(x, y, dn.drag_start_x, dn.drag_start_y) > style.tab_width * .05
if dn.dragging then
core.request_cursor("hand")
end
end
-- avoid sending on_touch_moved events when dragging tabs
if dn then return end
self.touched_node:on_touch_moved(x, y, dx, dy, ...)
end
function RootView:on_ime_text_editing(...)
core.active_view:on_ime_text_editing(...)
end

View File

@ -248,6 +248,23 @@ function View:get_content_bounds()
return x, y, x + self.size.x, y + self.size.y
end
---@param x number
---@param y number
---@param dx number
---@param dy number
---@param i number
function View:on_touch_moved(x, y, dx, dy, i)
if not self.scrollable then return end
if self.dragging_scrollbar then
local delta = self:get_scrollable_size() / self.size.y * dy
self.scroll.to.y = self.scroll.to.y + delta
end
self.hovered_scrollbar = self:scrollbar_overlaps_point(x, y)
self.scroll.to.y = self.scroll.to.y + -dy
self.scroll.to.x = self.scroll.to.x + -dx
end
---@return number x
---@return number y