Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paragraph: I would like to have the ability to get the offset of the text being drawn in Paragraph. #136

Open
blacknon opened this issue Apr 14, 2023 · 7 comments
Labels
enhancement New feature or request

Comments

@blacknon
Copy link

Problem

When displaying text in Paragraph, the calculation of the maximum number of lines is complicated by the need to wrap long horizontal lines.
For this reason, we would like the ability to get the block size of the text being displayed in a Paragraph.

@blacknon blacknon added the enhancement New feature or request label Apr 14, 2023
@mindoodoo
Copy link
Member

By offset you mean the amount of actual lines of the terminal that paragraph ends up taking ?

@sayanarijit
Copy link
Member

@blacknon could you pls provide a code example?

@max-ishere
Copy link

I need this too to implement a dynamic footer. the footer will occupy 100%w x N lines.

But actually what I'd like to have is a way to render generic dynamic widgets. Here's what I'm thinking (pseudo rust)

let widget: impl DynamicSizedWidget;

let size_hint = DynamicSize::new(10.., 1..5)
    //or maybe more verbose api:
    .width(10..)
    .height(1..5);

let size = some_buffer.render_dynamic_size(widget, size_hint);

let chunks = Layout.constraint(
    Length(terminal.size - size.height), ...;

What I think would be really cool is if you have a widget that you render or estimate the size of with these size hints and then combine together. When they're combined you can also give them the exact size that should be arbitrary, so you should be able to shrink the buffer. And at this stage is when the border is drawn and stuff is resized to fit the new size.

@blacknon
Copy link
Author

blacknon commented Jul 9, 2023

Sorry for the late reply.

By offset you mean the amount of actual lines of the terminal that paragraph ends up taking ?

Yes, that right.
In ncurses, it's can use getmaxyx to get the overall size including line wrapping in window and pad, but I felt we need a similar feature.

@joshka
Copy link
Member

joshka commented Jul 9, 2023

I wonder if we could get some of this by implementing StatefulWidget on Paragraph, which fills a ParagraphState { render_height: usize, render_width: usize } struct?

If implementing this, care should be taken to ensure that this struct is extensible for scrolling purposes, and we should initially gate it behind an experimental feature flag (as this particular feature will probably churn based on future scrolling / wrapping changes).

@deifactor
Copy link
Contributor

deifactor commented Sep 6, 2023

I worry that if we take that approach, it'll encourage more and more use of StatefulWidget to implement "draw, and also return some additional data as well".

I propose an approach along the lines of:

use std::cell::Cell;

#[derive(Copy, Clone)]
struct Size {
    width: usize,
    height: usize,
}

struct Paragraph {
    rendered_size: Cell<Option<Size>>,
}

impl Paragraph {
    fn size(&self) -> Size {
        if self.rendered_size.get().is_none() {
            let actual_size = todo!("layout etc");
            self.rendered_size.set(Some(actual_size));
        }
        self.rendered_size.get().unwrap()
    }
}

And then .draw() can just call size().

@a-kenji
Copy link
Contributor

a-kenji commented Sep 6, 2023

We already have a pattern that can do that in Block.inner() , we could do something similar with the paragraph. I imagine this will allow it to be fairly composable.

@joshka joshka changed the title I would like to have the ability to get the offset of the text being drawn in Paragraph. Paragraph: I would like to have the ability to get the offset of the text being drawn in Paragraph. Sep 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants