summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-02-10 00:32:03 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-02-10 00:32:03 +0100
commit81bed94148dfdc60997d14db603a4c7f2d13bb51 (patch)
tree5a1ad65b9de2d543c24f0b6ca42bf03c14330dd8
parent712c8e53f2385ed425173cab327e06a214248579 (diff)
downloadiced-81bed94148dfdc60997d14db603a4c7f2d13bb51.tar.gz
iced-81bed94148dfdc60997d14db603a4c7f2d13bb51.tar.bz2
iced-81bed94148dfdc60997d14db603a4c7f2d13bb51.zip
Use custom `Application::style` to enable transparency
-rw-r--r--examples/gradient/src/main.rs38
-rw-r--r--style/src/theme.rs17
2 files changed, 28 insertions, 27 deletions
diff --git a/examples/gradient/src/main.rs b/examples/gradient/src/main.rs
index 5fbf1c54..5eacd7a6 100644
--- a/examples/gradient/src/main.rs
+++ b/examples/gradient/src/main.rs
@@ -1,4 +1,5 @@
-use iced::theme::Palette;
+use iced::application;
+use iced::theme::{self, Theme};
use iced::widget::{
checkbox, column, container, horizontal_space, row, slider, text,
};
@@ -22,7 +23,7 @@ struct Gradient {
start: Color,
end: Color,
angle: Radians,
- transparent_window: bool,
+ transparent: bool,
}
#[derive(Debug, Clone, Copy)]
@@ -30,7 +31,7 @@ enum Message {
StartChanged(Color),
EndChanged(Color),
AngleChanged(Radians),
- SetTransparent(bool),
+ TransparentToggled(bool),
}
impl Sandbox for Gradient {
@@ -41,7 +42,7 @@ impl Sandbox for Gradient {
start: Color::WHITE,
end: Color::new(0.0, 0.0, 1.0, 1.0),
angle: Radians(0.0),
- transparent_window: false,
+ transparent: false,
}
}
@@ -54,8 +55,8 @@ impl Sandbox for Gradient {
Message::StartChanged(color) => self.start = color,
Message::EndChanged(color) => self.end = color,
Message::AngleChanged(angle) => self.angle = angle,
- Message::SetTransparent(transparent) => {
- self.transparent_window = transparent;
+ Message::TransparentToggled(transparent) => {
+ self.transparent = transparent;
}
}
}
@@ -65,7 +66,7 @@ impl Sandbox for Gradient {
start,
end,
angle,
- transparent_window,
+ transparent,
} = *self;
let gradient_box = container(horizontal_space(Length::Fill))
@@ -93,8 +94,8 @@ impl Sandbox for Gradient {
.align_items(Alignment::Center);
let transparency_toggle = iced::widget::Container::new(
- checkbox("Transparent window", transparent_window)
- .on_toggle(Message::SetTransparent),
+ checkbox("Transparent window", transparent)
+ .on_toggle(Message::TransparentToggled),
)
.padding(8);
@@ -108,17 +109,16 @@ impl Sandbox for Gradient {
.into()
}
- fn theme(&self) -> iced::Theme {
- if self.transparent_window {
- iced::Theme::custom(
- String::new(),
- Palette {
- background: Color::TRANSPARENT,
- ..iced::Theme::default().palette()
- },
- )
+ fn style(&self) -> theme::Application {
+ if self.transparent {
+ theme::Application::custom(|theme: &Theme| {
+ application::Appearance {
+ background_color: Color::TRANSPARENT,
+ text_color: theme.palette().text,
+ }
+ })
} else {
- iced::Theme::default()
+ theme::Application::Default
}
}
}
diff --git a/style/src/theme.rs b/style/src/theme.rs
index afb4d027..235aecbd 100644
--- a/style/src/theme.rs
+++ b/style/src/theme.rs
@@ -172,6 +172,15 @@ pub enum Application {
Custom(Box<dyn application::StyleSheet<Style = Theme>>),
}
+impl Application {
+ /// Creates a custom [`Application`] style.
+ pub fn custom(
+ custom: impl application::StyleSheet<Style = Theme> + 'static,
+ ) -> Self {
+ Self::Custom(Box::new(custom))
+ }
+}
+
impl application::StyleSheet for Theme {
type Style = Application;
@@ -196,14 +205,6 @@ impl<T: Fn(&Theme) -> application::Appearance> application::StyleSheet for T {
}
}
-impl<T: Fn(&Theme) -> application::Appearance + 'static> From<T>
- for Application
-{
- fn from(f: T) -> Self {
- Self::Custom(Box::new(f))
- }
-}
-
/// The style of a button.
#[derive(Default)]
pub enum Button {