summaryrefslogtreecommitdiffhomepage
path: root/compat/check-visible.cpp
blob: 4da649c7152dcdd9ff2a35d56137b667b5d9cb6b (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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.hpp"

static Timer timer;

constexpr int visible_timeout = 1000;
constexpr int invisible_timeout = 250;

#include <windows.h>

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

    if (w.isHidden() || w.windowState() & Qt::WindowMinimized)
    {
        visible = false;
        return;
    }

    {
        int ndisplays = GetSystemMetrics(SM_CMONITORS);
        if (ndisplays > 1)
        {
            visible = true;
            return;
        }
    }

    HWND hwnd = (HWND)w.winId();

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

    timer.start();

    if (RECT r; GetWindowRect(hwnd, &r))
    {
        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
    {
        eval_once(qDebug() << "check-visible: GetWindowRect failed");
        visible = true;
    }
}

#else

void set_is_visible(const QWidget& w, bool)
{
    QMutexLocker l(&lock);
    visible = !(w.isHidden() || w.windowState() & Qt::WindowMinimized);
}

#endif

bool check_is_visible()
{
    QMutexLocker l(&lock);
    return visible;
}

void force_is_visible(bool value)
{
    QMutexLocker l(&lock);
    visible = value;
}