2022-06-10 20:43:33 +10:00
|
|
|
#![allow(clippy::single_match)]
|
|
|
|
|
2021-03-07 20:43:23 +11:00
|
|
|
use simple_logger::SimpleLogger;
|
|
|
|
use winit::{
|
2023-05-29 04:02:59 +10:00
|
|
|
event::{ElementState, Event, KeyEvent, MouseButton, StartCause, WindowEvent},
|
2022-04-10 11:32:02 +10:00
|
|
|
event_loop::EventLoop,
|
2023-05-29 04:02:59 +10:00
|
|
|
keyboard::Key,
|
2021-03-07 20:43:23 +11:00
|
|
|
window::{Window, WindowBuilder, WindowId},
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
SimpleLogger::new().init().unwrap();
|
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
|
|
|
|
let window_1 = WindowBuilder::new().build(&event_loop).unwrap();
|
|
|
|
let window_2 = WindowBuilder::new().build(&event_loop).unwrap();
|
|
|
|
|
|
|
|
let mut switched = false;
|
|
|
|
let mut entered_id = window_2.id();
|
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| match event {
|
|
|
|
Event::NewEvents(StartCause::Init) => {
|
|
|
|
eprintln!("Switch which window is to be dragged by pressing \"x\".")
|
|
|
|
}
|
|
|
|
Event::WindowEvent { event, window_id } => match event {
|
2022-04-10 11:32:02 +10:00
|
|
|
WindowEvent::CloseRequested => control_flow.set_exit(),
|
2021-03-07 20:43:23 +11:00
|
|
|
WindowEvent::MouseInput {
|
|
|
|
state: ElementState::Pressed,
|
|
|
|
button: MouseButton::Left,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let window = if (window_id == window_1.id() && switched)
|
|
|
|
|| (window_id == window_2.id() && !switched)
|
|
|
|
{
|
|
|
|
&window_2
|
|
|
|
} else {
|
|
|
|
&window_1
|
|
|
|
};
|
|
|
|
|
|
|
|
window.drag_window().unwrap()
|
|
|
|
}
|
|
|
|
WindowEvent::CursorEntered { .. } => {
|
|
|
|
entered_id = window_id;
|
|
|
|
name_windows(entered_id, switched, &window_1, &window_2)
|
|
|
|
}
|
|
|
|
WindowEvent::KeyboardInput {
|
2023-05-29 04:02:59 +10:00
|
|
|
event:
|
|
|
|
KeyEvent {
|
2021-03-07 20:43:23 +11:00
|
|
|
state: ElementState::Released,
|
2023-05-29 04:02:59 +10:00
|
|
|
logical_key: Key::Character(c),
|
2021-03-07 20:43:23 +11:00
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
2023-05-29 04:02:59 +10:00
|
|
|
} if c == "x" => {
|
2021-03-07 20:43:23 +11:00
|
|
|
switched = !switched;
|
|
|
|
name_windows(entered_id, switched, &window_1, &window_2);
|
|
|
|
println!("Switched!")
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name_windows(window_id: WindowId, switched: bool, window_1: &Window, window_2: &Window) {
|
|
|
|
let (drag_target, other) =
|
|
|
|
if (window_id == window_1.id() && switched) || (window_id == window_2.id() && !switched) {
|
|
|
|
(&window_2, &window_1)
|
|
|
|
} else {
|
|
|
|
(&window_1, &window_2)
|
|
|
|
};
|
|
|
|
drag_target.set_title("drag target");
|
|
|
|
other.set_title("winit window");
|
|
|
|
}
|