Skip to content

Commit 81bed94

Browse files
committed
Use custom Application::style to enable transparency
1 parent 712c8e5 commit 81bed94

2 files changed

Lines changed: 28 additions & 27 deletions

File tree

examples/gradient/src/main.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use iced::theme::Palette;
1+
use iced::application;
2+
use iced::theme::{self, Theme};
23
use iced::widget::{
34
checkbox, column, container, horizontal_space, row, slider, text,
45
};
@@ -22,15 +23,15 @@ struct Gradient {
2223
start: Color,
2324
end: Color,
2425
angle: Radians,
25-
transparent_window: bool,
26+
transparent: bool,
2627
}
2728

2829
#[derive(Debug, Clone, Copy)]
2930
enum Message {
3031
StartChanged(Color),
3132
EndChanged(Color),
3233
AngleChanged(Radians),
33-
SetTransparent(bool),
34+
TransparentToggled(bool),
3435
}
3536

3637
impl Sandbox for Gradient {
@@ -41,7 +42,7 @@ impl Sandbox for Gradient {
4142
start: Color::WHITE,
4243
end: Color::new(0.0, 0.0, 1.0, 1.0),
4344
angle: Radians(0.0),
44-
transparent_window: false,
45+
transparent: false,
4546
}
4647
}
4748

@@ -54,8 +55,8 @@ impl Sandbox for Gradient {
5455
Message::StartChanged(color) => self.start = color,
5556
Message::EndChanged(color) => self.end = color,
5657
Message::AngleChanged(angle) => self.angle = angle,
57-
Message::SetTransparent(transparent) => {
58-
self.transparent_window = transparent;
58+
Message::TransparentToggled(transparent) => {
59+
self.transparent = transparent;
5960
}
6061
}
6162
}
@@ -65,7 +66,7 @@ impl Sandbox for Gradient {
6566
start,
6667
end,
6768
angle,
68-
transparent_window,
69+
transparent,
6970
} = *self;
7071

7172
let gradient_box = container(horizontal_space(Length::Fill))
@@ -93,8 +94,8 @@ impl Sandbox for Gradient {
9394
.align_items(Alignment::Center);
9495

9596
let transparency_toggle = iced::widget::Container::new(
96-
checkbox("Transparent window", transparent_window)
97-
.on_toggle(Message::SetTransparent),
97+
checkbox("Transparent window", transparent)
98+
.on_toggle(Message::TransparentToggled),
9899
)
99100
.padding(8);
100101

@@ -108,17 +109,16 @@ impl Sandbox for Gradient {
108109
.into()
109110
}
110111

111-
fn theme(&self) -> iced::Theme {
112-
if self.transparent_window {
113-
iced::Theme::custom(
114-
String::new(),
115-
Palette {
116-
background: Color::TRANSPARENT,
117-
..iced::Theme::default().palette()
118-
},
119-
)
112+
fn style(&self) -> theme::Application {
113+
if self.transparent {
114+
theme::Application::custom(|theme: &Theme| {
115+
application::Appearance {
116+
background_color: Color::TRANSPARENT,
117+
text_color: theme.palette().text,
118+
}
119+
})
120120
} else {
121-
iced::Theme::default()
121+
theme::Application::Default
122122
}
123123
}
124124
}

style/src/theme.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ pub enum Application {
172172
Custom(Box<dyn application::StyleSheet<Style = Theme>>),
173173
}
174174

175+
impl Application {
176+
/// Creates a custom [`Application`] style.
177+
pub fn custom(
178+
custom: impl application::StyleSheet<Style = Theme> + 'static,
179+
) -> Self {
180+
Self::Custom(Box::new(custom))
181+
}
182+
}
183+
175184
impl application::StyleSheet for Theme {
176185
type Style = Application;
177186

@@ -196,14 +205,6 @@ impl<T: Fn(&Theme) -> application::Appearance> application::StyleSheet for T {
196205
}
197206
}
198207

199-
impl<T: Fn(&Theme) -> application::Appearance + 'static> From<T>
200-
for Application
201-
{
202-
fn from(f: T) -> Self {
203-
Self::Custom(Box::new(f))
204-
}
205-
}
206-
207208
/// The style of a button.
208209
#[derive(Default)]
209210
pub enum Button {

0 commit comments

Comments
 (0)