From b5235d7ff8459b61a5cb6a7fcf29a1d02d302e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 27 Feb 2024 11:41:35 +0100 Subject: [PATCH 1/2] Use `application::StyleSheet` text color in `Themer` --- widget/src/helpers.rs | 2 ++ widget/src/themer.rs | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 71b0579d73..ed385ea59a 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -15,6 +15,7 @@ use crate::rule::{self, Rule}; use crate::runtime::Command; use crate::scrollable::{self, Scrollable}; use crate::slider::{self, Slider}; +use crate::style::application; use crate::text::{self, Text}; use crate::text_editor::{self, TextEditor}; use crate::text_input::{self, TextInput}; @@ -445,6 +446,7 @@ pub fn themer<'a, Message, Theme, Renderer>( ) -> Themer<'a, Message, Theme, Renderer> where Renderer: core::Renderer, + Theme: application::StyleSheet, { Themer::new(theme, content) } diff --git a/widget/src/themer.rs b/widget/src/themer.rs index e6ca6cfea2..a27e48137c 100644 --- a/widget/src/themer.rs +++ b/widget/src/themer.rs @@ -9,6 +9,7 @@ use crate::core::{ Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector, Widget, }; +use crate::style::application; /// A widget that applies any `Theme` to its contents. /// @@ -18,14 +19,17 @@ use crate::core::{ pub struct Themer<'a, Message, Theme, Renderer> where Renderer: crate::core::Renderer, + Theme: application::StyleSheet, { content: Element<'a, Message, Theme, Renderer>, theme: Theme, + style: Theme::Style, } impl<'a, Message, Theme, Renderer> Themer<'a, Message, Theme, Renderer> where Renderer: crate::core::Renderer, + Theme: application::StyleSheet, { /// Creates an empty [`Themer`] that applies the given `Theme` /// to the provided `content`. @@ -34,8 +38,9 @@ where T: Into>, { Self { - theme, content: content.into(), + theme, + style: Theme::Style::default(), } } } @@ -44,6 +49,7 @@ impl<'a, AnyTheme, Message, Theme, Renderer> Widget for Themer<'a, Message, Theme, Renderer> where Renderer: crate::core::Renderer, + Theme: application::StyleSheet, { fn tag(&self) -> tree::Tag { self.content.as_widget().tag() @@ -120,16 +126,20 @@ where tree: &Tree, renderer: &mut Renderer, _theme: &AnyTheme, - renderer_style: &renderer::Style, + _style: &renderer::Style, layout: Layout<'_>, cursor: mouse::Cursor, viewport: &Rectangle, ) { + let appearance = self.theme.appearance(&self.style); + self.content.as_widget().draw( tree, renderer, &self.theme, - renderer_style, + &renderer::Style { + text_color: appearance.text_color, + }, layout, cursor, viewport, @@ -248,7 +258,7 @@ impl<'a, AnyTheme, Message, Theme, Renderer> for Element<'a, Message, AnyTheme, Renderer> where Message: 'a, - Theme: 'a, + Theme: 'a + application::StyleSheet, Renderer: 'a + crate::core::Renderer, { fn from( From df666502f5c494fa5d009c2a4ed5e6fccf4ae4b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 27 Feb 2024 11:49:35 +0100 Subject: [PATCH 2/2] Add option to draw background for `themer` widget --- widget/src/themer.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/widget/src/themer.rs b/widget/src/themer.rs index a27e48137c..3a5fd82362 100644 --- a/widget/src/themer.rs +++ b/widget/src/themer.rs @@ -1,3 +1,4 @@ +use crate::container; use crate::core::event::{self, Event}; use crate::core::layout; use crate::core::mouse; @@ -6,8 +7,8 @@ use crate::core::renderer; use crate::core::widget::tree::{self, Tree}; use crate::core::widget::Operation; use crate::core::{ - Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector, - Widget, + Background, Clipboard, Element, Layout, Length, Point, Rectangle, Shell, + Size, Vector, Widget, }; use crate::style::application; @@ -24,6 +25,7 @@ where content: Element<'a, Message, Theme, Renderer>, theme: Theme, style: Theme::Style, + show_background: bool, } impl<'a, Message, Theme, Renderer> Themer<'a, Message, Theme, Renderer> @@ -41,8 +43,15 @@ where content: content.into(), theme, style: Theme::Style::default(), + show_background: false, } } + + /// Sets whether to draw the background color of the `Theme`. + pub fn background(mut self, background: bool) -> Self { + self.show_background = background; + self + } } impl<'a, AnyTheme, Message, Theme, Renderer> Widget @@ -133,6 +142,19 @@ where ) { let appearance = self.theme.appearance(&self.style); + if self.show_background { + container::draw_background( + renderer, + &container::Appearance { + background: Some(Background::Color( + appearance.background_color, + )), + ..container::Appearance::default() + }, + layout.bounds(), + ); + } + self.content.as_widget().draw( tree, renderer,