diff options
Diffstat (limited to 'compat/check-visible.cpp')
-rw-r--r-- | compat/check-visible.cpp | 105 |
1 files changed, 66 insertions, 39 deletions
diff --git a/compat/check-visible.cpp b/compat/check-visible.cpp index 84ddd6d9..621d941a 100644 --- a/compat/check-visible.cpp +++ b/compat/check-visible.cpp @@ -1,71 +1,98 @@ #include "check-visible.hpp" +#include <QMutex> +#include <QWidget> +#include <QDebug> + +static QMutex lock; +static bool visible = true; + #if defined _WIN32 #include "timer.hpp" +#include "macros.h" -#include <QMutexLocker> - -#include <windows.h> +static Timer timer; -constexpr int visible_timeout = 5000; +constexpr int visible_timeout = 1000; constexpr int invisible_timeout = 250; -static Timer timer; -static QMutex mtx; -static bool visible = true; +#include <windows.h> -never_inline OTR_COMPAT_EXPORT void set_is_visible(const QWidget& w, bool force) { - QMutexLocker l(&mtx); + QMutexLocker l(&lock); - if (!force && timer.elapsed_ms() < (visible ? visible_timeout : invisible_timeout)) + if (w.isHidden() || w.windowState() & Qt::WindowMinimized) + { + visible = false; return; + } - timer.start(); + { + int ndisplays = GetSystemMetrics(SM_CMONITORS); + if (ndisplays > 1) + { + visible = true; + return; + } + } + + HWND hwnd = (HWND)w.winId(); - const HWND id = (HWND) w.winId(); - const QPoint pt = w.mapToGlobal({ 0, 0 }); + if (!force && timer.elapsed_ms() < (visible ? visible_timeout : invisible_timeout)) + return; - const int W = w.width(), H = w.height(); + timer.start(); - const QPoint points[] = + if (RECT r; GetWindowRect(hwnd, &r)) { - pt, - pt + QPoint(W - 1, 0), - pt + QPoint(0, H - 1), - pt + QPoint(W - 1, H - 1), - pt + QPoint(W / 2, H / 2), - }; - - for (const QPoint& pt : points) + const int x = r.left, y = r.top; + const int w = r.right - x, h = r.bottom - y; + + const POINT xs[] { + { x + w - 1, y + 1 }, + { x + 1, y + h - 1 }, + { x + w - 1, y + h - 1 }, + { x + 1, y + 1 }, + { x + w/2, y + h/2 }, + }; + + visible = false; + + for (const POINT& pt : xs) + if (WindowFromPoint(pt) == hwnd) + { + + visible = true; + break; + } + } + else { - visible = WindowFromPoint({ pt.x(), pt.y() }) == id; - if (visible) - break; + eval_once(qDebug() << "check-visible: GetWindowRect failed"); + visible = true; } } -never_inline OTR_COMPAT_EXPORT -bool check_is_visible() -{ - QMutexLocker l(&mtx); - - return visible; -} - #else -never_inline OTR_COMPAT_EXPORT -void set_is_visible(const QWidget&, bool) +void set_is_visible(const QWidget& w, bool) { + QMutexLocker l(&lock); + visible = !(w.isHidden() || w.windowState() & Qt::WindowMinimized); } -never_inline OTR_COMPAT_EXPORT +#endif + bool check_is_visible() { - return true; + QMutexLocker l(&lock); + return visible; } -#endif +void force_is_visible(bool value) +{ + QMutexLocker l(&lock); + visible = value; +} |