Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Built-in Theme customization #1519

Merged
merged 3 commits into from
Nov 9, 2022
Merged

Built-in Theme customization #1519

merged 3 commits into from
Nov 9, 2022

Conversation

hecrj
Copy link
Member

@hecrj hecrj commented Nov 9, 2022

This PR introduces a Custom variant to every style available in the built-in Theme. These new variants take a boxed StyleSheet of the widget with a Theme style. This allows for complete customization on a case-by-case basis of any widget without the need to implement a custom theme, which has quite the steep entry barrier.

For instance, the Button style is defined now as follows:

#[derive(Default)]
pub enum Button {
    #[default]
    Primary,
    Secondary,
    Positive,
    Destructive,
    Text,
    Custom(Box<dyn button::StyleSheet<Style = Theme>>),
}

Thus, any user using the built-in Theme can implement now a custom button::StyleSheet and have full control over the appearance of a Button. For example:

use iced::widget::button;
use iced::theme::{self, Theme};

pub struct MyButtonStyle;

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

    fn active(&self, theme: &Theme) -> button::Appearance {
        // Complete control over the `button::Appearance` here!
        // ...
    }

    // ...
}

pub fn my_button_style() -> theme::Button {
    theme::Button::Custom(Box::new(MyButtonStyle))
}

And then you can use it on any Button:

button("I'm a button!").style(my_button_style())

Basically, this PR satisfies all of the use cases of our previous styling approach (before #1362 landed) while still encouraging a first-class theme with consistent styling.

@hecrj hecrj added feature New feature or request widget styling labels Nov 9, 2022
@hecrj hecrj added this to the 0.5.0 milestone Nov 9, 2022
@hecrj hecrj merged commit af6f794 into master Nov 9, 2022
@hecrj hecrj deleted the customizable-theme branch November 9, 2022 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request styling widget
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant