diff options
author | Bence Ferdinandy <bence@ferdinandy.com> | 2023-08-02 21:06:47 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-08-03 22:28:07 +0200 |
commit | 2fbce2e2c9aa782cc3d99a7232d78876b835e513 (patch) | |
tree | d3748edf1573b9c460efd729e833fdb8886539b2 | |
parent | 6713a8f4588f26f46c3e5fe0a69ead0f345617f3 (diff) | |
download | aerc-2fbce2e2c9aa782cc3d99a7232d78876b835e513.tar.gz |
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 <bence@ferdinandy.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Reviewed-by: Koni Marti <koni.marti@gmail.com>
-rw-r--r-- | lib/ui/tab.go | 28 |
1 files 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 } |