date_tray.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "ash/system/unified/date_tray.h"
  5. #include "ash/shell.h"
  6. #include "ash/strings/grit/ash_strings.h"
  7. #include "ash/system/model/clock_model.h"
  8. #include "ash/system/model/system_tray_model.h"
  9. #include "ash/system/time/time_tray_item_view.h"
  10. #include "ash/system/tray/tray_background_view.h"
  11. #include "ash/system/tray/tray_constants.h"
  12. #include "ash/system/tray/tray_container.h"
  13. #include "ash/system/unified/unified_system_tray.h"
  14. #include "ash/system/unified/unified_system_tray_model.h"
  15. #include "base/time/time.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. #include "ui/base/metadata/metadata_impl_macros.h"
  18. namespace ash {
  19. DateTray::DateTray(Shelf* shelf, UnifiedSystemTray* tray)
  20. : TrayBackgroundView(shelf, TrayBackgroundView::kStartRounded),
  21. time_view_(tray_container()->AddChildView(
  22. std::make_unique<TimeTrayItemView>(shelf, TimeView::Type::kDate))),
  23. unified_system_tray_(tray) {
  24. tray_container()->SetMargin(
  25. /*main_axis_margin=*/kUnifiedTrayContentPadding -
  26. ShelfConfig::Get()->status_area_hit_region_padding(),
  27. /*cross_axis_margin=*/0);
  28. scoped_unified_system_tray_observer_.Observe(unified_system_tray_);
  29. }
  30. DateTray::~DateTray() = default;
  31. bool DateTray::PerformAction(const ui::Event& event) {
  32. // Lets the `unified_system_tray_` decide whether to show the bubble or not,
  33. // since it's the owner of the bubble view.
  34. if (is_active()) {
  35. unified_system_tray_->CloseBubble();
  36. } else {
  37. // Need to set the date tray as active before notifying the system tray of
  38. // an action because we need the system tray to know that the date tray is
  39. // already active when it is creating the `UnifiedSystemTrayBubble`.
  40. SetIsActive(true);
  41. unified_system_tray_->OnDateTrayActionPerformed(event);
  42. }
  43. return true;
  44. }
  45. std::u16string DateTray::GetAccessibleNameForBubble() {
  46. if (unified_system_tray_->IsBubbleShown())
  47. return unified_system_tray_->GetAccessibleNameForQuickSettingsBubble();
  48. return GetAccessibleNameForTray();
  49. }
  50. void DateTray::HandleLocaleChange() {
  51. time_view_->HandleLocaleChange();
  52. }
  53. std::u16string DateTray::GetAccessibleNameForTray() {
  54. base::Time now = base::Time::Now();
  55. return l10n_util::GetStringFUTF16(
  56. IDS_ASH_DATE_TRAY_ACCESSIBLE_DESCRIPTION,
  57. base::TimeFormatFriendlyDate(now),
  58. base::TimeFormatTimeOfDayWithHourClockType(
  59. now, Shell::Get()->system_tray_model()->clock()->hour_clock_type(),
  60. base::kKeepAmPm));
  61. }
  62. void DateTray::UpdateLayout() {
  63. TrayBackgroundView::UpdateLayout();
  64. time_view_->UpdateAlignmentForShelf(shelf());
  65. }
  66. void DateTray::UpdateAfterLoginStatusChange() {
  67. SetVisiblePreferred(true);
  68. }
  69. void DateTray::OnOpeningCalendarView() {
  70. SetIsActive(true);
  71. }
  72. void DateTray::OnLeavingCalendarView() {
  73. SetIsActive(false);
  74. }
  75. BEGIN_METADATA(DateTray, ActionableView)
  76. END_METADATA
  77. } // namespace ash