From 4c2035273b869e75faf8046256f20b9511e88573 Mon Sep 17 00:00:00 2001 From: coolkiid <73600915+coolkiid@users.noreply.github.com> Date: Wed, 30 Apr 2025 22:45:30 +0800 Subject: [PATCH] feat: make tabs in header clickable according to the x-offset, calculate in which tab the mouse clicks. issue: #1403 --- yazi-plugin/preset/components/header.lua | 31 +++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/yazi-plugin/preset/components/header.lua b/yazi-plugin/preset/components/header.lua index 1dc851b9..0a6e2c88 100644 --- a/yazi-plugin/preset/components/header.lua +++ b/yazi-plugin/preset/components/header.lua @@ -110,7 +110,36 @@ function Header:redraw() end -- Mouse events -function Header:click(event, up) end +function Header:click(event, up) + local tab_count = #cx.tabs + if up or tab_count == 1 then + return + end + + local idx = 0 + local x_offset = self._area.w - event.x - 1 + + if th.mgr.tab_width > 2 then + for i = tab_count, 1, -1 do + -- 3 units for each tab padding, 1 unit for the index, 4 units in total. + local current_tab_width = math.min(th.mgr.tab_width + 1, #cx.tabs[i].name + 4) + if x_offset < current_tab_width then + idx = i - 1 + break + end + x_offset = x_offset - current_tab_width + end + else + -- 2 units for each tab padding, 1 unit for the index, 3 units in total. + -- early return if x-offset is greater than the number of tabs. + if x_offset > tab_count * 3 then + return + end + idx = tab_count - (x_offset // 3 + 1) + end + + ya.emit("tab_switch", { idx, false }) +end function Header:scroll(event, step) end