Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
debouncer.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <chrono>
7#include <functional>
8
9#include "utils/qt.h"
10
11#include <QTimer>
12
13namespace zrythm::utils
14{
15
26class Debouncer : public QObject
27{
28 Q_OBJECT
29
30public:
31 using Callback = std::function<void ()>;
32
40 std::chrono::milliseconds delay,
41 Callback callback,
42 QObject * parent = nullptr);
43
51
55 void debounce ();
56
61 void set_delay (std::chrono::milliseconds delay);
62
66 [[nodiscard]] std::chrono::milliseconds get_delay () const;
67
71 [[nodiscard]] bool is_pending () const;
72
76 void cancel ();
77
78private Q_SLOTS:
79 void execute_pending ();
80
81private:
83 std::chrono::milliseconds delay_;
84 Callback callback_;
85 bool pending_{ false };
86};
87
88} // namespace zrythm::utils
std::chrono::milliseconds get_delay() const
Get the current delay.
void set_delay(std::chrono::milliseconds delay)
Set the delay for future debounce operations.
void debounce()
Alternative method name for scheduling callback execution.
void operator()()
Call this to schedule the callback execution.
bool is_pending() const
Check if there's a pending callback execution.
Debouncer(std::chrono::milliseconds delay, Callback callback, QObject *parent=nullptr)
Construct a debouncer.
void cancel()
Cancel any pending callback execution.
A unique pointer for QObject objects that also works with QObject-based ownership.
Definition qt.h:38
String utilities.
Definition algorithms.h:12