Skip to content

Commit

Permalink
Merge pull request #55 from nazar-pc/cleanups
Browse files Browse the repository at this point in the history
Minor cleanups
  • Loading branch information
AaronErhardt authored Dec 27, 2023
2 parents acfe569 + 749ca7d commit 969b93a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
4 changes: 2 additions & 2 deletions examples/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ impl FactoryComponent for Counter {
type Input = CounterMsg;
type Output = CounterOutput;
type CommandOutput = ();
type Widgets = CounterWidgets;
type ParentWidget = gtk::Box;
// ANCHOR_END: factory_impl_start

// ANCHOR: factory_view
view! {
root = gtk::Box {
#[root]
gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
set_spacing: 10,

Expand Down
8 changes: 2 additions & 6 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,14 @@ impl SimpleComponent for AppModel {

gtk::Button {
set_label: "Increment",
connect_clicked[sender] => move |_| {
sender.input(AppMsg::Increment);
}
connect_clicked => AppMsg::Increment
},

// ANCHOR: widget_assign_fn
gtk::Button::with_label("Decrement") {
// ANCHOR_END: widget_assign_fn
// ANCHOR: connect
connect_clicked[sender] => move |_| {
sender.input(AppMsg::Decrement);
}
connect_clicked => AppMsg::Decrement
// ANCHOR_END: connect
},

Expand Down
29 changes: 15 additions & 14 deletions src/component_macro.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,31 @@ Sometimes we want to use a constructor function to initialize our widgets. For t

### Events

To connect events, we use this syntax:
To connect events, we use this general syntax:

```rust,no_run,noplayground
method_name[cloned_var1, cloned_var2, ...] => move |args, ...| { code... }
```

Again, there's no magic. The macro will simply assign a closure to a method. Because closures often need to capture local variables that don't implement the `Copy` trait, we need to clone these variables. Therefore, we can list the variables we want to clone in the square brackets after the method name.

For simple cases there's even a shorter syntax for just sending one input message that works with most event handlers.
So instead of this:
```rust,no_run,noplayground
{{#include ../examples/simple.rs:connect }}
method_name[sender] => move |_| { sender.input(Msg); },
```

You can simply write this:

```rust,no_run,noplayground
method_name => Msg,
```

> There's even a shorter syntax for just sending one input message that works with most event handlers.
> So instead of this:
>
> ```rust,no_run,noplayground
> method_name[sender] => move |_| { sender.input(Msg); },
> ```
>
> You can simply write this:
>
> ```rust,no_run,noplayground
> method_name => Msg,
> ```
This is what we used in this example:

```rust,no_run,noplayground
{{#include ../examples/simple.rs:connect }}
```

### UI updates

Expand Down
1 change: 0 additions & 1 deletion src/efficient_ui/factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ Let's look at the associated types one by one:
+ **Input**: The input message type.
+ **Output**: The output message type.
+ **CommandOutput**: The command output message type, we don't need it here.
+ **Widgets**: The name of the struct that stores out widgets, it will be created by the macro.
+ **ParentWidget**: The container widget used to store the widgets of the factory, for example `gtk::Box`.

### Creating the widget
Expand Down

0 comments on commit 969b93a

Please sign in to comment.