|
| 1 | +use std::cell::RefCell; |
| 2 | +use std::sync::OnceLock; |
| 3 | + |
| 4 | +use glib::Properties; |
| 5 | +use gtk::gdk; |
| 6 | +use gtk::prelude::*; |
| 7 | +use gtk::subclass::prelude::*; |
| 8 | +use gtk::{AccessibleRole, GestureClick, Label, accessible, glib}; |
| 9 | + |
| 10 | +// ANCHOR: subclass |
| 11 | +#[derive(Properties, Default)] |
| 12 | +#[properties(wrapper_type = super::CustomButton)] |
| 13 | +pub struct CustomButton { |
| 14 | + #[property(get, set)] |
| 15 | + label: RefCell<String>, |
| 16 | + child: RefCell<Option<Label>>, |
| 17 | +} |
| 18 | + |
| 19 | +#[glib::object_subclass] |
| 20 | +impl ObjectSubclass for CustomButton { |
| 21 | + const NAME: &'static str = "CustomButton"; |
| 22 | + type Type = super::CustomButton; |
| 23 | + type ParentType = gtk::Widget; |
| 24 | + |
| 25 | + fn class_init(klass: &mut Self::Class) { |
| 26 | + // Set the accessible role to Button |
| 27 | + klass.set_accessible_role(AccessibleRole::Button); |
| 28 | + klass.set_css_name("custom-button"); |
| 29 | + klass.set_layout_manager_type::<gtk::BinLayout>(); |
| 30 | + |
| 31 | + // Bind keyboard shortcuts for activation (Enter and Space) |
| 32 | + klass.add_binding_signal( |
| 33 | + gdk::Key::space, |
| 34 | + gdk::ModifierType::empty(), |
| 35 | + "activate", |
| 36 | + ); |
| 37 | + klass.add_binding_signal( |
| 38 | + gdk::Key::KP_Enter, |
| 39 | + gdk::ModifierType::empty(), |
| 40 | + "activate", |
| 41 | + ); |
| 42 | + klass.add_binding_signal( |
| 43 | + gdk::Key::Return, |
| 44 | + gdk::ModifierType::empty(), |
| 45 | + "activate", |
| 46 | + ); |
| 47 | + } |
| 48 | +} |
| 49 | +// ANCHOR_END: subclass |
| 50 | + |
| 51 | +// ANCHOR: object_impl |
| 52 | +#[glib::derived_properties] |
| 53 | +impl ObjectImpl for CustomButton { |
| 54 | + fn signals() -> &'static [glib::subclass::Signal] { |
| 55 | + static SIGNALS: OnceLock<Vec<glib::subclass::Signal>> = OnceLock::new(); |
| 56 | + SIGNALS.get_or_init(|| { |
| 57 | + vec![glib::subclass::Signal::builder("activate").action().build()] |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + fn constructed(&self) { |
| 62 | + self.parent_constructed(); |
| 63 | + |
| 64 | + let obj = self.obj(); |
| 65 | + // Make the widget focusable so keyboard users can reach it |
| 66 | + obj.set_focusable(true); |
| 67 | + // Also allow focusing by clicking |
| 68 | + obj.set_focus_on_click(true); |
| 69 | + |
| 70 | + // Create a child label and bind its text to our "label" property |
| 71 | + let child = Label::new(None); |
| 72 | + child.set_parent(&*obj); |
| 73 | + obj.update_relation(&[accessible::Relation::LabelledBy(&[child.upcast_ref()])]); |
| 74 | + obj.bind_property("label", &child, "label") |
| 75 | + .sync_create() |
| 76 | + .build(); |
| 77 | + self.child.replace(Some(child)); |
| 78 | + |
| 79 | + // Handle click events |
| 80 | + let gesture = GestureClick::new(); |
| 81 | + let button = obj.downgrade(); |
| 82 | + gesture.connect_released(move |_, _, _, _| { |
| 83 | + if let Some(button) = button.upgrade() { |
| 84 | + button.emit_by_name::<()>("activate", &[]); |
| 85 | + } |
| 86 | + }); |
| 87 | + obj.add_controller(gesture); |
| 88 | + |
| 89 | + // Add an activation handler |
| 90 | + obj.connect_local("activate", false, move |values| { |
| 91 | + let button = values[0].get::<super::CustomButton>().unwrap(); |
| 92 | + println!("Button '{}' activated!", button.label()); |
| 93 | + None |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + fn dispose(&self) { |
| 98 | + while let Some(child) = self.obj().first_child() { |
| 99 | + child.unparent(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl WidgetImpl for CustomButton {} |
| 105 | +// ANCHOR_END: object_impl |
0 commit comments