2023-06-20 04:46:38 +10:00
|
|
|
#[cfg(any(x11_platform, macos_platform, windows_platform))]
|
|
|
|
#[path = "util/fill.rs"]
|
|
|
|
mod fill;
|
|
|
|
|
2022-12-25 18:57:27 +11:00
|
|
|
#[cfg(any(x11_platform, macos_platform, windows_platform))]
|
2022-12-22 11:07:13 +11:00
|
|
|
fn main() {
|
|
|
|
use std::collections::HashMap;
|
2022-10-10 07:12:23 +11:00
|
|
|
|
2022-12-22 11:07:13 +11:00
|
|
|
use raw_window_handle::HasRawWindowHandle;
|
|
|
|
use winit::{
|
|
|
|
dpi::{LogicalPosition, LogicalSize, Position},
|
2023-05-29 04:02:59 +10:00
|
|
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
2022-12-22 11:07:13 +11:00
|
|
|
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
|
|
|
|
window::{Window, WindowBuilder, WindowId},
|
|
|
|
};
|
2022-10-10 07:12:23 +11:00
|
|
|
|
2022-12-22 11:07:13 +11:00
|
|
|
fn spawn_child_window(
|
|
|
|
parent: &Window,
|
|
|
|
event_loop: &EventLoopWindowTarget<()>,
|
|
|
|
windows: &mut HashMap<WindowId, Window>,
|
|
|
|
) {
|
|
|
|
let parent = parent.raw_window_handle();
|
|
|
|
let mut builder = WindowBuilder::new()
|
|
|
|
.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-23 06:35:33 +11:00
|
|
|
// `with_parent_window` is unsafe. Parent window must be a valid window.
|
2022-12-22 11:07:13 +11:00
|
|
|
builder = unsafe { builder.with_parent_window(Some(parent)) };
|
|
|
|
let child_window = builder.build(event_loop).unwrap();
|
2022-10-10 07:12:23 +11:00
|
|
|
|
2022-12-22 11:07:13 +11:00
|
|
|
let id = child_window.id();
|
|
|
|
windows.insert(id, child_window);
|
2023-01-27 15:18:58 +11:00
|
|
|
println!("child window created with id: {id:?}");
|
2022-12-22 11:07:13 +11:00
|
|
|
}
|
2022-10-10 07:12:23 +11:00
|
|
|
|
|
|
|
let mut windows = HashMap::new();
|
|
|
|
|
|
|
|
let event_loop: EventLoop<()> = EventLoop::new();
|
|
|
|
let parent_window = WindowBuilder::new()
|
|
|
|
.with_title("parent window")
|
|
|
|
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
|
|
|
|
.with_inner_size(LogicalSize::new(640.0f32, 480.0f32))
|
|
|
|
.build(&event_loop)
|
|
|
|
.unwrap();
|
|
|
|
|
2023-01-27 15:18:58 +11:00
|
|
|
println!("parent window: {parent_window:?})");
|
2022-10-10 07:12:23 +11:00
|
|
|
|
|
|
|
event_loop.run(move |event: Event<'_, ()>, event_loop, control_flow| {
|
|
|
|
*control_flow = ControlFlow::Wait;
|
|
|
|
|
|
|
|
if let Event::WindowEvent { event, window_id } = event {
|
|
|
|
match event {
|
|
|
|
WindowEvent::CloseRequested => {
|
|
|
|
windows.clear();
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
WindowEvent::CursorEntered { device_id: _ } => {
|
2022-12-22 11:07:13 +11:00
|
|
|
// On x11, println when the cursor entered in a window even if the child window is created
|
2022-10-10 07:12:23 +11:00
|
|
|
// by some key inputs.
|
|
|
|
// the child windows are always placed at (0, 0) with size (200, 200) in the parent window,
|
|
|
|
// so we also can see this log when we move the cursor arround (200, 200) in parent window.
|
2023-01-27 15:18:58 +11:00
|
|
|
println!("cursor entered in the window {window_id:?}");
|
2022-10-10 07:12:23 +11:00
|
|
|
}
|
|
|
|
WindowEvent::KeyboardInput {
|
2023-05-29 04:02:59 +10:00
|
|
|
event:
|
|
|
|
KeyEvent {
|
2022-10-10 07:12:23 +11:00
|
|
|
state: ElementState::Pressed,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
} => {
|
2022-12-23 06:35:33 +11:00
|
|
|
spawn_child_window(&parent_window, event_loop, &mut windows);
|
2022-10-10 07:12:23 +11:00
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
2023-06-20 04:46:38 +10:00
|
|
|
} else if let Event::RedrawRequested(wid) = event {
|
|
|
|
if let Some(window) = windows.get(&wid) {
|
|
|
|
fill::fill_window(window);
|
|
|
|
}
|
2022-10-10 07:12:23 +11:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-25 18:57:27 +11:00
|
|
|
#[cfg(not(any(x11_platform, macos_platform, windows_platform)))]
|
2022-10-10 07:12:23 +11:00
|
|
|
fn main() {
|
2022-12-22 11:07:13 +11:00
|
|
|
panic!("This example is supported only on x11, macOS, and Windows.");
|
2022-10-10 07:12:23 +11:00
|
|
|
}
|