capture_mode_toast_controller.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_CAPTURE_MODE_CAPTURE_MODE_TOAST_CONTROLLER_H_
  5. #define ASH_CAPTURE_MODE_CAPTURE_MODE_TOAST_CONTROLLER_H_
  6. #include "ash/ash_export.h"
  7. #include "base/timer/timer.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. #include "ui/views/widget/unique_widget_ptr.h"
  10. namespace gfx {
  11. class Rect;
  12. } // namespace gfx
  13. namespace views {
  14. class Widget;
  15. class Label;
  16. } // namespace views
  17. namespace ui {
  18. class Layer;
  19. } // namespace ui
  20. namespace ash {
  21. class CaptureModeSession;
  22. // Defines the capture toast type that Capture Mode is currently using.
  23. enum class CaptureToastType {
  24. kUserNudge,
  25. kCameraPreview,
  26. };
  27. // Controls the capture mode toast shown in the capture session.
  28. class ASH_EXPORT CaptureModeToastController {
  29. public:
  30. explicit CaptureModeToastController(CaptureModeSession* session);
  31. CaptureModeToastController(const CaptureModeToastController&) = delete;
  32. CaptureModeToastController& operator=(const CaptureModeToastController&) =
  33. delete;
  34. ~CaptureModeToastController();
  35. const CaptureToastType* current_toast_type() const {
  36. return current_toast_type_ ? &(*current_toast_type_) : nullptr;
  37. }
  38. views::Widget* capture_toast_widget() const {
  39. return capture_toast_widget_.get();
  40. }
  41. // Shows `capture_toast_widget_` with label text defined by the given
  42. // `capture_toast_type`; if `capture_toast_widget_` doesn't exist, creates
  43. // one. Otherwise, just updates the label text for it if needed.
  44. void ShowCaptureToast(CaptureToastType capture_toast_type);
  45. void MaybeDismissCaptureToast(CaptureToastType capture_toast_type,
  46. bool animate = true);
  47. // Called when we need to dismiss the current toast in spite of the toast
  48. // type. For example, when the settings menu is shown, the toast should be
  49. // dismissed no matter what type it is.
  50. void DismissCurrentToastIfAny();
  51. void MaybeRepositionCaptureToast();
  52. // Return the layer of `capture_toast_widget_` if it exists.
  53. ui::Layer* MaybeGetToastLayer();
  54. base::OneShotTimer* capture_toast_dismiss_timer_for_test() {
  55. return &capture_toast_dismiss_timer_;
  56. }
  57. private:
  58. // Initializes the toast widget and its contents.
  59. void BuildCaptureToastWidget(const std::u16string& label);
  60. gfx::Rect CalculateToastWidgetScreenBounds() const;
  61. // The session that owns `this`. Guaranteed to be non null for the lifetime of
  62. // `this`.
  63. CaptureModeSession* const capture_session_;
  64. // The capture toast widget and its contents view.
  65. views::UniqueWidgetPtr capture_toast_widget_;
  66. views::Label* toast_label_view_ = nullptr;
  67. // Stores the toast type of the `capture_toast_widget_` after it's created.
  68. absl::optional<CaptureToastType> current_toast_type_;
  69. // Started when `capture_toast_widget_` is shown for `kCameraPreview`. Runs
  70. // MaybeDismissCaptureToast() to fade out the capture toast if possible.
  71. base::OneShotTimer capture_toast_dismiss_timer_;
  72. base::WeakPtrFactory<CaptureModeToastController> weak_ptr_factory_{this};
  73. };
  74. } // namespace ash
  75. #endif // ASH_CAPTURE_MODE_CAPTURE_MODE_TOAST_CONTROLLER_H_