tray_utils.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) 2012 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/system/tray/tray_utils.h"
  5. #include <string>
  6. #include "ash/bubble/bubble_constants.h"
  7. #include "ash/constants/ash_features.h"
  8. #include "ash/public/cpp/shelf_config.h"
  9. #include "ash/public/cpp/shelf_types.h"
  10. #include "ash/shelf/shelf.h"
  11. #include "ash/shell.h"
  12. #include "ash/strings/grit/ash_strings.h"
  13. #include "ash/style/ash_color_provider.h"
  14. #include "ash/system/tray/hover_highlight_view.h"
  15. #include "ash/system/tray/tray_constants.h"
  16. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  17. #include "base/check.h"
  18. #include "base/strings/string_number_conversions.h"
  19. #include "ui/base/l10n/l10n_util.h"
  20. #include "ui/gfx/font_list.h"
  21. #include "ui/views/controls/label.h"
  22. namespace ash {
  23. void SetupLabelForTray(views::Label* label) {
  24. // The text is drawn on an transparent bg, so we must disable subpixel
  25. // rendering.
  26. label->SetSubpixelRenderingEnabled(false);
  27. label->SetAutoColorReadabilityEnabled(false);
  28. label->SetFontList(gfx::FontList().Derive(
  29. kTrayTextFontSizeIncrease, gfx::Font::NORMAL, gfx::Font::Weight::MEDIUM));
  30. }
  31. void SetupConnectedScrollListItem(HoverHighlightView* view) {
  32. SetupConnectedScrollListItem(view, absl::nullopt /* battery_percentage */);
  33. }
  34. void SetupConnectedScrollListItem(HoverHighlightView* view,
  35. absl::optional<uint8_t> battery_percentage) {
  36. DCHECK(view->is_populated());
  37. std::u16string status;
  38. if (battery_percentage) {
  39. view->SetSubText(l10n_util::GetStringFUTF16(
  40. IDS_ASH_STATUS_TRAY_BLUETOOTH_DEVICE_CONNECTED_WITH_BATTERY_LABEL,
  41. base::NumberToString16(battery_percentage.value())));
  42. } else {
  43. view->SetSubText(l10n_util::GetStringUTF16(
  44. IDS_ASH_STATUS_TRAY_NETWORK_STATUS_CONNECTED));
  45. }
  46. view->sub_text_label()->SetAutoColorReadabilityEnabled(false);
  47. view->sub_text_label()->SetEnabledColor(
  48. AshColorProvider::Get()->GetContentLayerColor(
  49. AshColorProvider::ContentLayerType::kTextColorPositive));
  50. }
  51. void SetupConnectingScrollListItem(HoverHighlightView* view) {
  52. DCHECK(view->is_populated());
  53. view->SetSubText(
  54. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_NETWORK_STATUS_CONNECTING));
  55. }
  56. SkColor TrayIconColor(session_manager::SessionState session_state) {
  57. if (!features::IsDarkLightModeEnabled() &&
  58. session_state == session_manager::SessionState::OOBE) {
  59. return kIconColorInOobe;
  60. }
  61. return AshColorProvider::Get()->GetContentLayerColor(
  62. AshColorProvider::ContentLayerType::kIconColorPrimary);
  63. }
  64. gfx::Insets GetTrayBubbleInsets() {
  65. // Decrease bottom and right insets to compensate for the adjustment of
  66. // the respective edges in Shelf::GetSystemTrayAnchorRect().
  67. gfx::Insets insets = gfx::Insets::TLBR(
  68. kBubbleMenuPadding, kBubbleMenuPadding, kBubbleMenuPadding - 1,
  69. kBubbleMenuPadding - (base::i18n::IsRTL() ? 0 : 1));
  70. // The work area in tablet mode always uses the in-app shelf height, which is
  71. // shorter than the standard shelf height. In this state, we need to add back
  72. // the difference to compensate (see crbug.com/1033302).
  73. bool in_tablet_mode = Shell::Get()->tablet_mode_controller() &&
  74. Shell::Get()->tablet_mode_controller()->InTabletMode();
  75. if (!in_tablet_mode)
  76. return insets;
  77. Shelf* shelf = Shelf::ForWindow(Shell::GetPrimaryRootWindow());
  78. bool is_bottom_alignment =
  79. shelf->alignment() == ShelfAlignment::kBottom ||
  80. shelf->alignment() == ShelfAlignment::kBottomLocked;
  81. if (!is_bottom_alignment)
  82. return insets;
  83. int height_compensation = GetBubbleInsetHotseatCompensation();
  84. insets.set_bottom(insets.bottom() + height_compensation);
  85. return insets;
  86. }
  87. int GetBubbleInsetHotseatCompensation() {
  88. int height_compensation = kTrayBubbleInsetHotseatCompensation;
  89. Shelf* shelf = Shelf::ForWindow(Shell::GetPrimaryRootWindow());
  90. switch (shelf->GetBackgroundType()) {
  91. case ShelfBackgroundType::kInApp:
  92. case ShelfBackgroundType::kOverview:
  93. // Certain modes do not require a height compensation.
  94. height_compensation = 0;
  95. break;
  96. case ShelfBackgroundType::kLogin:
  97. // The hotseat is not visible on the lock screen, so we need a smaller
  98. // height compensation.
  99. height_compensation = kTrayBubbleInsetTabletModeCompensation;
  100. break;
  101. default:
  102. break;
  103. }
  104. return height_compensation;
  105. }
  106. gfx::Insets GetSecondaryBubbleInsets() {
  107. Shelf* shelf = Shelf::ForWindow(Shell::GetPrimaryRootWindow());
  108. gfx::Insets insets;
  109. switch (shelf->alignment()) {
  110. case ShelfAlignment::kBottom:
  111. case ShelfAlignment::kBottomLocked:
  112. insets.set_bottom(kBubbleMenuPadding);
  113. break;
  114. case ShelfAlignment::kLeft:
  115. insets.set_left(kBubbleMenuPadding);
  116. break;
  117. case ShelfAlignment::kRight:
  118. insets.set_right(kBubbleMenuPadding);
  119. break;
  120. }
  121. return insets;
  122. }
  123. gfx::Insets GetInkDropInsets(TrayPopupInkDropStyle ink_drop_style) {
  124. if (ink_drop_style == TrayPopupInkDropStyle::HOST_CENTERED ||
  125. ink_drop_style == TrayPopupInkDropStyle::INSET_BOUNDS) {
  126. return gfx::Insets(kTrayPopupInkDropInset);
  127. }
  128. return gfx::Insets();
  129. }
  130. } // namespace ash