diff options
Diffstat (limited to 'libui/widget/list/list.ha')
| -rw-r--r-- | libui/widget/list/list.ha | 52 |
1 files changed, 37 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. |
