Skip to content

Commit 36aa410

Browse files
jf2048bilelmoussaoui
authored andcommitted
gtk4: Add convenience traits for binding template callbacks to classes
1 parent b1f11b1 commit 36aa410

File tree

30 files changed

+667
-31
lines changed

30 files changed

+667
-31
lines changed

book/listings/actions/5/window/imp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl ObjectSubclass for Window {
2222
type ParentType = gtk::ApplicationWindow;
2323

2424
fn class_init(klass: &mut Self::Class) {
25-
Self::bind_template(klass);
25+
klass.bind_template();
2626
}
2727

2828
fn instance_init(obj: &InitializingObject<Self>) {

book/listings/actions/6/window/imp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl ObjectSubclass for Window {
2525
type ParentType = gtk::ApplicationWindow;
2626

2727
fn class_init(klass: &mut Self::Class) {
28-
Self::bind_template(klass);
28+
klass.bind_template();
2929
}
3030

3131
fn instance_init(obj: &InitializingObject<Self>) {

book/listings/actions/7/window/imp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl ObjectSubclass for Window {
4040
type ParentType = gtk::ApplicationWindow;
4141

4242
fn class_init(klass: &mut Self::Class) {
43-
Self::bind_template(klass);
43+
klass.bind_template();
4444
}
4545

4646
fn instance_init(obj: &InitializingObject<Self>) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use glib::subclass::InitializingObject;
2+
use gtk::glib;
3+
use gtk::prelude::*;
4+
use gtk::subclass::prelude::*;
5+
use gtk::CompositeTemplate;
6+
7+
use crate::custom_button::CustomButton;
8+
9+
// ANCHOR: object
10+
// Object holding the state
11+
#[derive(CompositeTemplate, Default)]
12+
#[template(resource = "/org/gtk-rs/example/window.ui")]
13+
pub struct Window {
14+
#[template_child]
15+
pub button: TemplateChild<CustomButton>,
16+
}
17+
// ANCHOR_END: object
18+
19+
// ANCHOR: subclass
20+
// The central trait for subclassing a GObject
21+
#[glib::object_subclass]
22+
impl ObjectSubclass for Window {
23+
// `NAME` needs to match `class` attribute of template
24+
const NAME: &'static str = "MyGtkAppWindow";
25+
type Type = super::Window;
26+
type ParentType = gtk::ApplicationWindow;
27+
28+
fn class_init(klass: &mut Self::Class) {
29+
klass.bind_template();
30+
}
31+
32+
fn instance_init(obj: &InitializingObject<Self>) {
33+
obj.init_template();
34+
}
35+
}
36+
// ANCHOR_END: subclass
37+
38+
// ANCHOR: object_impl
39+
// Trait shared by all GObjects
40+
impl ObjectImpl for Window {
41+
fn constructed(&self, obj: &Self::Type) {
42+
// Call "constructed" on parent
43+
self.parent_constructed(obj);
44+
45+
// Connect to "clicked" signal of `button`
46+
self.button.connect_clicked(move |button| {
47+
// Set the label to "Hello World!" after the button has been clicked on
48+
button.set_label("Hello World!");
49+
});
50+
}
51+
}
52+
// ANCHOR_END: object_impl
53+
54+
// Trait shared by all widgets
55+
impl WidgetImpl for Window {}
56+
57+
// Trait shared by all windows
58+
impl WindowImpl for Window {}
59+
60+
// Trait shared by all application
61+
impl ApplicationWindowImpl for Window {}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use glib::subclass::InitializingObject;
2+
use gtk::glib;
3+
use gtk::prelude::*;
4+
use gtk::subclass::prelude::*;
5+
use gtk::CompositeTemplate;
6+
7+
use crate::custom_button::CustomButton;
8+
9+
// ANCHOR: object
10+
// Object holding the state
11+
#[derive(CompositeTemplate, Default)]
12+
#[template(resource = "/org/gtk-rs/example/window.ui")]
13+
pub struct Window {
14+
#[template_child]
15+
pub button: TemplateChild<CustomButton>,
16+
}
17+
// ANCHOR_END: object
18+
19+
// ANCHOR: subclass
20+
// The central trait for subclassing a GObject
21+
#[glib::object_subclass]
22+
impl ObjectSubclass for Window {
23+
// `NAME` needs to match `class` attribute of template
24+
const NAME: &'static str = "MyGtkAppWindow";
25+
type Type = super::Window;
26+
type ParentType = gtk::ApplicationWindow;
27+
28+
fn class_init(klass: &mut Self::Class) {
29+
klass.bind_template();
30+
klass.bind_template_callbacks();
31+
}
32+
33+
fn instance_init(obj: &InitializingObject<Self>) {
34+
obj.init_template();
35+
}
36+
}
37+
// ANCHOR_END: subclass
38+
39+
// ANCHOR: template_callbacks
40+
#[gtk::template_callbacks]
41+
impl Window {
42+
#[template_callback]
43+
fn handle_button_clicked(button: &CustomButton) {
44+
// Set the label to "Hello World!" after the button has been clicked on
45+
button.set_label("Hello World!");
46+
}
47+
}
48+
// ANCHOR_END: template_callbacks
49+
50+
// Trait shared by all GObjects
51+
impl ObjectImpl for Window {}
52+
53+
// Trait shared by all widgets
54+
impl WidgetImpl for Window {}
55+
56+
// Trait shared by all windows
57+
impl WindowImpl for Window {}
58+
59+
// Trait shared by all application
60+
impl ApplicationWindowImpl for Window {}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use std::cell::Cell;
2+
3+
use glib::subclass::InitializingObject;
4+
use gtk::glib;
5+
use gtk::prelude::*;
6+
use gtk::subclass::prelude::*;
7+
use gtk::CompositeTemplate;
8+
9+
use crate::custom_button::CustomButton;
10+
11+
// ANCHOR: object
12+
// Object holding the state
13+
#[derive(CompositeTemplate, Default)]
14+
#[template(resource = "/org/gtk-rs/example/window.ui")]
15+
pub struct Window {
16+
#[template_child]
17+
pub button: TemplateChild<CustomButton>,
18+
pub number: Cell<i32>,
19+
}
20+
// ANCHOR_END: object
21+
22+
// ANCHOR: subclass
23+
// The central trait for subclassing a GObject
24+
#[glib::object_subclass]
25+
impl ObjectSubclass for Window {
26+
// `NAME` needs to match `class` attribute of template
27+
const NAME: &'static str = "MyGtkAppWindow";
28+
type Type = super::Window;
29+
type ParentType = gtk::ApplicationWindow;
30+
31+
fn class_init(klass: &mut Self::Class) {
32+
klass.bind_template();
33+
klass.bind_template_callbacks();
34+
}
35+
36+
fn instance_init(obj: &InitializingObject<Self>) {
37+
obj.init_template();
38+
}
39+
}
40+
// ANCHOR_END: subclass
41+
42+
// ANCHOR: template_callbacks
43+
#[gtk::template_callbacks]
44+
impl Window {
45+
#[template_callback]
46+
fn handle_button_clicked(&self, button: &CustomButton) {
47+
let number_increased = self.number.get() + 1;
48+
self.number.set(number_increased);
49+
button.set_label(&number_increased.to_string())
50+
}
51+
}
52+
// ANCHOR_END: template_callbacks
53+
54+
// Trait shared by all GObjects
55+
impl ObjectImpl for Window {}
56+
57+
// Trait shared by all widgets
58+
impl WidgetImpl for Window {}
59+
60+
// Trait shared by all windows
61+
impl WindowImpl for Window {}
62+
63+
// Trait shared by all application
64+
impl ApplicationWindowImpl for Window {}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::cell::Cell;
2+
3+
use glib::subclass::InitializingObject;
4+
use gtk::glib;
5+
use gtk::prelude::*;
6+
use gtk::subclass::prelude::*;
7+
use gtk::CompositeTemplate;
8+
9+
use crate::custom_button::CustomButton;
10+
11+
// ANCHOR: object
12+
// Object holding the state
13+
#[derive(CompositeTemplate, Default)]
14+
#[template(resource = "/org/gtk-rs/example/window.ui")]
15+
pub struct Window {
16+
pub number: Cell<i32>,
17+
}
18+
// ANCHOR_END: object
19+
20+
// ANCHOR: subclass
21+
// The central trait for subclassing a GObject
22+
#[glib::object_subclass]
23+
impl ObjectSubclass for Window {
24+
// `NAME` needs to match `class` attribute of template
25+
const NAME: &'static str = "MyGtkAppWindow";
26+
type Type = super::Window;
27+
type ParentType = gtk::ApplicationWindow;
28+
29+
fn class_init(klass: &mut Self::Class) {
30+
klass.bind_template();
31+
klass.bind_template_callbacks();
32+
}
33+
34+
fn instance_init(obj: &InitializingObject<Self>) {
35+
obj.init_template();
36+
}
37+
}
38+
// ANCHOR_END: subclass
39+
40+
// ANCHOR: template_callbacks
41+
#[gtk::template_callbacks]
42+
impl Window {
43+
#[template_callback]
44+
fn handle_button_clicked(&self, button: &CustomButton) {
45+
let number_increased = self.number.get() + 1;
46+
self.number.set(number_increased);
47+
button.set_label(&number_increased.to_string())
48+
}
49+
}
50+
// ANCHOR_END: template_callbacks
51+
52+
// Trait shared by all GObjects
53+
impl ObjectImpl for Window {}
54+
55+
// Trait shared by all widgets
56+
impl WidgetImpl for Window {}
57+
58+
// Trait shared by all windows
59+
impl WindowImpl for Window {}
60+
61+
// Trait shared by all application
62+
impl ApplicationWindowImpl for Window {}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use std::cell::Cell;
2+
3+
use glib::subclass::InitializingObject;
4+
use gtk::glib;
5+
use gtk::prelude::*;
6+
use gtk::subclass::prelude::*;
7+
use gtk::CompositeTemplate;
8+
9+
use crate::custom_button::CustomButton;
10+
11+
// ANCHOR: object
12+
// Object holding the state
13+
#[derive(CompositeTemplate, Default)]
14+
#[template(resource = "/org/gtk-rs/example/window.ui")]
15+
pub struct Window {
16+
pub number: Cell<i32>,
17+
}
18+
// ANCHOR_END: object
19+
20+
// ANCHOR: subclass
21+
// The central trait for subclassing a GObject
22+
#[glib::object_subclass]
23+
impl ObjectSubclass for Window {
24+
// `NAME` needs to match `class` attribute of template
25+
const NAME: &'static str = "MyGtkAppWindow";
26+
type Type = super::Window;
27+
type ParentType = gtk::ApplicationWindow;
28+
29+
fn class_init(klass: &mut Self::Class) {
30+
// Register `CustomButton`
31+
CustomButton::ensure_type();
32+
33+
klass.bind_template();
34+
klass.bind_template_callbacks();
35+
}
36+
37+
fn instance_init(obj: &InitializingObject<Self>) {
38+
obj.init_template();
39+
}
40+
}
41+
// ANCHOR_END: subclass
42+
43+
// ANCHOR: template_callbacks
44+
#[gtk::template_callbacks]
45+
impl Window {
46+
#[template_callback]
47+
fn handle_button_clicked(&self, button: &CustomButton) {
48+
let number_increased = self.number.get() + 1;
49+
self.number.set(number_increased);
50+
button.set_label(&number_increased.to_string())
51+
}
52+
}
53+
// ANCHOR_END: template_callbacks
54+
55+
// Trait shared by all GObjects
56+
impl ObjectImpl for Window {}
57+
58+
// Trait shared by all widgets
59+
impl WidgetImpl for Window {}
60+
61+
// Trait shared by all windows
62+
impl WindowImpl for Window {}
63+
64+
// Trait shared by all application
65+
impl ApplicationWindowImpl for Window {}

book/listings/css/6/window/imp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl ObjectSubclass for Window {
1818
type ParentType = gtk::ApplicationWindow;
1919

2020
fn class_init(klass: &mut Self::Class) {
21-
Self::bind_template(klass);
21+
klass.bind_template();
2222
}
2323

2424
fn instance_init(obj: &InitializingObject<Self>) {

book/listings/css/7/window/imp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl ObjectSubclass for Window {
1818
type ParentType = gtk::ApplicationWindow;
1919

2020
fn class_init(klass: &mut Self::Class) {
21-
Self::bind_template(klass);
21+
klass.bind_template();
2222
}
2323

2424
fn instance_init(obj: &InitializingObject<Self>) {

0 commit comments

Comments
 (0)