From 2e9bf9f31698851ca373e5f1e7ba3e6e804e4db1 Mon Sep 17 00:00:00 2001 From: Victoria Brekenfeld Date: Tue, 31 Oct 2023 14:46:11 +0100 Subject: [PATCH] simple_window: Test xdg-activation --- examples/simple_window.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/examples/simple_window.rs b/examples/simple_window.rs index 369459f54..8cc097c32 100644 --- a/examples/simple_window.rs +++ b/examples/simple_window.rs @@ -1,11 +1,13 @@ use std::{convert::TryInto, time::Duration}; +use smithay_client_toolkit::activation::RequestData; use smithay_client_toolkit::reexports::calloop::{EventLoop, LoopHandle}; use smithay_client_toolkit::reexports::calloop_wayland_source::WaylandSource; use smithay_client_toolkit::{ + activation::{ActivationHandler, ActivationState}, compositor::{CompositorHandler, CompositorState}, - delegate_compositor, delegate_keyboard, delegate_output, delegate_pointer, delegate_registry, - delegate_seat, delegate_shm, delegate_xdg_shell, delegate_xdg_window, + delegate_activation, delegate_compositor, delegate_keyboard, delegate_output, delegate_pointer, + delegate_registry, delegate_seat, delegate_shm, delegate_xdg_shell, delegate_xdg_window, output::{OutputHandler, OutputState}, registry::{ProvidesRegistryState, RegistryState}, registry_handlers, @@ -54,6 +56,8 @@ fn main() { // Since we are not using the GPU in this example, we use wl_shm to allow software rendering to a buffer // we share with the compositor process. let shm = Shm::bind(&globals, &qh).expect("wl shm is not available."); + // If the compositor supports xdg-activation it probably wants us to use it to get focus + let xdg_activation = ActivationState::bind(&globals, &qh).ok(); // A window is created from a surface. let surface = compositor.create_surface(&qh); @@ -73,6 +77,18 @@ fn main() { // the correct options. window.commit(); + // To request focus, we first need to request a token + if let Some(activation) = xdg_activation.as_ref() { + activation.request_token( + &qh, + RequestData { + seat_and_serial: None, + surface: Some(window.wl_surface().clone()), + app_id: Some(String::from("io.github.smithay.client-toolkit.SimpleWindow")), + }, + ) + } + // We don't know how large the window will be yet, so lets assume the minimum size we suggested for the // initial memory allocation. let pool = SlotPool::new(256 * 256 * 4, &shm).expect("Failed to create pool"); @@ -84,6 +100,7 @@ fn main() { seat_state: SeatState::new(&globals, &qh), output_state: OutputState::new(&globals, &qh), shm, + xdg_activation, exit: false, first_configure: true, @@ -115,6 +132,7 @@ struct SimpleWindow { seat_state: SeatState, output_state: OutputState, shm: Shm, + xdg_activation: Option, exit: bool, first_configure: bool, @@ -219,6 +237,17 @@ impl WindowHandler for SimpleWindow { } } +impl ActivationHandler for SimpleWindow { + type RequestData = RequestData; + + fn new_token(&mut self, token: String, _data: &Self::RequestData) { + self.xdg_activation + .as_ref() + .unwrap() + .activate::(self.window.wl_surface(), token); + } +} + impl SeatHandler for SimpleWindow { fn seat_state(&mut self) -> &mut SeatState { &mut self.seat_state @@ -464,6 +493,7 @@ delegate_pointer!(SimpleWindow); delegate_xdg_shell!(SimpleWindow); delegate_xdg_window!(SimpleWindow); +delegate_activation!(SimpleWindow); delegate_registry!(SimpleWindow);