Skip to content

Commit

Permalink
Use Catalog approach for all widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Mar 24, 2024
1 parent e657dc2 commit f0ae9a0
Show file tree
Hide file tree
Showing 30 changed files with 1,130 additions and 915 deletions.
117 changes: 71 additions & 46 deletions core/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use text::{LineHeight, Shaping};
#[allow(missing_debug_implementations)]
pub struct Text<'a, Theme, Renderer>
where
Theme: Catalog,
Renderer: text::Renderer,
{
content: Cow<'a, str>,
Expand All @@ -29,18 +30,16 @@ where
vertical_alignment: alignment::Vertical,
font: Option<Renderer::Font>,
shaping: Shaping,
style: Style<'a, Theme>,
class: Theme::Class<'a>,
}

impl<'a, Theme, Renderer> Text<'a, Theme, Renderer>
where
Theme: Catalog,
Renderer: text::Renderer,
{
/// Create a new fragment of [`Text`] with the given contents.
pub fn new(content: impl Into<Cow<'a, str>>) -> Self
where
Theme: DefaultStyle + 'a,
{
pub fn new(content: impl Into<Cow<'a, str>>) -> Self {
Text {
content: content.into(),
size: None,
Expand All @@ -51,7 +50,7 @@ where
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
shaping: Shaping::Basic,
style: Box::new(Theme::default_style),
class: Theme::default(),
}
}

Expand All @@ -75,25 +74,6 @@ where
self
}

/// Sets the style of the [`Text`].
pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
self.style = Box::new(style);
self
}

/// Sets the [`Color`] of the [`Text`].
pub fn color(self, color: impl Into<Color>) -> Self {
self.color_maybe(Some(color))
}

/// Sets the [`Color`] of the [`Text`], if `Some`.
pub fn color_maybe(mut self, color: Option<impl Into<Color>>) -> Self {
let color = color.map(Into::into);

self.style = Box::new(move |_theme| Appearance { color });
self
}

/// Sets the width of the [`Text`] boundaries.
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width.into();
Expand Down Expand Up @@ -129,6 +109,42 @@ where
self.shaping = shaping;
self
}

