Skip to content

Commit

Permalink
fix: better handling of custom themes
Browse files Browse the repository at this point in the history
the stopwatch example works again
  • Loading branch information
wash2 committed Feb 3, 2024
1 parent 59cfdaf commit 767066f
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 133 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ exclude = [

[dependencies]
iced = { git = "https://github.com/iced-rs/iced", rev = "5540ac0", features = ["tokio"], optional = true }
# iced = { version = "0.12.0", features = [ "tokio" ], optional = true }
iced_runtime = { git = "https://github.com/iced-rs/iced", rev = "5540ac0", optional = true }
iced_widget = { git = "https://github.com/iced-rs/iced", rev = "5540ac0", optional = true }
iced_futures = { git = "https://github.com/iced-rs/iced", rev = "5540ac0", optional = true }
iced_core = { git = "https://github.com/iced-rs/iced", rev = "5540ac0", optional = true }
iced_style = { git = "https://github.com/iced-rs/iced", rev = "5540ac0", optional = true }
libcosmic = { git = "https://github.com/pop-os/libcosmic/", default-features = false, features = [ "tokio" ], optional = true, branch = "update-iced-latest" }
# libcosmic = { path = "../libcosmic", optional = true, features = [ "tokio" ]}
once_cell = { version = "1.18.0", optional = true }
float-cmp = "0.9"

4 changes: 2 additions & 2 deletions examples/stopwatch/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ mod widget {
#![allow(dead_code)]
use crate::theme::Theme;

pub type Renderer = iced::Renderer<Theme>;
pub type Element<'a, Message> = iced::Element<'a, Message, Renderer>;
pub type Renderer = iced::Renderer;
pub type Element<'a, Message> = iced::Element<'a, Message, Theme, Renderer>;
pub type Container<'a, Message> = iced::widget::Container<'a, Message, Renderer>;
pub type Button<'a, Message> = iced::widget::Button<'a, Message, Renderer>;
}
29 changes: 19 additions & 10 deletions examples/stopwatch/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/

use iced::widget::{button, container, text};
use iced::Background as B;
use iced::{application, color, Vector};
use iced::{application, color, Shadow, Vector};
use iced::{Background as B, Border};

#[derive(Default)]
pub struct Theme;
Expand Down Expand Up @@ -94,25 +94,34 @@ impl button::StyleSheet for Theme {
Button::Primary => button::Appearance {
background: Some(color!(0x25, 0x63, 0xeb).into()),
text_color: color!(0x00, 0x00, 0x00),
border_radius: 10.0.into(),
border_width: 10.0,
shadow_offset: Vector::new(3., 3.),
border_color: color!(0x25, 0x63, 0xeb),
border: Border {
radius: 10.0.into(),
width: 10.0,
color: color!(0x25, 0x63, 0xeb),
},
..Default::default()
},
Button::Secondary => button::Appearance {
background: Some(color!(0x3c, 0x38, 0x36).into()),
border_radius: 10.0.into(),
border: Border {
radius: 10.0.into(),
..Default::default()
},
shadow_offset: Vector::new(3., 3.),
text_color: color!(0xff, 0xff, 0xff),
..Default::default()
},
Button::Destructive => button::Appearance {
background: Some(color!(0xdc, 0x26, 0x26).into()),
text_color: color!(0xff, 0xff, 0xff),
border_radius: 10.0.into(),
shadow_offset: Vector::new(5., 5.),
border_color: color!(0xdc, 0x26, 0x26),
border_width: 10.0,
text_color: color!(0xff, 0xff, 0xff),
border: Border {
radius: 10.0.into(),
color: color!(0xdc, 0x26, 0x26),
width: 10.0,
},
shadow: Shadow::default(),
},
_ => panic!("This isn't a custom style exmaple, just skipping these for now"),
}
Expand Down
8 changes: 5 additions & 3 deletions src/keyframes/container.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::reexports::iced_core::{
widget::Id as IcedId, Element, Length, Padding, Pixels, Renderer as IcedRenderer,
};
use crate::reexports::{iced_widget, Theme};
use crate::reexports::{iced_style, iced_widget};

use crate::keyframes::{as_f32, get_length, Repeat};
use crate::timeline::Frame;
Expand Down Expand Up @@ -38,13 +38,14 @@ impl Id {
}

/// Used by [`crate::anim!`] macro
pub fn as_widget<'a, Message, Renderer>(
pub fn as_widget<'a, Message, Theme, Renderer>(
self,
timeline: &crate::Timeline,
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> iced_widget::Container<'a, Message, Theme, Renderer>
where
Renderer: IcedRenderer,
Theme: iced_style::container::StyleSheet,
{
Container::as_widget(self, timeline, content)
}
Expand Down Expand Up @@ -148,13 +149,14 @@ impl Container {
}
}

