|
| 1 | +use iced::widget::{column, container, row, slider, text}; |
| 2 | +use iced::{ |
| 3 | + gradient, Alignment, Background, BorderRadius, Color, Element, Length, |
| 4 | + Radians, Sandbox, Settings, |
| 5 | +}; |
| 6 | + |
| 7 | +pub fn main() -> iced::Result { |
| 8 | + Gradient::run(Settings::default()) |
| 9 | +} |
| 10 | + |
| 11 | +struct Gradient { |
| 12 | + left: Color, |
| 13 | + right: Color, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Clone, Copy)] |
| 17 | +enum Message { |
| 18 | + LeftChanged(Color), |
| 19 | + RightChanged(Color), |
| 20 | +} |
| 21 | + |
| 22 | +impl Sandbox for Gradient { |
| 23 | + type Message = Message; |
| 24 | + |
| 25 | + fn new() -> Self { |
| 26 | + let left = Color::new(0.2784314, 0.0627451, 0.4117647, 1.0); |
| 27 | + let right = Color::new(0.1882353, 0.772549, 0.8235294, 1.0); |
| 28 | + Self { left, right } |
| 29 | + } |
| 30 | + |
| 31 | + fn title(&self) -> String { |
| 32 | + String::from("Color gradient") |
| 33 | + } |
| 34 | + |
| 35 | + fn update(&mut self, message: Message) { |
| 36 | + match message { |
| 37 | + Message::LeftChanged(color) => self.left = color, |
| 38 | + Message::RightChanged(color) => self.right = color, |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + fn view(&self) -> Element<Message> { |
| 43 | + let left = self.left; |
| 44 | + let right = self.right; |
| 45 | + |
| 46 | + let gradient_box = container(text("")) |
| 47 | + .width(Length::Fill) |
| 48 | + .height(Length::Fill) |
| 49 | + .center_x() |
| 50 | + .center_y() |
| 51 | + .style(move |_: &_| { |
| 52 | + let gradient = gradient::Linear::new(Radians(0.0)) |
| 53 | + .add_stop(0.0, left) |
| 54 | + .add_stop(1.0, right) |
| 55 | + .into(); |
| 56 | + |
| 57 | + container::Appearance { |
| 58 | + text_color: None, |
| 59 | + background: Some(Background::Gradient(gradient)), |
| 60 | + border_radius: BorderRadius::default(), |
| 61 | + border_width: 0.0, |
| 62 | + border_color: Color::new(0.0, 0.0, 0.0, 0.0), |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + let range = 0.0..=1.0; |
| 67 | + let l = self.left; |
| 68 | + let r = self.right; |
| 69 | + |
| 70 | + let left_color_picker = row![ |
| 71 | + text("Right").width(48), |
| 72 | + slider(range.clone(), l.r, move |v| { |
| 73 | + Message::LeftChanged(Color::new(v, l.g, l.b, l.a)) |
| 74 | + }) |
| 75 | + .step(0.01), |
| 76 | + slider(range.clone(), l.g, move |v| { |
| 77 | + Message::LeftChanged(Color::new(l.r, v, l.b, l.a)) |
| 78 | + }) |
| 79 | + .step(0.01), |
| 80 | + slider(range.clone(), l.b, move |v| { |
| 81 | + Message::LeftChanged(Color::new(l.r, l.g, v, l.a)) |
| 82 | + }) |
| 83 | + .step(0.01), |
| 84 | + ] |
| 85 | + .spacing(8) |
| 86 | + .padding(8) |
| 87 | + .align_items(Alignment::Center); |
| 88 | + |
| 89 | + let right_color_picker = row![ |
| 90 | + text("Left").width(48), |
| 91 | + slider(range.clone(), r.r, move |v| { |
| 92 | + Message::RightChanged(Color::new(v, r.g, r.b, r.a)) |
| 93 | + }) |
| 94 | + .step(0.01), |
| 95 | + slider(range.clone(), r.g, move |v| { |
| 96 | + Message::RightChanged(Color::new(r.r, v, r.b, r.a)) |
| 97 | + }) |
| 98 | + .step(0.01), |
| 99 | + slider(range.clone(), r.b, move |v| { |
| 100 | + Message::RightChanged(Color::new(r.r, r.g, v, r.a)) |
| 101 | + }) |
| 102 | + .step(0.01), |
| 103 | + ] |
| 104 | + .spacing(8) |
| 105 | + .padding(8) |
| 106 | + .align_items(Alignment::Center); |
| 107 | + |
| 108 | + column![right_color_picker, left_color_picker, gradient_box].into() |
| 109 | + } |
| 110 | +} |
0 commit comments