Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Dec 9, 2023
1 parent a897f87 commit e1e3b38
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 79 deletions.
52 changes: 19 additions & 33 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 27 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,46 @@ Cross-platform user interface framework for Rust.

This crate provides a moduler GUI library that can be used as an entire framework, or with individual parts.

```rust
use viewbuilder::element::Text;

fn main() {
viewbuilder::launch(Text::new("Hello World"))
}
```

### Bring your own state management

```rust
use viewbuilder::element::{LinearLayout, Text};
use viewbuilder::Window;

fn main() {
let mut count = 0;
let layout = viewbuilder::view(
LinearLayout::builder()
.child(Text::new("Hello"))
.child(Text::new("World"))
.child(Text::new("Counter"))
.child(
Text::builder()
.on_click(move |text| {
count += 1;
text.get()
.borrow_mut()
.set_content(format!("High fives: {}", count));
})
.build("0"),
)
.build(),
);

Window::builder().title("Hello Example").build(layout);
Window::builder().title("Counter Example").build(layout);

viewbuilder::run()
}
```


## Features

- Cross-platform with desktop and mobile support
- Event handling with an HTML-like API
- State management with [dioxus](https://github.com/DioxusLabs/dioxus/) (optional)
Expand All @@ -52,9 +72,11 @@ fn main() {
- High performance rendering with [rust-skia](https://github.com/rust-skia/rust-skia)

## Getting started

Instatllation is simple with:

```sh
cargo add viewbuilder --features full
```
If you encounter errors, please check the instructions for building [rust-skia](https://github.com/rust-skia/rust-skia).

If you encounter errors, please check the instructions for building [rust-skia](https://github.com/rust-skia/rust-skia).
34 changes: 7 additions & 27 deletions examples/counter.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,20 @@
use std::cell::RefCell;
use std::rc::Rc;

use viewbuilder::element::{LinearLayout, Text};
use viewbuilder::Window;

fn main() {
let label = viewbuilder::view(Text::new("0"));
let count = Rc::new(RefCell::new(0));
let count_clone = count.clone();

let mut count = 0;
let layout = viewbuilder::view(
LinearLayout::builder()
.child(label)
.child(
Text::builder()
.on_click(move || {
let mut n = count.borrow_mut();
*n += 1;
label
.get()
.borrow_mut()
.set_content(format!("High fives: {}", n));
})
.build("More!"),
)
.child(Text::new("Counter"))
.child(
Text::builder()
.on_click(move || {
let mut n = count_clone.borrow_mut();
*n -= 1;
label
.get()
.on_click(move |text| {
count += 1;
text.get()
.borrow_mut()
.set_content(format!("High fives: {}", n));
.set_content(format!("High fives: {}", count));
})
.build("Less!"),
.build("0"),
)
.build(),
);
Expand Down
14 changes: 2 additions & 12 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
use viewbuilder::element::{LinearLayout, Text};
use viewbuilder::Window;
use viewbuilder::element::Text;

fn main() {
let layout = viewbuilder::view(
LinearLayout::builder()
.child(Text::new("Hello"))
.child(Text::new("World"))
.build(),
);

Window::builder().title("Hello Example").build(layout);

viewbuilder::run()
viewbuilder::launch(Text::new("Hello World"))
}
4 changes: 2 additions & 2 deletions src/element/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Element;
use crate::{Element, ElementRef};
use kurbo::Size;
use slotmap::DefaultKey;
use std::borrow::Cow;
Expand All @@ -7,7 +7,7 @@ use std::borrow::Cow;
pub struct TextBuilder {}

impl TextBuilder {
pub fn on_click(&mut self, _f: impl FnMut() + 'static) -> &mut Self {
pub fn on_click(&mut self, f: impl FnMut(ElementRef<Text>) + 'static) -> &mut Self {
self
}

Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,9 @@ pub fn view<E: Element + 'static>(element: E) -> ElementRef<E> {
pub fn run() {
UserInterface::current().run()
}

pub fn launch<E: Element + 'static>(element: E) {
let ui = UserInterface::current();
ui.view(element);
ui.run()
}

0 comments on commit e1e3b38

Please sign in to comment.