Skip to content

Commit

Permalink
Update book to 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronErhardt committed Mar 13, 2024
1 parent 969b93a commit e174edd
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 36 deletions.
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.7.0-beta.2"
relm4-components = "0.7.0-beta.2"
relm4 = "0.7.0"
relm4-components = "0.7.0"
tokio = { version = "1.25", features = ["rt", "macros", "time", "rt-multi-thread", "sync"] }
tracker = "0.2.0"
8 changes: 4 additions & 4 deletions examples/alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl SimpleComponent for Alert {
// ANCHOR: init_model
fn init(
settings: AlertSettings,
root: &Self::Root,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = Alert {
Expand Down Expand Up @@ -245,12 +245,12 @@ impl SimpleComponent for App {
}

// ANCHOR: app_init
fn init(_: (), root: &Self::Root, sender: ComponentSender<Self>) -> ComponentParts<Self> {
fn init(_: (), root: Self::Root, sender: ComponentSender<Self>) -> ComponentParts<Self> {
let model = App {
counter: 0,
alert_toggle: false,
dialog: Alert::builder()
.transient_for(root)
.transient_for(&root)
.launch(AlertSettings {
text: String::from("Do you want to quit without saving? (First alert)"),
secondary_text: Some(String::from("Your counter hasn't reached 42 yet")),
Expand All @@ -262,7 +262,7 @@ impl SimpleComponent for App {
})
.forward(sender.input_sender(), convert_alert_response),
second_dialog: Alert::builder()
.transient_for(root)
.transient_for(&root)
.launch(AlertSettings {
text: String::from("Do you want to quit without saving? (Second alert)"),
secondary_text: Some(String::from("Your counter hasn't reached 42 yet")),
Expand Down
4 changes: 2 additions & 2 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ impl AsyncComponent for App {
}

// ANCHOR: init_loading_widgets
fn init_loading_widgets(root: &mut Self::Root) -> Option<LoadingWidgets> {
fn init_loading_widgets(root: Self::Root) -> Option<LoadingWidgets> {
view! {
#[local_ref]
#[local]
root {
set_title: Some("Simple app"),
set_default_size: (300, 100),
Expand Down
4 changes: 2 additions & 2 deletions examples/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Component for CommandModel {
}

// Initialize the UI.
fn init(_: Self::Init, _: &Self::Root, _: ComponentSender<Self>) -> ComponentParts<Self> {
fn init(_: Self::Init, _: Self::Root, _: ComponentSender<Self>) -> ComponentParts<Self> {
todo!()
}

Expand Down Expand Up @@ -99,7 +99,7 @@ impl Component for SyncCommandModel {
}

// Initialize the UI.
fn init(_: Self::Init, _: &Self::Root, _: ComponentSender<Self>) -> ComponentParts<Self> {
fn init(_: Self::Init, _: Self::Root, _: ComponentSender<Self>) -> ComponentParts<Self> {
todo!()
}

Expand Down
8 changes: 4 additions & 4 deletions examples/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl SimpleComponent for HeaderModel {

fn init(
_params: Self::Init,
root: &Self::Root,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = HeaderModel;
Expand Down Expand Up @@ -127,7 +127,7 @@ impl SimpleComponent for DialogModel {

fn init(
params: Self::Init,
root: &Self::Root,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = DialogModel { hidden: params };
Expand Down Expand Up @@ -200,7 +200,7 @@ impl SimpleComponent for AppModel {
// ANCHOR: app_init
fn init(
params: Self::Init,
root: &Self::Root,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
// ANCHOR: forward
Expand All @@ -215,7 +215,7 @@ impl SimpleComponent for AppModel {
// ANCHOR_END: forward

let dialog = DialogModel::builder()
.transient_for(root)
.transient_for(&root)
.launch(true)
.forward(sender.input_sender(), |msg| match msg {
DialogOutput::Close => AppMsg::Close,
Expand Down
2 changes: 1 addition & 1 deletion examples/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl SimpleComponent for App {
// Initialize the UI.
fn init(
counter: Self::Init,
root: &Self::Root,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
// ANCHOR: output_to_parent
Expand Down
2 changes: 1 addition & 1 deletion examples/libadwaita/simple_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl SimpleComponent for AppModel {

fn init(
counter: Self::Init,
window: &Self::Root,
window: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = AppModel { counter };
Expand Down
2 changes: 1 addition & 1 deletion examples/macro_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl SimpleComponent for AppModel {
// Initialize the UI.
fn init(
init: Self::Init,
renamed_root: &Self::Root,
renamed_root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let counter = AppModel {
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl SimpleComponent for AppModel {
// Initialize the UI.
fn init(
counter: Self::Init,
root: &Self::Root,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = AppModel { counter };
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl SimpleComponent for AppModel {
/// Initialize the UI and model.
fn init(
counter: Self::Init,
window: &Self::Root,
window: Self::Root,
sender: ComponentSender<Self>,
) -> relm4::ComponentParts<Self> {
let model = AppModel { counter };
Expand Down
2 changes: 1 addition & 1 deletion examples/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl SimpleComponent for AppModel {
}

// Initialize the UI.
fn init(_: Self::Init, _: &Self::Root, _: ComponentSender<Self>) -> ComponentParts<Self> {
fn init(_: Self::Init, _: Self::Root, _: ComponentSender<Self>) -> ComponentParts<Self> {
todo!()
}

Expand Down
9 changes: 4 additions & 5 deletions examples/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl SimpleComponent for AppModel {
// Initialize the UI.
fn init(
_params: Self::Init,
root: &Self::Root,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
// ANCHOR: model_init
Expand All @@ -113,12 +113,8 @@ impl SimpleComponent for AppModel {
};
// ANCHOR_END: model_init

// ANCHOR: post_init
relm4::set_global_css(".identical { background: #00ad5c; }");

// Insert the macro code generation here
let widgets = view_output!();
// ANCHOR_END: post_init

ComponentParts { model, widgets }
}
Expand All @@ -141,8 +137,11 @@ impl SimpleComponent for AppModel {
// ANCHOR_END: update
}

// ANCHOR: main
fn main() {
let app = RelmApp::new("relm4.test.simple");
app.set_global_css(".identical { background: #00ad5c; }");
app.run::<AppModel>(());
}
// ANCHOR_END: main
// ANCHOR_END: all
2 changes: 1 addition & 1 deletion examples/widget_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl SimpleComponent for AppModel {

fn init(
counter: Self::Init,
root: &Self::Root,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = Self { counter };
Expand Down
2 changes: 1 addition & 1 deletion examples/widget_template_nested_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl SimpleComponent for AppModel {

fn init(
_init_param: Self::Init,
root: &Self::Root,
root: Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = Self {
Expand Down
2 changes: 1 addition & 1 deletion examples/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl SimpleComponent for AppModel {
}

// ANCHOR: worker_construction
fn init(_: (), root: &Self::Root, sender: ComponentSender<Self>) -> ComponentParts<Self> {
fn init(_: (), root: Self::Root, sender: ComponentSender<Self>) -> ComponentParts<Self> {
let model = AppModel {
counter: 0,
worker: AsyncHandler::builder()
Expand Down
11 changes: 6 additions & 5 deletions src/efficient_ui/tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,16 @@ Now we reached the interesting part of the code where we can actually make use o
{{#include ../../examples/tracker.rs:view }}
```

The overall UI is pretty simple: A window that contains a box. This box itself has two boxes that display the two icons and the two buttons to update them.
## The main function

In this example, we need some additional code in `fn main()` to add [custom CSS](https://docs.gtk.org/gtk4/css-properties.html) that sets the background color for elements with class name "identical".
Later, we just need to assign the "identical" class name to a widget to make it match the CSS selector.

We also added some additional code in `init` that runs before the view is constructed. In our case, we want to add [custom CSS](https://docs.gtk.org/gtk4/css-properties.html) that sets the background color for elements with class name "identical".

```rust,no_run,noplayground
{{#include ../../examples/tracker.rs:post_init }}
{{#include ../../examples/tracker.rs:main }}
```


### The `#[track]` attribute

The `#[track]` attribute is applied to method invocations in our view code. It allows us to add a condition to the update: if the condition is true, the method will be called, otherwise, it will be skipped. The attribute syntax looks like this:
Expand Down Expand Up @@ -161,7 +162,7 @@ The second `#[track]` attribute works similarly:
{{#include ../../examples/tracker.rs:track2 }}
```

> **Debugging Helper**
> **Using a tracker as debugging helper**
>
> Since the `#[track]` attribute parses expressions, you can use the following syntax to debug your trackers:
>
Expand Down
4 changes: 2 additions & 2 deletions src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ I also recommend reading the [gtk4-rs book](https://gtk-rs.org/gtk4-rs/git/book/
Add the packages you need to your `Cargo.toml`:

```toml
relm4 = "0.6.0"
relm4-components = "0.6.0"
relm4 = "0.7.0"
relm4-components = "0.7.0"
```

## Issues and feedback
Expand Down
15 changes: 14 additions & 1 deletion src/migrations/0_6_to_0_7.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Migration from v0.6 to v0.7

## Component changes

Switching between the `Component` and `AsyncComponent` traits has been a bit unpleasant in previous versions due to complex errors generated by the code generated by `async-trait`.
To avoid this, the signatures of `Component` and `AsyncComponent` were unified.

This means, that `Component::init()` and `SimpleComponent::init()` now take `Self::Root` as owned parameter instead of a reference (`&Self::Root`).
In most places, this just means that you have to remove a `&`.

## Factory changes

`ParentInput` and `forward_to_parent()` were removed from `FactoryComponent` and `AsyncFactoryComponent`.
Expand Down Expand Up @@ -62,4 +70,9 @@ impl SimpleComponent for App {
// ...
}
}
```
```

## Other changes

+ `set_global_css` and `set_global_css_from_file` are now methods of `RelmApp` to prevent calling them before initializing GTK
+ The `drawing` module was moved into the new `abstractions` module

0 comments on commit e174edd

Please sign in to comment.