chore: Sync with Relm4 new-approach branch

This commit is contained in:
Michael Aaron Murphy 2022-01-31 15:55:55 +01:00
parent c25812a45b
commit 3e4a00a8d8
No known key found for this signature in database
GPG key ID: B2732D4240C9212C
12 changed files with 123 additions and 463 deletions

View file

@ -1,6 +1,6 @@
# COSMIC Component System
This library is a GTK4 GUI framework inspired by [Relm](https://github.com/antoyo/relm), which is inspired by [Elm](https://guide.elm-lang.org/architecture/). The philosophy for this framework is to isolate custom widgets into reusable components. You start with a custom `Model` type that implements `Component`, which is used to register a component with an optional argument. On registration, the model is used to construct the view and its widgets in the `init_view()` function. An event-handler is also spawned to handle events from both the component and any component emitting events to it. Those events are received and handled in the `update()` function. Both the `init_view()` and `update()` methods also have access to an outbound sender, which the caller can forward and consume however desired. See the examples directory for a demonstration of how to create a component.
This library is a prototyping area for a rewrite of Relm4's component system.
## Using a Macro to Define a Component
@ -12,6 +12,10 @@ pub enum MyCustomInputMessage {
Variant2,
}
pub enum MyCustomCommand {
Action
}
component! {
// The model stores the state of this component.
pub struct MyCustomModel {
@ -47,17 +51,14 @@ component! {
root.append(&description);
ComponentInner {
Fuselage {
model: MyCustomModel { state: String::new() },
widgets: MyCustomWidgets { description },
input,
output
}
}
// Where events are received, with `component` is the `ComponentInner`,
// and `event` is a `MyCustomInputMessage` which was just received.
fn update(component, event) {
// Updates the model and view. `self` is the model.
fn update(&mut self, widgets, event, input, output) {
match event {
MyCustomInputMessage::Variant1 => {
@ -67,6 +68,12 @@ component! {
}
}
Some(MyCustomCommand::Action)
}
async fn command(command: MyCustomCommand, input) {
}
}
```
@ -74,7 +81,8 @@ component! {
Components can be created and have their output events forwarded:
```rs
let counter = InfoButton::init("Clicked 0 times".into(), "Click".into())
let counter = InfoButton::init()
.launch("Clicked 0 times".into(), "Click".into())
.forward(input.clone(), |event| match event {
InfoButtonOutput::Clicked => AppEvent::Increment
});
@ -85,18 +93,5 @@ The handle returned can be used to emit inputs to it, and to get the root widget
```rs
counter.emit(InfoButtonInput::SetDescription(format!("Clicked {} times", count)));
box.append(counter.widget());
```
## See Also
[Relm4](https://github.com/AaronErhardt/relm4) uses a similar approach, but closely follows the Elm model. This library was created as an alternative approach that makes developing reusable components with forwardable events simpler.
## License
Licensed under the [Mozilla Public License 2.0](https://choosealicense.com/licenses/mpl-2.0/).
### Contribution
Any contribution intentionally submitted for inclusion in the work by you shall be licensed under the Mozilla Public License 2.0 (MPL-2.0).
box.append(&counter.widget);
```