@@ -224,7 +224,7 @@ <h2 id="update-logic"><a class="header" href="#update-logic">Update Logic</a></h
224
224
and most idiomatic way to express this logic in Rust is by defining a method named < code > update</ code > in our application state.</ p >
225
225
< p > For our counter interface, we only need to properly increment or decrement the < code > value</ code > of our < code > Counter</ code > struct based on the < code > Message</ code >
226
226
we just defined:</ p >
227
- < pre > < code class ="language-rust "> impl Counter {
227
+ < pre > < code class ="language-rust ignore "> impl Counter {
228
228
fn update(&mut self, message: Message) {
229
229
match message {
230
230
Message::Increment => {
@@ -237,16 +237,16 @@ <h2 id="update-logic"><a class="header" href="#update-logic">Update Logic</a></h
237
237
}
238
238
}</ code > </ pre >
239
239
< p > Great! Now we are ready to process user interactions. For instance, imagine we initialized our counter like this:</ p >
240
- < pre > < code class ="language-rust "> let mut counter = Counter { value: 0 };</ code > </ pre >
240
+ < pre > < code class ="language-rust ignore "> let mut counter = Counter { value: 0 };</ code > </ pre >
241
241
< p > And let's say we wanted to simulate a user playing with our interface for a bit—pressing the increment button twice
242
242
and then the decrement button once. We could easily compute the final state of our counter with our < strong > update logic</ strong > :</ p >
243
- < pre > < code class ="language-rust "> counter.update(Message::Increment);
243
+ < pre > < code class ="language-rust ignore "> counter.update(Message::Increment);
244
244
counter.update(Message::Increment);
245
245
counter.update(Message::Decrement);</ code > </ pre >
246
246
< p > This would cause our < code > Counter</ code > to end up with a < code > value</ code > of < code > 1</ code > :</ p >
247
- < pre > < code class ="language-rust "> assert_eq!(counter.value, 1);</ code > </ pre >
247
+ < pre > < code class ="language-rust ignore "> assert_eq!(counter.value, 1);</ code > </ pre >
248
248
< p > In fact, we have just written a simple test for our application logic:</ p >
249
- < pre > < code class ="language-rust "> #[test]
249
+ < pre > < code class ="language-rust ignore "> #[test]
250
250
fn it_counts_properly() {
251
251
let mut counter = Counter { value: 0 };
252
252
@@ -280,7 +280,7 @@ <h3 id="the-buttons"><a class="header" href="#the-buttons">The Buttons</a></h3>
280
280
< p > In iced, widgets are independent values. The same way you can have an integer in a variable, you can have a widget as well.
281
281
These values are normally created using a < em > helper function</ em > from the < code > widget</ code > module.</ p >
282
282
< p > For our buttons, we can use the < code > button</ code > helper:</ p >
283
- < pre > < code class ="language-rust "> use iced::widget::button;
283
+ < pre > < code class ="language-rust ignore "> use iced::widget::button;
284
284
285
285
let increment = button("+");
286
286
let decrement = button("-");</ code > </ pre >
@@ -292,7 +292,7 @@ <h3 id="the-number"><a class="header" href="#the-number">The Number</a></h3>
292
292
for our counter value?</ p >
293
293
< p > While iced does not really have a < code > number</ code > widget, it does have a more generic < code > text</ code > widget that can be used
294
294
to display any kind of text—numbers included:</ p >
295
- < pre > < code class ="language-rust "> use iced::widget::text;
295
+ < pre > < code class ="language-rust ignore "> use iced::widget::text;
296
296
297
297
let counter = text(15);</ code > </ pre >
298
298
< p > Sweet! Like < code > button</ code > , < code > text</ code > also takes an argument used to describe its contents. Since we are just getting started, let's
@@ -302,14 +302,14 @@ <h3 id="the-layout"><a class="header" href="#the-layout">The Layout</a></h3>
302
302
< p > Not so fast! The widgets in our counter interface are displayed in a specific < strong > order</ strong > . Given our three widgets, there is a total of
303
303
< strong > six</ strong > different ways to order them! However, the order we want is: < code > increment</ code > , < code > counter</ code > , and < code > decrement</ code > .</ p >
304
304
< p > A very simple way of describing this order is to create a list with our widgets:</ p >
305
- < pre > < code class ="language-rust "> let interface = vec![increment, counter, decrement];</ code > </ pre >
305
+ < pre > < code class ="language-rust ignore "> let interface = vec![increment, counter, decrement];</ code > </ pre >
306
306
< p > But we are still missing something! It's not only the order that is specific, our interface also has a specific visual < strong > layout</ strong > .</ p >
307
307
< p > The widgets are positioned on top of each other, but they could very well be positioned from left to right instead. There is nothing
308
308
in our description so far that talks about the < strong > layout</ strong > of our widgets.</ p >
309
309
< p > In iced, layout is described using... well, more widgets! That's right. Not all widgets produce visual results directly; some may simply
310
310
manage the position of existing widgets. And since widgets are just values, they can be nested and composed nicely!</ p >
311
311
< p > The kind of vertical layout that we need for our counter can be achieved with the < code > column</ code > widget:</ p >
312
- < pre > < code class ="language-rust "> use iced::widget::column;
312
+ < pre > < code class ="language-rust ignore "> use iced::widget::column;
313
313
314
314
let interface = column![increment, counter, decrement];</ code > </ pre >
315
315
< p > This is very similar to our previous snippet! iced provides a < code > column!</ code > macro for creating a < code > column</ code > out of some widgets in a particular
@@ -323,7 +323,7 @@ <h3 id="the-interactions"><a class="header" href="#the-interactions">The Interac
323
323
< p > In iced, every widget has a specific type that enables further configuration using simple builder methods. The < code > button</ code >
324
324
helper returns an instance of < a href ="https://docs.rs/iced/0.12.1/iced/widget/struct.Button.html "> the < code > Button</ code > type</ a > , which has an < code > on_press</ code > method we can use to define the message it must
325
325
< strong > produce</ strong > when a user presses the button:</ p >
326
- < pre > < code class ="language-rust "> use iced::widget::button;
326
+ < pre > < code class ="language-rust ignore "> use iced::widget::button;
327
327
328
328
let increment = button("+").on_press(Message::Increment);
329
329
let decrement = button("-").on_press(Message::Decrement);</ code > </ pre >
@@ -340,7 +340,7 @@ <h3 id="the-interactions"><a class="header" href="#the-interactions">The Interac
340
340
< h3 id ="the-view "> < a class ="header " href ="#the-view "> The View</ a > </ h3 >
341
341
< p > We are almost there! There is only one thing left to do: connecting our application < strong > state</ strong > to the view logic.</ p >
342
342
< p > Let's bring together all the view logic we have written so far:</ p >
343
- < pre > < code class ="language-rust "> use iced::widget::{button, column, text};
343
+ < pre > < code class ="language-rust ignore "> use iced::widget::{button, column, text};
344
344
345
345
// The buttons
346
346
let increment = button("+").on_press(Message::Increment);
@@ -357,7 +357,7 @@ <h3 id="the-view"><a class="header" href="#the-view">The View</a></h3>
357
357
we want is to actually display the < code > value</ code > field of our < code > Counter</ code > state. This way, when a button is pressed and
358
358
our update logic is triggered, the text widget will display the new < code > value</ code > .</ p >
359
359
< p > We can easily do this by running our view logic in a method of our < code > Counter</ code > —just like we did with our update logic:</ p >
360
- < pre > < code class ="language-rust "> use iced::widget::{button, column, text};
360
+ < pre > < code class ="language-rust ignore "> use iced::widget::{button, column, text};
361
361
362
362
impl Counter {
363
363
fn view(&self) {
@@ -382,7 +382,7 @@ <h3 id="the-view"><a class="header" href="#the-view">The View</a></h3>
382
382
< p > Instead of throwing the < code > interface</ code > away, we need to return it. Remember, the purpose of our < strong > view logic</ strong > is
383
383
to dictate the widgets of our user interface; and the content of the < code > interface</ code > variable is precisely the
384
384
description of the interface we want:</ p >
385
- < pre > < code class ="language-rust "> use iced::widget::{button, column, text, Column};
385
+ < pre > < code class ="language-rust ignore "> use iced::widget::{button, column, text, Column};
386
386
387
387
impl Counter {
388
388
fn view(&self) -> Column<Message> {
@@ -413,7 +413,7 @@ <h3 id="the-view"><a class="header" href="#the-view">The View</a></h3>
413
413
< div align ="center " class ="right ">
414
414
< img alt ="A classical counter interface " src ="resources/counter-interface.svg " width ="50% ">
415
415
</ div >
416
- < pre > < code class ="language-rust "> use iced::widget::{button, column, text, Column};
416
+ < pre > < code class ="language-rust ignore "> use iced::widget::{button, column, text, Column};
417
417
418
418
impl Counter {
419
419
fn view(&self) -> Column<Message> {
0 commit comments