diff --git a/Cargo.lock b/Cargo.lock index 1d7c55b720..bc07004e13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2603,6 +2603,13 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fcfdc7a0362c9f4444381a9e697c79d435fe65b52a37466fc2c1184cee9edc6" +[[package]] +name = "fireside" +version = "0.1.0" +dependencies = [ + "gpui", +] + [[package]] name = "fixedbitset" version = "0.4.2" diff --git a/Cargo.toml b/Cargo.toml index 121d42223b..7c3ef9ee30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,7 @@ members = [ "crates/workspace", "crates/welcome", "crates/zed", - "crates/zed_actions", + "crates/zed_actions", "crates/fireside", ] default-members = ["crates/zed"] resolver = "2" diff --git a/crates/fireside/Cargo.toml b/crates/fireside/Cargo.toml new file mode 100644 index 0000000000..4b74fd184e --- /dev/null +++ b/crates/fireside/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "fireside" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +gpui = { path = "../gpui" } diff --git a/crates/fireside/src/main.rs b/crates/fireside/src/main.rs new file mode 100644 index 0000000000..96abe0e97b --- /dev/null +++ b/crates/fireside/src/main.rs @@ -0,0 +1,96 @@ +use gpui::*; + +struct Counter { + value: usize, +} + +impl Counter { + fn increment(&mut self, cx: &mut ModelContext) { + self.value += 1; + cx.notify(); + } +} + +struct Observer { + value: usize, +} + +impl Observer { + fn new(counter: &Model, cx: &mut ModelContext) -> Self { + cx.observe(counter, Self::counter_changed).detach(); + Self { + value: counter.read(cx).value * 2, + } + } + + fn counter_changed(&mut self, counter: Model, cx: &mut ModelContext) { + self.value = counter.read(cx).value * 2; + cx.notify(); + } +} + +struct RootView { + counter: Model, + observer: Model, +} + +impl RootView { + fn new(counter: Model, observer: Model, cx: &mut ViewContext) -> Self { + cx.observe(&counter, |_, _, cx| cx.notify()).detach(); + cx.observe(&observer, |_, _, cx| cx.notify()).detach(); + Self { counter, observer } + } +} + +impl Render for RootView { + fn render(&mut self, cx: &mut ViewContext) -> impl IntoElement { + div() + .size_full() + .p_4() + .border() + .border_color(rgb(0x2B2F31)) + .bg(rgb(0x202425)) + .text_color(rgb(0xECEDEE)) + .flex() + .flex_col() + .gap_2() + .child(format!("Counter: {}", self.counter.read(cx).value)) + .child(format!("Observer: {}", self.observer.read(cx).value)) + .child( + div() + .child("Increment") + .w_32() + .h_32() + .bg(red()) + .on_mouse_down( + MouseButton::Left, + cx.listener(|this, _, cx| { + this.counter.update(cx, |counter, cx| { + counter.increment(cx); + }) + }), + ), + ) + } +} + +fn main() { + App::new().run(|cx| { + cx.activate(true); + + let counter: Model = cx.new_model(|_| Counter { value: 0 }); + dbg!(counter.read(cx).value); + + let observer = cx.new_model(|cx| Observer::new(&counter, cx)); + + counter.update(cx, |counter, cx| { + println!("about to increment counter..."); + counter.increment(cx); + dbg!(counter.value); + }); + + let window = cx.open_window(WindowOptions::default(), |cx| { + cx.new_view(|cx| RootView::new(counter, observer, cx)) + }); + }); +}