Skip to content

Commit

Permalink
Allow str-like objects as labels
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Aug 28, 2021
1 parent e11ca64 commit 309db8a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 38 deletions.
14 changes: 8 additions & 6 deletions backend-embedded-graphics/src/themes/basic/button/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,12 @@ where
}

// Type alias to decouple button definition from theme
pub type StyledButton<'a, C> =
StyledButtonDecorator<C, Spacing<Label<&'static str, LabelStyle<MonoTextStyle<'a, C>>>>>;
pub type StyledButton<S, C> =
StyledButtonDecorator<C, Spacing<Label<S, LabelStyle<MonoTextStyle<'static, C>>>>>;

pub fn styled_button<C, S>(label: &'static str) -> StyledButton<C::PixelColor>
pub fn styled_button<ST, C, S>(label: ST) -> StyledButton<ST, C::PixelColor>
where
ST: AsRef<str>,
C: BasicTheme,
S: ButtonStyle<C::PixelColor>,
{
Expand All @@ -239,18 +240,19 @@ where
)
}

pub type StyledButtonStretched<'a, C> = StyledButtonDecorator<
pub type StyledButtonStretched<S, C> = StyledButtonDecorator<
C,
FillParent<
Label<&'static str, LabelStyle<MonoTextStyle<'a, C>>>,
Label<S, LabelStyle<MonoTextStyle<'static, C>>>,
HorizontalAndVertical,
Center,
Center,
>,
>;

