1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/unified/date_tray.h"
#include "ash/constants/tray_background_view_catalog.h"
#include "ash/glanceables/glanceables_controller.h"
#include "ash/public/cpp/ash_view_ids.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/model/clock_model.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/time/time_tray_item_view.h"
#include "ash/system/tray/tray_background_view.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_container.h"
#include "ash/system/unified/glanceable_tray_bubble.h"
#include "ash/system/unified/unified_system_tray.h"
#include "ash/system/unified/unified_system_tray_model.h"
#include "base/time/time.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/views/accessibility/view_accessibility.h"
namespace ash {
DateTray::DateTray(Shelf* shelf, UnifiedSystemTray* tray)
: TrayBackgroundView(shelf,
TrayBackgroundViewCatalogName::kDateTray,
TrayBackgroundView::kStartRounded),
time_tray_item_view_(tray_container()->AddChildView(
std::make_unique<TimeTrayItemView>(shelf, TimeView::Type::kDate))),
unified_system_tray_(tray) {
SetID(VIEW_ID_SA_DATE_TRAY);
SetCallback(
base::BindRepeating(&DateTray::OnButtonPressed, base::Unretained(this)));
SubscribeCallbacksForAccessibility();
tray_container()->SetMargin(
/*main_axis_margin=*/kUnifiedTrayContentPadding -
ShelfConfig::Get()->status_area_hit_region_padding(),
/*cross_axis_margin=*/0);
scoped_unified_system_tray_observer_.Observe(unified_system_tray_.get());
GetViewAccessibility().SetName(CalculateAccessibleName());
}
DateTray::~DateTray() = default;
std::u16string DateTray::GetAccessibleNameForBubble() {
if (unified_system_tray_->IsBubbleShown())
return unified_system_tray_->GetAccessibleNameForQuickSettingsBubble();
return CalculateAccessibleName();
}
void DateTray::HandleLocaleChange() {
time_tray_item_view_->HandleLocaleChange();
}
void DateTray::UpdateLayout() {
TrayBackgroundView::UpdateLayout();
time_tray_item_view_->UpdateAlignmentForShelf(shelf());
}
void DateTray::UpdateAfterLoginStatusChange() {
SetVisiblePreferred(true);
}
void DateTray::ShowBubble() {
// Never show System Tray bubble in kiosk app mode.
if (Shell::Get()->session_controller()->IsRunningInAppMode()) {
return;
}
GlanceablesController* const glanceables_controller =
ash::Shell::Get()->glanceables_controller();
if (glanceables_controller &&
glanceables_controller->AreGlanceablesAvailable()) {
ShowGlanceableBubble(/*from_keyboard=*/false);
}
}
void DateTray::CloseBubbleInternal() {
if (!is_active()) {
return;
}
if (bubble_) {
HideGlanceableBubble();
} else {
// Lets the `unified_system_tray_` close the bubble since it's the owner of
// the bubble view.
unified_system_tray_->CloseBubble();
}
}
void DateTray::HideBubbleWithView(const TrayBubbleView* bubble_view) {
if (bubble_ && bubble_->GetBubbleView() == bubble_view) {
CloseBubble();
}
}
void DateTray::HideBubble(const TrayBubbleView* bubble_view) {
CloseBubble();
}
void DateTray::ClickedOutsideBubble(const ui::LocatedEvent& event) {
if (bubble_) {
HideGlanceableBubble();
}
}
void DateTray::UpdateTrayItemColor(bool is_active) {
time_tray_item_view_->UpdateLabelOrImageViewColor(is_active);
}
void DateTray::OnOpeningCalendarView() {
SetIsActive(true);
}
void DateTray::OnLeavingCalendarView() {
SetIsActive(false);
}
std::u16string DateTray::CalculateAccessibleName() {
base::Time now = base::Time::Now();
return l10n_util::GetStringFUTF16(
IDS_ASH_DATE_TRAY_ACCESSIBLE_DESCRIPTION,
base::TimeFormatFriendlyDate(now),
base::TimeFormatTimeOfDayWithHourClockType(
now, Shell::Get()->system_tray_model()->clock()->hour_clock_type(),
base::kKeepAmPm));
}
void DateTray::OnButtonPressed(const ui::Event& event) {
// Lets the `unified_system_tray_` decide whether to show the bubble or not,
// since it's the owner of the bubble view.
if (is_active()) {
CloseBubble();
return;
}
GlanceablesController* const glanceables_controller =
ash::Shell::Get()->glanceables_controller();
if (glanceables_controller &&
glanceables_controller->AreGlanceablesAvailable()) {
// Hide the unified_system_tray_ bubble.
unified_system_tray_->CloseBubble();
// Open the glanceables bubble.
ShowGlanceableBubble(event.IsKeyEvent());
} else {
// Need to set the date tray as active before notifying the system tray of
// an action because we need the system tray to know that the date tray is
// already active when it is creating the `UnifiedSystemTrayBubble`.
SetIsActive(true);
unified_system_tray_->OnDateTrayActionPerformed(event);
}
}
void DateTray::OnTimeViewTextChanged(ax::mojom::StringAttribute attribute,
const std::optional<std::string>& name) {
GetViewAccessibility().SetName(CalculateAccessibleName());
}
void DateTray::ShowGlanceableBubble(bool from_keyboard) {
bubble_ = std::make_unique<GlanceableTrayBubble>(this, from_keyboard);
SetIsActive(true);
}
void DateTray::HideGlanceableBubble() {
bubble_.reset();
SetIsActive(false);
}
void DateTray::SubscribeCallbacksForAccessibility() {
time_view_text_changed_subscription_ =
time_tray_item_view_->time_view()
->GetViewAccessibility()
.AddStringAttributeChangedCallback(
ax::mojom::StringAttribute::kName,
base::BindRepeating(&DateTray::OnTimeViewTextChanged,
base::Unretained(this)));
}
BEGIN_METADATA(DateTray)
END_METADATA
} // namespace ash
|