diff options
| author | Julian Hurst <ark@mansus.space> | 2025-03-21 18:00:02 +0100 |
|---|---|---|
| committer | Julian Hurst <ark@mansus.space> | 2025-03-21 18:00:02 +0100 |
| commit | 6cb9d6f8756e690e6cd2360b8b1ab531da45c405 (patch) | |
| tree | de0ba6160942e1663cd3a298b878f1857598c124 | |
| parent | 1c3531d4da2e2f56bf09c38a756a8c5f7c615b80 (diff) | |
| download | hare-tui-6cb9d6f8756e690e6cd2360b8b1ab531da45c405.tar.gz | |
Simply and fix reframe and introduce bounds checks for setcursor
| -rw-r--r-- | cmd/il.ha | 16 | ||||
| -rw-r--r-- | tui/widget/list/scrolllist.ha | 26 |
2 files changed, 28 insertions, 14 deletions
@@ -61,6 +61,22 @@ export fn main() void = { if (r == 'K') { list::frameup(&li); }; + if (r == 'd') { + const nextpage = li.cursor + li.frame.end - li.frame.start; + fmt::errorln(nextpage)!; + list::setcursor(&li, nextpage); + fmt::errorln(li.frame.start)!; + fmt::errorln(li.frame.end)!; + fmt::errorln()!; + }; + if (r == 'u') { + const prevpage = li.cursor - (li.frame.end - li.frame.start); + fmt::errorln(prevpage)!; + list::setcursor(&li, prevpage); + fmt::errorln(li.frame.start)!; + fmt::errorln(li.frame.end)!; + fmt::errorln()!; + }; if (r == 'g') { list::top(&li); }; diff --git a/tui/widget/list/scrolllist.ha b/tui/widget/list/scrolllist.ha index 2f18ef0..0079363 100644 --- a/tui/widget/list/scrolllist.ha +++ b/tui/widget/list/scrolllist.ha @@ -120,27 +120,25 @@ export fn bottom(li: *scrolllist) void = { }; export fn setcursor(li: *scrolllist, newpos: int) void = { - li.cursor = newpos; + if (newpos < 0) { + li.cursor = 0; + } else if (newpos > len(li.items): int) { + li.cursor = len(li.items): int - 1; + } else { + li.cursor = newpos; + }; reframe(li); }; fn reframe(li: *scrolllist) void = { - if (li.cursor > li.frame.end) { + if (li.cursor >= li.frame.end) { const diff = li.frame.end - li.frame.start; - li.frame.start = if (li.cursor <= len(li.items): int - diff) li.cursor else len(li.items): int - diff; - li.frame.end = if (li.frame.start + diff > len(li.items): int) { - yield len(li.items): int; - } else { - yield li.frame.start + diff; - }; + li.frame.end = li.cursor + 1; + li.frame.start = li.frame.end - diff; } else if (li.cursor < li.frame.start) { const diff = li.frame.end - li.frame.start; - li.frame.end = if (li.cursor > diff) li.cursor else diff; - li.frame.start = if (li.frame.end - diff < 0) { - yield 0; - } else { - yield li.frame.end - diff; - }; + li.frame.start = li.cursor; + li.frame.end = li.frame.start + diff; }; assert(li.frame.start >= 0); assert(li.frame.end <= len(li.items): int); |
