shelf_tooltip_bubble.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2018 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/shelf/shelf_tooltip_bubble.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/style/ash_color_provider.h"
  7. #include "ash/system/tray/tray_constants.h"
  8. #include "ash/wm/collision_detection/collision_detection_utils.h"
  9. #include "ui/aura/window.h"
  10. #include "ui/views/controls/label.h"
  11. #include "ui/views/layout/fill_layout.h"
  12. namespace ash {
  13. namespace {
  14. // Shelf item tooltip height.
  15. constexpr int kTooltipHeight = 24;
  16. // The maximum width of the tooltip bubble. Borrowed the value from
  17. // ash/tooltip/tooltip_controller.cc
  18. constexpr int kTooltipMaxWidth = 250;
  19. // Shelf item tooltip internal text margins.
  20. constexpr int kTooltipTopBottomMargin = 4;
  21. constexpr int kTooltipLeftRightMargin = 8;
  22. } // namespace
  23. ShelfTooltipBubble::ShelfTooltipBubble(views::View* anchor,
  24. ShelfAlignment alignment,
  25. const std::u16string& text)
  26. : ShelfBubble(anchor, alignment) {
  27. set_margins(
  28. gfx::Insets::VH(kTooltipTopBottomMargin, kTooltipLeftRightMargin));
  29. set_close_on_deactivate(false);
  30. SetCanActivate(false);
  31. set_accept_events(false);
  32. set_shadow(views::BubbleBorder::NO_SHADOW);
  33. SetLayoutManager(std::make_unique<views::FillLayout>());
  34. views::Label* label = new views::Label(text);
  35. label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  36. const auto* color_provider = AshColorProvider::Get();
  37. const bool is_dark_light_mode_enabled = features::IsDarkLightModeEnabled();
  38. const SkColor tooltip_background = color_provider->GetBaseLayerColor(
  39. is_dark_light_mode_enabled
  40. ? AshColorProvider::BaseLayerType::kInvertedTransparent80
  41. : AshColorProvider::BaseLayerType::kTransparent80);
  42. const SkColor tooltip_text = color_provider->GetContentLayerColor(
  43. is_dark_light_mode_enabled
  44. ? AshColorProvider::ContentLayerType::kInvertedTextColorPrimary
  45. : AshColorProvider::ContentLayerType::kTextColorPrimary);
  46. set_color(tooltip_background);
  47. label->SetEnabledColor(tooltip_text);
  48. label->SetBackgroundColor(tooltip_background);
  49. AddChildView(label);
  50. CreateBubble();
  51. CollisionDetectionUtils::IgnoreWindowForCollisionDetection(
  52. GetWidget()->GetNativeWindow());
  53. }
  54. gfx::Size ShelfTooltipBubble::CalculatePreferredSize() const {
  55. const gfx::Size size = BubbleDialogDelegateView::CalculatePreferredSize();
  56. const int kTooltipMinHeight = kTooltipHeight - 2 * kTooltipTopBottomMargin;
  57. return gfx::Size(std::min(size.width(), kTooltipMaxWidth),
  58. std::max(size.height(), kTooltipMinHeight));
  59. }
  60. bool ShelfTooltipBubble::ShouldCloseOnPressDown() {
  61. // Let the manager close us.
  62. return true;
  63. }
  64. bool ShelfTooltipBubble::ShouldCloseOnMouseExit() {
  65. return true;
  66. }
  67. } // namespace ash