|
| 1 | +use gtk::prelude::*; |
| 2 | +use gtk::subclass::prelude::*; |
| 3 | +use gtk::{gdk, glib, graphene, gsk}; |
| 4 | + |
| 5 | +pub struct CustomPaintable { |
| 6 | + pub(super) height: i32, |
| 7 | + pub(super) width: i32, |
| 8 | + pub(super) paths: Vec<gsk::Path>, |
| 9 | + pub(super) colors: Vec<gdk::RGBA>, |
| 10 | + pub(super) stroke_path: gsk::Path, |
| 11 | + pub(super) stroke1: gsk::Stroke, |
| 12 | + pub(super) stroke2: gsk::Stroke, |
| 13 | + pub(super) stroke_color: gdk::RGBA, |
| 14 | +} |
| 15 | + |
| 16 | +impl Default for CustomPaintable { |
| 17 | + fn default() -> Self { |
| 18 | + /* Paths and colors extracted from gtk-logo.svg */ |
| 19 | + let paths = vec![ |
| 20 | + gsk::Path::parse("m3.12,66.17 -2.06,-51.46 32.93,24.7 v55.58 l-30.87,-28.82 z") |
| 21 | + .unwrap(), |
| 22 | + gsk::Path::parse("m34,95 49.4,-20.58 4.12,-51.46 -53.52,16.47 v55.58 z").unwrap(), |
| 23 | + gsk::Path::parse("m1.06,14.71 32.93,24.7 53.52,-16.47 -36.75,-21.88 -49.7,13.65 z") |
| 24 | + .unwrap(), |
| 25 | + ]; |
| 26 | + let colors = vec![ |
| 27 | + gdk::RGBA::parse("#e40000").unwrap(), |
| 28 | + gdk::RGBA::parse("#7fe719").unwrap(), |
| 29 | + gdk::RGBA::parse("#729fcf").unwrap(), |
| 30 | + ]; |
| 31 | + let stroke_path = gsk::Path::parse("m50.6,51.3 -47.3,14 z l33,23 z v-50").unwrap(); |
| 32 | + let stroke1 = gsk::Stroke::new(2.12); |
| 33 | + let stroke2 = gsk::Stroke::new(1.25); |
| 34 | + let stroke_color = gdk::RGBA::WHITE; |
| 35 | + |
| 36 | + let mut bounds = paths[0].stroke_bounds(&stroke1).unwrap(); |
| 37 | + let bounds2 = paths[1].stroke_bounds(&stroke1).unwrap(); |
| 38 | + bounds = bounds.union(&bounds2); |
| 39 | + let bounds3 = paths[2].stroke_bounds(&stroke1).unwrap(); |
| 40 | + bounds = bounds.union(&bounds3); |
| 41 | + let bounds_path = stroke_path.stroke_bounds(&stroke2).unwrap(); |
| 42 | + bounds = bounds.union(&bounds_path); |
| 43 | + let width = bounds.x() + bounds.width(); |
| 44 | + let height = bounds.y() + bounds.height(); |
| 45 | + Self { |
| 46 | + height: height as i32, |
| 47 | + width: width as i32, |
| 48 | + paths, |
| 49 | + colors, |
| 50 | + stroke_path, |
| 51 | + stroke1, |
| 52 | + stroke2, |
| 53 | + stroke_color, |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[glib::object_subclass] |
| 59 | +impl ObjectSubclass for CustomPaintable { |
| 60 | + const NAME: &'static str = "CustomPaintable"; |
| 61 | + type Type = super::CustomPaintable; |
| 62 | + type Interfaces = (gdk::Paintable,); |
| 63 | +} |
| 64 | + |
| 65 | +impl ObjectImpl for CustomPaintable {} |
| 66 | + |
| 67 | +impl PaintableImpl for CustomPaintable { |
| 68 | + fn flags(&self) -> gdk::PaintableFlags { |
| 69 | + // Fixed size and content |
| 70 | + gdk::PaintableFlags::SIZE | gdk::PaintableFlags::CONTENTS |
| 71 | + } |
| 72 | + |
| 73 | + fn intrinsic_width(&self) -> i32 { |
| 74 | + self.width |
| 75 | + } |
| 76 | + |
| 77 | + fn intrinsic_height(&self) -> i32 { |
| 78 | + self.height |
| 79 | + } |
| 80 | + |
| 81 | + fn snapshot(&self, snapshot: &gdk::Snapshot, width: f64, height: f64) { |
| 82 | + let bounds = graphene::Rect::new(0., 0., width as f32, height as f32); |
| 83 | + for (i, path) in self.paths.iter().enumerate() { |
| 84 | + snapshot.push_fill(path, gsk::FillRule::Winding); |
| 85 | + snapshot.append_color(&self.colors[i], &bounds); |
| 86 | + snapshot.pop(); |
| 87 | + } |
| 88 | + |
| 89 | + for _ in 0..3 { |
| 90 | + snapshot.push_stroke(&self.stroke_path, &self.stroke1); |
| 91 | + snapshot.append_color(&self.stroke_color, &bounds); |
| 92 | + snapshot.pop(); |
| 93 | + } |
| 94 | + |
| 95 | + snapshot.push_stroke(&self.stroke_path, &self.stroke2); |
| 96 | + snapshot.append_color(&self.stroke_color, &bounds); |
| 97 | + snapshot.pop(); |
| 98 | + } |
| 99 | +} |
0 commit comments