iced-yoda/examples/gradient/src/main.rs

147 lines
3.8 KiB
Rust
Raw Normal View History

use iced::application;
use iced::widget::{
checkbox, column, container, horizontal_space, row, slider, text, themer,
};
use iced::{gradient, window};
2023-09-02 13:44:45 +02:00
use iced::{
Alignment, Background, Color, Element, Length, Radians, Sandbox, Settings,
2024-03-06 21:27:03 +01:00
Theme,
2023-09-02 13:44:45 +02:00
};
pub fn main() -> iced::Result {
tracing_subscriber::fmt::init();
Gradient::run(Settings {
window: window::Settings {
transparent: true,
..Default::default()
},
..Default::default()
})
2023-09-02 13:44:45 +02:00
}
2023-09-07 07:37:57 +02:00
#[derive(Debug, Clone, Copy)]
2023-09-02 13:44:45 +02:00
struct Gradient {
2023-09-07 07:37:57 +02:00
start: Color,
end: Color,
angle: Radians,
transparent: bool,
2023-09-02 13:44:45 +02:00
}
#[derive(Debug, Clone, Copy)]
enum Message {
2023-09-07 07:37:57 +02:00
StartChanged(Color),
EndChanged(Color),
AngleChanged(Radians),
TransparentToggled(bool),
2023-09-02 13:44:45 +02:00
}
impl Sandbox for Gradient {
type Message = Message;
fn new() -> Self {
Self {
start: Color::WHITE,
end: Color::new(0.0, 0.0, 1.0, 1.0),
angle: Radians(0.0),
transparent: false,
2023-09-02 13:44:45 +02:00
}
}
fn title(&self) -> String {
2023-09-07 07:37:57 +02:00
String::from("Gradient")
2023-09-02 13:44:45 +02:00
}
fn update(&mut self, message: Message) {
match message {
2023-09-07 07:37:57 +02:00
Message::StartChanged(color) => self.start = color,
Message::EndChanged(color) => self.end = color,
2023-09-02 13:44:45 +02:00
Message::AngleChanged(angle) => self.angle = angle,
Message::TransparentToggled(transparent) => {
self.transparent = transparent;
}
2023-09-02 13:44:45 +02:00
}
}
fn view(&self) -> Element<Message> {
let Self {
start,
end,
angle,
transparent,
} = *self;
2023-09-02 13:44:45 +02:00
let appearance = {
let gradient = gradient::Linear::new(angle)
.add_stop(0.0, start)
.add_stop(1.0, end)
.into();
2023-09-02 13:44:45 +02:00
container::Appearance {
background: Some(Background::Gradient(gradient)),
..Default::default()
}
};
let gradient_box = themer(
appearance,
container(horizontal_space())
.width(Length::Fill)
.height(Length::Fill),
);
2023-09-02 13:44:45 +02:00
let angle_picker = row![
text("Angle").width(64),
slider(Radians::RANGE, self.angle, Message::AngleChanged)
.step(0.01)
2023-09-02 13:44:45 +02:00
]
.spacing(8)
.padding(8)
.align_items(Alignment::Center);
let transparency_toggle = iced::widget::Container::new(
checkbox("Transparent window", transparent)
.on_toggle(Message::TransparentToggled),
)
.padding(8);
2023-09-02 13:44:45 +02:00
column![
2023-09-07 07:37:57 +02:00
color_picker("Start", self.start).map(Message::StartChanged),
color_picker("End", self.end).map(Message::EndChanged),
2023-09-02 13:44:45 +02:00
angle_picker,
transparency_toggle,
gradient_box,
2023-09-02 13:44:45 +02:00
]
.into()
}
2024-03-06 21:27:03 +01:00
fn style(&self, theme: &Theme) -> application::Appearance {
if self.transparent {
2024-03-06 21:27:03 +01:00
application::Appearance {
background_color: Color::TRANSPARENT,
text_color: theme.palette().text,
}
} else {
2024-03-06 21:27:03 +01:00
application::default(theme)
}
}
2023-09-02 13:44:45 +02:00
}
2023-09-07 07:37:57 +02:00
fn color_picker(label: &str, color: Color) -> Element<'_, Color> {
row![
text(label).width(64),
slider(0.0..=1.0, color.r, move |r| { Color { r, ..color } })
.step(0.01),
slider(0.0..=1.0, color.g, move |g| { Color { g, ..color } })
.step(0.01),
slider(0.0..=1.0, color.b, move |b| { Color { b, ..color } })
.step(0.01),
slider(0.0..=1.0, color.a, move |a| { Color { a, ..color } })
.step(0.01),
2023-09-07 07:37:57 +02:00
]
.spacing(8)
.padding(8)
.align_items(Alignment::Center)
.into()
}