On Windows and MacOS, add Window::has_focus

This commit is contained in:
Amr Bashir 2023-01-17 03:30:14 +02:00 committed by GitHub
parent 067535eb38
commit a88d2e079d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 102 additions and 2 deletions

View file

@ -5,7 +5,7 @@ use std::{
hash::Hash,
sync::{
atomic::{AtomicBool, Ordering},
mpsc, Arc,
mpsc, Arc, RwLock,
},
time::{Duration, Instant},
};
@ -14,6 +14,7 @@ use android_activity::input::{InputEvent, KeyAction, Keycode, MotionAction};
use android_activity::{
AndroidApp, AndroidAppWaker, ConfigurationRef, InputStatus, MainEvent, Rect,
};
use once_cell::sync::Lazy;
use raw_window_handle::{
AndroidDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};
@ -27,6 +28,8 @@ use crate::{
window::{self, CursorGrabMode, ResizeDirection, Theme, WindowButtons, WindowLevel},
};
static HAS_FOCUS: Lazy<RwLock<bool>> = Lazy::new(|| RwLock::new(true));
fn ndk_keycode_to_virtualkeycode(keycode: Keycode) -> Option<event::VirtualKeyCode> {
match keycode {
Keycode::A => Some(VirtualKeyCode::A),
@ -394,6 +397,7 @@ impl<T: 'static> EventLoop<T> {
warn!("TODO: find a way to notify application of content rect change");
}
MainEvent::GainedFocus => {
*HAS_FOCUS.write().unwrap() = true;
sticky_exit_callback(
event::Event::WindowEvent {
window_id: window::WindowId(WindowId),
@ -405,6 +409,7 @@ impl<T: 'static> EventLoop<T> {
);
}
MainEvent::LostFocus => {
*HAS_FOCUS.write().unwrap() = false;
sticky_exit_callback(
event::Event::WindowEvent {
window_id: window::WindowId(WindowId),
@ -1064,6 +1069,10 @@ impl Window {
None
}
pub fn has_focus(&self) -> bool {
*HAS_FOCUS.read().unwrap()
}
pub fn title(&self) -> String {
String::new()
}