Compare commits

...

1 Commits

Author SHA1 Message Date
Nathan Sobo
fab0806028 Add fireside crate demoing GPUI things 2024-01-25 22:16:37 -07:00
4 changed files with 113 additions and 1 deletions

7
Cargo.lock generated
View File

@@ -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"

View File

@@ -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"

View File

@@ -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" }

View File

@@ -0,0 +1,96 @@
use gpui::*;
struct Counter {
value: usize,
}
impl Counter {
fn increment(&mut self, cx: &mut ModelContext<Self>) {
self.value += 1;
cx.notify();
}
}
struct Observer {
value: usize,
}
impl Observer {
fn new(counter: &Model<Counter>, cx: &mut ModelContext<Self>) -> Self {
cx.observe(counter, Self::counter_changed).detach();
Self {
value: counter.read(cx).value * 2,
}
}
fn counter_changed(&mut self, counter: Model<Counter>, cx: &mut ModelContext<Self>) {
self.value = counter.read(cx).value * 2;
cx.notify();
}
}
struct RootView {
counter: Model<Counter>,
observer: Model<Observer>,
}
impl RootView {
fn new(counter: Model<Counter>, observer: Model<Observer>, cx: &mut ViewContext<Self>) -> 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<Self>) -> 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<Counter> = 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))
});
});
}