1 // This file is part of Desktop App Toolkit, 2 // a set of libraries for developing nice desktop applications. 3 // 4 // For license and copyright information please follow this link: 5 // https://github.com/desktop-app/legal/blob/master/LEGAL 6 // 7 #include "ui/inactive_press.h" 8 9 #include "base/timer.h" 10 #include "base/qt_connection.h" 11 12 #include <QtCore/QPointer> 13 14 namespace Ui { 15 namespace { 16 17 constexpr auto kInactivePressTimeout = crl::time(200); 18 19 struct InactivePressedWidget { 20 QWidget *widget = nullptr; 21 base::qt_connection connection; 22 base::Timer timer; 23 }; 24 25 std::unique_ptr<InactivePressedWidget> Tracker; 26 27 } // namespace 28 MarkInactivePress(not_null<QWidget * > widget,bool was)29void MarkInactivePress(not_null<QWidget*> widget, bool was) { 30 if (!was) { 31 if (WasInactivePress(widget)) { 32 Tracker = nullptr; 33 } 34 return; 35 } 36 37 Tracker = std::make_unique<InactivePressedWidget>(); 38 Tracker->widget = widget; 39 Tracker->connection = QObject::connect(widget, &QWidget::destroyed, [=] { 40 Tracker->connection.release(); 41 Tracker = nullptr; 42 }); 43 Tracker->timer.setCallback([=] { 44 Tracker = nullptr; 45 }); 46 Tracker->timer.callOnce(kInactivePressTimeout); 47 } 48 WasInactivePress(not_null<QWidget * > widget)49[[nodiscard]] bool WasInactivePress(not_null<QWidget*> widget) { 50 return Tracker && (Tracker->widget == widget); 51 } 52 53 } // namespace Ui 54