diff --git a/examples/theme.rs b/examples/theme.rs index 7b52618..2bd8cd5 100644 --- a/examples/theme.rs +++ b/examples/theme.rs @@ -1,8 +1,7 @@ -use gooey::styles::components::TextColor; +use gooey::styles::components::{TextColor, WidgetBackground}; use gooey::styles::{ColorSource, ColorTheme, FixedTheme, SurfaceTheme, Theme, ThemePair}; use gooey::value::{Dynamic, MapEach}; use gooey::widget::MakeWidget; -use gooey::widgets::label::LabelBackground; use gooey::widgets::{Input, Label, Stack}; use gooey::Run; use kludgine::Color; @@ -250,9 +249,9 @@ fn color_theme(theme: Dynamic, label: &str) -> impl MakeWidget { fn swatch(background: Dynamic, label: &str, text: Dynamic) -> impl MakeWidget { Label::new(label) + .with(&TextColor, text) + .with(&WidgetBackground, background) .fit_horizontally() .fit_vertically() - .with(&TextColor, text) - .with(&LabelBackground, background) .expand() } diff --git a/src/context.rs b/src/context.rs index fd2add8..018a6dc 100644 --- a/src/context.rs +++ b/src/context.rs @@ -13,7 +13,7 @@ use kludgine::shapes::{Shape, StrokeOptions}; use kludgine::Kludgine; use crate::graphics::Graphics; -use crate::styles::components::{HighlightColor, VisualOrder}; +use crate::styles::components::{HighlightColor, VisualOrder, WidgetBackground}; use crate::styles::{ComponentDefaultvalue, ComponentDefinition, Styles, Theme, ThemePair}; use crate::value::Dynamic; use crate::widget::{EventHandling, ManagedWidget, WidgetId, WidgetInstance, WidgetRef}; @@ -488,6 +488,9 @@ impl<'context, 'window, 'clip, 'gfx, 'pass> GraphicsContext<'context, 'window, ' "redraw called without set_widget_layout" ); + let background = self.query_style(&WidgetBackground); + self.gfx.fill(background); + self.current_node .tree .note_widget_rendered(self.current_node.id()); diff --git a/src/styles/components.rs b/src/styles/components.rs index 43a33bb..aa8078e 100644 --- a/src/styles/components.rs +++ b/src/styles/components.rs @@ -376,3 +376,21 @@ impl FocusableWidgets { matches!(self, Self::OnlyTextual) } } + +/// A [`Color`] to be used as a highlight color. +#[derive(Clone, Copy, Eq, PartialEq, Debug)] +pub struct WidgetBackground; + +impl NamedComponent for WidgetBackground { + fn name(&self) -> Cow<'_, ComponentName> { + Cow::Owned(ComponentName::named::("widget_background_color")) + } +} + +impl ComponentDefinition for WidgetBackground { + type ComponentType = Color; + + fn default_value(&self, _context: &WidgetContext<'_, '_>) -> Color { + Color::CLEAR_WHITE + } +} diff --git a/src/widgets/label.rs b/src/widgets/label.rs index e3c77d5..416e67d 100644 --- a/src/widgets/label.rs +++ b/src/widgets/label.rs @@ -1,14 +1,12 @@ //! A read-only text widget. -use std::borrow::Cow; use kludgine::figures::units::{Px, UPx}; use kludgine::figures::{IntoUnsigned, Point, ScreenScale, Size}; use kludgine::text::{MeasuredText, Text, TextOrigin}; -use kludgine::Color; -use crate::context::{GraphicsContext, LayoutContext, WidgetContext}; +use crate::context::{GraphicsContext, LayoutContext}; use crate::styles::components::{IntrinsicPadding, TextColor}; -use crate::styles::{ComponentDefinition, ComponentGroup, ComponentName, NamedComponent}; +use crate::styles::ComponentGroup; use crate::value::{IntoValue, Value}; use crate::widget::Widget; use crate::{ConstraintLimit, Name}; @@ -37,17 +35,13 @@ impl Widget for Label { let size = context.gfx.region().size; let center = Point::from(size) / 2; - let styles = context.query_styles(&[&TextColor, &LabelBackground]); - - let background = styles.get(&LabelBackground, context); - context.gfx.fill(background); if let Some(measured) = &self.prepared_text { context .gfx .draw_measured_text(measured, TextOrigin::Center, center, None, None); } else { - let text_color = styles.get(&TextColor, context); + let text_color = context.query_style(&TextColor); self.text.map(|contents| { context.gfx.draw_text( Text::new(contents, text_color) @@ -90,21 +84,3 @@ impl ComponentGroup for Label { Name::new("Label") } } - -/// A [`Color`] to be used as a highlight color. -#[derive(Clone, Copy, Eq, PartialEq, Debug)] -pub struct LabelBackground; - -impl NamedComponent for LabelBackground { - fn name(&self) -> Cow<'_, ComponentName> { - Cow::Owned(ComponentName::named::