Refactored LabelBackground to WidgetBackground

This commit is contained in:
Jonathan Johnson 2023-11-10 18:15:45 -08:00
parent 81f6f8c4d3
commit d844a44b33
No known key found for this signature in database
GPG key ID: A66D6A34D6620579
4 changed files with 28 additions and 32 deletions

View file

@ -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<ColorTheme>, label: &str) -> impl MakeWidget {
fn swatch(background: Dynamic<Color>, label: &str, text: Dynamic<Color>) -> impl MakeWidget {
Label::new(label)
.with(&TextColor, text)
.with(&WidgetBackground, background)
.fit_horizontally()
.fit_vertically()
.with(&TextColor, text)
.with(&LabelBackground, background)
.expand()
}

View file

@ -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());

View file

@ -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::<Global>("widget_background_color"))
}
}
impl ComponentDefinition for WidgetBackground {
type ComponentType = Color;
fn default_value(&self, _context: &WidgetContext<'_, '_>) -> Color {
Color::CLEAR_WHITE
}
}

View file

@ -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::<Label>("background_color"))
}
}
impl ComponentDefinition for LabelBackground {
type ComponentType = Color;
fn default_value(&self, _context: &WidgetContext<'_, '_>) -> Color {
Color::CLEAR_WHITE
}
}