Skip to content

Runtime theme switching: swap DefaultProperties without a restart #1819

Description

@mark-ik

Hi,

I'm a hobbyist making a music practice app using Xilem, among other things. I have an issue with swapping themes at runtime; the widgets and buttons and whatnot only update at init time, so you have to restart (no bueno!).

screencap of the problem in my appImage

(Old screenshot, but pictured: a music practice app's settings page. The buttons remain black from the dark theme when I switch to the light theme, which is not the intended behavior.)

I wanted to ask whether I should submit a pull request with a fix. masonry's render_root.rs even carries a // TODO: Find better ways to customize default property set.

I don't want to step on any toes though, so let me be up front in saying I use AI. It's all tools to me, and I couldn't find any AI policy, but no hard feelings if y'all don't want submissions of this nature.

screencap of the problem fixed in my appImage

(Pictured: a music practice app's settings page, with more options reflecting additional development. There are more theme presets and configurable primary/secondary/tertiary/neutral colors, and switching themes changes the buttons to the correct color palette because the fix is in this build)

the fix (two small additions)

masonry_core: RenderRoot gains a runtime setter:

// Replace the tree-wide default properties at runtime and repaint.
pub fn set_default_properties(&mut self, default_properties: Arc<DefaultProperties>) {
    self.property_arena.default_properties = default_properties;
    self.request_render_all();
}

xilem: WindowView takes them reactively, applied only when the Arc identity changes (so steady-state frames don't re-apply):

pub fn with_default_properties(mut self, props: Arc<DefaultProperties>) -> Self { ... }
// in rebuild(): if the Arc changed, window.render_root().set_default_properties(props.clone());

This is just swapping at runtime, not the broader set of changes required for per-widget TextInput::background_color or more granular configuration. But I've run it for weeks, seems to work fine. Apologies if I've made mistakes in this issue, I never really do this >_<

Relevant issues:

#1765
#1786

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions