inactive_press.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. #include "base/timer.h"
  9. #include "base/qt_connection.h"
  10. #include <QtCore/QPointer>
  11. namespace Ui {
  12. namespace {
  13. constexpr auto kInactivePressTimeout = crl::time(200);
  14. struct InactivePressedWidget {
  15. QWidget *widget = nullptr;
  16. base::qt_connection connection;
  17. base::Timer timer;
  18. };
  19. std::unique_ptr<InactivePressedWidget> Tracker;
  20. } // namespace
  21. void MarkInactivePress(not_null<QWidget*> widget, bool was) {
  22. if (!was) {
  23. if (WasInactivePress(widget)) {
  24. Tracker = nullptr;
  25. }
  26. return;
  27. }
  28. Tracker = std::make_unique<InactivePressedWidget>();
  29. Tracker->widget = widget;
  30. Tracker->connection = QObject::connect(widget, &QWidget::destroyed, [=] {
  31. Tracker->connection.release();
  32. Tracker = nullptr;
  33. });
  34. Tracker->timer.setCallback([=] {
  35. Tracker = nullptr;
  36. });
  37. Tracker->timer.callOnce(kInactivePressTimeout);
  38. }
  39. [[nodiscard]] bool WasInactivePress(not_null<QWidget*> widget) {
  40. return Tracker && (Tracker->widget == widget);
  41. }
  42. } // namespace Ui