preiter93 / tui-widget-list

A widget list implementation for the ratatui ecosystem
MIT License
18 stars 5 forks source link

Feature: Access to cross-axis size as input to ListableWidget::highlight? #14

Closed madsbv closed 5 months ago

madsbv commented 5 months ago

Would it be possible to add the cross-axis size of the list as an input to the highlight method of the ListableWidget trait?

My use case is that I have a list consisting of elements displaying text collected at runtime, which is of variable length and possibly spans multiple lines after wrapping. I'd like to be able to adjust the size of the selected element according to the amount of text in it, i.e. for a vertical list, I'd like to be able to set the height of the highlighted element to something like text.len()/width plus a little padding.

If this is something you would consider adding, I'd be happy to write up a PR for this, either as a change to the ListableWidget trait itself, or as a new DynamicListableWidget or something like that to avoid breaking changes. Let me know what you think!

preiter93 commented 5 months ago

Hey madsbv,

I'll think about it. I can see use cases where the methofs of the listable trait need access to info that is only available during rendering. So makes sense to support it.

Let me know if you have another idea than passing the width. I would like it to be more general.

Not sure if we can achive this non breaking. But I would prefer to break over adding a new widget at this early point of the library

preiter93 commented 5 months ago

I've sketched here a rough way forward https://github.com/preiter93/tui-widget-list/pull/15/files#diff-172747fc43e38d33de743b2d5f564ad6440570670af7aa912235399448a55c7cL10

this would change the ListableWidget trait considerably

preiter93 commented 5 months ago

I've sketched it out further. Managed to do it non breaking, but I deprecated the ListableWidget trait in favor of the new ListWidget trait.

If you are interested you can try it out

tui-widget-list = { git = "https://github.com/preiter93/tui-widget-list.git", branch = "v0.9" }

and follow along

https://github.com/preiter93/tui-widget-list/blob/v0.9/examples/simple.rs#L63

No guarantee though that I wont change some of the names before I publish it :)

madsbv commented 5 months ago

This looks great, thanks! I'll give it a try

madsbv commented 5 months ago

One more thing that could be useful to have in the context is the index of the ListWidget in the list at render-time. This would allow you to easily do things like have different background colors for alternating list items, to improve contrast between neighboring elements.

preiter93 commented 5 months ago

yeah good idea :) Added it.

Did you have a chance to test it out? I am welcome for suggestions.

I've did some changes to the api after I've read https://github.com/ratatui-org/ratatui/issues/996

pub trait PreRender: Widget {
    fn pre_render(&mut self, context: &PreRenderContext) -> u16;

I will create a new major release in the next days.

preiter93 commented 5 months ago

Released as v0.9