@@ -4,7 +4,7 @@ Until now, whenever we constructed pre-defined widgets we relied on the [builder
4
4
As a reminder, that is how we used it to build our trusty "Hello World!" app.
5
5
6
6
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/hello_world/3/main.rs " >listings/hello_world/3/main.rs</a >
7
- ``` rust ,no_run,noplayground
7
+ ``` rust
8
8
{{#rustdoc_include .. / listings / hello_world / 3 / main . rs: all }}
9
9
```
10
10
@@ -57,7 +57,7 @@ Then, we create a `build.rs` at the root of our package with the following conte
57
57
This will compile the resources whenever we trigger a build with cargo and then statically link our executable to them.
58
58
59
59
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/build.rs " >listings/build.rs</a >
60
- ``` rust ,no_run,noplayground
60
+ ``` rust
61
61
fn main () {
62
62
glib_build_tools :: compile_resources (
63
63
& [" composite_templates/1/resources" ],
@@ -71,13 +71,13 @@ Finally, we register and include the resources by calling the macro [`gio::resou
71
71
In your own apps take care to register the resources before creating the ` gtk::Application ` .
72
72
73
73
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/1/main.rs " >listings/composite_templates/1/main.rs</a >
74
- ``` rust ,no_run,noplayground
74
+ ``` rust
75
75
{{#rustdoc_include .. / listings / composite_templates / 1 / main . rs}}
76
76
```
77
77
78
78
Within our code we create a custom widget inheriting from ` gtk::ApplicationWindow ` to make use of our template.
79
79
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/1/window/mod.rs " >listings/composite_templates/1/window/mod.rs</a >
80
- ``` rust ,no_run,noplayground
80
+ ``` rust
81
81
{{#rustdoc_include .. / listings / composite_templates / 1 / window / mod . rs}}
82
82
```
83
83
@@ -90,23 +90,23 @@ You use it by adding a struct member with the same name as one `id` attribute in
90
90
This will be useful later, when we want to add a callback to our button.
91
91
92
92
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/1/window/imp.rs " >listings/composite_templates/1/window/imp.rs</a >
93
- ``` rust ,no_run,noplayground
93
+ ``` rust
94
94
{{#rustdoc_include .. / listings / composite_templates / 1 / window / imp . rs: object }}
95
95
```
96
96
97
97
Within the ` ObjectSubclass ` trait, we make sure that ` NAME ` corresponds to ` class ` in the template and ` ParentType ` corresponds to ` parent ` in the template.
98
98
We also bind and initialize the template in ` class_init ` and ` instance_init ` .
99
99
100
100
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/1/window/imp.rs " >listings/composite_templates/1/window/imp.rs</a >
101
- ``` rust ,no_run,noplayground
101
+ ``` rust
102
102
{{#rustdoc_include .. / listings / composite_templates / 1 / window / imp . rs: subclass }}
103
103
```
104
104
105
105
Finally, we connect the callback to the "clicked" signal of ` button ` within ` constructed ` .
106
106
The button is easily available thanks to the stored reference in ` self ` .
107
107
108
108
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/1/window/imp.rs " >listings/composite_templates/1/window/imp.rs</a >
109
- ``` rust ,no_run,noplayground
109
+ ``` rust
110
110
{{#rustdoc_include .. / listings / composite_templates / 1 / window / imp . rs: object_impl }}
111
111
```
112
112
@@ -118,21 +118,21 @@ As usual, we define the implementation struct within `imp.rs`.
118
118
Note the ` NAME ` we define here, we will need it later to refer to it in the template.
119
119
120
120
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/2/custom_button/imp.rs " >listings/composite_templates/2/custom_button/imp.rs</a >
121
- ``` rust ,no_run,noplayground
121
+ ``` rust
122
122
{{#rustdoc_include .. / listings / composite_templates / 2 / custom_button / imp . rs: imp }}
123
123
```
124
124
125
125
We also define the public struct in ` mod.rs ` .
126
126
127
127
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/2/custom_button/mod.rs " >listings/composite_templates/2/custom_button/mod.rs</a >
128
- ``` rust ,no_run,noplayground
128
+ ``` rust
129
129
{{#rustdoc_include .. / listings / composite_templates / 2 / custom_button / mod . rs: mod }}
130
130
```
131
131
132
132
Since we want to refer to a ` CustomButton ` now we also have to change the type of the template child to it.
133
133
134
134
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/2/window/imp.rs " >listings/composite_templates/2/window/imp.rs</a >
135
- ``` rust ,no_run,noplayground
135
+ ``` rust
136
136
{{#rustdoc_include .. / listings / composite_templates / 2 / window / imp . rs: object }}
137
137
```
138
138
@@ -162,23 +162,23 @@ It takes a function of type `Fn(&Self)`.
162
162
This means that ` handle_button_clicked ` has a single parameter of type ` &CustomButton ` .
163
163
164
164
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/3/window/imp.rs " >listings/composite_templates/3/window/imp.rs</a >
165
- ``` rust ,no_run,noplayground
165
+ ``` rust
166
166
{{#rustdoc_include .. / listings / composite_templates / 3 / window / imp . rs: template_callbacks }}
167
167
```
168
168
169
169
Then we have to bind the template callbacks with [ ` bind_template_callbacks ` ] ( https://gtk-rs.org/gtk4-rs/stable/latest/docs/gtk4/subclass/widget/trait.CompositeTemplateCallbacksClass.html#tymethod.bind_template_callbacks ) .
170
170
We also need to remove the ` button.connect_clicked ` callback implemented in ` window/imp.rs ` .
171
171
172
172
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/3/window/imp.rs " >listings/composite_templates/3/window/imp.rs</a >
173
- ``` rust ,no_run,noplayground
173
+ ``` rust
174
174
{{#rustdoc_include .. / listings / composite_templates / 3 / window / imp . rs: subclass }}
175
175
```
176
176
177
177
We can also access the state of our widget.
178
178
Let's say we want to manipulate a ` number ` stored in ` imp::Window ` .
179
179
180
180
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/4/window/imp.rs " >listings/composite_templates/4/window/imp.rs</a >
181
- ``` rust ,no_run,noplayground
181
+ ``` rust
182
182
{{#rustdoc_include .. / listings / composite_templates / 4 / window / imp . rs: object }}
183
183
```
184
184
@@ -193,7 +193,7 @@ Now we can add `&self` as first parameter to `handle_button_clicked`.
193
193
This lets us access the state of the window and therefore manipulate ` number ` .
194
194
195
195
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/4/window/imp.rs " >listings/composite_templates/4/window/imp.rs</a >
196
- ``` rust ,no_run,noplayground
196
+ ``` rust
197
197
{{#rustdoc_include .. / listings / composite_templates / 4 / window / imp . rs: template_callbacks }}
198
198
```
199
199
@@ -203,7 +203,7 @@ Now that we use template callbacks we don't access the template child anymore.
203
203
Let's remove it.
204
204
205
205
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/5/window/imp.rs " >listings/composite_templates/5/window/imp.rs</a >
206
- ``` rust ,no_run,noplayground
206
+ ``` rust
207
207
{{#rustdoc_include .. / listings / composite_templates / 5 / window / imp . rs: object }}
208
208
```
209
209
@@ -220,7 +220,7 @@ It also ensures that the widget type is registered.
220
220
Luckily we can also do that by ourselves.
221
221
222
222
Filename: <a class =file-link href =" https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/composite_templates/6/window/imp.rs " >listings/composite_templates/6/window/imp.rs</a >
223
- ``` rust ,no_run,noplayground
223
+ ``` rust
224
224
{{#rustdoc_include .. / listings / composite_templates / 6 / window / imp . rs: subclass }}
225
225
```
226
226
0 commit comments