last_user_input.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 "base/last_user_input.h"
  8. #include "base/event_filter.h"
  9. #include "base/platform/base_platform_last_input.h"
  10. #include <QtCore/QCoreApplication>
  11. namespace base {
  12. crl::time LastUserInputTime() {
  13. Expects(QCoreApplication::instance() != nullptr);
  14. if (const auto specific = base::Platform::LastUserInputTime()) {
  15. return *specific;
  16. }
  17. static auto result = crl::time(0);
  18. static const auto isInputEvent = [](not_null<QEvent*> e) {
  19. switch (e->type()) {
  20. case QEvent::MouseMove:
  21. case QEvent::KeyPress:
  22. case QEvent::MouseButtonPress:
  23. case QEvent::TouchBegin:
  24. case QEvent::Wheel: return true;
  25. }
  26. return false;
  27. };
  28. static const auto updateResult = [](not_null<QEvent*> e) {
  29. if (isInputEvent(e)) {
  30. result = crl::now();
  31. }
  32. return base::EventFilterResult::Continue;
  33. };
  34. [[maybe_unused]] static const auto watcher = base::install_event_filter(
  35. QCoreApplication::instance(),
  36. updateResult);
  37. return result;
  38. }
  39. crl::time SinceLastUserInput() {
  40. return crl::now() - LastUserInputTime();
  41. }
  42. } // namespace base