rootview.lua: Refactor Node:draw_tab

This commit is contained in:
Jipok 2021-12-21 02:20:31 +05:00
parent 2e9619f630
commit 6c1c983d1c
2 changed files with 44 additions and 36 deletions

View File

@ -260,8 +260,8 @@ end
local function close_button_location(x, w) local function close_button_location(x, w)
local cw = style.icon_font:get_width("C") local cw = style.icon_font:get_width("C")
local pad = style.padding.y local pad = style.padding.x / 2
return x + w - pad - cw, cw, pad return x + w - cw - pad, cw, pad
end end
@ -476,51 +476,59 @@ function Node:update()
end end
end end
function Node:draw_tab(text, is_active, is_hovered, is_close_hovered, x, y, w, h, standalone) function Node:draw_tab_title(view, font, is_active, is_hovered, x, y, w, h)
local text = view and view:get_name() or ""
local dots_width = font:get_width("")
local align = "center"
if font:get_width(text) > w then
align = "left"
for i = 1, #text do
local reduced_text = text:sub(1, #text - i)
if font:get_width(reduced_text) + dots_width <= w then
text = reduced_text .. ""
break
end
end
end
local color = style.dim
if is_active then color = style.text end
if is_hovered then color = style.text end
common.draw_text(font, color, text, align, x, y, w, h)
end
function Node:draw_tab_borders(view, is_active, is_hovered, x, y, w, h, standalone)
-- Tabs deviders
local ds = style.divider_size local ds = style.divider_size
local dots_width = style.font:get_width("")
local color = style.dim local color = style.dim
local padding_y = style.padding.y local padding_y = style.padding.y
renderer.draw_rect(x + w, y + padding_y, ds, h - padding_y * 2, style.dim) renderer.draw_rect(x + w, y + padding_y, ds, h - padding_y*2, style.dim)
if standalone then if standalone then
renderer.draw_rect(x-1, y-1, w+2, h+2, style.background2) renderer.draw_rect(x-1, y-1, w+2, h+2, style.background2)
end end
-- Full border
if is_active then if is_active then
color = style.text color = style.text
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)
end end
local cx, cw, cspace = close_button_location(x, w) return x + ds, y, w - ds*2, h
end
function Node:draw_tab(view, is_active, is_hovered, is_close_hovered, x, y, w, h, standalone)
x, y, w, h = self:draw_tab_borders(view, is_active, is_hovered, x, y, w, h, standalone)
-- Close button
local cx, cw, cpad = close_button_location(x, w)
local show_close_button = ((is_active or is_hovered) and not standalone and config.tab_close_button) local show_close_button = ((is_active or is_hovered) and not standalone and config.tab_close_button)
if show_close_button then if show_close_button then
local close_style = is_close_hovered and style.text or style.dim local close_style = is_close_hovered and style.text or style.dim
common.draw_text(style.icon_font, close_style, "C", nil, cx, y, 0, h) common.draw_text(style.icon_font, close_style, "C", nil, cx, y, cw, h)
end end
if is_hovered then -- Title
color = style.text x = x + cpad
end w = cx - x
local padx = style.padding.x core.push_clip_rect(x, y, w, h)
-- Normally we should substract "cspace" from text_avail_width and from the self:draw_tab_title(view, style.font, is_active, is_hovered, x, y, w, h)
-- clipping width. It is the padding space we give to the left and right of the
-- close button. However, since we are using dots to terminate filenames, we
-- choose to ignore "cspace" accepting that the text can possibly "touch" the
-- close button.
local text_avail_width = cx - x - padx
core.push_clip_rect(x, y, cx - x, h)
x, w = x + padx, w - padx * 2
local align = "center"
if style.font:get_width(text) > text_avail_width then
align = "left"
for i = 1, #text do
local reduced_text = text:sub(1, #text - i)
if style.font:get_width(reduced_text) + dots_width <= text_avail_width then
text = reduced_text .. ""
break
end
end
end
common.draw_text(style.font, color, text, align, x, y, w, h)
core.pop_clip_rect() core.pop_clip_rect()
end end
@ -547,7 +555,7 @@ function Node:draw_tabs()
for i = self.tab_offset, self.tab_offset + tabs_number - 1 do for i = self.tab_offset, self.tab_offset + tabs_number - 1 do
local view = self.views[i] local view = self.views[i]
local x, y, w, h = self:get_tab_rect(i) local x, y, w, h = self:get_tab_rect(i)
self:draw_tab(view:get_name(), view == self.active_view, self:draw_tab(view, view == self.active_view,
i == self.hovered_tab, i == self.hovered_close, i == self.hovered_tab, i == self.hovered_close,
x, y, w, h) x, y, w, h)
end end
@ -688,7 +696,7 @@ function Node:get_split_type(mouse_x, mouse_y)
local local_mouse_x = mouse_x - x local local_mouse_x = mouse_x - x
local local_mouse_y = mouse_y - y local local_mouse_y = mouse_y - y
if local_mouse_y < 0 then if local_mouse_y < 0 then
return "tab" return "tab"
else else

View File

@ -254,7 +254,7 @@ function RootView:on_mouse_moved(x, y, dx, dy)
self.root_node:on_mouse_moved(x, y, dx, dy) self.root_node:on_mouse_moved(x, y, dx, dy)
self.overlapping_node = self.root_node:get_child_overlapping_point(x, y) self.overlapping_node = self.root_node:get_child_overlapping_point(x, y)
local div = self.root_node:get_divider_overlapping_point(x, y) local div = self.root_node:get_divider_overlapping_point(x, y)
local tab_index = self.overlapping_node and self.overlapping_node:get_tab_overlapping_point(x, y) local tab_index = self.overlapping_node and self.overlapping_node:get_tab_overlapping_point(x, y)
if self.overlapping_node and self.overlapping_node:get_scroll_button_index(x, y) then if self.overlapping_node and self.overlapping_node:get_scroll_button_index(x, y) then
@ -381,8 +381,8 @@ function RootView:draw_grabbed_tab()
local _,_, w, h = dn.node:get_tab_rect(dn.idx) local _,_, w, h = dn.node:get_tab_rect(dn.idx)
local x = self.mouse.x - w / 2 local x = self.mouse.x - w / 2
local y = self.mouse.y - h / 2 local y = self.mouse.y - h / 2
local text = dn.node.views[dn.idx] and dn.node.views[dn.idx]:get_name() or "" local view = dn.node.views[dn.idx]
self.root_node:draw_tab(text, true, true, false, x, y, w, h, true) self.root_node:draw_tab(view, true, true, false, x, y, w, h, true)
end end