feat: add support to trigger refresh through external events.

- Allow users to trigger a dispatch by using
`Res<ExternalEventDispatcher>`.
This commit is contained in:
Naman Agrawal
2025-08-28 17:30:43 +05:30
parent a91508672e
commit 541e0e646f
2 changed files with 178 additions and 8 deletions
+136
View File
@@ -0,0 +1,136 @@
use std::time::Duration;
use bevy::{color::palettes::basic::*, prelude::*, window::WindowResolution, winit::WinitPlugin};
use bevy_wayland::{prelude::*, ExternalEventDispatcher};
const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15);
const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25);
const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35);
fn main() {
App::new()
.add_plugins((
DefaultPlugins
.build()
.disable::<WinitPlugin>()
.set(WindowPlugin {
primary_window: Some(Window {
resolution: WindowResolution::new(400.0, 400.0),
present_mode: bevy::window::PresentMode::AutoVsync,
..Default::default()
}),
..Default::default()
}),
WaylandPlugin,
))
.add_systems(Startup, (setup, external_tick_sender))
.add_systems(Update, (button_system, exit_on_esc))
.run();
}
fn external_tick_sender(external_event_dispatcher: Res<ExternalEventDispatcher>) {
let displatcher = external_event_dispatcher.clone();
let mut count = 5;
std::thread::spawn(move || loop {
println!("Spawned Thread");
std::thread::sleep(Duration::from_secs(1));
displatcher.dispatch().unwrap();
count -= 1;
if count < 0 {
return;
}
});
}
#[allow(clippy::type_complexity)]
fn button_system(
mut interaction_query: Query<
(
&Interaction,
&mut BackgroundColor,
&mut BorderColor,
&Children,
),
(Changed<Interaction>, With<Button>),
>,
mut text_query: Query<&mut Text>,
) {
//info!("Button system was called!!");
for (interaction, mut color, mut border_color, children) in &mut interaction_query {
let mut text = text_query.get_mut(children[0]).unwrap();
match *interaction {
Interaction::Pressed => {
**text = "Press".to_string();
*color = PRESSED_BUTTON.into();
border_color.0 = RED.into();
}
Interaction::Hovered => {
**text = "Hover".to_string();
*color = HOVERED_BUTTON.into();
border_color.0 = Color::WHITE;
}
Interaction::None => {
**text = "Button".to_string();
*color = NORMAL_BUTTON.into();
border_color.0 = Color::BLACK;
}
}
}
}
fn setup(mut commands: Commands, assets: Res<AssetServer>, windows: Query<Entity, With<Window>>) {
for entity in &windows {
commands.entity(entity).insert((LayerShellSettings {
anchor: Anchor::TOP | Anchor::LEFT,
layer: Layer::Bottom,
..Default::default()
},));
}
// ui camera
commands.spawn(Camera2d);
commands.spawn(button(&assets));
}
fn exit_on_esc(keys: Res<ButtonInput<KeyCode>>) {
if keys.just_pressed(KeyCode::Escape) {
std::process::exit(0);
}
}
fn button(asset_server: &AssetServer) -> impl Bundle + use<> {
(
Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
children![(
Button,
Node {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(5.0)),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
..default()
},
BorderColor(Color::BLACK),
BorderRadius::MAX,
BackgroundColor(NORMAL_BUTTON),
children![(
Text::new("Button"),
TextFont {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 33.0,
..default()
},
TextColor(Color::srgb(0.9, 0.9, 0.9)),
TextShadow::default(),
)]
)],
)
}
+42 -8
View File
@@ -1,9 +1,15 @@
use std::{
num::NonZero,
sync::mpsc::SendError,
time::{Duration, Instant},
};
use bevy::{app::PluginsState, prelude::*}; use bevy::{app::PluginsState, prelude::*};
use smithay_client_toolkit::{ use smithay_client_toolkit::{
delegate_registry, delegate_registry,
output::OutputState, output::OutputState,
reexports::{ reexports::{
calloop::EventLoop, calloop::{self, channel::Sender, EventLoop},
calloop_wayland_source::WaylandSource, calloop_wayland_source::WaylandSource,
client::{globals::registry_queue_init, Connection}, client::{globals::registry_queue_init, Connection},
}, },
@@ -27,23 +33,46 @@ pub mod prelude {
pub use smithay_client_toolkit::shell::wlr_layer::{Anchor, KeyboardInteractivity, Layer}; pub use smithay_client_toolkit::shell::wlr_layer::{Anchor, KeyboardInteractivity, Layer};
} }
pub struct Tick;
#[derive(Resource, Clone)]
pub struct ExternalEventDispatcher(Sender<Tick>);
impl ExternalEventDispatcher {
fn new(tx: Sender<Tick>) -> Self {
Self(tx)
}
pub fn dispatch(&self) -> Result<(), SendError<Tick>> {
self.0.send(Tick)
}
}
#[derive(Default)] #[derive(Default)]
pub struct WaylandPlugin; pub struct WaylandPlugin;
impl Plugin for WaylandPlugin { impl Plugin for WaylandPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
let connection = let connection =
Connection::connect_to_env().expect("failed to connect to wayland socket!"); Connection::connect_to_env().expect("Failed to connect to wayland socket!");
let event_loop = let event_loop =
EventLoop::<WaylandState>::try_new().expect("failed to create event_loop!"); EventLoop::<WaylandState>::try_new().expect("Failed to create event_loop!");
let (globals, event_queue) = registry_queue_init::<WaylandState>(&connection) let (globals, event_queue) = registry_queue_init::<WaylandState>(&connection)
.expect("failed to init registry queue"); .expect("Failed to init registry queue");
let qh = event_queue.handle(); let qh = event_queue.handle();
let loop_handle = event_loop.handle(); let loop_handle = event_loop.handle();
WaylandSource::new(connection.clone(), event_queue) WaylandSource::new(connection.clone(), event_queue)
.insert(loop_handle.clone()) .insert(loop_handle.clone())
.expect("failed to insert wayland source to event loop"); .expect("Failed to insert wayland source to event loop");
let (tx, rx) = calloop::channel::channel::<Tick>();
loop_handle
.insert_source(rx, |_, _, state| {
info!("External event was received!");
if state.plugins_state() == PluginsState::Cleaned {
state.update();
}
})
.expect("Failed to insert external tick channel!");
app.insert_resource(ExternalEventDispatcher::new(tx));
app.insert_non_send_resource(RegistryState::new(&globals)); app.insert_non_send_resource(RegistryState::new(&globals));
app.insert_non_send_resource(connection.clone()); app.insert_non_send_resource(connection.clone());
app.insert_non_send_resource(globals); app.insert_non_send_resource(globals);
@@ -66,14 +95,19 @@ pub fn runner(mut app: App, mut event_loop: EventLoop<'_, WaylandState>) -> AppE
app.finish(); app.finish();
app.cleanup(); app.cleanup();
} }
let mut state = WaylandState(app); let mut state = WaylandState(app);
loop { loop {
// TODO: Error handling let frame_start = Instant::now();
let _ = event_loop.dispatch(Duration::from_millis(5000), &mut state);
if state.plugins_state() == PluginsState::Cleaned { if state.plugins_state() == PluginsState::Cleaned {
state.update(); state.update();
} }
let _ = event_loop.dispatch(None, &mut state); let _ = event_loop.dispatch(Duration::from_millis(0), &mut state);
// TODO: Poll until delta time is greater than target frame time.
if Instant::now() - frame_start < Duration::from_millis(16) {
std::thread::sleep(Duration::from_millis(16) - (frame_start - Instant::now()));
}
let _ = event_loop.dispatch(Duration::from_millis(0), &mut state);
} }
} }