bugfix: keyboard input was causing the application to crash.

This commit is contained in:
Naman Agrawal
2025-08-22 18:17:53 +05:30
parent e98d52a57f
commit e1fcc70667
6 changed files with 47 additions and 40 deletions
+33 -27
View File
@@ -6,7 +6,7 @@ use bevy::{
window::{WindowCreated, WindowResolution}, window::{WindowCreated, WindowResolution},
winit::WinitPlugin, winit::WinitPlugin,
}; };
use bevy_wayland::{layer_shell::LayerShellSettings, WaylandPlugin}; use bevy_wayland::{input_region::InputRegion, layer_shell::LayerShellSettings, WaylandPlugin};
use smithay_client_toolkit::shell::wlr_layer::{Anchor, Layer}; use smithay_client_toolkit::shell::wlr_layer::{Anchor, Layer};
const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15); const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15);
@@ -30,7 +30,10 @@ fn main() {
)) ))
.init_resource::<NewWindowInfo>() .init_resource::<NewWindowInfo>()
.add_systems(Startup, setup) .add_systems(Startup, setup)
.add_systems(Update, (button_system, setup_new_window, spawn_window)) .add_systems(
Update,
(button_system, spawn_window, setup_new_window, exit_on_esc),
)
.run(); .run();
} }
@@ -75,11 +78,11 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>, windows: Query<Entity
for entity in &windows { for entity in &windows {
commands.entity(entity).insert(( commands.entity(entity).insert((
LayerShellSettings { LayerShellSettings {
anchor: Anchor::all(), anchor: Anchor::TOP | Anchor::LEFT,
layer: Layer::Bottom, layer: Layer::Bottom,
..Default::default() ..Default::default()
}, },
WindowTimer(Timer::new(Duration::from_secs(5), TimerMode::Once)), InputRegion(Rect::new(0., 0., 200., 200.)),
)); ));
} }
// ui camera // ui camera
@@ -87,36 +90,39 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>, windows: Query<Entity
commands.spawn(button(&assets)); commands.spawn(button(&assets));
} }
fn exit_on_esc(keys: Res<ButtonInput<KeyCode>>) {
if keys.just_pressed(KeyCode::Escape) {
std::process::exit(0);
}
}
fn spawn_window( fn spawn_window(
mut commands: Commands, mut commands: Commands,
mut windows: Query<(Entity, &mut WindowTimer)>, mut windows: Query<(Entity, &mut WindowTimer)>,
mut new_window_info: ResMut<NewWindowInfo>, mut new_window_info: ResMut<NewWindowInfo>,
keys: Res<ButtonInput<KeyCode>>,
time: Res<Time>, time: Res<Time>,
) { ) {
for (entity, mut timer) in &mut windows { if keys.pressed(KeyCode::KeyN) {
timer.tick(time.delta()); println!("Pressed");
let new_window_entity = commands
.spawn((
Window {
title: "UI Only Window".to_string(),
resolution: (400., 50.).into(),
..default()
},
LayerShellSettings {
layer: Layer::Top,
anchor: Anchor::TOP | Anchor::LEFT,
..default()
},
WindowTimer(Timer::new(Duration::from_secs(5), TimerMode::Once)),
))
.id();
if timer.finished() { new_window_info.entity = Some(new_window_entity);
commands.entity(entity).remove::<WindowTimer>(); new_window_info.is_setup_pending = true;
let new_window_entity = commands
.spawn((
Window {
title: "UI Only Window".to_string(),
resolution: (400., 50.).into(),
..default()
},
LayerShellSettings {
layer: Layer::Top,
anchor: Anchor::TOP | Anchor::LEFT,
..default()
},
WindowTimer(Timer::new(Duration::from_secs(5), TimerMode::Once)),
))
.id();
new_window_info.entity = Some(new_window_entity);
new_window_info.is_setup_pending = true;
}
} }
} }
+3 -1
View File
@@ -409,6 +409,7 @@ impl KeyboardHandler for WaylandState {
let active_window_entity = **self.world().resource::<ActiveWindow>(); let active_window_entity = **self.world().resource::<ActiveWindow>();
let keyboard_event = let keyboard_event =
convert_keyboard_event(event, active_window_entity, ButtonState::Pressed); convert_keyboard_event(event, active_window_entity, ButtonState::Pressed);
self.world_mut().send_event(keyboard_event.clone());
self.world_mut() self.world_mut()
.send_event(WindowEvent::KeyboardInput(keyboard_event)); .send_event(WindowEvent::KeyboardInput(keyboard_event));
} }
@@ -425,6 +426,7 @@ impl KeyboardHandler for WaylandState {
let mut keyboard_event = let mut keyboard_event =
convert_keyboard_event(event, active_window_entity, ButtonState::Pressed); convert_keyboard_event(event, active_window_entity, ButtonState::Pressed);
keyboard_event.repeat = true; keyboard_event.repeat = true;
self.world_mut().send_event(keyboard_event.clone());
self.world_mut() self.world_mut()
.send_event(WindowEvent::KeyboardInput(keyboard_event)); .send_event(WindowEvent::KeyboardInput(keyboard_event));
} }
@@ -440,6 +442,7 @@ impl KeyboardHandler for WaylandState {
let active_window_entity = **self.world().resource::<ActiveWindow>(); let active_window_entity = **self.world().resource::<ActiveWindow>();
let keyboard_event = let keyboard_event =
convert_keyboard_event(event, active_window_entity, ButtonState::Released); convert_keyboard_event(event, active_window_entity, ButtonState::Released);
self.world_mut().send_event(keyboard_event.clone());
self.world_mut() self.world_mut()
.send_event(WindowEvent::KeyboardInput(keyboard_event)); .send_event(WindowEvent::KeyboardInput(keyboard_event));
} }
@@ -454,7 +457,6 @@ impl KeyboardHandler for WaylandState {
_raw_modifiers: smithay_client_toolkit::seat::keyboard::RawModifiers, _raw_modifiers: smithay_client_toolkit::seat::keyboard::RawModifiers,
_layout: u32, _layout: u32,
) { ) {
todo!()
} }
} }
delegate_keyboard!(WaylandState); delegate_keyboard!(WaylandState);
+5 -5
View File
@@ -47,11 +47,11 @@ impl SeatHandler for WaylandState {
capability: smithay_client_toolkit::seat::Capability, capability: smithay_client_toolkit::seat::Capability,
) { ) {
if capability == Capability::Keyboard { if capability == Capability::Keyboard {
//let mut seat_state = self.world_mut().non_send_resource_mut::<SeatState>(); let mut seat_state = self.world_mut().non_send_resource_mut::<SeatState>();
//let wl_keyboard = seat_state let wl_keyboard = seat_state
// .get_keyboard(qh, &seat, None) .get_keyboard(qh, &seat, None)
// .expect("error while attaching keyboard!"); .expect("error while attaching keyboard!");
//self.world_mut().insert_non_send_resource(wl_keyboard); self.world_mut().insert_non_send_resource(wl_keyboard);
info!("Keyboard Attached"); info!("Keyboard Attached");
} }
if capability == Capability::Pointer { if capability == Capability::Pointer {
+2 -2
View File
@@ -7,7 +7,7 @@ use smithay_client_toolkit::{
use crate::{input_region, surface_handler::WaylandSurfaces, WaylandState}; use crate::{input_region, surface_handler::WaylandSurfaces, WaylandState};
#[derive(Component, Deref)] #[derive(Component, Deref)]
pub struct InputRegion(Rect); pub struct InputRegion(pub Rect);
pub struct InputRegionPlugin; pub struct InputRegionPlugin;
impl Plugin for InputRegionPlugin { impl Plugin for InputRegionPlugin {
@@ -27,7 +27,7 @@ fn update_input_region(
let region = Region::new(compositor.as_ref()).unwrap(); let region = Region::new(compositor.as_ref()).unwrap();
region.add( region.add(
input_region.min.x as i32, input_region.min.x as i32,
input_region.max.y as i32, input_region.min.y as i32,
input_region.width() as i32, input_region.width() as i32,
input_region.height() as i32, input_region.height() as i32,
); );
+3 -3
View File
@@ -1,7 +1,7 @@
use bevy::{platform::collections::HashMap, prelude::*, ui::update}; use bevy::{platform::collections::HashMap, prelude::*, ui::update};
use smithay_client_toolkit::{ use smithay_client_toolkit::{
delegate_layer, delegate_layer,
reexports::client::{globals::GlobalList, Proxy, QueueHandle}, reexports::client::{globals::GlobalList, Connection, Proxy, QueueHandle},
shell::{ shell::{
wlr_layer::{ wlr_layer::{
Anchor, KeyboardInteractivity, Layer, LayerShell, LayerShellHandler, LayerSurface, Anchor, KeyboardInteractivity, Layer, LayerShell, LayerShellHandler, LayerSurface,
@@ -43,7 +43,7 @@ impl LayerShellWindow {
.set_exclusive_zone(self.layer_shell_settings.exclusive_zone); .set_exclusive_zone(self.layer_shell_settings.exclusive_zone);
let (width, height) = self.layer_shell_settings.size; let (width, height) = self.layer_shell_settings.size;
self.layer_surface.set_size(width, height); self.layer_surface.set_size(400, 400);
let (top, right, bottom, left) = self.layer_shell_settings.margin; let (top, right, bottom, left) = self.layer_shell_settings.margin;
self.layer_surface.set_margin(top, right, bottom, left); self.layer_surface.set_margin(top, right, bottom, left);
@@ -95,7 +95,7 @@ impl Default for LayerShellSettings {
size: Default::default(), size: Default::default(),
exclusive_zone: Default::default(), exclusive_zone: Default::default(),
margin: Default::default(), margin: Default::default(),
keyboard_interactivity: Default::default(), keyboard_interactivity: KeyboardInteractivity::OnDemand,
layer: Layer::Top, layer: Layer::Top,
} }
} }
+1 -2
View File
@@ -60,11 +60,10 @@ pub fn runner(mut app: App, mut event_loop: EventLoop<'_, WaylandState>) -> AppE
let mut state = WaylandState(app); let mut state = WaylandState(app);
loop { loop {
// TODO: Error handling // TODO: Error handling
let _ = event_loop.dispatch(None, &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);
} }
} }