pub fn as_widget<'a, Message, Renderer>(
pub fn as_widget<'a, Message, Theme, Renderer>(
id: Id,
timeline: &crate::Timeline,
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> iced_widget::Container<'a, Message, Theme, Renderer>
where
Renderer: IcedRenderer,
Theme: iced_style::container::StyleSheet,
{
let id: IcedId = id.into();

Expand Down
17 changes: 9 additions & 8 deletions src/keyframes/style_button.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::keyframes::{as_f32, get_length, Repeat};
use crate::reexports::iced_core::{widget, Element, Length, Padding, Renderer as IcedRenderer};
use crate::reexports::iced_style::button::StyleSheet;
use crate::reexports::Theme;
use crate::reexports::ButtonStyleSheet;
use crate::timeline::{Frame, Interped};
use crate::{Ease, Linear, MovementType};

Expand Down Expand Up @@ -36,14 +35,15 @@ impl Id {
}

/// Used by [`crate::anim!`] macro
pub fn as_widget<'a, Message, Renderer>(
pub fn as_widget<'a, Message, Theme, Renderer>(
self,
style: fn(u8) -> <Theme as StyleSheet>::Style,
style: fn(u8) -> <Theme as ButtonStyleSheet>::Style,
timeline: &crate::Timeline,
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> crate::widget::Button<'a, Message, Renderer>
) -> crate::widget::Button<'a, Message, Theme, Renderer>
where
Renderer: IcedRenderer,
Theme: ButtonStyleSheet,
{
StyleButton::as_widget(self, style, timeline, content)
}
Expand Down Expand Up @@ -150,14 +150,15 @@ impl StyleButton {

// Returns a cosmic-time button, not a default iced button. The difference shouldn't
// matter to the end user. Though it is an implementation detail.
pub fn as_widget<'a, Message, Renderer>(
pub fn as_widget<'a, Message, Theme, Renderer>(
id: Id,
style: fn(u8) -> <Theme as StyleSheet>::Style,
style: fn(u8) -> <Theme as ButtonStyleSheet>::Style,
timeline: &crate::Timeline,
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> crate::widget::Button<'a, Message, Renderer>
) -> crate::widget::Button<'a, Message, Theme, Renderer>
where
Renderer: IcedRenderer,
Theme: ButtonStyleSheet,
{
let id: widget::Id = id.into();

Expand Down
70 changes: 64 additions & 6 deletions src/keyframes/style_container.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::reexports::iced_core::{
widget::Id as IcedId, Element, Length, Padding, Pixels, Renderer as IcedRenderer,
};
use crate::reexports::iced_style::container::StyleSheet;
use crate::reexports::{iced_widget, Theme};
use crate::reexports::iced_style::{self, container::StyleSheet};

Check warning on line 4 in src/keyframes/style_container.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, stable)

unused import: `self`

Check warning on line 4 in src/keyframes/style_container.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, beta)

unused import: `self`

Check warning on line 4 in src/keyframes/style_container.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, beta)

unused import: `self`

Check warning on line 4 in src/keyframes/style_container.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, stable)

unused import: `self`

Check failure on line 4 in src/keyframes/style_container.rs

View workflow job for this annotation

GitHub Actions / all

unused import: `self`

Check failure on line 4 in src/keyframes/style_container.rs

View workflow job for this annotation

GitHub Actions / all

unused import: `self`

Check warning on line 4 in src/keyframes/style_container.rs

View workflow job for this annotation

GitHub Actions / all

unused import: `self`

Check warning on line 4 in src/keyframes/style_container.rs

View workflow job for this annotation

GitHub Actions / all

unused import: `self`

use crate::keyframes::{as_f32, get_length, Repeat};
use crate::timeline::{Frame, Interped};
Expand Down Expand Up @@ -38,15 +37,33 @@ impl Id {
Chain::with_children(self, children)
}

#[cfg(feature = "iced")]
/// Used by [`crate::anim!`] macro
pub fn as_widget<'a, Message, Renderer>(
pub fn as_widget<'a, Message, Theme, Renderer>(
self,
style: fn(u8) -> <Theme as StyleSheet>::Style,
timeline: &crate::Timeline,
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> crate::widget::Container<'a, Message, Renderer>
) -> crate::widget::Container<'a, Message, Theme, Renderer>
where
Renderer: IcedRenderer,
Theme: StyleSheet,
{
StyleContainer::as_widget(self, style, timeline, content)
}

