Skip to content

Commit

Permalink
Use DefaultStyle trait for Text widget
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Mar 8, 2024
1 parent 7161cb4 commit 30b7ab9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
42 changes: 32 additions & 10 deletions core/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::renderer;
use crate::text::{self, Paragraph};
use crate::widget::tree::{self, Tree};
use crate::{
Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Widget,
Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Theme,
Widget,
};

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

Expand Down Expand Up @@ -312,6 +316,7 @@ where

impl<'a, Theme, Renderer> From<&'a str> for Text<'a, Theme, Renderer>
where
Theme: DefaultStyle,
Renderer: text::Renderer,
{
fn from(content: &'a str) -> Self {
Expand All @@ -322,7 +327,7 @@ where
impl<'a, Message, Theme, Renderer> From<&'a str>
for Element<'a, Message, Theme, Renderer>
where
Theme: 'a,
Theme: DefaultStyle + 'a,
Renderer: text::Renderer + 'a,
{
fn from(content: &'a str) -> Self {
Expand All @@ -339,9 +344,12 @@ pub struct Appearance {
pub color: Option<Color>,
}

/// The style of some [`Text`].
#[derive(Debug)]
enum Style<Theme> {
pub enum Style<Theme> {
/// The [`Text`] is styled using some theme.
Themed(fn(&Theme) -> Appearance),
/// The [`Text`] is styled using some [`Color`].
Colored(Option<Color>),
}

Expand All @@ -353,14 +361,28 @@ impl<Theme> Clone for Style<Theme> {

impl<Theme> Copy for Style<Theme> {}

impl<Theme> Default for Style<Theme> {
fn default() -> Self {
/// The default style of some [`Text`].
pub trait DefaultStyle: Sized {
/// Returns the default style of some [`Text`].
fn default_style() -> Style<Self>;
}

impl DefaultStyle for Theme {
fn default_style() -> Style<Self> {
Style::Colored(None)
}
}

impl<Theme> From<fn(&Theme) -> Appearance> for Style<Theme> {
fn from(f: fn(&Theme) -> Appearance) -> Self {
Style::Themed(f)
impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> {
Style::Themed(|appearance| *appearance)
}
}

impl DefaultStyle for Color {
fn default_style() -> Style<Self> {
Style::Themed(|color| Appearance {
color: Some(*color),
})
}
}
1 change: 1 addition & 0 deletions widget/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub fn text<'a, Theme, Renderer>(
text: impl ToString,
) -> Text<'a, Theme, Renderer>
where
Theme: core::widget::text::DefaultStyle,
Renderer: core::text::Renderer,
{
Text::new(text.to_string())
Expand Down

0 comments on commit 30b7ab9

Please sign in to comment.