summaryrefslogtreecommitdiffhomepage
path: root/compat/check-visible.cpp
blob: 96dba60f0cc904e6466675787c9f254939dca77a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "check-visible.hpp"

#if defined _WIN32

#include "timer.hpp"

#include <QMutex>

#include <windows.h>

constexpr int visible_timeout = 5000;
constexpr int invisible_timeout = 250;

static Timer timer;
static QMutex mtx;
static bool visible = true;

void set_is_visible(const QWidget& w, bool force)
{
    QMutexLocker l(&mtx);

    if (!force && timer.elapsed_ms() < (visible ? visible_timeout : invisible_timeout))
        return;

    timer.start();

    const HWND id = (HWND) w.winId();
    const QPoint pt = w.mapToGlobal({ 0, 0 });

    const int W = w.width(), H = w.height();

    const QPoint points[] =
    {
        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)
    {
        visible = WindowFromPoint({ pt.x(), pt.y() }) == id;
        if (visible)
            break;
    }
}

bool check_is_visible()
{
    QMutexLocker l(&mtx);

    return visible;
}

#else

void set_is_visible(const QWidget&, bool)
{
}

bool check_is_visible()
{
    return true;
}

#endif