diff options
| -rw-r--r-- | libui/widget/list/list.ha | 58 | ||||
| -rw-r--r-- | main.ha | 7 |
2 files changed, 58 insertions, 7 deletions
diff --git a/libui/widget/list/list.ha b/libui/widget/list/list.ha index 6637869..6710915 100644 --- a/libui/widget/list/list.ha +++ b/libui/widget/list/list.ha @@ -82,14 +82,19 @@ export fn finish(list: *widget::widget) void = { // list's current items, the cursor will be set to 0z and [[reframe]] will be // called to reset the frame. export fn setitems(list: *listwidget, items: str...) void = { - const reset = len(items) < len(list.items); + const doreset = len(items) < len(list.items); list.items = strings::dupall(items); - if (reset) { - list.cursor = 0z; - reframe(list); + if (doreset) { + reset(list); }; }; +export fn reset(list: *listwidget) void = { + list.cursor = 0z; + list.frame.start = 0u16; + list.frame.end = list.sz.rows; +}; + // Add a listener to the given list. //export fn addlistener(list: *listwidget, l: listener) void = { //append(list.listeners, l); @@ -150,21 +155,60 @@ export fn notify(l: *listwidget, r: rune) bool = { // Reset the list's frame based on the cursor. Returns whether the frame was // updated. -fn reframe(l: *listwidget) bool = { +export 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; + l.frame.end = l.frame.start + l.sz.rows; + //l.frame.end -= 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; + l.frame.end = l.cursor: u16 + 1; reframed = true; }; return reframed; }; +export fn resize(l: *listwidget, oldsz: ttysize) bool = { + if (l.frame.end - l.frame.start != l.sz.rows) { + if (l.cursor < (l.frame.end / 2)) { + fmt::fprintln(os::stderr, "closer to start")!; + l.frame.end = if (l.frame.start + l.sz.rows > len(l.items)) { + l.frame.start = len(l.items): u16 - l.sz.rows; + yield len(l.items): u16; + } else { + yield l.frame.start + l.sz.rows; + }; + for (l.cursor > l.frame.end) { + fmt::fprintln(os::stderr, "looping")!; + l.frame.start += 1; + l.frame.end += 1; + }; + } else { + fmt::fprintln(os::stderr, "closer to end")!; + l.frame.start = if (l.frame.end: int - l.sz.rows: int < 0) { + l.frame.end = 0 + l.sz.rows; + yield 0; + } else { + yield l.frame.end - l.sz.rows; + }; + for (l.cursor < l.frame.start) { + fmt::fprintln(os::stderr, "looping")!; + l.frame.start -= 1; + l.frame.end -= 1; + }; + }; + return true; + }; + return false; +}; + +fn cursorinframe(l: *listwidget) bool = { + return l.cursor >= l.frame.start && l.cursor < l.frame.end; +}; + // 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) { @@ -28,8 +28,15 @@ fn sighandler(sig: int, info: *signal::siginfo, ucontext: *void) void = { } else { yield len(u.list.items); }; + const oldsz = list::ttysize { + rows = u.list.sz.rows, + cols = u.list.sz.cols, + }; u.list.sz.rows = rows: u16; u.list.sz.cols = sz.columns; + list::resize(u.list, oldsz); + libui::clear(u.list.widget.ui); + list::print(u.list)!; }; }; |
