Skip to content

Commit f44042b

Browse files
authored
Merge pull request #124 from stdware/dev
Make hit-test items auto-delete
2 parents 63bc501 + e9d163b commit f44042b

File tree

7 files changed

+81
-21
lines changed

7 files changed

+81
-21
lines changed

qmsetup

src/core/contexts/abstractwindowcontext.cpp

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,27 @@ namespace QWK {
2626
notifyWinIdChange();
2727
}
2828

29-
bool AbstractWindowContext::setHitTestVisible(const QObject *obj, bool visible) {
29+
bool AbstractWindowContext::setHitTestVisible(QObject *obj, bool visible) {
3030
Q_ASSERT(obj);
3131
if (!obj) {
3232
return false;
3333
}
3434

35+
auto it = m_hitTestVisibleItems.find(obj);
3536
if (visible) {
37+
if (it != m_hitTestVisibleItems.end()) {
38+
return true;
39+
}
40+
connect(obj, &QObject::destroyed, this,
41+
&AbstractWindowContext::_q_hitTestVisibleItemDestroyed);
3642
m_hitTestVisibleItems.insert(obj);
3743
} else {
38-
m_hitTestVisibleItems.remove(obj);
44+
if (it == m_hitTestVisibleItems.end()) {
45+
return false;
46+
}
47+
disconnect(obj, &QObject::destroyed, this,
48+
&AbstractWindowContext::_q_hitTestVisibleItemDestroyed);
49+
m_hitTestVisibleItems.erase(it);
3950
}
4051
return true;
4152
}
@@ -47,27 +58,39 @@ namespace QWK {
4758
return false;
4859
}
4960

50-
if (m_systemButtons[button] == obj) {
51-
return false;
61+
auto org = m_systemButtons[button];
62+
if (org == obj) {
63+
return true;
64+
}
65+
66+
if (org) {
67+
disconnect(org, &QObject::destroyed, this,
68+
&AbstractWindowContext::_q_systemButtonDestroyed);
69+
}
70+
if (obj) {
71+
connect(obj, &QObject::destroyed, this,
72+
&AbstractWindowContext::_q_systemButtonDestroyed);
5273
}
5374
m_systemButtons[button] = obj;
5475
return true;
5576
}
5677

5778
bool AbstractWindowContext::setTitleBar(QObject *item) {
5879
Q_ASSERT(item);
59-
if (m_titleBar == item) {
80+
auto org = m_titleBar;
81+
if (org == item) {
6082
return false;
6183
}
6284

63-
if (m_titleBar) {
85+
if (org) {
6486
// Since the title bar is changed, all items inside it should be dereferenced right away
65-
for (auto &button : m_systemButtons) {
66-
button = nullptr;
67-
}
68-
m_hitTestVisibleItems.clear();
87+
removeSystemButtonsAndHitTestItems();
88+
disconnect(org, &QObject::destroyed, this,
89+
&AbstractWindowContext::_q_titleBarDistroyed);
90+
}
91+
if (item) {
92+
connect(item, &QObject::destroyed, this, &AbstractWindowContext::_q_titleBarDistroyed);
6993
}
70-
7194
m_titleBar = item;
7295
return true;
7396
}
@@ -278,4 +301,38 @@ namespace QWK {
278301
return false;
279302
}
280303

304+
void AbstractWindowContext::removeSystemButtonsAndHitTestItems() {
305+
for (auto &button : m_systemButtons) {
306+
if (!button) {
307+
continue;
308+
}
309+
disconnect(button, &QObject::destroyed, this,
310+
&AbstractWindowContext::_q_systemButtonDestroyed);
311+
button = nullptr;
312+
}
313+
for (auto &item : m_hitTestVisibleItems) {
314+
disconnect(item, &QObject::destroyed, this,
315+
&AbstractWindowContext::_q_hitTestVisibleItemDestroyed);
316+
}
317+
m_hitTestVisibleItems.clear();
318+
}
319+
320+
void AbstractWindowContext::_q_titleBarDistroyed(QObject *obj) {
321+
Q_UNUSED(obj)
322+
removeSystemButtonsAndHitTestItems();
323+
m_titleBar = nullptr;
324+
}
325+
326+
void AbstractWindowContext::_q_hitTestVisibleItemDestroyed(QObject *obj) {
327+
m_hitTestVisibleItems.remove(obj);
328+
}
329+
330+
void AbstractWindowContext::_q_systemButtonDestroyed(QObject *obj) {
331+
for (auto &item : m_systemButtons) {
332+
if (item == obj) {
333+
item = nullptr;
334+
}
335+
}
336+
}
337+
281338
}

src/core/contexts/abstractwindowcontext_p.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace QWK {
4747
inline WindowItemDelegate *delegate() const;
4848

4949
inline bool isHitTestVisible(const QObject *obj) const;
50-
bool setHitTestVisible(const QObject *obj, bool visible);
50+
bool setHitTestVisible(QObject *obj, bool visible);
5151

5252
inline QObject *systemButton(WindowAgentBase::SystemButton button) const;
5353
bool setSystemButton(WindowAgentBase::SystemButton button, QObject *obj);
@@ -124,6 +124,13 @@ namespace QWK {
124124

125125
QVariantHash m_windowAttributes;
126126
std::unique_ptr<WinIdChangeEventFilter> m_winIdChangeEventFilter;
127+
128+
void removeSystemButtonsAndHitTestItems();
129+
130+
private:
131+
void _q_titleBarDistroyed(QObject *obj);
132+
void _q_hitTestVisibleItemDestroyed(QObject *obj);
133+
void _q_systemButtonDestroyed(QObject *obj);
127134
};
128135

129136
inline QObject *AbstractWindowContext::host() const {

src/quick/quickwindowagent.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,8 @@ namespace QWK {
9090
Q_D(const QuickWindowAgent);
9191
return d->context->isHitTestVisible(item);
9292
}
93-
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
93+
9494
void QuickWindowAgent::setHitTestVisible(QQuickItem *item, bool visible) {
95-
#else
96-
void QuickWindowAgent::setHitTestVisible(const QQuickItem *item, bool visible) {
97-
#endif
9895
Q_D(QuickWindowAgent);
9996
d->context->setHitTestVisible(item, visible);
10097
}

src/quick/quickwindowagent.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ namespace QWK {
3333

3434
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
3535
Q_INVOKABLE bool isHitTestVisible(QQuickItem *item) const;
36-
Q_INVOKABLE void setHitTestVisible(QQuickItem *item, bool visible = true);
3736
#else
3837
Q_INVOKABLE bool isHitTestVisible(const QQuickItem *item) const;
39-
Q_INVOKABLE void setHitTestVisible(const QQuickItem *item, bool visible = true);
4038
#endif
39+
Q_INVOKABLE void setHitTestVisible(QQuickItem *item, bool visible = true);
4140

4241
#ifdef Q_OS_MAC
4342
// The system button area APIs are experimental, very likely to change in the future.

src/widgets/widgetwindowagent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ namespace QWK {
123123
You're supposed to make sure that the specified widget \a w is a child or descendant
124124
of the title bar widget.
125125
*/
126-
void WidgetWindowAgent::setHitTestVisible(const QWidget *w, bool visible) {
126+
void WidgetWindowAgent::setHitTestVisible(QWidget *w, bool visible) {
127127
Q_D(WidgetWindowAgent);
128128
d->context->setHitTestVisible(w, visible);
129129
}

src/widgets/widgetwindowagent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace QWK {
4040
#endif
4141

4242
bool isHitTestVisible(const QWidget *w) const;
43-
void setHitTestVisible(const QWidget *w, bool visible = true);
43+
void setHitTestVisible(QWidget *w, bool visible = true);
4444

4545
Q_SIGNALS:
4646
void titleBarChanged(QWidget *w);

0 commit comments

Comments
 (0)