From dbf800f59831852ace25f95d92875fe360735566 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Mon, 16 May 2022 18:19:41 +0200 Subject: Add page up and page down --- libui/widget/list/list.ha | 52 +++++++++++++++++++++++++++++++++-------------- main.ha | 4 ++++ 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/libui/widget/list/list.ha b/libui/widget/list/list.ha index 57239f3..8500482 100644 --- a/libui/widget/list/list.ha +++ b/libui/widget/list/list.ha @@ -137,12 +137,29 @@ export fn notify(l: *listwidget, r: rune) bool = { return false; }; +// Reset the list's frame based on the cursor. Returns whether the frame was +// updated. +fn reframe(l: *listwidget) bool = { + let reframed: bool = false; + if (l.cursor < l.frame.start) { + l.frame.end -= l.frame.start - l.cursor: u16; + l.frame.start = l.cursor: u16; + reframed = true; + }; + if (l.cursor >= l.frame.end) { + l.frame.start += l.cursor: u16 - l.frame.end + 1; + l.frame.end = l.cursor: u16; + reframed = true; + }; + return reframed; +}; + // Move the list's cursor down one item. Returns the new cursor. export fn down(l: *listwidget) size = { if (l.cursor < len(l.items) - 1) { l.cursor += 1; + reframe(l); }; - reframe(l); return l.cursor; }; @@ -150,26 +167,31 @@ export fn down(l: *listwidget) size = { export fn up(l: *listwidget) size = { if (l.cursor > 0) { l.cursor -= 1; + reframe(l); }; - reframe(l); return l.cursor; }; -// Reset the list's frame based on the cursor. Returns whether the frame was -// updated. -fn reframe(l: *listwidget) bool = { - let reframed: bool = false; - if (l.cursor < l.frame.start) { - l.frame.end -= l.frame.start - l.cursor: u16; - l.frame.start = l.cursor: u16; - reframed = true; +// Move the list's cursor up one page. Returns the new cursor. +export fn pageup(l: *listwidget) size = { + if (l.cursor: int - l.sz.rows: int >= 0) { + l.cursor -= l.sz.rows; + } else { + l.cursor = 0z; }; - if (l.cursor >= l.frame.end) { - l.frame.start += l.cursor: u16 - l.frame.end + 1; - l.frame.end = l.cursor: u16; - reframed = true; + reframe(l); + return l.cursor; +}; + +// Move the list's cursor down one page. Returns the new cursor. +export fn pagedown(l: *listwidget) size = { + if (l.cursor + l.sz.rows < len(l.items)) { + l.cursor += l.sz.rows; + } else { + l.cursor = len(l.items) - 1; }; - return reframed; + reframe(l); + return l.cursor; }; // Move the list's cursor to the top (first item). Returns the new cursor. diff --git a/main.ha b/main.ha index 0aeda39..1af7662 100644 --- a/main.ha +++ b/main.ha @@ -35,6 +35,10 @@ fn runehandler(l: *widget::widget, r: rune) bool = { list::down(l); case 'k' => list::up(l); + case 'd' => + list::pagedown(l); + case 'u' => + list::pageup(l); case 'l' => // to print properly suspend the ui, print, then resume libui::suspend(&l.ui); -- cgit v1.2.3