Skip to content

Commit

Permalink
Simplify example
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Dec 13, 2023
1 parent fb85c9f commit a62a50c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

10 changes: 6 additions & 4 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use concoct::{Handle, Object, Slot};
use concoct::{Handle, Object, Slot, Signal};
use web_sys::wasm_bindgen::JsCast;

pub trait View {
Expand Down Expand Up @@ -59,9 +59,7 @@ pub struct MouseEvent;
pub struct ElementBuilder {}

impl ElementBuilder {
pub fn on_click(&mut self, _f: Handle<impl Slot<MouseEvent>>) -> &mut Self {
self
}


pub fn child(&mut self, _view: impl ViewGroup) -> &mut Self {
self
Expand Down Expand Up @@ -108,6 +106,10 @@ impl View for Element {
}
}

impl Signal<MouseEvent> for Element {

}

pub struct AppendChild<V>(pub Handle<V>);

impl<V: View + 'static> Slot<AppendChild<V>> for Element {
Expand Down
38 changes: 7 additions & 31 deletions web-examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use concoct::{Handle, Object, Slot};
use viewbuilder::web::{self, Element, Text};
use viewbuilder::web::{Element, Text};

#[derive(Clone, Copy)]
enum Message {
Expand All @@ -24,17 +24,10 @@ impl Slot<Message> for Counter {
}
}

struct CounterButton {
msg: Message,
counter: Handle<Counter>,
}

impl Object for CounterButton {}

impl Slot<web::MouseEvent> for CounterButton {
fn handle(&mut self, _cx: concoct::Handle<Self>, _msg: web::MouseEvent) {
self.counter.send(self.msg);
}
fn counter_button(counter: &Handle<Counter>, label: &str, msg: Message) -> Handle<Element> {
let button = Element::builder().child(Text::new(label)).build().start();
button.map(&counter, move |_| msg);
button
}

#[viewbuilder::main]
Expand All @@ -47,28 +40,11 @@ fn main() {
}
.start();

let increment_button = CounterButton {
msg: Message::Increment,
counter: counter.clone(),
}
.start();
let decrement_button = CounterButton {
msg: Message::Decrement,
counter,
}
.start();

Element::builder()
.child((
text,
Element::builder()
.on_click(increment_button)
.child(Text::new("Up High!"))
.build(),
Element::builder()
.on_click(decrement_button)
.child(Text::new("Down Low!"))
.build(),
counter_button(&counter, "Up high!", Message::Increment),
counter_button(&counter, "Down low!", Message::Decrement),
))
.build()
.start();
Expand Down

0 comments on commit a62a50c

Please sign in to comment.