Skip to content

Commit

Permalink
Implement custom helper for theme::Button
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed May 19, 2023
1 parent 59663d2 commit 96aa037
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 6 additions & 8 deletions examples/tour/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,19 @@ impl Sandbox for Tour {

if steps.has_previous() {
controls = controls.push(
button("Back").on_press(Message::BackPressed).style(
theme::Button::Custom(Box::new(
CustomButtonStyle::Secondary,
)),
),
button("Back")
.on_press(Message::BackPressed)
.style(theme::Button::custom(CustomButtonStyle::Secondary)),
);
}

controls = controls.push(horizontal_space(Length::Fill));

if steps.can_continue() {
controls = controls.push(
button("Next").on_press(Message::NextPressed).style(
theme::Button::Custom(Box::new(CustomButtonStyle::Primary)),
),
button("Next")
.on_press(Message::NextPressed)
.style(theme::Button::custom(CustomButtonStyle::Primary)),
);
}

Expand Down
9 changes: 9 additions & 0 deletions style/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ pub enum Button {
Custom(Box<dyn button::StyleSheet<Style = Theme>>),
}

impl Button {
/// Creates a custom [`Button`] style variant.
pub fn custom(
style_sheet: impl button::StyleSheet<Style = Theme> + 'static,
) -> Self {
Self::Custom(Box::new(style_sheet))
}
}

impl button::StyleSheet for Theme {
type Style = Button;

Expand Down

0 comments on commit 96aa037

Please sign in to comment.