From 0fe7f789693a12bf61c5a3cba32b69d0e3a8bb9a Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Thu, 24 Oct 2024 07:34:27 -0700 Subject: [PATCH] Consistently apply overridden theme Refs #196 --- CHANGELOG.md | 2 ++ src/context.rs | 21 +++++++++++++++++---- src/tree.rs | 2 +- src/widget.rs | 4 ++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1504df3a4..4c37bebba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -128,6 +128,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `GraphicsContext::fill` now properly fills the entire region of the widget. - `Slider` now correctly calculates its width when in a fully `SizeToFit` layout. +- `ThemedMode` is now properly applied consistently. Previously sometimes the + window's theme mode would be used instead of the overridden mode. ### Added diff --git a/src/context.rs b/src/context.rs index d727299a9..39455d6ac 100644 --- a/src/context.rs +++ b/src/context.rs @@ -913,7 +913,10 @@ impl<'context> WidgetContext<'context> { ) -> Self { let enabled = current_node.enabled(&window.handle()); let tree = current_node.tree(); - Self { + + let (effective_styles, overridden_theme, overridden_theme_mode) = + current_node.overridden_theme(); + let mut context = Self { pending_state: PendingState::Owned(PendingWidgetState { focus: tree .focused_widget() @@ -926,7 +929,7 @@ impl<'context> WidgetContext<'context> { unmounting: false, }), tree, - effective_styles: current_node.effective_styles(), + effective_styles, cache: WidgetCacheKey { kludgine_id: Some(window.kludgine_id()), theme_mode, @@ -937,7 +940,16 @@ impl<'context> WidgetContext<'context> { font_state, theme: Cow::Borrowed(theme), window, + }; + + if let Some(theme) = overridden_theme { + context.theme = Cow::Owned(theme.get_tracking_redraw(&context)); + } + if let Some(mode) = overridden_theme_mode { + context.cache.theme_mode = mode.get_tracking_redraw(&context); } + + context } /// Returns a new instance that borrows from `self`. @@ -965,7 +977,7 @@ impl<'context> WidgetContext<'context> { Widget::Managed: MapManagedWidget>, { widget.manage(self).map(|current_node| { - let (effective_styles, theme, theme_mode) = current_node.overidden_theme(); + let (effective_styles, theme, theme_mode) = current_node.overridden_theme(); let theme = if let Some(theme) = theme { Cow::Owned(theme.get_tracking_redraw(self)) } else { @@ -1171,7 +1183,8 @@ impl<'context> WidgetContext<'context> { /// Attaches `theme_mode` to the widget hierarchy for this widget. /// /// All children nodes will use this theme mode. - pub fn attach_theme_mode(&self, theme_mode: Value) { + pub fn attach_theme_mode(&mut self, theme_mode: Value) { + self.cache.theme_mode = theme_mode.get(); self.current_node.attach_theme_mode(theme_mode); } diff --git a/src/tree.rs b/src/tree.rs index caa84f975..80a34cfb4 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -384,7 +384,7 @@ impl Tree { data.nodes.get_mut(id).expect("missing widget").theme_mode = Some(theme); } - pub(crate) fn overriden_theme( + pub(crate) fn overridden_theme( &self, id: LotId, ) -> (Styles, Option>, Option>) { diff --git a/src/widget.rs b/src/widget.rs index 1e314eb09..9eeed47da 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -1991,10 +1991,10 @@ impl MountedWidget { self.tree().attach_theme_mode(self.node_id, theme); } - pub(crate) fn overidden_theme( + pub(crate) fn overridden_theme( &self, ) -> (Styles, Option>, Option>) { - self.tree().overriden_theme(self.node_id) + self.tree().overridden_theme(self.node_id) } pub(crate) fn begin_layout(&self, constraints: Size) -> Option> {