wingy 0.31.1

Cross-platform window creation library.
Documentation
//! A demonstration of embedding a wingy window in an existing X11 application.
use std::error::Error;

#[cfg(x11_platform)]
fn main() -> Result<(), Box<dyn Error>> {
    use softbuffer::{Context, Surface};
    use wingy::application::ApplicationHandler;
    use wingy::event::WindowEvent;
    use wingy::event_loop::{ActiveEventLoop, EventLoop, OwnedDisplayHandle};
    use wingy::platform::x11::WindowAttributesX11;
    use wingy::window::{Window, WindowAttributes, WindowId};

    #[path = "util/fill.rs"]
    mod fill;
    #[path = "util/tracing.rs"]
    mod tracing;

    #[derive(Debug)]
    pub struct XEmbedDemo {
        parent_window_id: u32,
        surface: Option<Surface<OwnedDisplayHandle, Box<dyn Window>>>,
    }

    impl ApplicationHandler for XEmbedDemo {
        fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
            let mut window_attributes = WindowAttributes::default()
                .with_title("An embedded window!")
                .with_surface_size(wingy::dpi::LogicalSize::new(128.0, 128.0));
            let x11_attrs =
                WindowAttributesX11::default().with_embed_parent_window(self.parent_window_id);
            window_attributes = window_attributes.with_platform_attributes(Box::new(x11_attrs));

            let window = event_loop.create_window(window_attributes).unwrap();
            let context = Context::new(event_loop.owned_display_handle()).unwrap();
            let surface = Surface::new(&context, window).unwrap();
            self.surface = Some(surface);
        }

        fn window_event(
            &mut self,
            event_loop: &dyn ActiveEventLoop,
            _window_id: WindowId,
            event: WindowEvent,
        ) {
            match event {
                WindowEvent::CloseRequested => event_loop.exit(),
                WindowEvent::RedrawRequested => {
                    let surface = self.surface.as_mut().unwrap();
                    surface.window().pre_present_notify();
                    fill::fill(surface);
                },
                _ => (),
            }
        }

        fn about_to_wait(&mut self, _event_loop: &dyn ActiveEventLoop) {
            self.surface.as_ref().unwrap().window().request_redraw();
        }
    }

    // First argument should be a 32-bit X11 window ID.
    let parent_window_id = std::env::args()
        .nth(1)
        .ok_or("Expected a 32-bit X11 window ID as the first argument.")?
        .parse::<u32>()?;

    tracing::init();
    let event_loop = EventLoop::new()?;

    Ok(event_loop.run_app(XEmbedDemo { parent_window_id, surface: None })?)
}

#[cfg(not(x11_platform))]
fn main() -> Result<(), Box<dyn Error>> {
    panic!("This example is only supported on X11 platforms.")
}