|
| 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 | + first: Color, |
| 13 | + second: Color, |
| 14 | + angle: f32, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug, Clone, Copy)] |
| 18 | +enum Message { |
| 19 | + FirstChanged(Color), |
| 20 | + SecondChanged(Color), |
| 21 | + AngleChanged(f32), |
| 22 | +} |
| 23 | + |
| 24 | +impl Sandbox for Gradient { |
| 25 | + type Message = Message; |
| 26 | + |
| 27 | + fn new() -> Self { |
| 28 | + let first = Color::new(0.2784314, 0.0627451, 0.4117647, 1.0); |
| 29 | + let second = Color::new(0.1882353, 0.772549, 0.8235294, 1.0); |
| 30 | + |
| 31 | + Self { |
| 32 | + first, |
| 33 | + second, |
| 34 | + angle: 0.0, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + fn title(&self) -> String { |
| 39 | + String::from("Color gradient") |
| 40 | + } |
| 41 | + |
| 42 | + fn update(&mut self, message: Message) { |
| 43 | + match message { |
| 44 | + Message::FirstChanged(color) => self.first = color, |
| 45 | + Message::SecondChanged(color) => self.second = color, |
| 46 | + Message::AngleChanged(angle) => self.angle = angle, |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + fn view(&self) -> Element<Message> { |
| 51 | + let first = self.first; |
| 52 | + let second = self.second; |
| 53 | + let angle = self.angle; |
| 54 | + |
| 55 | + let gradient_box = container(text("")) |
| 56 | + .width(Length::Fill) |
| 57 | + .height(Length::Fill) |
| 58 | + .center_x() |
| 59 | + .center_y() |
| 60 | + .style(move |_: &_| { |
| 61 | + let gradient = gradient::Linear::new(Radians(angle)) |
| 62 | + .add_stop(0.0, first) |
| 63 | + .add_stop(1.0, second) |
| 64 | + .into(); |
| 65 | + |
| 66 | + container::Appearance { |
| 67 | + text_color: None, |
| 68 | + background: Some(Background::Gradient(gradient)), |
| 69 | + border_radius: BorderRadius::default(), |
| 70 | + border_width: 0.0, |
| 71 | + border_color: Color::new(0.0, 0.0, 0.0, 0.0), |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + let range = 0.0..=1.0; |
| 76 | + let l = self.first; |
| 77 | + let r = self.second; |
| 78 | + |
| 79 | + let first_color_picker = row![ |
| 80 | + text("First").width(64), |
| 81 | + slider(range.clone(), l.r, move |v| { |
| 82 | + Message::FirstChanged(Color::new(v, l.g, l.b, l.a)) |
| 83 | + }) |
| 84 | + .step(0.01), |
| 85 | + slider(range.clone(), l.g, move |v| { |
| 86 | + Message::FirstChanged(Color::new(l.r, v, l.b, l.a)) |
| 87 | + }) |
| 88 | + .step(0.01), |
| 89 | + slider(range.clone(), l.b, move |v| { |
| 90 | + Message::FirstChanged(Color::new(l.r, l.g, v, l.a)) |
| 91 | + }) |
| 92 | + .step(0.01), |
| 93 | + ] |
| 94 | + .spacing(8) |
| 95 | + .padding(8) |
| 96 | + .align_items(Alignment::Center); |
| 97 | + |
| 98 | + let second_color_picker = row![ |
| 99 | + text("Second").width(64), |
| 100 | + slider(range.clone(), r.r, move |v| { |
| 101 | + Message::SecondChanged(Color::new(v, r.g, r.b, r.a)) |
| 102 | + }) |
| 103 | + .step(0.01), |
| 104 | + slider(range.clone(), r.g, move |v| { |
| 105 | + Message::SecondChanged(Color::new(r.r, v, r.b, r.a)) |
| 106 | + }) |
| 107 | + .step(0.01), |
| 108 | + slider(range.clone(), r.b, move |v| { |
| 109 | + Message::SecondChanged(Color::new(r.r, r.g, v, r.a)) |
| 110 | + }) |
| 111 | + .step(0.01), |
| 112 | + ] |
| 113 | + .spacing(8) |
| 114 | + .padding(8) |
| 115 | + .align_items(Alignment::Center); |
| 116 | + |
| 117 | + let angle_picker = row![ |
| 118 | + text("Angle").width(64), |
| 119 | + slider(0.0..=std::f32::consts::PI * 2.0, self.angle, move |v| { |
| 120 | + Message::AngleChanged(v) |
| 121 | + }) |
| 122 | + .step(0.01) |
| 123 | + ] |
| 124 | + .spacing(8) |
| 125 | + .padding(8) |
| 126 | + .align_items(Alignment::Center); |
| 127 | + |
| 128 | + column![ |
| 129 | + first_color_picker, |
| 130 | + second_color_picker, |
| 131 | + angle_picker, |
| 132 | + gradient_box |
| 133 | + ] |
| 134 | + .into() |
| 135 | + } |
| 136 | +} |
0 commit comments