Skip to content

Get allowed size in component #144

@barsoosayque

Description

@barsoosayque

Let's consider a simple progress bar component: I need to render a background bar and a bar above the background, which is easy. But if I try to create a handler for clicking on the progress bar, I can get the column -- bug I can't get percents from it (e.g. column 4 in a 10 wide progress bar is 40%). For example this is my implementation of a progress bar:

#[derive(Default, Props)]
pub struct ProgressBarProps {
    pub progress: f32,
    pub handler: Handler<'static, f32>,
}

#[component]
pub fn ProgressBar(mut hooks: Hooks, props: &ProgressBarProps) -> impl Into<AnyElement<'static>> {
    let (width, _) = hooks.use_terminal_size();
    let amount = props.progress.clamp(0.0, 1.0);
        
    hooks.use_local_terminal_events(|event| match event {
        TerminalEvent::FullscreenMouse(FullscreenMouseEvent {
            column,
            kind: MouseEventKind::Down(_),
            ..
        }) => props.handler(/* ??? */),
        _ => {},
    });

    element! {
        View(width: Percent(100.0), height: 1, overflow: Overflow::Hidden) {
            View(position: Position::Absolute) {
                Text(content: "·".repeat(width as usize), weight: Weight::Light)
            }
            View(width: Percent(100.0 * amount), position: Position::Absolute) {
                Text(content: "—".repeat(width as usize), color: Color::Magenta)
            }
        }
    }
}

I can render it fine, but since I can't seem to get width of the parent component, I'm required to use the whole terminal width -- which can work with Overflow::Hidden for other components, but won't work for math and events ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestions about the library

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions