2022-06-10 20:43:33 +10:00
|
|
|
#![allow(clippy::single_match)]
|
|
|
|
|
2022-05-07 12:29:25 +10:00
|
|
|
use log::LevelFilter;
|
|
|
|
use simple_logger::SimpleLogger;
|
|
|
|
use winit::{
|
2023-06-23 05:12:14 +10:00
|
|
|
dpi::{PhysicalPosition, PhysicalSize},
|
2023-05-29 04:02:59 +10:00
|
|
|
event::{ElementState, Event, Ime, WindowEvent},
|
2022-05-07 12:29:25 +10:00
|
|
|
event_loop::{ControlFlow, EventLoop},
|
2023-05-29 04:02:59 +10:00
|
|
|
keyboard::{Key, KeyCode},
|
2023-01-30 02:46:46 +11:00
|
|
|
window::{ImePurpose, WindowBuilder},
|
2022-05-07 12:29:25 +10:00
|
|
|
};
|
|
|
|
|
2023-06-20 04:46:38 +10:00
|
|
|
#[path = "util/fill.rs"]
|
|
|
|
mod fill;
|
|
|
|
|
2023-04-11 21:50:52 +10:00
|
|
|
fn main() -> Result<(), impl std::error::Error> {
|
2022-05-07 12:29:25 +10:00
|
|
|
SimpleLogger::new()
|
|
|
|
.with_level(LevelFilter::Trace)
|
|
|
|
.init()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
println!("IME position will system default");
|
|
|
|
println!("Click to set IME position to cursor's");
|
|
|
|
println!("Press F2 to toggle IME. See the documentation of `set_ime_allowed` for more info");
|
2023-01-30 02:46:46 +11:00
|
|
|
println!("Press F3 to cycle through IME purposes.");
|
2022-05-07 12:29:25 +10:00
|
|
|
|
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
|
|
|
|
let window = WindowBuilder::new()
|
|
|
|
.with_inner_size(winit::dpi::LogicalSize::new(256f64, 128f64))
|
|
|
|
.build(&event_loop)
|
|
|
|
.unwrap();
|
|
|
|
|
2023-01-30 02:46:46 +11:00
|
|
|
let mut ime_purpose = ImePurpose::Normal;
|
2022-05-07 12:29:25 +10:00
|
|
|
let mut ime_allowed = true;
|
|
|
|
window.set_ime_allowed(ime_allowed);
|
|
|
|
|
|
|
|
let mut may_show_ime = false;
|
|
|
|
let mut cursor_position = PhysicalPosition::new(0.0, 0.0);
|
|
|
|
let mut ime_pos = PhysicalPosition::new(0.0, 0.0);
|
|
|
|
|
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
*control_flow = ControlFlow::Wait;
|
|
|
|
match event {
|
|
|
|
Event::WindowEvent {
|
|
|
|
event: WindowEvent::CloseRequested,
|
|
|
|
..
|
|
|
|
} => *control_flow = ControlFlow::Exit,
|
|
|
|
Event::WindowEvent {
|
|
|
|
event: WindowEvent::CursorMoved { position, .. },
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
cursor_position = position;
|
|
|
|
}
|
|
|
|
Event::WindowEvent {
|
|
|
|
event:
|
|
|
|
WindowEvent::MouseInput {
|
|
|
|
state: ElementState::Released,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
println!(
|
|
|
|
"Setting ime position to {}, {}",
|
|
|
|
cursor_position.x, cursor_position.y
|
|
|
|
);
|
|
|
|
ime_pos = cursor_position;
|
|
|
|
if may_show_ime {
|
2023-06-23 05:12:14 +10:00
|
|
|
window.set_ime_cursor_area(ime_pos, PhysicalSize::new(10, 10));
|
2022-05-07 12:29:25 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::WindowEvent {
|
|
|
|
event: WindowEvent::Ime(event),
|
|
|
|
..
|
|
|
|
} => {
|
2023-01-27 15:18:58 +11:00
|
|
|
println!("{event:?}");
|
2022-05-07 12:29:25 +10:00
|
|
|
may_show_ime = event != Ime::Disabled;
|
|
|
|
if may_show_ime {
|
2023-06-23 05:12:14 +10:00
|
|
|
window.set_ime_cursor_area(ime_pos, PhysicalSize::new(10, 10));
|
2022-05-07 12:29:25 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::WindowEvent {
|
2023-05-29 04:02:59 +10:00
|
|
|
event: WindowEvent::KeyboardInput { event, .. },
|
2022-05-07 12:29:25 +10:00
|
|
|
..
|
|
|
|
} => {
|
2023-05-29 04:02:59 +10:00
|
|
|
println!("key: {event:?}");
|
2022-05-07 12:29:25 +10:00
|
|
|
|
2023-05-29 04:02:59 +10:00
|
|
|
if event.state == ElementState::Pressed && event.physical_key == KeyCode::F2 {
|
2022-05-07 12:29:25 +10:00
|
|
|
ime_allowed = !ime_allowed;
|
|
|
|
window.set_ime_allowed(ime_allowed);
|
2023-01-30 02:46:46 +11:00
|
|
|
println!("\nIME allowed: {ime_allowed}\n");
|
|
|
|
}
|
2023-05-29 04:02:59 +10:00
|
|
|
if event.state == ElementState::Pressed && event.logical_key == Key::F3 {
|
2023-01-30 02:46:46 +11:00
|
|
|
ime_purpose = match ime_purpose {
|
|
|
|
ImePurpose::Normal => ImePurpose::Password,
|
|
|
|
ImePurpose::Password => ImePurpose::Terminal,
|
|
|
|
_ => ImePurpose::Normal,
|
|
|
|
};
|
|
|
|
window.set_ime_purpose(ime_purpose);
|
|
|
|
println!("\nIME purpose: {ime_purpose:?}\n");
|
2022-05-07 12:29:25 +10:00
|
|
|
}
|
|
|
|
}
|
2023-06-20 04:46:38 +10:00
|
|
|
Event::RedrawRequested(_) => {
|
|
|
|
fill::fill_window(&window);
|
|
|
|
}
|
2022-05-07 12:29:25 +10:00
|
|
|
_ => (),
|
|
|
|
}
|
2023-04-11 21:50:52 +10:00
|
|
|
})
|
2022-05-07 12:29:25 +10:00
|
|
|
}
|