anvil: implement xdg-activation

This commit is contained in:
Poly 2021-07-31 20:21:07 +02:00
parent f24332f4b4
commit f0cdb235d7
2 changed files with 51 additions and 1 deletions

View File

@ -19,6 +19,7 @@ use smithay::{
seat::{CursorImageStatus, KeyboardHandle, PointerHandle, Seat, XkbConfig}, seat::{CursorImageStatus, KeyboardHandle, PointerHandle, Seat, XkbConfig},
shm::init_shm_global, shm::init_shm_global,
tablet_manager::{init_tablet_manager_global, TabletSeatTrait}, tablet_manager::{init_tablet_manager_global, TabletSeatTrait},
xdg_activation::{init_xdg_activation_global, XdgActivationEvent},
}, },
}; };
@ -85,6 +86,29 @@ impl<BackendData: Backend + 'static> AnvilState<BackendData> {
let shell_handles = init_shell::<BackendData>(display.clone(), log.clone()); let shell_handles = init_shell::<BackendData>(display.clone(), log.clone());
init_xdg_output_manager(&mut display.borrow_mut(), log.clone()); init_xdg_output_manager(&mut display.borrow_mut(), log.clone());
init_xdg_activation_global(
&mut display.borrow_mut(),
|state, req, mut ddata| {
let anvil_state = ddata.get::<AnvilState<BackendData>>().unwrap();
match req {
XdgActivationEvent::RequestActivation {
token,
token_data,
surface,
} => {
if token_data.timestamp.elapsed().as_secs() < 10 {
// Just grant the wish
anvil_state.window_map.borrow_mut().bring_surface_to_top(&surface);
} else {
// Discard the request
state.lock().unwrap().remove_request(&token);
}
}
XdgActivationEvent::DestroyActivationRequest { .. } => {}
}
},
log.clone(),
);
let socket_name = if listen_on_socket { let socket_name = if listen_on_socket {
let socket_name = display let socket_name = display

View File

@ -2,7 +2,10 @@ use std::cell::RefCell;
use std::sync::Mutex; use std::sync::Mutex;
use smithay::{ use smithay::{
reexports::{wayland_protocols::xdg_shell::server::xdg_toplevel, wayland_server::protocol::wl_surface}, reexports::{
wayland_protocols::xdg_shell::server::xdg_toplevel,
wayland_server::protocol::wl_surface::{self, WlSurface},
},
utils::{Logical, Point, Rectangle}, utils::{Logical, Point, Rectangle},
wayland::{ wayland::{
compositor::{with_states, with_surface_tree_downward, SubsurfaceCachedState, TraversalAction}, compositor::{with_states, with_surface_tree_downward, SubsurfaceCachedState, TraversalAction},
@ -297,6 +300,29 @@ impl WindowMap {
None None
} }
pub fn bring_surface_to_top(&mut self, surface: &WlSurface) {
let found = self.windows.iter().enumerate().find(|(_, w)| {
if let Some(s) = w.toplevel.get_surface() {
s.as_ref().equals(surface.as_ref())
} else {
false
}
});
if let Some((i, _)) = found {
let winner = self.windows.remove(i);
// Take activation away from all the windows
for window in self.windows.iter() {
window.toplevel.set_activated(false);
}
// Give activation to our winner
winner.toplevel.set_activated(true);
self.windows.insert(0, winner);
}
}
pub fn get_surface_and_bring_to_top( pub fn get_surface_and_bring_to_top(
&mut self, &mut self,
point: Point<f64, Logical>, point: Point<f64, Logical>,