power_save_blocker.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #pragma once
  8. #include <QtCore/QPointer>
  9. class QWindow;
  10. namespace base {
  11. enum class PowerSaveBlockType {
  12. PreventAppSuspension,
  13. PreventDisplaySleep,
  14. kCount,
  15. };
  16. class PowerSaveBlocker final {
  17. public:
  18. PowerSaveBlocker(
  19. PowerSaveBlockType type,
  20. const QString &description,
  21. QWindow *window);
  22. ~PowerSaveBlocker();
  23. [[nodiscard]] PowerSaveBlockType type() const {
  24. return _type;
  25. }
  26. [[nodiscard]] const QString &description() const {
  27. return _description;
  28. }
  29. [[nodiscard]] QPointer<QWindow> window() const;
  30. private:
  31. const PowerSaveBlockType _type = {};
  32. const QString _description;
  33. const QPointer<QWindow> _window;
  34. };
  35. // DescriptionResolver -> QString, WindowResolver -> QPointer<QWindow>.
  36. template <typename DescriptionResolver, typename WindowResolver>
  37. void UpdatePowerSaveBlocker(
  38. std::unique_ptr<PowerSaveBlocker> &blocker,
  39. bool block,
  40. PowerSaveBlockType type,
  41. DescriptionResolver &&description,
  42. WindowResolver &&window) {
  43. if (block && !blocker) {
  44. blocker = std::make_unique<PowerSaveBlocker>(
  45. type,
  46. description(),
  47. window());
  48. } else if (!block && blocker) {
  49. blocker = nullptr;
  50. }
  51. }
  52. } // namespace base