feat: Add basic support for foreign top level management.
- Allow users to minimize other applications by sending a `ForeignToplevelEvent`.
This commit is contained in:
Generated
+1
@@ -1101,6 +1101,7 @@ dependencies = [
|
||||
"raw-window-handle",
|
||||
"smithay-client-toolkit 0.20.0",
|
||||
"wayland-backend",
|
||||
"wayland-protocols-wlr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -17,3 +17,4 @@ lazy_static = "1.5.0"
|
||||
raw-window-handle = "0.6.2"
|
||||
smithay-client-toolkit = "0.20.0"
|
||||
wayland-backend = { version = "0.3.11", features = ["client_system"] }
|
||||
wayland-protocols-wlr = "0.3.9"
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
use bevy::{color::palettes::basic::*, prelude::*, window::WindowResolution, winit::WinitPlugin};
|
||||
use bevy_wayland::{foreign_toplevel_manager::ForeignToplevelEvent, prelude::*};
|
||||
|
||||
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)
|
||||
.add_systems(Update, (button_system, exit_on_esc))
|
||||
.run();
|
||||
}
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
fn button_system(
|
||||
mut interaction_query: Query<
|
||||
(
|
||||
&Interaction,
|
||||
&mut BackgroundColor,
|
||||
&mut BorderColor,
|
||||
&Children,
|
||||
),
|
||||
(Changed<Interaction>, With<Button>),
|
||||
>,
|
||||
mut foreign_toplevel_event_writer: EventWriter<ForeignToplevelEvent>,
|
||||
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();
|
||||
foreign_toplevel_event_writer.write(ForeignToplevelEvent::MinimizeOthers);
|
||||
}
|
||||
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::Top,
|
||||
..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(),
|
||||
)]
|
||||
)],
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
use bevy::prelude::*;
|
||||
use smithay_client_toolkit::{
|
||||
reexports::client::{event_created_child, Dispatch, QueueHandle},
|
||||
registry::RegistryState,
|
||||
};
|
||||
use wayland_protocols_wlr::foreign_toplevel::v1::client::{
|
||||
zwlr_foreign_toplevel_handle_v1::{self, ZwlrForeignToplevelHandleV1},
|
||||
zwlr_foreign_toplevel_manager_v1::{self, ZwlrForeignToplevelManagerV1},
|
||||
};
|
||||
|
||||
use crate::WaylandState;
|
||||
#[derive(Debug, Copy, Clone, Event)]
|
||||
pub enum ForeignToplevelEvent {
|
||||
MinimizeOthers,
|
||||
}
|
||||
|
||||
#[derive(Default, Deref, DerefMut)]
|
||||
struct ForeignToplevels(Vec<ZwlrForeignToplevelHandleV1>);
|
||||
|
||||
pub struct ForeignToplevelManagerPlugin;
|
||||
impl Plugin for ForeignToplevelManagerPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
let registry_state = app.world().non_send_resource::<RegistryState>();
|
||||
let queue_handle = app.world().non_send_resource::<QueueHandle<WaylandState>>();
|
||||
let foreign_top_level_manager =
|
||||
registry_state.bind_one::<ZwlrForeignToplevelManagerV1, _, _>(queue_handle, 2..=3, ());
|
||||
if let Ok(foreign_top_level_manager) = foreign_top_level_manager {
|
||||
info!("Foreign toplevel manager was bound!");
|
||||
app.insert_non_send_resource(foreign_top_level_manager);
|
||||
app.insert_non_send_resource(ForeignToplevels::default());
|
||||
app.add_event::<ForeignToplevelEvent>();
|
||||
app.add_systems(Update, foreign_top_level_event_handler);
|
||||
} else {
|
||||
let bind_error = foreign_top_level_manager.err().unwrap();
|
||||
error!("Couldn't bind foreign toplevel manager! {:?}", bind_error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn foreign_top_level_event_handler(
|
||||
foreign_top_levels: NonSendMut<ForeignToplevels>,
|
||||
mut events: EventReader<ForeignToplevelEvent>,
|
||||
) {
|
||||
for event in events.read() {
|
||||
match event {
|
||||
ForeignToplevelEvent::MinimizeOthers => {
|
||||
info!("Minimizing other windows");
|
||||
for toplevel in foreign_top_levels.iter() {
|
||||
toplevel.set_minimized();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dispatch<ZwlrForeignToplevelManagerV1, ()> for WaylandState {
|
||||
fn event(
|
||||
state: &mut Self,
|
||||
_proxy: &ZwlrForeignToplevelManagerV1,
|
||||
event: <ZwlrForeignToplevelManagerV1 as smithay_client_toolkit::reexports::client::Proxy>::Event,
|
||||
_data: &(),
|
||||
_conn: &smithay_client_toolkit::reexports::client::Connection,
|
||||
_qhandle: &QueueHandle<Self>,
|
||||
) {
|
||||
let mut foreign_toplevels = state
|
||||
.world_mut()
|
||||
.non_send_resource_mut::<ForeignToplevels>();
|
||||
match event {
|
||||
wayland_protocols_wlr::foreign_toplevel::v1::client::zwlr_foreign_toplevel_manager_v1::Event::Toplevel { toplevel } => {
|
||||
foreign_toplevels.push(toplevel);
|
||||
},
|
||||
wayland_protocols_wlr::foreign_toplevel::v1::client::zwlr_foreign_toplevel_manager_v1::Event::Finished => {},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
event_created_child!(WaylandState, ZwlrForeignToplevelManagerV1, [
|
||||
// Opcode 0 is the `toplevel` event. It creates a new `zwlr_foreign_toplevel_handle_v1`.
|
||||
zwlr_foreign_toplevel_manager_v1::EVT_TOPLEVEL_OPCODE => (
|
||||
zwlr_foreign_toplevel_handle_v1::ZwlrForeignToplevelHandleV1,
|
||||
()
|
||||
)
|
||||
]);
|
||||
}
|
||||
|
||||
impl Dispatch<ZwlrForeignToplevelHandleV1, ()> for WaylandState {
|
||||
fn event(
|
||||
_state: &mut Self,
|
||||
_proxy: &ZwlrForeignToplevelHandleV1,
|
||||
_event: <ZwlrForeignToplevelHandleV1 as smithay_client_toolkit::reexports::client::Proxy>::Event,
|
||||
_data: &(),
|
||||
_conn: &smithay_client_toolkit::reexports::client::Connection,
|
||||
_qhandle: &QueueHandle<Self>,
|
||||
) {
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,5 +1,4 @@
|
||||
use std::{
|
||||
num::NonZero,
|
||||
sync::mpsc::SendError,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
@@ -18,6 +17,7 @@ use smithay_client_toolkit::{
|
||||
seat::SeatState,
|
||||
};
|
||||
|
||||
pub mod foreign_toplevel_manager;
|
||||
mod input_handler;
|
||||
pub mod input_region;
|
||||
pub mod layer_shell;
|
||||
@@ -85,6 +85,7 @@ impl Plugin for WaylandPlugin {
|
||||
layer_shell::LayerShellPlugin,
|
||||
session_lock::SessionLockPlugin,
|
||||
input_region::InputRegionPlugin,
|
||||
foreign_toplevel_manager::ForeignToplevelManagerPlugin,
|
||||
));
|
||||
app.set_runner(|app| runner(app, event_loop));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user