Skip to content

Commit

Permalink
Update book to latest beta
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronErhardt committed Oct 26, 2023
1 parent a580aea commit acfe569
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 34 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/book-next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ jobs:
- run: mdbook build -d book
working-directory: ./

- name: Link Checker
uses: lycheeverse/[email protected]
with:
args: book
fail: true

- name: Deploy
uses: peaceiris/actions-gh-pages@v3
# Only deploy when ref is next
Expand All @@ -60,4 +66,5 @@ jobs:
- uses: codespell-project/actions-codespell@master
with:
check_filenames: true
path: src
ignore_words_list: crate,statics,relm
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ members = [

[dev-dependencies]
rand = "0.8.5"
relm4 = "0.6.2"
relm4-components = "0.6.2"
relm4 = "0.7.0-beta.2"
relm4-components = "0.7.0-beta.2"
tokio = { version = "1.25", features = ["rt", "macros", "time", "rt-multi-thread", "sync"] }
tracker = "0.2.0"
2 changes: 1 addition & 1 deletion examples/alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl SimpleComponent for App {

connect_close_request[sender] => move |_| {
sender.input(AppMsg::CloseRequest);
gtk::Inhibit(true)
gtk::glib::Propagation::Stop
},

gtk::Box {
Expand Down
2 changes: 1 addition & 1 deletion examples/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl SimpleComponent for AppModel {
},
connect_close_request[sender] => move |_| {
sender.input(AppMsg::CloseRequest);
gtk::Inhibit(true)
gtk::glib::Propagation::Stop
}
}
}
Expand Down
28 changes: 13 additions & 15 deletions examples/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ impl FactoryComponent for Counter {
type Output = CounterOutput;
type CommandOutput = ();
type Widgets = CounterWidgets;
type ParentInput = AppMsg;
type ParentWidget = gtk::Box;
// ANCHOR_END: factory_impl_start

Expand Down Expand Up @@ -69,39 +68,29 @@ impl FactoryComponent for Counter {
gtk::Button {
set_label: "Up",
connect_clicked[sender, index] => move |_| {
sender.output(CounterOutput::MoveUp(index.clone()))
sender.output(CounterOutput::MoveUp(index.clone())).unwrap();
}
},

#[name(move_down_button)]
gtk::Button {
set_label: "Down",
connect_clicked[sender, index] => move |_| {
sender.output(CounterOutput::MoveDown(index.clone()))
sender.output(CounterOutput::MoveDown(index.clone())).unwrap();
}
},

#[name(to_front_button)]
gtk::Button {
set_label: "To Start",
connect_clicked[sender, index] => move |_| {
sender.output(CounterOutput::SendFront(index.clone()))
sender.output(CounterOutput::SendFront(index.clone())).unwrap();
}
}
}
}
// ANCHOR_END: factory_view

// ANCHOR: output_to_parent
fn forward_to_parent(output: Self::Output) -> Option<AppMsg> {
Some(match output {
CounterOutput::SendFront(index) => AppMsg::SendFront(index),
CounterOutput::MoveUp(index) => AppMsg::MoveUp(index),
CounterOutput::MoveDown(index) => AppMsg::MoveDown(index),
})
}
// ANCHOR_END: output_to_parent

// ANCHOR: factory_init_model
fn init_model(value: Self::Init, _index: &DynamicIndex, _sender: FactorySender<Self>) -> Self {
Self { value }
Expand Down Expand Up @@ -183,7 +172,16 @@ impl SimpleComponent for App {
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let counters = FactoryVecDeque::new(gtk::Box::default(), sender.input_sender());
// ANCHOR: output_to_parent
let counters = FactoryVecDeque::builder()
.launch(gtk::Box::default())
.forward(sender.input_sender(), |output| match output {
CounterOutput::SendFront(index) => AppMsg::SendFront(index),
CounterOutput::MoveUp(index) => AppMsg::MoveUp(index),
CounterOutput::MoveDown(index) => AppMsg::MoveDown(index),
});
// ANCHOR_END: output_to_parent

let model = App {
created_widgets: counter,
counters,
Expand Down
2 changes: 1 addition & 1 deletion examples/libadwaita/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"

[dev-dependencies]
rand = "0.8.4"
relm4 = { version = "0.6.0", features = ["libadwaita"] }
relm4 = { version = "0.7.0-beta.2", features = ["libadwaita"] }

[[example]]
name = "simple_manual"
Expand Down
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
- [0.2 to 0.4](migrations/0_2_to_0_4.md)
- [0.4 to 0.5](migrations/0_4_to_0_5.md)
- [0.5 to 0.6](migrations/0_5_to_0_6.md)
- [0.6 to 0.7](migrations/0_6_to_0_7.md)
17 changes: 3 additions & 14 deletions src/efficient_ui/factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ Let's look at the associated types one by one:
+ **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.
+ **ParentInput**: The input message type of the parent component.
+ **ParentWidget**: The container widget used to store the widgets of the factory, for example `gtk::Box`.

### Creating the widget
Expand All @@ -101,17 +100,6 @@ Also, we just need to implement the `init_model` function because `init_widgets`
{{#include ../../examples/factory.rs:factory_init_model }}
```

### Forwarding messages

Factories can implement the `forward_to_parent` method to send messages to their parent component.

If `Some` is returned, a message is forwarded.
If `None` is returned, nothing happens.

```rust,no_run,noplayground
{{#include ../../examples/factory.rs:output_to_parent }}
```

## The main component

Now, we have implemented the `FactoryComponent` type for the elements in our factory.
Expand All @@ -129,10 +117,11 @@ First we define the model and the input message type and then start the trait im
### Initializing the factory

We skip the `view` macro for a moment and look at the `init` method.
You see that we are initializing the `FactoryVecDeque` with a container widget.
You see that we are initializing the `FactoryVecDeque` using a builder pattern.
First, we call `FactoryVecDeque::builder()` to create the builder and use `launch()` to set the root widget of the factory.
This widget will store all the widgets created by the factory.

We also pass an input sender so the `forward_to_parent` method we defined earlier can send input messages to our main component.
Then, we use the `forward()` method to pass all output messages of our factory (with type `CounterOutput`) to the input of our component (with type `AppMsg`).

The last trick we have up our sleeves is to define a local variable `counter_box` that is a reference to the container widget of our factory.
We'll use it in the `view` macro in the next section.
Expand Down
65 changes: 65 additions & 0 deletions src/migrations/0_6_to_0_7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Migration from v0.6 to v0.7

## Factory changes

`ParentInput` and `forward_to_parent()` were removed from `FactoryComponent` and `AsyncFactoryComponent`.
Instead, factories now support basically the same builder pattern as components.

### Example

Replace this:

```rust
#[relm4::factory]
impl FactoryComponent for Counter {
type ParentInput = AppMsg;

fn forward_to_parent(output: Self::Output) -> Option<AppMsg> {
Some(match output {
CounterOutput::SendFront(index) => AppMsg::SendFront(index),
CounterOutput::MoveUp(index) => AppMsg::MoveUp(index),
CounterOutput::MoveDown(index) => AppMsg::MoveDown(index),
})
}

// ...
}

#[relm4::component]
impl SimpleComponent for App {
// ...

fn init(
counter: Self::Init,
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let counters = FactoryVecDeque::new(gtk::Box::default(), sender.input_sender());
// ...
}
}
```

With this:

```rust
#[relm4::component]
impl SimpleComponent for App {
// ...

fn init(
counter: Self::Init,
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let counters = FactoryVecDeque::builder()
.launch(gtk::Box::default())
.forward(sender.input_sender(), |output| match output {
CounterOutput::SendFront(index) => AppMsg::SendFront(index),
CounterOutput::MoveUp(index) => AppMsg::MoveUp(index),
CounterOutput::MoveDown(index) => AppMsg::MoveDown(index),
});
// ...
}
}
```

0 comments on commit acfe569

Please sign in to comment.