Show tab close button when hovering inactive tabs

This commit is contained in:
Francesco Abbate 2021-02-27 15:29:18 +01:00
parent ca6d1843bc
commit 4cda8fc122
1 changed files with 19 additions and 9 deletions

View File

@ -56,6 +56,7 @@ function Node:new(type)
if self.type == "leaf" then if self.type == "leaf" then
self:add_view(EmptyView()) self:add_view(EmptyView())
end end
self.hovered_close = 0
end end
@ -233,17 +234,24 @@ function Node:get_tab_overlapping_point(px, py)
end end
local function close_button_location(x, y, w, h)
local cw = style.icon_font:get_width("C")
local pad = style.padding.y
return x + w - pad - cw, y, cw, h
end
function Node:tab_mouse_moved(px, py) function Node:tab_mouse_moved(px, py)
local tab_index = self:get_tab_overlapping_point(px, py) local tab_index = self:get_tab_overlapping_point(px, py)
if tab_index and self.views[tab_index] == self.active_view then if tab_index then
local x, y, w, h = self:get_tab_rect(tab_index) local x, y, w, h = self:get_tab_rect(tab_index)
local pad, icon_w = style.padding.y, style.icon_font:get_width("C") local cx, cy, cw, ch = close_button_location(x, y, w, h)
if px >= x + w - icon_w - pad and px < x + w - pad and py >= y and py < y + h then if px >= cx and px < cx + cw and py >= cy and py < cy + ch then
self.hovered_close = true self.hovered_close = tab_index
return return
end end
end end
self.hovered_close = false self.hovered_close = 0
end end
@ -392,9 +400,11 @@ function Node:draw_tabs()
renderer.draw_rect(x, y, w, h, style.background) renderer.draw_rect(x, y, w, h, style.background)
renderer.draw_rect(x + w, y, ds, h, style.divider) renderer.draw_rect(x + w, y, ds, h, style.divider)
renderer.draw_rect(x - ds, y, ds, h, style.divider) renderer.draw_rect(x - ds, y, ds, h, style.divider)
local pad = style.padding.y end
local close_style = self.hovered_close and style.text or style.dim if view == self.active_view or i == self.hovered_tab then
common.draw_text(style.icon_font, close_style, "C", "right", x + pad, y, w - 2 * pad, h) local cx, cy, cw, ch = close_button_location(x, y, w, h)
local close_style = self.hovered_close == i and style.text or style.dim
common.draw_text(style.icon_font, close_style, "C", nil, cx, cy, 0, ch)
end end
if i == self.hovered_tab then if i == self.hovered_tab then
color = style.text color = style.text
@ -572,7 +582,7 @@ function RootView:on_mouse_pressed(button, x, y, clicks)
local idx = node:get_tab_overlapping_point(x, y) local idx = node:get_tab_overlapping_point(x, y)
if idx then if idx then
node:set_active_view(node.views[idx]) node:set_active_view(node.views[idx])
if button == "middle" or node.hovered_close then if button == "middle" or node.hovered_close == idx then
node:close_active_view(self.root_node) node:close_active_view(self.root_node)
end end
else else