pub fn styled_button_stretched<C, S>(label: &'static str) -> StyledButtonStretched<C::PixelColor>
pub fn styled_button_stretched<ST, C, S>(label: ST) -> StyledButtonStretched<ST, C::PixelColor>
where
ST: AsRef<str>,
C: BasicTheme,
S: ButtonStyle<C::PixelColor>,
{
Expand Down
7 changes: 4 additions & 3 deletions backend-embedded-graphics/src/themes/basic/check_box/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,12 @@ where
}

// Type alias to decouple toggle button definition from theme
pub type StyledCheckBox<'a, C> =
StyledCheckBoxDecorator<C, Label<&'static str, LabelStyle<MonoTextStyle<'a, C>>>>;
pub type StyledCheckBox<S, C> =
StyledCheckBoxDecorator<C, Label<S, LabelStyle<MonoTextStyle<'static, C>>>>;

pub fn styled_check_box<C, S>(label: &'static str) -> StyledCheckBox<C::PixelColor>
pub fn styled_check_box<ST, C, S>(label: ST) -> StyledCheckBox<ST, C::PixelColor>
where
ST: AsRef<str>,
C: BasicTheme,
S: CheckBoxVisualStyle<C::PixelColor>,
{
Expand Down
40 changes: 22 additions & 18 deletions backend-embedded-graphics/src/themes/basic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,42 @@ pub trait BasicTheme: Sized {
styled_label::<Self, Self::LabelStyle, _>(label)
}

fn primary_button(label: &'static str) -> StyledButton<Self::PixelColor> {
styled_button::<Self, Self::PrimaryButton>(label)
fn primary_button<S: AsRef<str>>(label: S) -> StyledButton<S, Self::PixelColor> {
styled_button::<_, Self, Self::PrimaryButton>(label)
}

fn secondary_button(label: &'static str) -> StyledButton<Self::PixelColor> {
styled_button::<Self, Self::SecondaryButton>(label)
fn secondary_button<S: AsRef<str>>(label: S) -> StyledButton<S, Self::PixelColor> {
styled_button::<_, Self, Self::SecondaryButton>(label)
}

fn primary_button_stretched(label: &'static str) -> StyledButtonStretched<Self::PixelColor> {
styled_button_stretched::<Self, Self::PrimaryButton>(label)
fn primary_button_stretched<S: AsRef<str>>(
label: S,
) -> StyledButtonStretched<S, Self::PixelColor> {
styled_button_stretched::<_, Self, Self::PrimaryButton>(label)
}

fn secondary_button_stretched(label: &'static str) -> StyledButtonStretched<Self::PixelColor> {
styled_button_stretched::<Self, Self::SecondaryButton>(label)
fn secondary_button_stretched<S: AsRef<str>>(
label: S,
) -> StyledButtonStretched<S, Self::PixelColor> {
styled_button_stretched::<_, Self, Self::SecondaryButton>(label)
}

fn toggle_button(label: &'static str) -> StyledToggleButton<Self::PixelColor> {
styled_toggle_button::<Self, Self::ToggleButton>(label)
fn toggle_button<S: AsRef<str>>(label: S) -> StyledToggleButton<S, Self::PixelColor> {
styled_toggle_button::<_, Self, Self::ToggleButton>(label)
}

fn toggle_button_stretched(
label: &'static str,
) -> StyledToggleButtonStretched<Self::PixelColor> {
styled_toggle_button_stretched::<Self, Self::ToggleButton>(label)
fn toggle_button_stretched<S: AsRef<str>>(
label: S,
) -> StyledToggleButtonStretched<S, Self::PixelColor> {
styled_toggle_button_stretched::<_, Self, Self::ToggleButton>(label)
}

fn check_box(label: &'static str) -> StyledCheckBox<Self::PixelColor> {
styled_check_box::<Self, Self::CheckBox>(label)
fn check_box<S: AsRef<str>>(label: S) -> StyledCheckBox<S, Self::PixelColor> {
styled_check_box::<_, Self, Self::CheckBox>(label)
}

fn radio_button(label: &'static str) -> StyledRadioButton<Self::PixelColor> {
styled_radio_button::<Self, Self::RadioButton>(label)
fn radio_button<S: AsRef<str>>(label: S) -> StyledRadioButton<S, Self::PixelColor> {
styled_radio_button::<_, Self, Self::RadioButton>(label)
}

fn horizontal_scrollbar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,12 @@ where
}

// Type alias to decouple toggle button definition from theme
pub type StyledRadioButton<'a, C> =
StyledRadioButtonDecorator<C, Label<&'static str, LabelStyle<MonoTextStyle<'a, C>>>>;
pub type StyledRadioButton<S, C> =
StyledRadioButtonDecorator<C, Label<S, LabelStyle<MonoTextStyle<'static, C>>>>;

pub fn styled_radio_button<C, S>(label: &'static str) -> StyledRadioButton<C::PixelColor>
pub fn styled_radio_button<ST, C, S>(label: ST) -> StyledRadioButton<ST, C::PixelColor>
where
ST: AsRef<str>,
C: BasicTheme,
S: RadioButtonVisualStyle<C::PixelColor>,
{
Expand Down
18 changes: 10 additions & 8 deletions backend-embedded-graphics/src/themes/basic/toggle_button/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,12 @@ where
}

// Type alias to decouple toggle button definition from theme
pub type StyledToggleButton<'a, C> =
StyledToggleButtonDecorator<C, Spacing<Label<&'static str, LabelStyle<MonoTextStyle<'a, C>>>>>;
pub type StyledToggleButton<S, C> =
StyledToggleButtonDecorator<C, Spacing<Label<S, LabelStyle<MonoTextStyle<'static, C>>>>>;

pub fn styled_toggle_button<C, S>(label: &'static str) -> StyledToggleButton<C::PixelColor>
pub fn styled_toggle_button<ST, C, S>(label: ST) -> StyledToggleButton<ST, C::PixelColor>
where
ST: AsRef<str>,
C: BasicTheme,
S: ToggleButtonStyle<C::PixelColor>,
{
Expand All @@ -277,20 +278,21 @@ where
)
}

pub type StyledToggleButtonStretched<'a, C> = StyledToggleButtonDecorator<
pub type StyledToggleButtonStretched<S, C> = StyledToggleButtonDecorator<
C,
FillParent<
Label<&'static str, LabelStyle<MonoTextStyle<'a, C>>>,
Label<S, LabelStyle<MonoTextStyle<'static, C>>>,
HorizontalAndVertical,
Center,
Center,
>,
>;

pub fn styled_toggle_button_stretched<C, S>(
label: &'static str,
) -> StyledToggleButtonStretched<C::PixelColor>
pub fn styled_toggle_button_stretched<ST, C, S>(
label: ST,
) -> StyledToggleButtonStretched<ST, C::PixelColor>
where
ST: AsRef<str>,
C: BasicTheme,
S: ToggleButtonStyle<C::PixelColor>,
{
Expand Down

0 comments on commit 309db8a

Please sign in to comment.