Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion xilem/src/window_view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright 2025 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

use masonry::{theme::BACKGROUND_COLOR, util::debug_panic};
use std::sync::Arc;

use masonry::{core::DefaultProperties, theme::BACKGROUND_COLOR, util::debug_panic};
use masonry_winit::app::{NewWindow, Window, WindowId};

use crate::core::{MessageCtx, Mut, View, ViewElement, ViewMarker};
Expand All @@ -16,6 +18,8 @@ pub struct WindowView<State: 'static> {
pub(crate) masonry_root: MasonryRoot<State>,
/// The base color of the window.
pub(crate) base_color: Option<Color>,
/// Tree-wide default properties, applied on `Arc` identity change.
pub(crate) default_properties: Option<Arc<DefaultProperties>>,
}

pub(crate) type WindowViewState = <Box<AnyWidgetView<(), ()>> as View<(), (), ViewCtx>>::ViewState;
Expand All @@ -37,6 +41,7 @@ pub fn window<V: WidgetView<State>, State: 'static>(
options: WindowOptions::new(title),
masonry_root: MasonryRoot::new(root_view),
base_color: None,
default_properties: None,
}
}

Expand All @@ -57,6 +62,15 @@ impl<State> WindowView<State> {
self.base_color = Some(color);
self
}

/// Set tree-wide default properties for runtime theme swaps.
///
/// Applied on `Arc` identity change; cache the value so a new identity
/// only appears when the theme actually changes.
pub fn with_default_properties(mut self, default_properties: Arc<DefaultProperties>) -> Self {
self.default_properties = Some(default_properties);
self
}
}

/// A newtype wrapper around [`NewWindow`] for implementing [`ViewElement`].
Expand Down Expand Up @@ -110,6 +124,15 @@ impl<State> View<State, (), ViewCtx> for WindowView<State> {
*window.base_color() = base_color;
}

if let Some(props) = &self.default_properties
&& prev
.default_properties
.as_ref()
.is_none_or(|p| !Arc::ptr_eq(p, props))
{
window.render_root().set_default_properties(props.clone());
}
Comment on lines +127 to +134

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine for now, but I'd add a TODO comment to mention the case where default_properties go from Some to None.


self.masonry_root.rebuild(
&prev.masonry_root,
root_widget_view_state,
Expand Down
Loading