diff --git a/examples/squeezer_bin/main.rs b/examples/squeezer_bin/main.rs index a7ae71d1e445..ae61af9b0c9e 100644 --- a/examples/squeezer_bin/main.rs +++ b/examples/squeezer_bin/main.rs @@ -14,7 +14,9 @@ fn main() -> glib::ExitCode { let mode_switch = gtk::Switch::new(); let switch_label = gtk::Label::new(Some("keep aspect ratio")); let squeezer = SqueezerBin::default(); - squeezer.set_child(Some(>k::Label::new(Some("Hello World!")))); + squeezer.set_child(Some( + gtk::Label::new(Some("Hello World!")).upcast::(), + )); headerbar.pack_start(&mode_switch); headerbar.pack_start(&switch_label); diff --git a/examples/squeezer_bin/squeezer_bin/imp.rs b/examples/squeezer_bin/squeezer_bin/imp.rs index 2b50984f3985..298f67e3a6a9 100644 --- a/examples/squeezer_bin/squeezer_bin/imp.rs +++ b/examples/squeezer_bin/squeezer_bin/imp.rs @@ -16,12 +16,43 @@ fn child_size(child: &impl IsA) -> ((i32, i32), (i32, i32)) { #[derive(Debug, Default, glib::Properties)] #[properties(wrapper_type = super::SqueezerBin)] pub struct SqueezerBin { - #[property(get, explicit_notify)] + #[property(get, set = Self::set_child, explicit_notify, nullable)] pub(super) child: RefCell>, - #[property(get, explicit_notify)] + #[property(get, set = Self::set_keep_aspect_ratio, explicit_notify)] pub(super) keep_aspect_ratio: Cell, } +impl SqueezerBin { + fn set_child(&self, widget: Option<>k::Widget>) { + if widget == self.child.borrow().as_ref() { + return; + } + + if let Some(child) = self.child.borrow_mut().take() { + child.unparent(); + } + + if let Some(w) = widget { + self.child.replace(Some(w.clone())); + w.set_parent(&*self.obj()); + } + + self.obj().queue_resize(); + self.obj().notify_child(); + } + + fn set_keep_aspect_ratio(&self, keep_aspect_ratio: bool) { + if self.keep_aspect_ratio.get() == keep_aspect_ratio { + return; + } + + self.keep_aspect_ratio.set(keep_aspect_ratio); + + self.obj().queue_resize(); + self.obj().notify_keep_aspect_ratio(); + } +} + #[glib::object_subclass] impl ObjectSubclass for SqueezerBin { const NAME: &'static str = "SqueezerBin"; diff --git a/examples/squeezer_bin/squeezer_bin/mod.rs b/examples/squeezer_bin/squeezer_bin/mod.rs index 82ffc72b34c2..e7fc5c91c879 100644 --- a/examples/squeezer_bin/squeezer_bin/mod.rs +++ b/examples/squeezer_bin/squeezer_bin/mod.rs @@ -1,6 +1,6 @@ mod imp; -use gtk::{glib, prelude::*, subclass::prelude::*}; +use gtk::glib; glib::wrapper! { pub struct SqueezerBin(ObjectSubclass) @@ -12,37 +12,3 @@ impl Default for SqueezerBin { glib::Object::new() } } - -impl SqueezerBin { - pub fn set_child(&self, widget: Option<&impl IsA>) { - let imp = self.imp(); - let widget = widget.map(|w| w.as_ref()); - if widget == imp.child.borrow().as_ref() { - return; - } - - if let Some(child) = imp.child.borrow_mut().take() { - child.unparent(); - } - - if let Some(w) = widget { - imp.child.replace(Some(w.clone())); - w.set_parent(self); - } - - self.queue_resize(); - self.notify("child") - } - - pub fn set_keep_aspect_ratio(&self, keep_aspect_ratio: bool) { - let imp = self.imp(); - if imp.keep_aspect_ratio.get() == keep_aspect_ratio { - return; - } - - imp.keep_aspect_ratio.set(keep_aspect_ratio); - - self.queue_resize(); - self.notify("keep-aspect-ratio") - } -}