Skip to content

Commit

Permalink
examples: Update composite_template to use template callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jf2048 committed Nov 10, 2021
1 parent b11d782 commit dffbca4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
2 changes: 2 additions & 0 deletions examples/composite_template/ex_menu_button/ex_menu_button.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</property>
<child>
<object class="GtkToggleButton" id="toggle">
<signal name="toggled" handler="toggle_toggled" swapped="true"/>
<property name="child">
<object class="GtkBox">
<property name="spacing">6</property>
Expand All @@ -28,6 +29,7 @@
</child>
<child>
<object class="GtkPopover" id="popover">
<signal name="closed" handler="popover_closed" swapped="true"/>
<property name="child">
<object class="GtkLabel">
<property name="label">Hello from a custom child widget!</property>
Expand Down
32 changes: 14 additions & 18 deletions examples/composite_template/ex_menu_button/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use gtk::subclass::prelude::*;
use gtk::{glib, CompositeTemplate};

#[derive(Debug, Default, CompositeTemplate)]
#[template(file = "ex_menu_button.ui")]
#[template(file = "ex_menu_button.ui", callbacks)]
pub struct ExMenuButton {
#[template_child]
pub toggle: TemplateChild<gtk::ToggleButton>,
Expand All @@ -26,25 +26,21 @@ impl ObjectSubclass for ExMenuButton {
}
}

impl ObjectImpl for ExMenuButton {
fn constructed(&self, obj: &Self::Type) {
self.parent_constructed(obj);

let popover = &*self.popover;
self.toggle
.connect_toggled(glib::clone!(@weak popover => move |toggle| {
if toggle.is_active() {
popover.popup();
}
}));

let toggle = &*self.toggle;
self.popover
.connect_closed(glib::clone!(@weak toggle => move |_| {
toggle.set_active(false);
}));
#[gtk::template_callbacks]
impl ExMenuButton {
#[template_callback]
fn toggle_toggled(&self, toggle: gtk::ToggleButton) {
if toggle.is_active() {
self.popover.popup();
}
}
#[template_callback(name = "popover_closed")]
fn unset_toggle(&self) {
self.toggle.set_active(false);
}
}

impl ObjectImpl for ExMenuButton {
// Needed for direct subclasses of GtkWidget;
// Here you need to unparent all direct children
// of your template.
Expand Down

0 comments on commit dffbca4

Please sign in to comment.