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

@ -1,5 +1,7 @@
//! Handling of various keyboard events.
use std::sync::atomic::Ordering;
use sctk::reexports::client::protocol::wl_keyboard::KeyState;
use sctk::seat::keyboard::Event as KeyboardEvent;
@ -22,6 +24,9 @@ pub(super) fn handle_keyboard(
KeyboardEvent::Enter { surface, .. } => {
let window_id = wayland::make_wid(&surface);
let window_handle = winit_state.window_map.get_mut(&window_id).unwrap();
window_handle.has_focus.store(true, Ordering::Relaxed);
// Window gained focus.
event_sink.push_window_event(WindowEvent::Focused(true), window_id);
@ -44,6 +49,9 @@ pub(super) fn handle_keyboard(
);
}
let window_handle = winit_state.window_map.get_mut(&window_id).unwrap();
window_handle.has_focus.store(false, Ordering::Relaxed);
// Window lost focus.
event_sink.push_window_event(WindowEvent::Focused(false), window_id);

View file

@ -79,6 +79,9 @@ pub struct Window {
/// Grabbing mode.
cursor_grab_mode: Mutex<CursorGrabMode>,
/// Whether the window has keyboard focus.
has_focus: Arc<AtomicBool>,
}
impl Window {
@ -244,6 +247,7 @@ impl Window {
window.surface().commit();
let size = Arc::new(Mutex::new(LogicalSize::new(width, height)));
let has_focus = Arc::new(AtomicBool::new(true));
// We should trigger redraw and commit the surface for the newly created window.
let mut window_user_request = WindowUserRequest::new();
@ -258,6 +262,7 @@ impl Window {
&event_loop_window_target.env,
window,
size.clone(),
has_focus.clone(),
window_requests.clone(),
);
@ -318,6 +323,7 @@ impl Window {
resizeable: AtomicBool::new(attributes.resizable),
decorated: AtomicBool::new(attributes.decorations),
cursor_grab_mode: Mutex::new(CursorGrabMode::None),
has_focus,
};
Ok(window)
@ -650,6 +656,10 @@ impl Window {
}
#[inline]
pub fn has_focus(&self) -> bool {
self.has_focus.load(Ordering::Relaxed)
}
pub fn title(&self) -> String {
String::new()
}

View file

@ -1,5 +1,6 @@
use std::cell::Cell;
use std::mem::ManuallyDrop;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex};
use sctk::reexports::client::protocol::wl_compositor::WlCompositor;
@ -154,6 +155,9 @@ pub struct WindowHandle {
/// Whether the window is resizable.
pub is_resizable: Cell<bool>,
/// Whether the window has keyboard focus.
pub has_focus: Arc<AtomicBool>,
/// Allow IME events for that window.
pub ime_allowed: Cell<bool>,
@ -187,6 +191,7 @@ impl WindowHandle {
env: &Environment<WinitEnv>,
window: Window<WinitFrame>,
size: Arc<Mutex<LogicalSize<u32>>>,
has_focus: Arc<AtomicBool>,
pending_window_requests: Arc<Mutex<Vec<WindowRequest>>>,
) -> Self {
let xdg_activation = env.get_global::<XdgActivationV1>();
@ -209,6 +214,7 @@ impl WindowHandle {
attention_requested: Cell::new(false),
compositor,
ime_allowed: Cell::new(false),
has_focus,
}
}