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

Introduce theme::Custom::with_fn to generate completely custom themes #2067

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

## [Unreleased]
### Added
- `Theme::Custom::with_fn` for custom extended palette generation. [#2067](https://github.com/iced-rs/iced/pull/2067)

### Changed
- Updated `wgpu` to `0.17`. [#2065](https://github.com/iced-rs/iced/pull/2065)

Expand Down
27 changes: 22 additions & 5 deletions style/src/theme.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Use the built-in theme and styles.
pub mod palette;

use self::palette::Extended;
pub use self::palette::Palette;
pub use palette::Palette;

use crate::application;
use crate::button;
Expand Down Expand Up @@ -40,7 +39,16 @@ pub enum Theme {
impl Theme {
/// Creates a new custom [`Theme`] from the given [`Palette`].
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`].
Expand All @@ -66,15 +74,24 @@ impl Theme {
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Custom {
palette: Palette,
extended: Extended,
extended: palette::Extended,
}

impl Custom {
/// Creates a [`Custom`] theme from the given [`Palette`].
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 {
palette,
extended: Extended::generate(palette),
extended: generate(palette),
}
}
}
Expand Down
Loading