Skip to content

Commit

Permalink
examples/squeezer_bin: Mark properties as writable
Browse files Browse the repository at this point in the history
Fixes #1757
  • Loading branch information
bilelmoussaoui committed Jun 2, 2024
1 parent 55f8aa9 commit f041688
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
4 changes: 3 additions & 1 deletion examples/squeezer_bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&gtk::Label::new(Some("Hello World!"))));
squeezer.set_child(Some(
gtk::Label::new(Some("Hello World!")).upcast::<gtk::Widget>(),
));

headerbar.pack_start(&mode_switch);
headerbar.pack_start(&switch_label);
Expand Down
35 changes: 33 additions & 2 deletions examples/squeezer_bin/squeezer_bin/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,43 @@ fn child_size(child: &impl IsA<gtk::Widget>) -> ((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<Option<gtk::Widget>>,
#[property(get, explicit_notify)]
#[property(get, set = Self::set_keep_aspect_ratio, explicit_notify)]
pub(super) keep_aspect_ratio: Cell<bool>,
}

impl SqueezerBin {
fn set_child(&self, widget: Option<&gtk::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";
Expand Down
36 changes: 1 addition & 35 deletions examples/squeezer_bin/squeezer_bin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod imp;

use gtk::{glib, prelude::*, subclass::prelude::*};
use gtk::glib;

glib::wrapper! {
pub struct SqueezerBin(ObjectSubclass<imp::SqueezerBin>)
Expand All @@ -12,37 +12,3 @@ impl Default for SqueezerBin {
glib::Object::new()
}
}

impl SqueezerBin {
pub fn set_child(&self, widget: Option<&impl IsA<gtk::Widget>>) {
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")
}
}

0 comments on commit f041688

Please sign in to comment.