2022-06-10 13:43:33 +03:00
|
|
|
#![allow(clippy::single_match)]
|
|
|
|
|
|
2021-03-07 10:43:23 +01:00
|
|
|
use simple_logger::SimpleLogger;
|
|
|
|
|
use winit::{
|
2023-05-28 20:02:59 +02:00
|
|
|
event::{ElementState, Event, KeyEvent, MouseButton, StartCause, WindowEvent},
|
2022-04-09 18:32:02 -07:00
|
|
|
event_loop::EventLoop,
|
2023-05-28 20:02:59 +02:00
|
|
|
keyboard::Key,
|
2021-03-07 10:43:23 +01:00
|
|
|
window::{Window, WindowBuilder, WindowId},
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-19 11:46:38 -07:00
|
|
|
#[path = "util/fill.rs"]
|
|
|
|
|
mod fill;
|
|
|
|
|
|
2023-04-11 12:50:52 +01:00
|
|
|
fn main() -> Result<(), impl std::error::Error> {
|
2021-03-07 10:43:23 +01:00
|
|
|
SimpleLogger::new().init().unwrap();
|
2023-08-13 23:20:09 +04:00
|
|
|
let event_loop = EventLoop::new().unwrap();
|
2021-03-07 10:43:23 +01:00
|
|
|
|
|
|
|
|
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();
|
2023-10-11 01:16:16 +03:30
|
|
|
let mut cursor_location = None;
|
2021-03-07 10:43:23 +01:00
|
|
|
|
2023-09-07 08:25:04 +02:00
|
|
|
event_loop.run(move |event, elwt| match event {
|
2021-03-07 10:43:23 +01:00
|
|
|
Event::NewEvents(StartCause::Init) => {
|
|
|
|
|
eprintln!("Switch which window is to be dragged by pressing \"x\".")
|
|
|
|
|
}
|
|
|
|
|
Event::WindowEvent { event, window_id } => match event {
|
2023-09-07 08:25:04 +02:00
|
|
|
WindowEvent::CloseRequested => elwt.exit(),
|
2023-10-11 01:16:16 +03:30
|
|
|
WindowEvent::CursorMoved { position, .. } => cursor_location = Some(position),
|
|
|
|
|
WindowEvent::MouseInput { state, button, .. } => {
|
2021-03-07 10:43:23 +01:00
|
|
|
let window = if (window_id == window_1.id() && switched)
|
|
|
|
|
|| (window_id == window_2.id() && !switched)
|
|
|
|
|
{
|
|
|
|
|
&window_2
|
|
|
|
|
} else {
|
|
|
|
|
&window_1
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-11 01:16:16 +03:30
|
|
|
match (button, state) {
|
|
|
|
|
(MouseButton::Left, ElementState::Pressed) => window.drag_window().unwrap(),
|
|
|
|
|
(MouseButton::Right, ElementState::Released) => {
|
|
|
|
|
if let Some(position) = cursor_location {
|
|
|
|
|
window.show_window_menu(position);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
}
|
2021-03-07 10:43:23 +01:00
|
|
|
}
|
|
|
|
|
WindowEvent::CursorEntered { .. } => {
|
|
|
|
|
entered_id = window_id;
|
|
|
|
|
name_windows(entered_id, switched, &window_1, &window_2)
|
|
|
|
|
}
|
|
|
|
|
WindowEvent::KeyboardInput {
|
2023-05-28 20:02:59 +02:00
|
|
|
event:
|
|
|
|
|
KeyEvent {
|
2021-03-07 10:43:23 +01:00
|
|
|
state: ElementState::Released,
|
2023-05-28 20:02:59 +02:00
|
|
|
logical_key: Key::Character(c),
|
2021-03-07 10:43:23 +01:00
|
|
|
..
|
|
|
|
|
},
|
|
|
|
|
..
|
2023-10-11 01:16:16 +03:30
|
|
|
} => match c.as_str() {
|
|
|
|
|
"x" => {
|
|
|
|
|
switched = !switched;
|
|
|
|
|
name_windows(entered_id, switched, &window_1, &window_2);
|
|
|
|
|
println!("Switched!")
|
|
|
|
|
}
|
|
|
|
|
"d" => {
|
|
|
|
|
let window = if (window_id == window_1.id() && switched)
|
|
|
|
|
|| (window_id == window_2.id() && !switched)
|
|
|
|
|
{
|
|
|
|
|
&window_2
|
|
|
|
|
} else {
|
|
|
|
|
&window_1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
window.set_decorations(!window.is_decorated());
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
|
|
|
|
},
|
2023-08-27 16:15:09 +02:00
|
|
|
WindowEvent::RedrawRequested => {
|
|
|
|
|
if window_id == window_1.id() {
|
|
|
|
|
fill::fill_window(&window_1);
|
|
|
|
|
} else if window_id == window_2.id() {
|
|
|
|
|
fill::fill_window(&window_2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-07 10:43:23 +01:00
|
|
|
_ => (),
|
|
|
|
|
},
|
2023-08-27 16:15:09 +02:00
|
|
|
|
2021-03-07 10:43:23 +01:00
|
|
|
_ => (),
|
2023-04-11 12:50:52 +01:00
|
|
|
})
|
2021-03-07 10:43:23 +01: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");
|
|
|
|
|
}
|