Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanups #55

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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