Skip to content

Commit f041688

Browse files
examples/squeezer_bin: Mark properties as writable
Fixes #1757
1 parent 55f8aa9 commit f041688

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

examples/squeezer_bin/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ fn main() -> glib::ExitCode {
1414
let mode_switch = gtk::Switch::new();
1515
let switch_label = gtk::Label::new(Some("keep aspect ratio"));
1616
let squeezer = SqueezerBin::default();
17-
squeezer.set_child(Some(&gtk::Label::new(Some("Hello World!"))));
17+
squeezer.set_child(Some(
18+
gtk::Label::new(Some("Hello World!")).upcast::<gtk::Widget>(),
19+
));
1820

1921
headerbar.pack_start(&mode_switch);
2022
headerbar.pack_start(&switch_label);

examples/squeezer_bin/squeezer_bin/imp.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,43 @@ fn child_size(child: &impl IsA<gtk::Widget>) -> ((i32, i32), (i32, i32)) {
1616
#[derive(Debug, Default, glib::Properties)]
1717
#[properties(wrapper_type = super::SqueezerBin)]
1818
pub struct SqueezerBin {
19-
#[property(get, explicit_notify)]
19+
#[property(get, set = Self::set_child, explicit_notify, nullable)]
2020
pub(super) child: RefCell<Option<gtk::Widget>>,
21-
#[property(get, explicit_notify)]
21+
#[property(get, set = Self::set_keep_aspect_ratio, explicit_notify)]
2222
pub(super) keep_aspect_ratio: Cell<bool>,
2323
}
2424

25+
impl SqueezerBin {
26+
fn set_child(&self, widget: Option<&gtk::Widget>) {
27+
if widget == self.child.borrow().as_ref() {
28+
return;
29+
}
30+
31+
if let Some(child) = self.child.borrow_mut().take() {
32+
child.unparent();
33+
}
34+
35+
if let Some(w) = widget {
36+
self.child.replace(Some(w.clone()));
37+
w.set_parent(&*self.obj());
38+
}
39+
40+
self.obj().queue_resize();
41+
self.obj().notify_child();
42+
}
43+
44+
fn set_keep_aspect_ratio(&self, keep_aspect_ratio: bool) {
45+
if self.keep_aspect_ratio.get() == keep_aspect_ratio {
46+
return;
47+
}
48+
49+
self.keep_aspect_ratio.set(keep_aspect_ratio);
50+
51+
self.obj().queue_resize();
52+
self.obj().notify_keep_aspect_ratio();
53+
}
54+
}
55+
2556
#[glib::object_subclass]
2657
impl ObjectSubclass for SqueezerBin {
2758
const NAME: &'static str = "SqueezerBin";
Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod imp;
22

3-
use gtk::{glib, prelude::*, subclass::prelude::*};
3+
use gtk::glib;
44

55
glib::wrapper! {
66
pub struct SqueezerBin(ObjectSubclass<imp::SqueezerBin>)
@@ -12,37 +12,3 @@ impl Default for SqueezerBin {
1212
glib::Object::new()
1313
}
1414
}
15-
16-
impl SqueezerBin {
17-
pub fn set_child(&self, widget: Option<&impl IsA<gtk::Widget>>) {
18-
let imp = self.imp();
19-
let widget = widget.map(|w| w.as_ref());
20-
if widget == imp.child.borrow().as_ref() {
21-
return;
22-
}
23-
24-
if let Some(child) = imp.child.borrow_mut().take() {
25-
child.unparent();
26-
}
27-
28-
if let Some(w) = widget {
29-
imp.child.replace(Some(w.clone()));
30-
w.set_parent(self);
31-
}
32-
33-
self.queue_resize();
34-
self.notify("child")
35-
}
36-
37-
pub fn set_keep_aspect_ratio(&self, keep_aspect_ratio: bool) {
38-
let imp = self.imp();
39-
if imp.keep_aspect_ratio.get() == keep_aspect_ratio {
40-
return;
41-
}
42-
43-
imp.keep_aspect_ratio.set(keep_aspect_ratio);
44-
45-
self.queue_resize();
46-
self.notify("keep-aspect-ratio")
47-
}
48-
}

0 commit comments

Comments
 (0)