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

@ -605,6 +605,10 @@ impl Window {
}
#[inline]
pub fn has_focus(&self) -> bool {
x11_or_wayland!(match self; Window(window) => window.has_focus())
}
pub fn title(&self) -> String {
x11_or_wayland!(match self; Window(window) => window.title())
}

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,
}
}

View file

@ -930,6 +930,10 @@ impl<T: 'static> EventProcessor<T> {
let window_id = mkwid(xev.event);
let position = PhysicalPosition::new(xev.event_x, xev.event_y);
if let Some(window) = self.with_window(xev.event, Arc::clone) {
window.shared_state_lock().has_focus = true;
}
callback(Event::WindowEvent {
window_id,
event: Focused(true),
@ -1002,6 +1006,10 @@ impl<T: 'static> EventProcessor<T> {
event: WindowEvent::ModifiersChanged(ModifiersState::empty()),
});
if let Some(window) = self.with_window(xev.event, Arc::clone) {
window.shared_state_lock().has_focus = false;
}
callback(Event::WindowEvent {
window_id,
event: Focused(false),

View file

@ -55,6 +55,7 @@ pub struct SharedState {
pub resize_increments: Option<Size>,
pub base_size: Option<Size>,
pub visibility: Visibility,
pub has_focus: bool,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
@ -94,6 +95,7 @@ impl SharedState {
max_inner_size: None,
resize_increments: None,
base_size: None,
has_focus: true,
})
}
}
@ -1602,6 +1604,10 @@ impl UnownedWindow {
}
#[inline]
pub fn has_focus(&self) -> bool {
self.shared_state_lock().has_focus
}
pub fn title(&self) -> String {
String::new()
}