Skip to content

Commit

Permalink
feat: add support for reactive Bevy desktop apps
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Dec 7, 2024
1 parent b4e784e commit 7c65ba9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repository = "https://github.com/actuate-rs/actuate"

[features]
animation = ["ecs", "dep:bevy_math", "dep:bevy_time", "dep:tokio"]
ecs = ["dep:bevy_app", "dep:bevy_ecs", "dep:bevy_hierarchy", "dep:bevy_utils"]
ecs = ["std", "dep:bevy_app", "dep:bevy_ecs", "dep:bevy_hierarchy", "dep:bevy_utils", "dep:bevy_winit"]
executor = ["std", "dep:tokio"]
material = ["ecs", "picking", "dep:bevy_color", "dep:bevy_text", "dep:bevy_ui"]
picking = ["dep:bevy_picking"]
Expand Down Expand Up @@ -37,6 +37,7 @@ bevy_text = { version = "0.15.0", optional = true }
bevy_time = { version = "0.15.0", optional = true }
bevy_ui = { version = "0.15.0", optional = true }
bevy_utils = { version = "0.15.0", optional = true }
bevy_winit = { version = "0.15.0", optional = true }
crossbeam-queue = { version = "0.3.11", default-features = false, features = ["alloc"] }
futures = "0.3.31"
hashbrown = "0.15.2"
Expand Down
2 changes: 2 additions & 0 deletions examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use actuate::prelude::*;
use bevy::prelude::*;
use bevy_winit::WinitSettings;

// Counter composable.
#[derive(Data)]
Expand Down Expand Up @@ -44,6 +45,7 @@ fn setup(mut commands: Commands) {
fn main() {
App::new()
.add_plugins((DefaultPlugins, ActuatePlugin))
.insert_resource(WinitSettings::desktop_app())
.add_systems(Startup, setup)
.run();
}
24 changes: 23 additions & 1 deletion src/ecs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ use bevy_ecs::{
world::{CommandQueue, World},
};
use bevy_utils::HashMap;
use bevy_winit::{EventLoopProxy, EventLoopProxyWrapper, WakeUp};
use core::fmt;
use slotmap::{DefaultKey, SlotMap};
use std::{
cell::{Cell, RefCell},
collections::BTreeSet,
mem, ptr,
rc::Rc,
sync::Arc,
task::{Context, Wake, Waker},
};

#[cfg(feature = "picking")]
Expand Down Expand Up @@ -186,6 +189,16 @@ impl<C: Compose> Compose for CompositionContent<C> {
}
}

struct RuntimeWaker {
proxy: EventLoopProxy<WakeUp>,
}

impl Wake for RuntimeWaker {
fn wake(self: Arc<Self>) {
self.proxy.send_event(WakeUp).unwrap();
}
}

fn compose(world: &mut World) {
RUNTIME_CONTEXT.with(|runtime_cx| {
let mut cell = runtime_cx.borrow_mut();
Expand Down Expand Up @@ -217,11 +230,20 @@ fn compose(world: &mut World) {
rt.commands.borrow_mut().apply(world);
drop(rt);

let proxy = (*world
.get_resource::<EventLoopProxyWrapper<WakeUp>>()
.unwrap())
.clone();
let rt = &mut *world.non_send_resource_mut::<Runtime>();
let mut composers = rt.composers.borrow_mut();
for rt_composer in composers.values_mut() {
let waker = Waker::from(Arc::new(RuntimeWaker {
proxy: proxy.clone(),
}));
let mut cx = Context::from_waker(&waker);

// TODO handle composition error.
let _ = rt_composer.composer.try_compose();
let _ = rt_composer.composer.poll_compose(&mut cx);
}
}

Expand Down

0 comments on commit 7c65ba9

Please sign in to comment.