/// Sets the style of the [`Text`].
#[must_use]
pub fn style(mut self, style: impl Fn(&Theme) -> Style + 'a) -> Self
where
Theme::Class<'a>: From<StyleFn<'a, Theme>>,
{
self.class = (Box::new(style) as StyleFn<'a, Theme>).into();
self
}

/// Sets the [`Color`] of the [`Text`].
pub fn color(self, color: impl Into<Color>) -> Self
where
Theme::Class<'a>: From<StyleFn<'a, Theme>>,
{
self.color_maybe(Some(color))
}

/// Sets the [`Color`] of the [`Text`], if `Some`.
pub fn color_maybe(self, color: Option<impl Into<Color>>) -> Self
where
Theme::Class<'a>: From<StyleFn<'a, Theme>>,
{
let color = color.map(Into::into);

self.style(move |_theme| Style { color })
}

/// Sets the style class of the [`Text`].
#[cfg(feature = "advanced")]
#[must_use]
pub fn class(mut self, class: impl Into<Theme::Class<'a>>) -> Self {
self.class = class.into();
self
}
}

/// The internal state of a [`Text`] widget.
Expand All @@ -138,6 +154,7 @@ pub struct State<P: Paragraph>(P);
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Text<'a, Theme, Renderer>
where
Theme: Catalog,
Renderer: text::Renderer,
{
fn tag(&self) -> tree::Tag {
Expand Down Expand Up @@ -182,15 +199,15 @@ where
tree: &Tree,
renderer: &mut Renderer,
theme: &Theme,
style: &renderer::Style,
defaults: &renderer::Style,
layout: Layout<'_>,
_cursor_position: mouse::Cursor,
viewport: &Rectangle,
) {
let state = tree.state.downcast_ref::<State<Renderer::Paragraph>>();
let appearance = (self.style)(theme);
let style = theme.style(&self.class);

draw(renderer, style, layout, state, appearance, viewport);
draw(renderer, defaults, layout, state, style, viewport);
}
}

Expand Down Expand Up @@ -250,7 +267,7 @@ pub fn draw<Renderer>(
style: &renderer::Style,
layout: Layout<'_>,
state: &State<Renderer::Paragraph>,
appearance: Appearance,
appearance: Style,
viewport: &Rectangle,
) where
Renderer: text::Renderer,
Expand Down Expand Up @@ -281,7 +298,7 @@ pub fn draw<Renderer>(
impl<'a, Message, Theme, Renderer> From<Text<'a, Theme, Renderer>>
for Element<'a, Message, Theme, Renderer>
where
Theme: 'a,
Theme: Catalog + 'a,
Renderer: text::Renderer + 'a,
{
fn from(
Expand All @@ -293,7 +310,7 @@ where

impl<'a, Theme, Renderer> From<&'a str> for Text<'a, Theme, Renderer>
where
Theme: DefaultStyle + 'a,
Theme: Catalog + 'a,
Renderer: text::Renderer,
{
fn from(content: &'a str) -> Self {
Expand All @@ -304,7 +321,7 @@ where
impl<'a, Message, Theme, Renderer> From<&'a str>
for Element<'a, Message, Theme, Renderer>
where
Theme: DefaultStyle + 'a,
Theme: Catalog + 'a,
Renderer: text::Renderer + 'a,
{
fn from(content: &'a str) -> Self {
Expand All @@ -314,30 +331,38 @@ where

/// The appearance of some text.
#[derive(Debug, Clone, Copy, Default)]
pub struct Appearance {
pub struct Style {
/// The [`Color`] of the text.
///
/// The default, `None`, means using the inherited color.
pub color: Option<Color>,
}

/// The style of some [`Text`].
pub type Style<'a, Theme> = Box<dyn Fn(&Theme) -> Appearance + 'a>;
/// The theme catalog of a [`Text`].
pub trait Catalog: Sized {
/// The item class of this [`Catalog`].
type Class<'a>;

/// The default style of some [`Text`].
pub trait DefaultStyle {
/// Returns the default style of some [`Text`].
fn default_style(&self) -> Appearance;
/// The default class produced by this [`Catalog`].
fn default<'a>() -> Self::Class<'a>;

/// The [`Style`] of a class with the given status.
fn style(&self, item: &Self::Class<'_>) -> Style;
}

impl DefaultStyle for Theme {
fn default_style(&self) -> Appearance {
Appearance::default()
/// A styling function for a [`Text`].
///
/// This is just a boxed closure: `Fn(&Theme, Status) -> Style`.
pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme) -> Style + 'a>;

impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self>;

fn default<'a>() -> Self::Class<'a> {
Box::new(|_theme| Style::default())
}
}

impl DefaultStyle for Color {
fn default_style(&self) -> Appearance {
Appearance { color: Some(*self) }
fn style(&self, class: &Self::Class<'_>) -> Style {
class(self)
}
}
10 changes: 2 additions & 8 deletions examples/component/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ mod numeric_input {

impl<Message, Theme> Component<Message, Theme> for NumericInput<Message>
where
Theme: text::DefaultStyle
+ button::Catalog
+ text_input::DefaultStyle
+ 'static,
Theme: text::Catalog + button::Catalog + text_input::Catalog + 'static,
{
type State = ();
type Event = Event;
Expand Down Expand Up @@ -151,10 +148,7 @@ mod numeric_input {
impl<'a, Message, Theme> From<NumericInput<Message>>
for Element<'a, Message, Theme>
where
Theme: text::DefaultStyle
+ button::Catalog
+ text_input::DefaultStyle
+ 'static,
Theme: text::Catalog + button::Catalog + text_input::Catalog + 'static,
Message: 'a,
{
fn from(numeric_input: NumericInput<Message>) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion examples/gradient/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Gradient {
} = *self;

let gradient_box = container(horizontal_space())
.style(move |_theme, _status| {
.style(move |_theme| {
let gradient = gradient::Linear::new(angle)
.add_stop(0.0, start)
.add_stop(1.0, end);
Expand Down
8 changes: 4 additions & 4 deletions examples/layout/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ impl Layout {
} else {
self.example.view()
})
.style(|theme, _status| {
.style(|theme| {
let palette = theme.extended_palette();

container::Appearance::default()
container::Style::default()
.with_border(palette.background.strong.color, 4.0)
})
.padding(4)
Expand Down Expand Up @@ -245,10 +245,10 @@ fn application<'a>() -> Element<'a, Message> {
.padding(10)
.align_items(Alignment::Center),
)
.style(|theme, _status| {
.style(|theme| {
let palette = theme.extended_palette();

container::Appearance::default()
container::Style::default()
.with_border(palette.background.strong.color, 1)
});

Expand Down
28 changes: 8 additions & 20 deletions examples/pane_grid/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,39 +338,30 @@ mod style {
use iced::widget::container;
use iced::{Border, Theme};

pub fn title_bar_active(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
pub fn title_bar_active(theme: &Theme) -> container::Style {
let palette = theme.extended_palette();

container::Appearance {
container::Style {
text_color: Some(palette.background.strong.text),
background: Some(palette.background.strong.color.into()),
..Default::default()
}
}

pub fn title_bar_focused(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
pub fn title_bar_focused(theme: &Theme) -> container::Style {
let palette = theme.extended_palette();

container::Appearance {
container::Style {
text_color: Some(palette.primary.strong.text),
background: Some(palette.primary.strong.color.into()),
..Default::default()
}
}

pub fn pane_active(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
pub fn pane_active(theme: &Theme) -> container::Style {
let palette = theme.extended_palette();

container::Appearance {
container::Style {
background: Some(palette.background.weak.color.into()),
border: Border {
width: 2.0,
Expand All @@ -381,13 +372,10 @@ mod style {
}
}

pub fn pane_focused(
theme: &Theme,
_status: container::Status,
) -> container::Appearance {
pub fn pane_focused(theme: &Theme) -> container::Style {
let palette = theme.extended_palette();

container::Appearance {
container::Style {
background: Some(palette.background.weak.color.into()),
border: Border {
width: 2.0,
Expand Down
4 changes: 2 additions & 2 deletions examples/scrollable/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ impl Default for ScrollableDemo {
}
}

fn progress_bar_custom_style(theme: &Theme) -> progress_bar::Appearance {
progress_bar::Appearance {
fn progress_bar_custom_style(theme: &Theme) -> progress_bar::Style {
progress_bar::Style {
background: theme.extended_palette().background.strong.color.into(),
bar: Color::from_rgb8(250, 85, 134).into(),
border: Border::default(),
Expand Down
Loading

0 comments on commit f0ae9a0

Please sign in to comment.