From 5081d49f9f08596c07a8ed32430a4fa3e1baf352 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 24 Jul 2019 23:46:17 +0200 Subject: Add puglHasFocus() --- pugl/detail/mac.m | 9 +++++++++ pugl/detail/win.c | 8 +++++++- pugl/detail/x11.c | 9 +++++++++ pugl/pugl.h | 6 ++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pugl/detail/mac.m b/pugl/detail/mac.m index a438701..385070d 100644 --- a/pugl/detail/mac.m +++ b/pugl/detail/mac.m @@ -778,6 +778,15 @@ puglGrabFocus(PuglView* view) [window makeFirstResponder:view->impl->wrapperView]; } +bool +puglHasFocus(const PuglView* view) +{ + PuglInternals* const impl = view->impl; + + return ([[impl->wrapperView window] isKeyWindow] && + [[impl->wrapperView window] firstResponder] == impl->wrapperView); +} + void puglRequestAttention(PuglView* view) { diff --git a/pugl/detail/win.c b/pugl/detail/win.c index 42b9427..a4597b5 100644 --- a/pugl/detail/win.c +++ b/pugl/detail/win.c @@ -631,10 +631,16 @@ puglGrabFocus(PuglView* view) SetFocus(view->impl->hwnd); } +bool +puglHasFocus(const PuglView* view) +{ + return GetFocus() == view->impl->hwnd; +} + void puglRequestAttention(PuglView* view) { - if (!view->impl->mouseTracked || GetFocus() != view->impl->hwnd) { + if (!view->impl->mouseTracked || !puglHasFocus(view)) { FlashWindow(view->impl->hwnd, TRUE); SetTimer(view->impl->hwnd, PUGL_URGENT_TIMER_ID, 500, NULL); view->impl->flashing = true; diff --git a/pugl/detail/x11.c b/pugl/detail/x11.c index a766ace..cb9f0e2 100644 --- a/pugl/detail/x11.c +++ b/pugl/detail/x11.c @@ -443,6 +443,15 @@ puglGrabFocus(PuglView* view) view->impl->display, view->impl->win, RevertToNone, CurrentTime); } +bool +puglHasFocus(const PuglView* view) +{ + int revertTo = 0; + Window focusedWindow = 0; + XGetInputFocus(view->impl->display, &focusedWindow, &revertTo); + return focusedWindow == view->impl->win; +} + void puglRequestAttention(PuglView* view) { diff --git a/pugl/pugl.h b/pugl/pugl.h index f352766..b176696 100644 --- a/pugl/pugl.h +++ b/pugl/pugl.h @@ -640,6 +640,12 @@ puglSetEventFunc(PuglView* view, PuglEventFunc eventFunc); PUGL_API PUGL_DEPRECATED_BY("puglInitWindowHint") void puglIgnoreKeyRepeat(PuglView* view, bool ignore); +/** + Return true iff `view` has the input focus. +*/ +PUGL_API bool +puglHasFocus(const PuglView* view); + /** Grab the input focus. */ -- cgit v1.2.1