Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
DebouncedAction.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <phosphorcompositor_export.h>
7
8#include <QObject>
9#include <QRect>
10#include <QTimer>
11#include <functional>
12
13namespace PhosphorCompositor {
14
34class PHOSPHORCOMPOSITOR_EXPORT DebouncedScreenAction : public QObject
35{
36 Q_OBJECT
37
38public:
43 explicit DebouncedScreenAction(int debounceMs, QObject* parent = nullptr)
44 : QObject(parent)
45 {
46 m_debounce.setSingleShot(true);
47 m_debounce.setInterval(debounceMs);
48 connect(&m_debounce, &QTimer::timeout, this, &DebouncedScreenAction::onDebounceTimeout);
49 }
50
51 void stop()
52 {
53 m_debounce.stop();
54 m_pending = false;
55 }
56
64 void geometryChanged(const QRect& currentGeometry)
65 {
66 if (m_hasLastGeometry && currentGeometry == m_lastGeometry && !m_pending) {
67 if (m_applyInProgress) {
68 requestReapply();
69 }
70 return;
71 }
72 m_lastGeometry = currentGeometry;
73 m_hasLastGeometry = true;
74 m_pending = true;
75 m_debounce.start();
76 }
77
83 {
84 if (m_applyInProgress) {
85 m_reapplyPending = true;
86 return;
87 }
88 Q_EMIT applyRequested();
89 }
90
95 {
96 m_applyInProgress = true;
97 }
98
104 {
105 m_applyInProgress = false;
106 if (m_reapplyPending) {
107 m_reapplyPending = false;
108 QTimer::singleShot(0, this, [this]() {
109 Q_EMIT applyRequested();
110 });
111 }
112 }
113
115 {
116 return m_pending || m_applyInProgress;
117 }
118
119 void setLastGeometry(const QRect& geo)
120 {
121 m_lastGeometry = geo;
122 m_hasLastGeometry = true;
123 }
124
125Q_SIGNALS:
128
129private Q_SLOTS:
130 void onDebounceTimeout()
131 {
132 // Subclass or signal receiver provides currentGeometry
133 // For now, just signal that we need to apply
134 m_pending = false;
135 Q_EMIT applyRequested();
136 }
137
138private:
139 QTimer m_debounce;
140 QRect m_lastGeometry;
141 bool m_hasLastGeometry = false;
142 bool m_pending = false;
143 bool m_applyInProgress = false;
144 bool m_reapplyPending = false;
145};
146
147} // namespace PhosphorCompositor
Compositor-agnostic debounced action with coalescing.
Definition DebouncedAction.h:35
void setLastGeometry(const QRect &geo)
Definition DebouncedAction.h:119
DebouncedScreenAction(int debounceMs, QObject *parent=nullptr)
Definition DebouncedAction.h:43
void applyRequested()
Emitted when an apply is requested (debounced change or explicit reapply)
void markApplyStarted()
Call when the apply operation starts (before async D-Bus call).
Definition DebouncedAction.h:94
void geometryChanged(const QRect &currentGeometry)
Signal that the screen geometry has changed.
Definition DebouncedAction.h:64
void markApplyCompleted()
Call when the apply operation completes.
Definition DebouncedAction.h:103
void stop()
Definition DebouncedAction.h:51
bool isChangeInProgress() const
Definition DebouncedAction.h:114
void requestReapply()
Request a re-apply (e.g., daemon-triggered).
Definition DebouncedAction.h:82
Definition AutotileState.h:16