system_nudge.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2021 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_SYSTEM_TRAY_SYSTEM_NUDGE_H_
  5. #define ASH_SYSTEM_TRAY_SYSTEM_NUDGE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "ash/ash_export.h"
  9. #include "ash/constants/notifier_catalogs.h"
  10. #include "ash/shelf/shelf.h"
  11. #include "ash/shelf/shelf_observer.h"
  12. #include "ash/shell.h"
  13. #include "ash/shell_observer.h"
  14. #include "ash/style/ash_color_provider.h"
  15. #include "ash/system/tray/system_nudge_label.h"
  16. #include "base/memory/weak_ptr.h"
  17. #include "base/scoped_observation.h"
  18. #include "ui/gfx/paint_vector_icon.h"
  19. #include "ui/views/widget/unique_widget_ptr.h"
  20. namespace aura {
  21. class Window;
  22. }
  23. namespace views {
  24. class Widget;
  25. class View;
  26. } // namespace views
  27. namespace ash {
  28. // Creates and manages the nudge widget and its contents view for a contextual
  29. // system nudge. The nudge displays an icon and a label view in a shelf-colored
  30. // system bubble with rounded corners.
  31. class ASH_EXPORT SystemNudge : public ShelfObserver, ShellObserver {
  32. public:
  33. SystemNudge(const std::string& name,
  34. NudgeCatalogName catalog_name,
  35. int icon_size,
  36. int icon_label_spacing,
  37. int nudge_padding,
  38. bool anchor_status_area = false,
  39. AshColorProvider::ContentLayerType icon_color_layer_type =
  40. AshColorProvider::ContentLayerType::kIconColorPrimary);
  41. SystemNudge(const SystemNudge&) = delete;
  42. SystemNudge& operator=(const SystemNudge&) = delete;
  43. ~SystemNudge() override;
  44. // ShelfObserver:
  45. void OnAutoHideStateChanged(ShelfAutoHideState new_state) override;
  46. void OnHotseatStateChanged(HotseatState old_state,
  47. HotseatState new_state) override;
  48. // ShellObserver:
  49. void OnShelfAlignmentChanged(aura::Window* root_window,
  50. ShelfAlignment old_alignment) override;
  51. // Displays the nudge.
  52. void Show();
  53. // Closes the nudge.
  54. void Close();
  55. views::Widget* widget() { return widget_.get(); }
  56. protected:
  57. // Each SystemNudge subclass must override these methods to customize
  58. // their nudge by creating a label and getting an icon specific to the feature
  59. // being nudged. These will be called only when needed by Show().
  60. // Creates and initializes a view representing the label for the nudge.
  61. virtual std::unique_ptr<SystemNudgeLabel> CreateLabelView() const = 0;
  62. // Gets the VectorIcon shown to the side of the label for the nudge.
  63. virtual const gfx::VectorIcon& GetIcon() const = 0;
  64. // Gets the string to announce for accessibility. This will be used if a
  65. // screen reader is enabled when the view is shown.
  66. virtual std::u16string GetAccessibilityText() const = 0;
  67. private:
  68. class SystemNudgeView;
  69. struct SystemNudgeParams {
  70. // The name for the widget.
  71. std::string name;
  72. // The catalog name for the system nudge.
  73. NudgeCatalogName catalog_name;
  74. // The size of the icon.
  75. int icon_size;
  76. // The size of the space between icon and label.
  77. int icon_label_spacing;
  78. // The padding which separates the nudge's border with its inner contents.
  79. int nudge_padding;
  80. // If true, the nudge will be on the same side of the status area.
  81. // Otherwise the nudge will be on the left/right side of the window for
  82. // non-RTL/RTL locale.
  83. bool anchor_status_area = false;
  84. // The color of the icon.
  85. AshColorProvider::ContentLayerType icon_color_layer_type =
  86. AshColorProvider::ContentLayerType::kIconColorPrimary;
  87. };
  88. // Calculate and set widget bounds based on a fixed width and a variable
  89. // height to correctly fit the label contents.
  90. void CalculateAndSetWidgetBounds();
  91. views::UniqueWidgetPtr widget_;
  92. SystemNudgeView* nudge_view_ = nullptr; // not_owned
  93. aura::Window* const root_window_;
  94. SystemNudgeParams params_;
  95. base::ScopedObservation<Shelf, ShelfObserver> shelf_observation_{this};
  96. base::ScopedObservation<Shell,
  97. ShellObserver,
  98. &Shell::AddShellObserver,
  99. &Shell::RemoveShellObserver>
  100. shell_observation_{this};
  101. base::WeakPtrFactory<SystemNudge> weak_factory_{this};
  102. };
  103. } // namespace ash
  104. #endif // ASH_SYSTEM_TRAY_SYSTEM_NUDGE_H_