Skip to content

Commit

Permalink
simple_window: Test xdg-activation
Browse files Browse the repository at this point in the history
  • Loading branch information
Drakulix authored and wash2 committed Nov 1, 2023
1 parent 958c229 commit 2e9bf9f
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions examples/simple_window.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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,
Expand Down Expand Up @@ -115,6 +132,7 @@ struct SimpleWindow {
seat_state: SeatState,
output_state: OutputState,
shm: Shm,
xdg_activation: Option<ActivationState>,

exit: bool,
first_configure: bool,
Expand Down Expand Up @@ -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::<SimpleWindow>(self.window.wl_surface(), token);
}
}

impl SeatHandler for SimpleWindow {
fn seat_state(&mut self) -> &mut SeatState {
&mut self.seat_state
Expand Down Expand Up @@ -464,6 +493,7 @@ delegate_pointer!(SimpleWindow);

delegate_xdg_shell!(SimpleWindow);
delegate_xdg_window!(SimpleWindow);
delegate_activation!(SimpleWindow);

delegate_registry!(SimpleWindow);

Expand Down

0 comments on commit 2e9bf9f

Please sign in to comment.