a few changes

This commit is contained in:
2024-12-19 01:13:16 +01:00
parent 7374063cce
commit 90c308e949
4 changed files with 41 additions and 17 deletions
+6 -9
View File
@@ -25,6 +25,7 @@ fn app() -> Element {
``` ```
## A slightly more advanced app (counter) ## A slightly more advanced app (counter)
This app uses state management and a function using the state as a dependent
```rust title='src/main.rs' ```rust title='src/main.rs'
use osui::prelude::*; use osui::prelude::*;
@@ -33,20 +34,16 @@ fn main() {
} }
fn app() -> Element { fn app() -> Element {
let count = State::new(0);
rsx! { rsx! {
// Create a button with a on_click handler
button { button {
on_click: fn(btn: &mut Button, _, document) { on_click: fn(_, _, _) @count {
let count = document.use_state::<u32>("count"); count += 1;
*count += 1;
btn.children.set_text(&count.to_string());
}, },
"0" "The current count is: {count}"
} }
// Create a elemental field
@count: u32;
} }
} }
``` ```
+16
View File
@@ -114,4 +114,20 @@ fn app() -> Element {
} }
} }
} }
```
## Instructions
Add an instruction by putting a `@` before it, like this:
```rust
pub fn app() -> Element {
rsx! {
@SetStyle(css! { // the SetStyle instruction
"title": {
color: Green,
}
})
text { class: "title", static "Hello world!" }
}
}
``` ```
+12 -7
View File
@@ -22,21 +22,26 @@ rsx! {
} }
``` ```
### With dependents ### With dependents
If you have a `State<T>` and want to use it in a `fn()`, you have to put a @ before the code block like this If you have a `State<T>` and want to use it in a `fn()`, Pub a @ before the code block like this
```rust ```rust
fn app() -> Element { pub fn app() -> Element {
let count = State::new(); let count = State::new(0);
rsx! { rsx! {
button { button {
on_click: fn(btn: &mut Button, event, document) @count { on_click: fn(_, _, _) @count {
let count = count.use_state(); // locks count so it can be used count += 1;
}, },
"Current count: {count}" "The current count is: {count}"
} }
} }
} }
``` ```
### How to call a handler
```rust
my_handler.clone().call();
```
> :::info you can add multiple decedents by separating them via `,` like: @var1, var2, var3 > :::info you can add multiple decedents by separating them via `,` like: @var1, var2, var3
> :::info `fn() {}` is a part of the OSUI macro `rsx!`. In rust you would normally use `|| {}` > :::info `fn() {}` is a part of the OSUI macro `rsx!`. In rust you would normally use `|| {}`
+7 -1
View File
@@ -2,7 +2,9 @@
title: Element title: Element
--- ---
`Element` is a type alias of `Box<dyn ElementWidget>`. It makes the code look cleaner A element is a very important part of a UI library, It's what makes it possible.
`Element` is a type alias of `Box<dyn ElementWidget>`. It makes the code look cleaner.
# ElementWidget # ElementWidget
`ElementWidget` is a trait that defines the Elements and it's rendering / event updates. `ElementWidget` is a trait that defines the Elements and it's rendering / event updates.
@@ -16,6 +18,10 @@ title: Element
- **optional** - **optional**
- This function updates the element when there is a event - This function updates the element when there is a event
### `event(&mut self, document: &mut Document)`
- **optional**
- This function runs when the app first runs. It helps with element initialization after the `rsx!` stage.
# ElementCore # ElementCore
`ElementCore` is a trait that will automatically be implemented by the `osui_element::element` proc-macro. All of it's functions are used by OSUI. `ElementCore` is a trait that will automatically be implemented by the `osui_element::element` proc-macro. All of it's functions are used by OSUI.