|
| 1 | +use gtk4::glib; |
| 2 | + |
| 3 | +glib::wrapper! { |
| 4 | + pub struct ColourButton(ObjectSubclass<underlying::ColourButton>) |
| 5 | + @extends gtk4::Widget, gtk4::Button, |
| 6 | + @implements gtk4::Accessible, gtk4::Buildable, gtk4::ConstraintTarget, gtk4::Actionable; |
| 7 | +} |
| 8 | + |
| 9 | +mod underlying { |
| 10 | + use std::cell::Cell; |
| 11 | + |
| 12 | + use gtk4::{ |
| 13 | + gdk::RGBA, |
| 14 | + glib::{self, Properties}, |
| 15 | + graphene::Rect, |
| 16 | + prelude::*, |
| 17 | + subclass::prelude::*, |
| 18 | + }; |
| 19 | + use kcshot_data::colour::Colour; |
| 20 | + |
| 21 | + use crate::ext::DisposeExt; |
| 22 | + |
| 23 | + #[derive(Debug, Properties)] |
| 24 | + #[properties(wrapper_type = super::ColourButton)] |
| 25 | + pub struct ColourButton { |
| 26 | + #[property(get, set)] |
| 27 | + colour: Cell<Colour>, |
| 28 | + } |
| 29 | + |
| 30 | + impl Default for ColourButton { |
| 31 | + fn default() -> Self { |
| 32 | + Self { |
| 33 | + colour: Cell::new(Colour { |
| 34 | + red: 0, |
| 35 | + green: 0, |
| 36 | + blue: 0, |
| 37 | + alpha: 0, |
| 38 | + }), |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + #[glib::object_subclass] |
| 44 | + impl ObjectSubclass for ColourButton { |
| 45 | + const NAME: &'static str = "KCShotColourButton"; |
| 46 | + type Type = super::ColourButton; |
| 47 | + type ParentType = gtk4::Button; |
| 48 | + } |
| 49 | + |
| 50 | + #[glib::derived_properties] |
| 51 | + impl ObjectImpl for ColourButton { |
| 52 | + fn constructed(&self) { |
| 53 | + self.obj().set_halign(gtk4::Align::Center); |
| 54 | + self.obj().set_valign(gtk4::Align::Center); |
| 55 | + } |
| 56 | + |
| 57 | + fn dispose(&self) { |
| 58 | + self.obj().dispose_children(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + impl WidgetImpl for ColourButton { |
| 63 | + fn snapshot(&self, snapshot: >k4::Snapshot) { |
| 64 | + let colour = self.colour.get(); |
| 65 | + |
| 66 | + let w = self.obj().width() as f32; |
| 67 | + let h = self.obj().height() as f32; |
| 68 | + |
| 69 | + if colour.alpha != 0 { |
| 70 | + snapshot.append_color(&colour.into(), &Rect::new(0.0, 0.0, w, h)); |
| 71 | + } else { |
| 72 | + let black = RGBA::new(0.0, 0.0, 0.0, 1.0); |
| 73 | + let magenta = RGBA::new(1.0, 0.0, 0.8, 1.0); |
| 74 | + snapshot.append_color(&black, &Rect::new(0.0, 0.0, w / 2.0, h / 2.0)); |
| 75 | + snapshot.append_color(&magenta, &Rect::new(w / 2.0, 0.0, w / 2.0, h / 2.0)); |
| 76 | + snapshot.append_color(&magenta, &Rect::new(0.0, h / 2.0, w / 2.0, h / 2.0)); |
| 77 | + snapshot.append_color(&black, &Rect::new(w / 2.0, h / 2.0, w / 2.0, h / 2.0)); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + impl ButtonImpl for ColourButton {} |
| 83 | +} |
0 commit comments