api: add ApplicationHandler and matching run APIs
Add a simple `ApplicationHandler` trait since winit is moving towards trait based API. Add `run_app` group of APIs to accept `&mut impl ApplicationHandler` deprecating the old `run` APIs. Part-of: https://github.com/rust-windowing/winit/issues/3432
This commit is contained in:
parent
fc8a008b25
commit
d123cd2f8e
18 changed files with 821 additions and 651 deletions
|
|
@ -3,16 +3,52 @@ use std::error::Error;
|
|||
|
||||
#[cfg(x11_platform)]
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
use winit::{
|
||||
event::{Event, WindowEvent},
|
||||
event_loop::EventLoop,
|
||||
platform::x11::WindowAttributesExtX11,
|
||||
window::Window,
|
||||
};
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop};
|
||||
use winit::platform::x11::WindowAttributesExtX11;
|
||||
use winit::window::{Window, WindowId};
|
||||
|
||||
#[path = "util/fill.rs"]
|
||||
mod fill;
|
||||
|
||||
pub struct XEmbedDemo {
|
||||
parent_window_id: u32,
|
||||
window: Option<Window>,
|
||||
}
|
||||
|
||||
impl ApplicationHandler for XEmbedDemo {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
let window_attributes = Window::default_attributes()
|
||||
.with_title("An embedded window!")
|
||||
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
|
||||
.with_embed_parent_window(self.parent_window_id);
|
||||
|
||||
self.window = Some(event_loop.create_window(window_attributes).unwrap());
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
&mut self,
|
||||
event_loop: &ActiveEventLoop,
|
||||
_window_id: WindowId,
|
||||
event: WindowEvent,
|
||||
) {
|
||||
let window = self.window.as_ref().unwrap();
|
||||
match event {
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
WindowEvent::RedrawRequested => {
|
||||
window.pre_present_notify();
|
||||
fill::fill_window(window);
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
|
||||
self.window.as_ref().unwrap().request_redraw();
|
||||
}
|
||||
}
|
||||
|
||||
// First argument should be a 32-bit X11 window ID.
|
||||
let parent_window_id = std::env::args()
|
||||
.nth(1)
|
||||
|
|
@ -22,35 +58,11 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
tracing_subscriber::fmt::init();
|
||||
let event_loop = EventLoop::new()?;
|
||||
|
||||
let mut window = None;
|
||||
event_loop.run(move |event, event_loop| match event {
|
||||
Event::Resumed => {
|
||||
let window_attributes = Window::default_attributes()
|
||||
.with_title("An embedded window!")
|
||||
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
|
||||
.with_embed_parent_window(parent_window_id);
|
||||
|
||||
window = Some(event_loop.create_window(window_attributes).unwrap());
|
||||
}
|
||||
Event::WindowEvent { event, .. } => {
|
||||
let window = window.as_ref().unwrap();
|
||||
|
||||
match event {
|
||||
WindowEvent::CloseRequested => event_loop.exit(),
|
||||
WindowEvent::RedrawRequested => {
|
||||
window.pre_present_notify();
|
||||
fill::fill_window(window);
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
Event::AboutToWait => {
|
||||
window.as_ref().unwrap().request_redraw();
|
||||
}
|
||||
_ => (),
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
let mut app = XEmbedDemo {
|
||||
parent_window_id,
|
||||
window: None,
|
||||
};
|
||||
event_loop.run_app(&mut app).map_err(Into::into)
|
||||
}
|
||||
|
||||
#[cfg(not(x11_platform))]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue