From 2fbce2e2c9aa782cc3d99a7232d78876b835e513 Mon Sep 17 00:00:00 2001 From: Bence Ferdinandy Date: Wed, 2 Aug 2023 21:06:47 +0200 Subject: mouse: fix offset in tab title clickable area Since templates have been introduced into the tab titles the clickable area has been offset from the actual title. This is because the clickable areas are calculated based on the tab names, which can now be different from the acually shown titles. Extract the logic of getting the display name of a tab from TabStrip and add as method of Tab. Also extract the magic constant 32. Use the method and const in both clicked and Draw. Switch from using len() to runewidth, when calculating the length of the display name. Fixes: https://todo.sr.ht/~rjarry/aerc/162 Signed-off-by: Bence Ferdinandy Acked-by: Robin Jarry Reviewed-by: Koni Marti --- lib/ui/tab.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/ui/tab.go b/lib/ui/tab.go index 6a5cbf50..43b6e14f 100644 --- a/lib/ui/tab.go +++ b/lib/ui/tab.go @@ -9,6 +9,8 @@ import ( "git.sr.ht/~rjarry/aerc/config" ) +const tabRuneWidth int = 32 // TODO: make configurable + type Tabs struct { tabs []*Tab TabStrip *TabStrip @@ -36,6 +38,17 @@ func (t *Tab) SetTitle(s string) { t.title = s } +func (t *Tab) GetDisplayName() string { + name := t.Name + if t.title != "" { + name = t.title + } + if t.pinned { + name = t.uiConf.PinnedTabMarker + name + } + return name +} + type ( TabStrip Tabs TabContent Tabs @@ -361,17 +374,11 @@ func (strip *TabStrip) Draw(ctx *Context) { if strip.curIndex == i { style = uiConfig.GetStyleSelected(config.STYLE_TAB) } - tabWidth := 32 + tabWidth := tabRuneWidth if ctx.Width()-x < tabWidth { tabWidth = ctx.Width() - x - 2 } - name := tab.Name - if tab.title != "" { - name = tab.title - } - if tab.pinned { - name = uiConfig.PinnedTabMarker + name - } + name := tab.GetDisplayName() trunc := runewidth.Truncate(name, tabWidth, "…") x += ctx.Printf(x, 0, style, " %s ", trunc) if x >= ctx.Width() { @@ -441,8 +448,9 @@ func (strip *TabStrip) MouseEvent(localX int, localY int, event tcell.Event) { func (strip *TabStrip) clicked(mouseX int, mouseY int) (int, bool) { x := 0 for i, tab := range strip.tabs { - trunc := runewidth.Truncate(tab.Name, 32, "…") - length := len(trunc) + 2 + name := tab.GetDisplayName() + trunc := runewidth.Truncate(name, tabRuneWidth, "…") + length := runewidth.StringWidth(trunc) + 2 if x <= mouseX && mouseX < x+length { return i, true } -- cgit