Skip to content

Fix component invalidating widgets unnecessarily - #3444

Open
edwloef wants to merge 1 commit into
iced-rs:masterfrom
edwloef:component-invalidate-widgets
Open

Fix component invalidating widgets unnecessarily#3444
edwloef wants to merge 1 commit into
iced-rs:masterfrom
edwloef:component-invalidate-widgets

Conversation

@edwloef

@edwloef edwloef commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Given this MCVE:

use iced::widget::{button, column, component, sensor, space, text, Renderer};

fn main() -> iced::Result {
    iced::application(|| false, update, view).run()
}

fn update(state: &mut bool, _: ()) {
    *state = true;
}

fn view(state: &bool) -> impl Into<iced::Element<'_, ()>> {
    column![
        button("show").on_press(()),
        state.then(|| component(Component))
    ]
}

struct Component;

impl<'a> component::Component<'a, ()> for Component {
    type State = bool;
    type Event = ();

    fn update(&mut self, state: &mut bool, _: (), _: &Renderer) -> Option<()> {
        *state = true;
        None
    }

    fn view(&self, state: &bool) -> iced::Element<'a, ()> {
        sensor(if *state {
            text("hi").height(20.8).into()
        } else {
            iced::Element::new(space())
        })
        .on_show(|_| ())
        .into()
    }
}

Clicking "show" wouldn't immediately show "hi", rather it would only show on an external event, such as the window resizing, happening. This PR fixes that.

I also just don't see any reason why this case should invalidate the entire widget tree, rather than just the layout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant