Use custom Application::style to enable transparency

This commit is contained in:
Héctor Ramón Jiménez 2024-02-10 00:32:03 +01:00
parent 712c8e53f2
commit 81bed94148
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 28 additions and 27 deletions

View file

@ -1,4 +1,5 @@
use iced::theme::Palette; use iced::application;
use iced::theme::{self, Theme};
use iced::widget::{ use iced::widget::{
checkbox, column, container, horizontal_space, row, slider, text, checkbox, column, container, horizontal_space, row, slider, text,
}; };
@ -22,7 +23,7 @@ struct Gradient {
start: Color, start: Color,
end: Color, end: Color,
angle: Radians, angle: Radians,
transparent_window: bool, transparent: bool,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -30,7 +31,7 @@ enum Message {
StartChanged(Color), StartChanged(Color),
EndChanged(Color), EndChanged(Color),
AngleChanged(Radians), AngleChanged(Radians),
SetTransparent(bool), TransparentToggled(bool),
} }
impl Sandbox for Gradient { impl Sandbox for Gradient {
@ -41,7 +42,7 @@ impl Sandbox for Gradient {
start: Color::WHITE, start: Color::WHITE,
end: Color::new(0.0, 0.0, 1.0, 1.0), end: Color::new(0.0, 0.0, 1.0, 1.0),
angle: Radians(0.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::StartChanged(color) => self.start = color,
Message::EndChanged(color) => self.end = color, Message::EndChanged(color) => self.end = color,
Message::AngleChanged(angle) => self.angle = angle, Message::AngleChanged(angle) => self.angle = angle,
Message::SetTransparent(transparent) => { Message::TransparentToggled(transparent) => {
self.transparent_window = transparent; self.transparent = transparent;
} }
} }
} }
@ -65,7 +66,7 @@ impl Sandbox for Gradient {
start, start,
end, end,
angle, angle,
transparent_window, transparent,
} = *self; } = *self;
let gradient_box = container(horizontal_space(Length::Fill)) let gradient_box = container(horizontal_space(Length::Fill))
@ -93,8 +94,8 @@ impl Sandbox for Gradient {
.align_items(Alignment::Center); .align_items(Alignment::Center);
let transparency_toggle = iced::widget::Container::new( let transparency_toggle = iced::widget::Container::new(
checkbox("Transparent window", transparent_window) checkbox("Transparent window", transparent)
.on_toggle(Message::SetTransparent), .on_toggle(Message::TransparentToggled),
) )
.padding(8); .padding(8);
@ -108,17 +109,16 @@ impl Sandbox for Gradient {
.into() .into()
} }
fn theme(&self) -> iced::Theme { fn style(&self) -> theme::Application {
if self.transparent_window { if self.transparent {
iced::Theme::custom( theme::Application::custom(|theme: &Theme| {
String::new(), application::Appearance {
Palette { background_color: Color::TRANSPARENT,
background: Color::TRANSPARENT, text_color: theme.palette().text,
..iced::Theme::default().palette() }
}, })
)
} else { } else {
iced::Theme::default() theme::Application::Default
} }
} }
} }

View file

@ -172,6 +172,15 @@ pub enum Application {
Custom(Box<dyn application::StyleSheet<Style = Theme>>), 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 { impl application::StyleSheet for Theme {
type Style = Application; 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. /// The style of a button.
#[derive(Default)] #[derive(Default)]
pub enum Button { pub enum Button {