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

@ -56,6 +56,7 @@ impl<T> EventLoopWindowTarget<T> {
canvas: &Rc<RefCell<backend::Canvas>>,
id: WindowId,
prevent_default: bool,
has_focus: Rc<RefCell<bool>>,
) {
self.runner.add_canvas(RootWindowId(id), canvas);
let mut canvas = canvas.borrow_mut();
@ -65,7 +66,9 @@ impl<T> EventLoopWindowTarget<T> {
canvas.on_touch_end(prevent_default);
let runner = self.runner.clone();
let has_focus_clone = has_focus.clone();
canvas.on_blur(move || {
*has_focus_clone.borrow_mut() = false;
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Focused(false),
@ -73,7 +76,9 @@ impl<T> EventLoopWindowTarget<T> {
});
let runner = self.runner.clone();
let has_focus_clone = has_focus.clone();
canvas.on_focus(move || {
*has_focus_clone.borrow_mut() = true;
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Focused(true),
@ -191,6 +196,8 @@ impl<T> EventLoopWindowTarget<T> {
let runner_touch = self.runner.clone();
canvas.on_mouse_press(
move |pointer_id, position, button, modifiers| {
*has_focus.borrow_mut() = true;
// A mouse down event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the
// user code has the correct cursor position.

View file

@ -23,6 +23,7 @@ pub struct Window {
register_redraw_request: Box<dyn Fn()>,
resize_notify_fn: Box<dyn Fn(PhysicalSize<u32>)>,
destroy_fn: Option<Box<dyn FnOnce()>>,
has_focus: Rc<RefCell<bool>>,
}
impl Window {
@ -42,7 +43,8 @@ impl Window {
let register_redraw_request = Box::new(move || runner.request_redraw(RootWI(id)));
target.register(&canvas, id, prevent_default);
let has_focus = Rc::new(RefCell::new(false));
target.register(&canvas, id, prevent_default, has_focus.clone());
let runner = target.runner.clone();
let resize_notify_fn = Box::new(move |new_size| {
@ -62,6 +64,7 @@ impl Window {
register_redraw_request,
resize_notify_fn,
destroy_fn: Some(destroy_fn),
has_focus,
};
backend::set_canvas_size(
@ -399,6 +402,10 @@ impl Window {
}
#[inline]
pub fn has_focus(&self) -> bool {
*self.has_focus.borrow()
}
pub fn title(&self) -> String {
String::new()
}