bubble_utils.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. #include "ash/bubble/bubble_utils.h"
  5. #include <memory>
  6. #include "ash/capture_mode/capture_mode_util.h"
  7. #include "ash/public/cpp/shell_window_ids.h"
  8. #include "ash/root_window_controller.h"
  9. #include "ash/shell.h"
  10. #include "ash/style/ash_color_provider.h"
  11. #include "base/bind.h"
  12. #include "base/callback.h"
  13. #include "base/check.h"
  14. #include "ui/aura/window.h"
  15. #include "ui/events/event.h"
  16. #include "ui/events/types/event_type.h"
  17. #include "ui/views/controls/label.h"
  18. namespace ash {
  19. namespace bubble_utils {
  20. namespace {
  21. // A label which invokes a constructor-specified callback in `OnThemeChanged()`.
  22. class LabelWithThemeChangedCallback : public views::Label {
  23. public:
  24. using ThemeChangedCallback = base::RepeatingCallback<void(views::Label*)>;
  25. LabelWithThemeChangedCallback(const std::u16string& text,
  26. ThemeChangedCallback theme_changed_callback)
  27. : views::Label(text),
  28. theme_changed_callback_(std::move(theme_changed_callback)) {}
  29. LabelWithThemeChangedCallback(const LabelWithThemeChangedCallback&) = delete;
  30. LabelWithThemeChangedCallback& operator=(
  31. const LabelWithThemeChangedCallback&) = delete;
  32. ~LabelWithThemeChangedCallback() override = default;
  33. private:
  34. // views::Label:
  35. void OnThemeChanged() override {
  36. views::Label::OnThemeChanged();
  37. theme_changed_callback_.Run(this);
  38. }
  39. ThemeChangedCallback theme_changed_callback_;
  40. };
  41. } // namespace
  42. bool ShouldCloseBubbleForEvent(const ui::LocatedEvent& event) {
  43. // Should only be called for "press" type events.
  44. DCHECK(event.type() == ui::ET_MOUSE_PRESSED ||
  45. event.type() == ui::ET_TOUCH_PRESSED ||
  46. event.type() == ui::ET_GESTURE_LONG_PRESS ||
  47. event.type() == ui::ET_GESTURE_TAP ||
  48. event.type() == ui::ET_GESTURE_TWO_FINGER_TAP)
  49. << event.type();
  50. // Users in a capture session may be trying to capture the bubble.
  51. if (capture_mode_util::IsCaptureModeActive())
  52. return false;
  53. aura::Window* target = static_cast<aura::Window*>(event.target());
  54. if (!target)
  55. return false;
  56. RootWindowController* root_controller =
  57. RootWindowController::ForWindow(target);
  58. if (!root_controller)
  59. return false;
  60. // Bubbles can spawn menus, so don't close for clicks inside menus.
  61. aura::Window* menu_container =
  62. root_controller->GetContainer(kShellWindowId_MenuContainer);
  63. if (menu_container->Contains(target))
  64. return false;
  65. // Taps on virtual keyboard should not close bubbles.
  66. aura::Window* keyboard_container =
  67. root_controller->GetContainer(kShellWindowId_VirtualKeyboardContainer);
  68. if (keyboard_container->Contains(target))
  69. return false;
  70. // Touch text selection controls should not close bubbles.
  71. // https://crbug.com/1165938
  72. aura::Window* settings_bubble_container =
  73. root_controller->GetContainer(kShellWindowId_SettingBubbleContainer);
  74. if (settings_bubble_container->Contains(target))
  75. return false;
  76. return true;
  77. }
  78. void ApplyStyle(views::Label* label, LabelStyle style) {
  79. label->SetAutoColorReadabilityEnabled(false);
  80. AshColorProvider::ContentLayerType text_color;
  81. switch (style) {
  82. case LabelStyle::kBadge:
  83. text_color = AshColorProvider::ContentLayerType::kTextColorPrimary;
  84. label->SetFontList(gfx::FontList({"Roboto"}, gfx::Font::NORMAL, 14,
  85. gfx::Font::Weight::MEDIUM));
  86. break;
  87. case LabelStyle::kBody:
  88. text_color = AshColorProvider::ContentLayerType::kTextColorPrimary;
  89. label->SetFontList(gfx::FontList({"Roboto"}, gfx::Font::NORMAL, 14,
  90. gfx::Font::Weight::NORMAL));
  91. break;
  92. case LabelStyle::kChipBody:
  93. text_color = AshColorProvider::ContentLayerType::kTextColorPrimary;
  94. label->SetFontList(gfx::FontList({"Roboto"}, gfx::Font::NORMAL, 10,
  95. gfx::Font::Weight::MEDIUM));
  96. break;
  97. case LabelStyle::kChipTitle:
  98. text_color = AshColorProvider::ContentLayerType::kTextColorPrimary;
  99. label->SetFontList(gfx::FontList({"Roboto"}, gfx::Font::NORMAL, 13,
  100. gfx::Font::Weight::NORMAL));
  101. break;
  102. case LabelStyle::kHeader:
  103. text_color = AshColorProvider::ContentLayerType::kTextColorPrimary;
  104. label->SetFontList(gfx::FontList({"Roboto"}, gfx::Font::NORMAL, 16,
  105. gfx::Font::Weight::MEDIUM));
  106. break;
  107. case LabelStyle::kSubtitle:
  108. text_color = AshColorProvider::ContentLayerType::kTextColorSecondary;
  109. label->SetFontList(gfx::FontList({"Roboto"}, gfx::Font::NORMAL, 12,
  110. gfx::Font::Weight::NORMAL));
  111. break;
  112. }
  113. label->SetEnabledColor(
  114. AshColorProvider::Get()->GetContentLayerColor(text_color));
  115. }
  116. std::unique_ptr<views::Label> CreateLabel(LabelStyle style,
  117. const std::u16string& text) {
  118. auto label = std::make_unique<LabelWithThemeChangedCallback>(
  119. text,
  120. /*theme_changed_callback=*/base::BindRepeating(
  121. [](LabelStyle style, views::Label* label) {
  122. ApplyStyle(label, style);
  123. },
  124. style));
  125. // Apply `style` to `label` manually in case the view is painted without ever
  126. // having being added to the view hierarchy. In such cases, the `label` will
  127. // not receive an `OnThemeChanged()` event. This occurs, for example, with
  128. // holding space drag images.
  129. ApplyStyle(label.get(), style);
  130. return label;
  131. }
  132. } // namespace bubble_utils
  133. } // namespace ash