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},
|
|
|
|
};
|
|
|
|
|
2023-06-20 04:46:38 +10:00
|
|
|
#[path = "util/fill.rs"]
|
|
|
|
mod fill;
|
|
|
|
|
2021-03-07 20:43:23 +11:00
|
|
|
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!")
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
2023-06-20 04:46:38 +10:00
|
|
|
Event::RedrawRequested(wid) => {
|
|
|
|
if wid == window_1.id() {
|
|
|
|
fill::fill_window(&window_1);
|
|
|
|
} else if wid == window_2.id() {
|
|
|
|
fill::fill_window(&window_2);
|
|
|
|
}
|
|
|
|
}
|
2021-03-07 20:43:23 +11:00
|
|
|
_ => (),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|