2023-10-14 19:07:39 -07:00
|
|
|
#[cfg(all(
|
|
|
|
|
feature = "rwh_06",
|
|
|
|
|
any(x11_platform, macos_platform, windows_platform)
|
|
|
|
|
))]
|
|
|
|
|
#[allow(deprecated)]
|
2023-04-11 12:50:52 +01:00
|
|
|
fn main() -> Result<(), impl std::error::Error> {
|
2022-12-22 08:07:13 +08:00
|
|
|
use std::collections::HashMap;
|
2022-10-10 05:12:23 +09:00
|
|
|
|
2024-01-31 17:29:59 +04:00
|
|
|
use winit::dpi::{LogicalPosition, LogicalSize, Position};
|
|
|
|
|
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
|
|
|
|
|
use winit::event_loop::{ActiveEventLoop, EventLoop};
|
|
|
|
|
use winit::raw_window_handle::HasRawWindowHandle;
|
|
|
|
|
use winit::window::Window;
|
|
|
|
|
|
|
|
|
|
#[path = "util/fill.rs"]
|
|
|
|
|
mod fill;
|
2022-10-10 05:12:23 +09:00
|
|
|
|
2024-01-31 17:29:59 +04:00
|
|
|
fn spawn_child_window(parent: &Window, event_loop: &ActiveEventLoop) -> Window {
|
2023-10-14 19:07:39 -07:00
|
|
|
let parent = parent.raw_window_handle().unwrap();
|
2024-01-31 17:29:59 +04:00
|
|
|
let mut window_attributes = Window::default_attributes()
|
2022-12-22 08:07:13 +08:00
|
|
|
.with_title("child window")
|
|
|
|
|
.with_inner_size(LogicalSize::new(200.0f32, 200.0f32))
|
|
|
|
|
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
|
|
|
|
|
.with_visible(true);
|
2022-12-22 21:35:33 +02:00
|
|
|
// `with_parent_window` is unsafe. Parent window must be a valid window.
|
2024-01-31 17:29:59 +04:00
|
|
|
window_attributes = unsafe { window_attributes.with_parent_window(Some(parent)) };
|
2022-10-10 05:12:23 +09:00
|
|
|
|
2024-01-31 17:29:59 +04:00
|
|
|
event_loop.create_window(window_attributes).unwrap()
|
2022-12-22 08:07:13 +08:00
|
|
|
}
|
2022-10-10 05:12:23 +09:00
|
|
|
|
|
|
|
|
let mut windows = HashMap::new();
|
|
|
|
|
|
2023-08-13 23:20:09 +04:00
|
|
|
let event_loop: EventLoop<()> = EventLoop::new().unwrap();
|
2024-01-31 17:29:59 +04:00
|
|
|
let mut parent_window_id = None;
|
2022-10-10 05:12:23 +09:00
|
|
|
|
2024-01-31 17:29:59 +04:00
|
|
|
event_loop.run(move |event: Event<()>, event_loop| {
|
|
|
|
|
match event {
|
|
|
|
|
Event::Resumed => {
|
|
|
|
|
let attributes = Window::default_attributes()
|
|
|
|
|
.with_title("parent window")
|
|
|
|
|
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
|
|
|
|
|
.with_inner_size(LogicalSize::new(640.0f32, 480.0f32));
|
|
|
|
|
let window = event_loop.create_window(attributes).unwrap();
|
2022-10-10 05:12:23 +09:00
|
|
|
|
2024-01-31 17:29:59 +04:00
|
|
|
parent_window_id = Some(window.id());
|
|
|
|
|
|
|
|
|
|
println!("Parent window id: {parent_window_id:?})");
|
|
|
|
|
windows.insert(window.id(), window);
|
|
|
|
|
}
|
|
|
|
|
Event::WindowEvent { window_id, event } => match event {
|
2022-10-10 05:12:23 +09:00
|
|
|
WindowEvent::CloseRequested => {
|
|
|
|
|
windows.clear();
|
2024-01-31 17:29:59 +04:00
|
|
|
event_loop.exit();
|
2022-10-10 05:12:23 +09:00
|
|
|
}
|
|
|
|
|
WindowEvent::CursorEntered { device_id: _ } => {
|
2022-12-22 08:07:13 +08:00
|
|
|
// On x11, println when the cursor entered in a window even if the child window is created
|
2022-10-10 05:12:23 +09:00
|
|
|
// by some key inputs.
|
|
|
|
|
// the child windows are always placed at (0, 0) with size (200, 200) in the parent window,
|
2024-02-19 11:58:44 +07:00
|
|
|
// so we also can see this log when we move the cursor around (200, 200) in parent window.
|
2023-01-27 07:18:58 +03:00
|
|
|
println!("cursor entered in the window {window_id:?}");
|
2022-10-10 05:12:23 +09:00
|
|
|
}
|
|
|
|
|
WindowEvent::KeyboardInput {
|
2023-05-28 20:02:59 +02:00
|
|
|
event:
|
|
|
|
|
KeyEvent {
|
2022-10-10 05:12:23 +09:00
|
|
|
state: ElementState::Pressed,
|
|
|
|
|
..
|
|
|
|
|
},
|
|
|
|
|
..
|
|
|
|
|
} => {
|
2024-01-31 17:29:59 +04:00
|
|
|
let parent_window = windows.get(&parent_window_id.unwrap()).unwrap();
|
|
|
|
|
let child_window = spawn_child_window(parent_window, event_loop);
|
|
|
|
|
let child_id = child_window.id();
|
|
|
|
|
println!("Child window created with id: {child_id:?}");
|
|
|
|
|
windows.insert(child_id, child_window);
|
2022-10-10 05:12:23 +09:00
|
|
|
}
|
2023-08-27 16:15:09 +02:00
|
|
|
WindowEvent::RedrawRequested => {
|
|
|
|
|
if let Some(window) = windows.get(&window_id) {
|
|
|
|
|
fill::fill_window(window);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-10 05:12:23 +09:00
|
|
|
_ => (),
|
2024-01-31 17:29:59 +04:00
|
|
|
},
|
|
|
|
|
_ => (),
|
2022-10-10 05:12:23 +09:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-31 17:29:59 +04:00
|
|
|
#[cfg(all(
|
2023-10-14 19:07:39 -07:00
|
|
|
feature = "rwh_06",
|
2024-01-31 17:29:59 +04:00
|
|
|
not(any(x11_platform, macos_platform, windows_platform))
|
|
|
|
|
))]
|
2022-10-10 05:12:23 +09:00
|
|
|
fn main() {
|
2023-10-14 19:07:39 -07:00
|
|
|
panic!("This example is supported only on x11, macOS, and Windows, with the `rwh_06` feature enabled.");
|
2022-10-10 05:12:23 +09:00
|
|
|
}
|