diff options
author | Nomeji <nomeji@saucisseroyale.cc> | 2022-06-23 12:05:32 +0200 |
---|---|---|
committer | delthas <delthas@dille.cc> | 2022-07-22 17:50:50 +0200 |
commit | b75cb83397e32fc3bc8ead40f34efadafda6fb70 (patch) | |
tree | df3a9aedc19dab333fd054d40576ee47ca4c250a /ui | |
parent | Fix dropping words in /msg, /query, /part (diff) |
Scroll to the channel when changing the horizontal channel
Previously, when running CTRL+N or CTRL+P to go to the next or previous
channel, with the horizontal channel mode, the selected channel could
appear offscreen.
This patch ensures that we scroll just enough to make the newly selected
chan visible.
Thanks to Nomeji for his hard work on this. Adapted slightly to
refactor existing code to use the newly created width computation
routine.
Diffstat (limited to 'ui')
-rw-r--r-- | ui/buffers.go | 48 | ||||
-rw-r--r-- | ui/ui.go | 16 |
2 files changed, 48 insertions, 16 deletions
diff --git a/ui/buffers.go b/ui/buffers.go index 4bf4d20..f1f71b3 100644 --- a/ui/buffers.go +++ b/ui/buffers.go @@ -607,14 +607,7 @@ func (bs *BufferList) HorizontalBufferOffset(x int, offset int) int { return -1 } } - if b.title == "" { - x -= stringWidth(b.netName) - } else { - x -= stringWidth(b.title) - } - if 0 < b.highlights { - x -= 2 + len(fmt.Sprintf("%d", b.highlights)) - } + x -= bufferWidth(&b) if x < 0 { return offset + i } @@ -622,19 +615,42 @@ func (bs *BufferList) HorizontalBufferOffset(x int, offset int) int { return -1 } +func (bs *BufferList) GetLeftMost(screenWidth int) int { + width := 0 + var leftMost int + + for leftMost = bs.current; leftMost >= 0; leftMost-- { + if leftMost < bs.current { + width++ + } + width += bufferWidth(&bs.list[leftMost]) + if width > screenWidth { + return leftMost + 1 // Went offscreen, need to go one step back + } + } + + return 0 +} + +func bufferWidth(b *buffer) int { + width := 0 + if b.title == "" { + width += stringWidth(b.netName) + } else { + width += stringWidth(b.title) + } + if 0 < b.highlights { + width += 2 + len(fmt.Sprintf("%d", b.highlights)) + } + return width +} + func (bs *BufferList) DrawHorizontalBufferList(screen tcell.Screen, x0, y0, width int, offset *int) { x := width for i := len(bs.list) - 1; i >= 0; i-- { b := &bs.list[i] x-- - if b.title == "" { - x -= stringWidth(b.netName) - } else { - x -= stringWidth(b.title) - } - if 0 < b.highlights { - x -= 2 + len(fmt.Sprintf("%d", b.highlights)) - } + x -= bufferWidth(b) if x <= 10 { break } @@ -452,6 +452,22 @@ func (ui *UI) Draw(members []irc.Member) { ui.screen.Show() } +func (ui *UI) HorizontalBufferScrollTo() { + w, _ := ui.screen.Size() + screenWidth := w - ui.memberWidth + if ui.bs.current < ui.channelOffset { + ui.channelOffset = ui.bs.current + return + } + + leftMost := ui.bs.GetLeftMost(screenWidth) + + if ui.channelOffset >= leftMost { + return + } + ui.channelOffset = leftMost +} + func (ui *UI) drawStatusBar(x0, y, width int) { clearArea(ui.screen, x0, y, width, 1) |