summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-05-01 22:45:47 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-05-01 22:45:47 +0200
commit03ca7eea6c05b32c6273284c35883506e4cf6eb1 (patch)
treee13d1cb3c850a04ce814549e5c53d762a8b3a86e /examples
parent573929d5ec99981ae3a4a0d675f1248932d56e61 (diff)
downloadiced-03ca7eea6c05b32c6273284c35883506e4cf6eb1.tar.gz
iced-03ca7eea6c05b32c6273284c35883506e4cf6eb1.tar.bz2
iced-03ca7eea6c05b32c6273284c35883506e4cf6eb1.zip
Reuse triangle path with transforms in `color_palette`
Diffstat (limited to 'examples')
-rw-r--r--examples/color_palette/src/main.rs71
1 files changed, 28 insertions, 43 deletions
diff --git a/examples/color_palette/src/main.rs b/examples/color_palette/src/main.rs
index ff399e76..b3ad98d0 100644
--- a/examples/color_palette/src/main.rs
+++ b/examples/color_palette/src/main.rs
@@ -1,6 +1,6 @@
use iced::{
canvas, slider, Align, Canvas, Color, Column, Element, Length, Row,
- Sandbox, Settings, Slider, Text,
+ Sandbox, Settings, Slider, Text, Vector,
};
use palette::{self, Limited};
use std::marker::PhantomData;
@@ -146,7 +146,7 @@ impl Theme {
impl canvas::Drawable for Theme {
fn draw(&self, frame: &mut canvas::Frame) {
- use canvas::{Fill, Path};
+ use canvas::Path;
use iced::{HorizontalAlignment, VerticalAlignment};
use iced_native::{Point, Size};
use palette::{Hsl, Srgb};
@@ -158,6 +158,13 @@ impl canvas::Drawable for Theme {
height: frame.height() / 2.0 - pad,
};
+ let triangle = Path::new(|path| {
+ path.move_to(Point { x: 0.0, y: -0.5 });
+ path.line_to(Point { x: -0.5, y: 0.0 });
+ path.line_to(Point { x: 0.5, y: 0.0 });
+ path.close();
+ });
+
let mut text = canvas::Text {
horizontal_alignment: HorizontalAlignment::Center,
vertical_alignment: VerticalAlignment::Top,
@@ -171,48 +178,26 @@ impl canvas::Drawable for Theme {
y: 0.0,
};
let rect = Path::rectangle(anchor, box_size);
- frame.fill(&rect, Fill::Color(color));
-
- if self.base == color {
- let cx = anchor.x + box_size.width / 2.0;
- let tri_w = 10.0;
-
- let tri = Path::new(|path| {
- path.move_to(Point {
- x: cx - tri_w,
- y: 0.0,
- });
- path.line_to(Point {
- x: cx + tri_w,
- y: 0.0,
- });
- path.line_to(Point { x: cx, y: tri_w });
- path.line_to(Point {
- x: cx - tri_w,
- y: 0.0,
- });
+ frame.fill(&rect, color);
+
+ // We show a little indicator for the base color
+ if color == self.base {
+ let triangle_x = anchor.x + box_size.width / 2.0;
+
+ frame.with_save(|frame| {
+ frame.translate(Vector::new(triangle_x, 0.0));
+ frame.scale(10.0);
+ frame.rotate(std::f32::consts::PI);
+
+ frame.fill(&triangle, Color::WHITE);
});
- frame.fill(&tri, Fill::Color(Color::WHITE));
-
- let tri = Path::new(|path| {
- path.move_to(Point {
- x: cx - tri_w,
- y: box_size.height,
- });
- path.line_to(Point {
- x: cx + tri_w,
- y: box_size.height,
- });
- path.line_to(Point {
- x: cx,
- y: box_size.height - tri_w,
- });
- path.line_to(Point {
- x: cx - tri_w,
- y: box_size.height,
- });
+
+ frame.with_save(|frame| {
+ frame.translate(Vector::new(triangle_x, box_size.height));
+ frame.scale(10.0);
+
+ frame.fill(&triangle, Color::WHITE);
});
- frame.fill(&tri, Fill::Color(Color::WHITE));
}
frame.fill_text(canvas::Text {
@@ -242,7 +227,7 @@ impl canvas::Drawable for Theme {
};
let rect = Path::rectangle(anchor, box_size);
- frame.fill(&rect, Fill::Color(color));
+ frame.fill(&rect, color);
frame.fill_text(canvas::Text {
content: color_hex_string(&color),