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

Application-wide Theming Set/Changed at Runtime #1277

Closed

Conversation

emann
Copy link

@emann emann commented Mar 5, 2022

A precursor for #1022. Allows users to create entirely custom themes that can be set/updated during runtime.

Implementation Details

  • renderer::Style was already passed around to Widget::draw() calls so I just added fields to the struct to store a
    Box<dyn a_widget::StyleSheet + 'static> for each widget.
  • Added fn styling(&self) -> renderer::Style to Sandbox, Application, and a few other places where needed which allows the user to dynamically select which style to use. A default implementation is provided that returns what were previously hardcoded values for the current default style of an iced application.
  • Widget::draw() implementations for widgets that allow custom styling have been updated to fall back on the renderer::Style's style sheet if no custom styling was specified
    • the style_sheet: Box<dyn StyleSheet + 'a> field for said structs has been changed to
      custom_style_sheet: Option<Box<dyn StyleSheet + 'a>>
  • Added some methods to some StyleSheet traits that take some state data (e.g. is_mouse_over,) and return the style that should be used. The default implementation of these methods operate identically to the implementations that were previously in the draw() methods (delegating to one of the other style sheet's methods which return a my_widget::Style e.g. hovered(), active(), etc.) but both clean up the code in the draw() methods and allow for greater flexibility when creating custom themes.

Example

The application_theme example project
application_theme_demo

@emann emann marked this pull request as draft March 5, 2022 07:14
@emann
Copy link
Author

emann commented Mar 5, 2022

@hecrj marking as a draft as there are still some ToDo items (see below) but wanted to see if you had any input/requests in the meantime.

ToDo

  • Rename renderer::Style to renderer::Theme and update params/docs/etc. accordingly
  • Make sure all examples still work (or at least the ones that work on master)
  • Maybe remove the styling example as it doesn't really show anything special anymore???

@hecrj
Copy link
Member

hecrj commented Mar 6, 2022

I'm not sure this is the right approach.

Instead of listing the StyleSheet of every widget, I believe we should come up with a Theme type that contains a specific color palette that all widgets can use (even custom ones!).

For instance, something like:

pub struct Theme {
    pub base: Color,
    pub action: Color,
    pub text: Color,
    pub positive: Color,
    pub negative: Color,
    // and more...
}

Then, all of the widgets can use the colors of this Theme to style themselves by default, using some colors directly or maybe mixing some of them!

Before we write any code, the first step would be deciding the different fields of this Theme struct. Then, we can start by migrating the widgets to the new styling strategy one by one.

@emann
Copy link
Author

emann commented Mar 7, 2022

@hecrj I love the idea of a color palette but we also need to be able to specify widget-specific styling e.g. button border radius, slider handle shape, etc. How should we go about doing so? Adding fields to the Theme struct for each widget's specific styling seems to make sense, but then what if someone wants to change those depending on active hovered etc.? I've definitely seen buttons change shape when hovered for example.

@artursapek
Copy link
Contributor

artursapek commented Apr 13, 2022

For instance, something like:

pub struct Theme {
    pub base: Color,
    pub action: Color,
    pub text: Color,
    pub positive: Color,
    pub negative: Color,
    // and more...
}

this looks familiar :-D

@hecrj
Copy link
Member

hecrj commented Jul 9, 2022

Closing since the RFC efforts have landed with #1362. Thank you for starting all of it! 😁

@hecrj hecrj closed this Jul 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants