From 80e2d1b08bd8e6d9a049367d49c1508ce8ffe2a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 4 May 2020 23:46:15 +0200 Subject: [PATCH] Adapt `color_palette` to new `canvas` API --- examples/color_palette/src/main.rs | 48 +++++++++++++++++------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/examples/color_palette/src/main.rs b/examples/color_palette/src/main.rs index 073a6734..cec6ac79 100644 --- a/examples/color_palette/src/main.rs +++ b/examples/color_palette/src/main.rs @@ -1,9 +1,10 @@ +use iced::canvas::{self, Cursor, Frame, Geometry, Path}; use iced::{ - canvas, slider, Align, Canvas, Color, Column, Element, HorizontalAlignment, - Length, Point, Row, Sandbox, Settings, Size, Slider, Text, Vector, + slider, Align, Canvas, Color, Column, Element, HorizontalAlignment, Length, + Point, Rectangle, Row, Sandbox, Settings, Size, Slider, Text, Vector, VerticalAlignment, }; -use palette::{self, Limited}; +use palette::{self, Hsl, Limited, Srgb}; use std::marker::PhantomData; use std::ops::RangeInclusive; @@ -23,7 +24,6 @@ pub struct ColorPalette { hwb: ColorPicker, lab: ColorPicker, lch: ColorPicker, - canvas_layer: canvas::layer::Cache, } #[derive(Debug, Clone, Copy)] @@ -58,7 +58,6 @@ impl Sandbox for ColorPalette { }; self.theme = Theme::new(srgb.clamp()); - self.canvas_layer.clear(); } fn view(&mut self) -> Element { @@ -80,12 +79,7 @@ impl Sandbox for ColorPalette { .push(self.hwb.view(hwb).map(Message::HwbColorChanged)) .push(self.lab.view(lab).map(Message::LabColorChanged)) .push(self.lch.view(lch).map(Message::LchColorChanged)) - .push( - Canvas::new() - .width(Length::Fill) - .height(Length::Fill) - .push(self.canvas_layer.with(&self.theme)), - ) + .push(self.theme.view()) .into() } } @@ -95,11 +89,12 @@ pub struct Theme { lower: Vec, base: Color, higher: Vec, + canvas_cache: canvas::Cache, } impl Theme { pub fn new(base: impl Into) -> Theme { - use palette::{Hsl, Hue, Shade, Srgb}; + use palette::{Hue, Shade}; let base = base.into(); @@ -130,6 +125,7 @@ impl Theme { .iter() .map(|&color| Srgb::from(color).clamp().into()) .collect(), + canvas_cache: canvas::Cache::default(), } } @@ -143,13 +139,15 @@ impl Theme { .chain(std::iter::once(&self.base)) .chain(self.higher.iter()) } -} -impl canvas::Drawable for Theme { - fn draw(&self, frame: &mut canvas::Frame) { - use canvas::Path; - use palette::{Hsl, Srgb}; + pub fn view(&mut self) -> Element { + Canvas::new(self) + .width(Length::Fill) + .height(Length::Fill) + .into() + } + fn draw(&self, frame: &mut Frame) { let pad = 20.0; let box_size = Size { @@ -176,8 +174,7 @@ impl canvas::Drawable for Theme { x: (i as f32) * box_size.width, y: 0.0, }; - let rect = Path::rectangle(anchor, box_size); - frame.fill(&rect, color); + frame.fill_rectangle(anchor, box_size, color); // We show a little indicator for the base color if color == self.base { @@ -225,8 +222,7 @@ impl canvas::Drawable for Theme { y: box_size.height + 2.0 * pad, }; - let rect = Path::rectangle(anchor, box_size); - frame.fill(&rect, color); + frame.fill_rectangle(anchor, box_size, color); frame.fill_text(canvas::Text { content: color_hex_string(&color), @@ -240,6 +236,16 @@ impl canvas::Drawable for Theme { } } +impl canvas::Program for Theme { + fn draw(&self, bounds: Rectangle, _cursor: Cursor) -> Vec { + let theme = self.canvas_cache.draw(bounds.size(), |frame| { + self.draw(frame); + }); + + vec![theme] + } +} + impl Default for Theme { fn default() -> Self { Theme::new(Color::from_rgb8(75, 128, 190))