chore: simplified layer shell example

- The layer shell example now opens only one window instead of opening
multiple windows.
This commit is contained in:
Naman Agrawal
2025-08-27 11:07:24 +05:30
parent e1fcc70667
commit ce15260e3d
4 changed files with 99 additions and 78 deletions
@@ -28,12 +28,8 @@ fn main() {
}),
WaylandPlugin,
))
.init_resource::<NewWindowInfo>()
.add_systems(Startup, setup)
.add_systems(
Update,
(button_system, spawn_window, setup_new_window, exit_on_esc),
)
.add_systems(Update, (button_system, exit_on_esc))
.run();
}
@@ -72,18 +68,13 @@ fn button_system(
}
}
#[derive(Component, Deref, DerefMut)]
struct WindowTimer(Timer);
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()
},
InputRegion(Rect::new(0., 0., 200., 200.)),
));
commands.entity(entity).insert((LayerShellSettings {
anchor: Anchor::TOP | Anchor::LEFT,
layer: Layer::Bottom,
..Default::default()
},));
}
// ui camera
commands.spawn(Camera2d);
@@ -96,69 +87,6 @@ fn exit_on_esc(keys: Res<ButtonInput<KeyCode>>) {
}
}
fn spawn_window(
mut commands: Commands,
mut windows: Query<(Entity, &mut WindowTimer)>,
mut new_window_info: ResMut<NewWindowInfo>,
keys: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
if keys.pressed(KeyCode::KeyN) {
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();
new_window_info.entity = Some(new_window_entity);
new_window_info.is_setup_pending = true;
}
}
#[derive(Resource, Default)]
struct NewWindowInfo {
entity: Option<Entity>,
is_setup_pending: bool,
}
fn setup_new_window(
mut commands: Commands,
mut window_created_events: EventReader<WindowCreated>,
mut new_window_info: ResMut<NewWindowInfo>,
asset_server: Res<AssetServer>, // For fonts
) {
for event in window_created_events.read() {
if Some(event.window) == new_window_info.entity && new_window_info.is_setup_pending {
info!(
"New UI window created (ID: {:?}), setting up its camera and UI.",
event.window
);
commands.spawn((
Camera {
target: bevy::render::camera::RenderTarget::Window(
bevy::window::WindowRef::Entity(event.window),
),
clear_color: ClearColorConfig::Custom(Color::default()),
..default()
},
Camera2d,
));
new_window_info.is_setup_pending = false; // Mark as setup complete
}
}
}
fn button(asset_server: &AssetServer) -> impl Bundle + use<> {
(
Node {