#[cfg(feature = "libcosmic")]
/// Used by [`crate::anim!`] macro
pub fn as_widget<'a, Message, Theme, Renderer>(
self,
style: fn(u8) -> <Theme as StyleSheet>::Style,
timeline: &crate::Timeline,
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> crate::widget::Container<'a, Message, Theme, Renderer>
where
Renderer: IcedRenderer,
Theme: StyleSheet + cosmic::cosmic_theme::LayeredTheme,
<Theme as iced_style::container::StyleSheet>::Style: From<cosmic::theme::Container>,
{
StyleContainer::as_widget(self, style, timeline, content)
}
Expand Down Expand Up @@ -159,14 +176,55 @@ impl StyleContainer {

// Returns a cosmic-time container, not a default iced button. The difference shouldn't
// matter to the end user. Though it is an implementation detail.
pub fn as_widget<'a, Message, Renderer>(
#[cfg(feature = "iced")]
pub fn as_widget<'a, Message, Theme, Renderer>(
id: Id,
style: fn(u8) -> <Theme as StyleSheet>::Style,
timeline: &crate::Timeline,
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> crate::widget::Container<'a, Message, Theme, Renderer>
where
Renderer: IcedRenderer,
Theme: StyleSheet,
{
let id: IcedId = id.into();

let container = crate::widget::Container::new(content)
.width(get_length(&id, timeline, 0, Length::Shrink))
.height(get_length(&id, timeline, 1, Length::Shrink))
.padding([
timeline.get(&id, 2).map_or(0., |m| m.value),
timeline.get(&id, 3).map_or(0., |m| m.value),
timeline.get(&id, 4).map_or(0., |m| m.value),
timeline.get(&id, 5).map_or(0., |m| m.value),
])
.max_width(timeline.get(&id, 6).map_or(f32::INFINITY, |m| m.value))
.max_height(timeline.get(&id, 7).map_or(f32::INFINITY, |m| m.value));

if let Some(Interped {
previous,
next,
percent,
..
}) = timeline.get(&id, 8)
{
container.blend_style(style(previous as u8), style(next as u8), percent)
} else {
container
}
}

#[cfg(feature = "libcosmic")]
pub fn as_widget<'a, Message, Theme, Renderer>(
id: Id,
style: fn(u8) -> <Theme as StyleSheet>::Style,
timeline: &crate::Timeline,
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> crate::widget::Container<'a, Message, Renderer>
) -> crate::widget::Container<'a, Message, Theme, Renderer>
where
Renderer: IcedRenderer,
Theme: StyleSheet + cosmic::cosmic_theme::LayeredTheme,
<Theme as iced_style::container::StyleSheet>::Style: From<cosmic::theme::Container>,
{
let id: IcedId = id.into();

Expand Down
15 changes: 10 additions & 5 deletions src/keyframes/toggler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::reexports::iced_core::{text, widget::Id as IcedId, Renderer as IcedRenderer};
use crate::reexports::{
iced_core::{text, widget::Id as IcedId, Renderer as IcedRenderer},
iced_style,
};

use crate::keyframes::Repeat;
use crate::timeline::Frame;
Expand Down Expand Up @@ -36,16 +39,17 @@ impl Id {
}

/// Used by [`crate::anim!`] macro
pub fn as_widget<'a, Message, Renderer, F>(
pub fn as_widget<'a, Message, Theme, Renderer, F>(
self,
timeline: &crate::Timeline,
label: impl Into<Option<String>>,
is_toggled: bool,
f: F,
) -> crate::widget::Toggler<'a, Message, Renderer>
) -> crate::widget::Toggler<'a, Message, Theme, Renderer>
where
Renderer: IcedRenderer + text::Renderer,
F: 'a + Fn(Chain, bool) -> Message,
Theme: iced_style::toggler::StyleSheet,
{
Toggler::as_widget(self, timeline, label, is_toggled, f)
}
Expand Down Expand Up @@ -184,16 +188,17 @@ impl Toggler {
}
}

pub fn as_widget<'a, Message, Renderer, F>(
pub fn as_widget<'a, Message, Theme, Renderer, F>(
id: Id,
timeline: &crate::Timeline,
label: impl Into<Option<String>>,
is_toggled: bool,
f: F,
) -> crate::widget::Toggler<'a, Message, Renderer>
) -> crate::widget::Toggler<'a, Message, Theme, Renderer>
where
Renderer: IcedRenderer + text::Renderer,
F: 'a + Fn(Chain, bool) -> Message,
Theme: iced_style::toggler::StyleSheet,
{
crate::widget::Toggler::new(id.clone(), label, is_toggled, f).percent(
timeline
Expand Down
1 change: 1 addition & 0 deletions src/reexports/iced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub use iced_core;
pub use iced_futures;
pub use iced_runtime;
pub use iced_style;
pub use iced_style::button::StyleSheet as ButtonStyleSheet;
pub use iced_widget;
1 change: 1 addition & 0 deletions src/reexports/libcosmic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub use cosmic::iced_futures;
pub use cosmic::iced_runtime;
pub use cosmic::iced_style;
pub use cosmic::iced_widget;
pub use cosmic::widget::button::StyleSheet as ButtonStyleSheet;
pub use cosmic::Theme;
3 changes: 2 additions & 1 deletion src/reexports/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
mod libcosmic;
#[cfg(feature = "libcosmic")]
pub use self::libcosmic::{
iced, iced_core, iced_futures, iced_runtime, iced_style, iced_widget, Theme,
iced, iced_core, iced_futures, iced_runtime, iced_style, iced_widget, ButtonStyleSheet, Theme,
};

#[cfg(feature = "iced")]
Expand All @@ -13,4 +13,5 @@ mod _iced;
#[cfg(feature = "iced")]
pub use self::_iced::{
iced, iced::Theme, iced_core, iced_futures, iced_runtime, iced_style, iced_widget,
ButtonStyleSheet,
};
Loading

0 comments on commit 767066f

Please sign in to comment.