Merge pull request #2067 from iced-rs/custom-extended-palette-generation

Introduce `theme::Custom::with_fn` to generate completely custom themes
This commit is contained in:
Héctor Ramón 2023-09-03 02:41:56 +02:00 committed by GitHub
commit 3b0d1b1ed4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 5 deletions

View file

@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [Unreleased]
### Added
- `Theme::Custom::with_fn` for custom extended palette generation. [#2067](https://github.com/iced-rs/iced/pull/2067)
### Changed ### Changed
- Updated `wgpu` to `0.17`. [#2065](https://github.com/iced-rs/iced/pull/2065) - Updated `wgpu` to `0.17`. [#2065](https://github.com/iced-rs/iced/pull/2065)

View file

@ -1,8 +1,7 @@
//! Use the built-in theme and styles. //! Use the built-in theme and styles.
pub mod palette; pub mod palette;
use self::palette::Extended; pub use palette::Palette;
pub use self::palette::Palette;
use crate::application; use crate::application;
use crate::button; use crate::button;
@ -40,7 +39,16 @@ pub enum Theme {
impl Theme { impl Theme {
/// Creates a new custom [`Theme`] from the given [`Palette`]. /// Creates a new custom [`Theme`] from the given [`Palette`].
pub fn custom(palette: Palette) -> Self { pub fn custom(palette: Palette) -> Self {
Self::Custom(Box::new(Custom::new(palette))) Self::custom_with_fn(palette, palette::Extended::generate)
}
/// Creates a new custom [`Theme`] from the given [`Palette`], with
/// a custom generator of a [`palette::Extended`].
pub fn custom_with_fn(
palette: Palette,
generate: impl FnOnce(Palette) -> palette::Extended,
) -> Self {
Self::Custom(Box::new(Custom::with_fn(palette, generate)))
} }
/// Returns the [`Palette`] of the [`Theme`]. /// Returns the [`Palette`] of the [`Theme`].
@ -66,15 +74,24 @@ impl Theme {
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct Custom { pub struct Custom {
palette: Palette, palette: Palette,
extended: Extended, extended: palette::Extended,
} }
impl Custom { impl Custom {
/// Creates a [`Custom`] theme from the given [`Palette`]. /// Creates a [`Custom`] theme from the given [`Palette`].
pub fn new(palette: Palette) -> Self { pub fn new(palette: Palette) -> Self {
Self::with_fn(palette, palette::Extended::generate)
}
/// Creates a [`Custom`] theme from the given [`Palette`] with
/// a custom generator of a [`palette::Extended`].
pub fn with_fn(
palette: Palette,
generate: impl FnOnce(Palette) -> palette::Extended,
) -> Self {
Self { Self {
palette, palette,
extended: Extended::generate(palette), extended: generate(palette),
} }
} }
} }