x11_embed/
x11_embed.rs

1//! A demonstration of embedding a winit window in an existing X11 application.
2use std::error::Error;
3
4#[cfg(x11_platform)]
5fn main() -> Result<(), Box<dyn Error>> {
6    use rio_window::application::ApplicationHandler;
7    use rio_window::event::WindowEvent;
8    use rio_window::event_loop::{ActiveEventLoop, EventLoop};
9    use rio_window::platform::x11::WindowAttributesExtX11;
10    use rio_window::window::{Window, WindowId};
11
12    #[path = "util/fill.rs"]
13    mod fill;
14
15    pub struct XEmbedDemo {
16        parent_window_id: u32,
17        window: Option<Window>,
18    }
19
20    impl ApplicationHandler for XEmbedDemo {
21        fn resumed(&mut self, event_loop: &ActiveEventLoop) {
22            let window_attributes = Window::default_attributes()
23                .with_title("An embedded window!")
24                .with_inner_size(rio_window::dpi::LogicalSize::new(128.0, 128.0))
25                .with_embed_parent_window(self.parent_window_id);
26
27            self.window = Some(event_loop.create_window(window_attributes).unwrap());
28        }
29
30        fn window_event(
31            &mut self,
32            event_loop: &ActiveEventLoop,
33            _window_id: WindowId,
34            event: WindowEvent,
35        ) {
36            let window = self.window.as_ref().unwrap();
37            match event {
38                WindowEvent::CloseRequested => event_loop.exit(),
39                WindowEvent::RedrawRequested => {
40                    window.pre_present_notify();
41                    fill::fill_window(window);
42                }
43                _ => (),
44            }
45        }
46
47        fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
48            self.window.as_ref().unwrap().request_redraw();
49        }
50    }
51
52    // First argument should be a 32-bit X11 window ID.
53    let parent_window_id = std::env::args()
54        .nth(1)
55        .ok_or("Expected a 32-bit X11 window ID as the first argument.")?
56        .parse::<u32>()?;
57
58    tracing_subscriber::fmt::init();
59    let event_loop = EventLoop::new()?;
60
61    let mut app = XEmbedDemo {
62        parent_window_id,
63        window: None,
64    };
65    event_loop.run_app(&mut app).map_err(Into::into)
66}
67
68#[cfg(not(x11_platform))]
69fn main() -> Result<(), Box<dyn Error>> {
70    println!("This example is only supported on X11 platforms.");
71    Ok(())
72}