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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
ash / system / focus_mode / focus_mode_tray.cc [blame]
// Copyright 2023 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/focus_mode/focus_mode_tray.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/constants/tray_background_view_catalog.h"
#include "ash/glanceables/common/glanceables_util.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/typography.h"
#include "ash/system/focus_mode/focus_mode_controller.h"
#include "ash/system/focus_mode/focus_mode_countdown_view.h"
#include "ash/system/focus_mode/focus_mode_ending_moment_view.h"
#include "ash/system/focus_mode/focus_mode_session.h"
#include "ash/system/focus_mode/focus_mode_util.h"
#include "ash/system/progress_indicator/progress_indicator.h"
#include "ash/system/toast/anchored_nudge_manager_impl.h"
#include "ash/system/tray/tray_bubble_wrapper.h"
#include "ash/system/tray/tray_container.h"
#include "ash/system/tray/tray_utils.h"
#include "ash/wm/splitview/split_view_utils.h"
#include "base/check.h"
#include "chromeos/constants/chromeos_features.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/image_model.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_id.h"
#include "ui/compositor/layer.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/animation_builder.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout_view.h"
namespace ash {
namespace {
constexpr int kIconSize = 20;
constexpr int kBubbleInset = 16;
constexpr int kTaskItemViewInsets = 6;
constexpr int kTaskItemViewCornerRadius = 16;
constexpr int kProgressIndicatorThickness = 2;
constexpr auto kTaskTitleLabelInsets = gfx::Insets::TLBR(0, 12, 0, 18);
constexpr auto kProgressIndicatorInsets = gfx::Insets(-6);
constexpr base::TimeDelta kStartAnimationDelay = base::Milliseconds(300);
constexpr base::TimeDelta kTaskItemViewFadeOutDuration =
base::Milliseconds(200);
std::u16string GetAccessibleBubbleName(
const FocusModeSession::Snapshot& session_snapshot,
const std::u16string& task_title,
const size_t congratulatory_index) {
if (session_snapshot.state == FocusModeSession::State::kEnding) {
std::u16string title =
focus_mode_util::GetCongratulatoryTextAndEmoji(congratulatory_index);
std::u16string body = l10n_util::GetStringUTF16(
IDS_ASH_STATUS_TRAY_FOCUS_MODE_ENDING_MOMENT_BODY);
return l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_FOCUS_MODE_ENDING_MOMENT_DIALOG, title, body);
}
const std::u16string time_remaining =
focus_mode_util::GetDurationString(session_snapshot.remaining_time,
/*digital_format=*/false);
return task_title.empty()
? l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_FOCUS_MODE_TRAY_BUBBLE_ACCESSIBLE_NAME,
time_remaining)
: l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_FOCUS_MODE_TRAY_BUBBLE_TASK_ACCESSIBLE_NAME,
time_remaining, task_title);
}
} // namespace
class FocusModeTray::TaskItemView : public views::BoxLayoutView {
METADATA_HEADER(TaskItemView, views::BoxLayoutView)
public:
TaskItemView(const std::u16string& title, PressedCallback callback) {
SetBorder(views::CreateEmptyBorder(kTaskItemViewInsets));
// Set the background color is not opaque.
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
SetBackground(views::CreateThemedRoundedRectBackground(
cros_tokens::kCrosSysSystemOnBase, kTaskItemViewCornerRadius));
const bool is_network_connected = glanceables_util::IsNetworkConnected();
radio_button_ =
AddChildView(std::make_unique<views::ImageButton>(std::move(callback)));
radio_button_->SetImageModel(
views::Button::STATE_NORMAL,
ui::ImageModel::FromVectorIcon(kRadioButtonUncheckedIcon,
is_network_connected
? cros_tokens::kCrosSysPrimary
: cros_tokens::kCrosSysDisabled,
kIconSize));
const std::u16string radio_text = l10n_util::GetStringUTF16(
IDS_ASH_STATUS_TRAY_FOCUS_MODE_TASK_VIEW_RADIO_BUTTON);
views::ViewAccessibility& radio_button_view_a11y =
radio_button_->GetViewAccessibility();
radio_button_view_a11y.SetName(radio_text);
radio_button_view_a11y.SetDescription(title);
radio_button_->SetTooltipText(radio_text);
radio_button_->SetEnabled(is_network_connected);
task_title_ = AddChildView(std::make_unique<views::Label>());
TypographyProvider::Get()->StyleLabel(TypographyToken::kCrosButton2,
*task_title_);
task_title_->SetEnabledColorId(is_network_connected
? cros_tokens::kCrosSysOnSurface
: cros_tokens::kCrosSysDisabled);
task_title_->SetText(title);
task_title_->SetTooltipText(title);
task_title_->SetBorder(views::CreateEmptyBorder(kTaskTitleLabelInsets));
task_title_->SetEnabled(is_network_connected);
}
TaskItemView(const TaskItemView&) = delete;
TaskItemView& operator=(const TaskItemView&) = delete;
~TaskItemView() override {
radio_button_ = nullptr;
task_title_ = nullptr;
}
const views::ImageButton* GetRadioButton() const { return radio_button_; }
const views::Label* GetTaskTitle() const { return task_title_; }
bool GetWasCompleted() const { return was_completed_; }
void UpdateTitle(const std::u16string& title) {
radio_button_->GetViewAccessibility().SetDescription(title);
task_title_->SetText(title);
task_title_->SetTooltipText(title);
}
// Sets `radio_button_` as toggled which will update the button with a check
// icon, and adds a strike through on `task_title_`.
void UpdateStyleToCompleted() {
if (was_completed_) {
return;
}
was_completed_ = true;
radio_button_->SetImageModel(
views::Button::STATE_NORMAL,
ui::ImageModel::FromVectorIcon(kDoneIcon, cros_tokens::kCrosSysPrimary,
kIconSize));
task_title_->SetFontList(
TypographyProvider::Get()
->ResolveTypographyToken(TypographyToken::kCrosButton2)
.DeriveWithStyle(gfx::Font::FontStyle::STRIKE_THROUGH));
task_title_->SetEnabledColorId(cros_tokens::kCrosSysSecondary);
}
private:
bool was_completed_ = false;
raw_ptr<views::ImageButton> radio_button_ = nullptr;
raw_ptr<views::Label> task_title_ = nullptr;
};
BEGIN_METADATA(FocusModeTray, TaskItemView)
END_METADATA
FocusModeTray::FocusModeTray(Shelf* shelf)
: TrayBackgroundView(shelf,
TrayBackgroundViewCatalogName::kFocusMode,
RoundedCornerBehavior::kAllRounded),
image_view_(tray_container()->AddChildView(
std::make_unique<views::ImageView>())) {
SetCallback(base::BindRepeating(&FocusModeTray::FocusModeIconActivated,
weak_ptr_factory_.GetWeakPtr()));
image_view_->SetHorizontalAlignment(views::ImageView::Alignment::kCenter);
image_view_->SetVerticalAlignment(views::ImageView::Alignment::kCenter);
image_view_->SetPreferredSize(gfx::Size(kTrayItemSize, kTrayItemSize));
tray_container()->SetPaintToLayer();
tray_container()->layer()->SetFillsBoundsOpaquely(false);
progress_indicator_ =
ProgressIndicator::CreateDefaultInstance(base::BindRepeating(
[](FocusModeTray* view) -> std::optional<float> {
if (!view->visible_preferred() || view->is_active()) {
return 0.0f;
}
if (view->show_progress_ring_after_animation_) {
return ProgressIndicator::kForcedShow;
}
auto* controller = FocusModeController::Get();
// `kProgressComplete` is only returned by an ending moment, so that
// we can know when the pulse animation is done.
if (controller->in_ending_moment()) {
if (!view->bounce_in_animation_finished_) {
return ProgressIndicator::kForcedShow;
}
bool is_animating = false;
if (auto* progress_ring_animation =
view->progress_indicator_->animation_registry()
->GetProgressRingAnimationForKey(
view->progress_indicator_->animation_key())) {
is_animating = progress_ring_animation->IsAnimating();
}
// After the pulse animation, the ring isn't shown when the value
// is left at `kProgressComplete`, so we need to set it manually
// to `kForcedShow` to show the ring.
if (!is_animating && (view->progress_indicator_->progress() ==
ProgressIndicator::kProgressComplete)) {
view->show_progress_ring_after_animation_ = true;
return ProgressIndicator::kForcedShow;
}
return ProgressIndicator::kProgressComplete;
}
return controller->current_session()
->GetSnapshot(base::Time::Now())
.progress;
},
base::Unretained(this)));
progress_indicator_->SetInnerIconVisible(false);
progress_indicator_->SetInnerRingVisible(false);
progress_indicator_->SetOuterRingStrokeWidth(kProgressIndicatorThickness);
progress_indicator_->SetColorId(cros_tokens::kCrosRefPrimary70);
tray_container()->layer()->Add(
progress_indicator_->CreateLayer(base::BindRepeating(
[](TrayContainer* view, ui::ColorId color_id) {
return view->GetColorProvider()->GetColor(color_id);
},
base::Unretained(tray_container()))));
UpdateProgressRing();
auto* controller = FocusModeController::Get();
SetVisiblePreferred(controller->in_focus_session() ||
controller->in_ending_moment());
tasks_observation_.Observe(&controller->tasks_model());
controller->AddObserver(this);
UpdateAccessibleName();
}
FocusModeTray::~FocusModeTray() {
if (bubble_) {
bubble_->bubble_view()->ResetDelegate();
}
tasks_observation_.Reset();
FocusModeController::Get()->RemoveObserver(this);
}
void FocusModeTray::ClickedOutsideBubble(const ui::LocatedEvent& event) {
Shelf* target_shelf =
Shelf::ForWindow(static_cast<aura::Window*>(event.target()));
auto* target_tray = target_shelf->status_area_widget()->focus_mode_tray();
// Do not reset the focus session if the located event is on a different
// `FocusModeTray` view.
if (shelf() != target_shelf && target_tray->EventTargetsTray(event)) {
CloseBubbleAndMaybeReset(/*should_reset=*/false);
return;
}
CloseBubble();
}
std::u16string FocusModeTray::GetAccessibleNameForBubble() {
if (!session_snapshot_) {
return std::u16string();
}
auto* focus_mode_controller = FocusModeController::Get();
const FocusModeTask* selected_task =
focus_mode_controller->tasks_model().selected_task();
const std::u16string task_title =
selected_task ? base::UTF8ToUTF16(selected_task->title)
: std::u16string();
return GetAccessibleBubbleName(session_snapshot_.value(), task_title,
focus_mode_controller->congratulatory_index());
}
void FocusModeTray::HideBubbleWithView(const TrayBubbleView* bubble_view) {
if (bubble_->bubble_view() == bubble_view) {
CloseBubble();
}
}
void FocusModeTray::HideBubble(const TrayBubbleView* bubble_view) {
if (bubble_->bubble_view() == bubble_view) {
CloseBubble();
}
}
TrayBubbleView* FocusModeTray::GetBubbleView() {
return bubble_ ? bubble_->bubble_view() : nullptr;
}
void FocusModeTray::CloseBubbleInternal() {
CloseBubbleAndMaybeReset(/*should_reset=*/true);
}
void FocusModeTray::ShowBubble() {
if (bubble_) {
return;
}
auto* controller = FocusModeController::Get();
CHECK(controller->current_session());
auto bubble_view =
std::make_unique<TrayBubbleView>(CreateInitParamsForTrayBubble(
/*tray=*/this, /*anchor_to_shelf_corner=*/false));
bubble_view_container_ =
bubble_view->AddChildView(std::make_unique<views::BoxLayoutView>());
bubble_view_container_->SetOrientation(
views::BoxLayout::Orientation::kVertical);
bubble_view_container_->SetBorder(
views::CreateEmptyBorder(gfx::Insets(kBubbleInset)));
bubble_view_container_->SetBetweenChildSpacing(kBubbleInset);
countdown_view_ = bubble_view_container_->AddChildView(
std::make_unique<FocusModeCountdownView>(/*include_end_button=*/true));
ending_moment_view_ = bubble_view_container_->AddChildView(
std::make_unique<FocusModeEndingMomentView>());
session_snapshot_ =
controller->current_session()->GetSnapshot(base::Time::Now());
UpdateAccessibleName();
bubble_view->UpdateAccessibleName();
UpdateBubbleViews(session_snapshot_.value());
bubble_ = std::make_unique<TrayBubbleWrapper>(this);
bubble_->ShowBubble(std::move(bubble_view));
SetIsActive(true);
progress_indicator_->layer()->SetOpacity(0);
UpdateProgressRing();
controller->tasks_model().RequestUpdate();
if (session_snapshot_->state == FocusModeSession::State::kEnding) {
controller->OnEndingBubbleShown();
AnchoredNudgeManager::Get()->MaybeRecordNudgeAction(
NudgeCatalogName::kFocusModeEndingMomentNudge);
}
}
void FocusModeTray::UpdateTrayItemColor(bool is_active) {
UpdateTrayIcon();
}
void FocusModeTray::OnThemeChanged() {
TrayBackgroundView::OnThemeChanged();
UpdateTrayIcon();
}
void FocusModeTray::OnAnimationEnded() {
TrayBackgroundView::OnAnimationEnded();
// The bounce-in animation can happen on a session start or on an ending
// moment start. Only for the bounce-in animation during the ending moment, we
// will set `bounce_in_animation_finished_` to tell the progress callback the
// animation was ended.
auto* controller = FocusModeController::Get();
if (!visible_preferred() || !controller->in_ending_moment()) {
return;
}
bounce_in_animation_finished_ = true;
controller->MaybeShowEndingMomentNudge();
}
void FocusModeTray::OnFocusModeChanged(FocusModeSession::State session_state) {
UpdateProgressRing();
show_progress_ring_after_animation_ = false;
progress_ring_update_threshold_ = 0.0;
auto* focus_mode_controller = FocusModeController::Get();
auto current_session = focus_mode_controller->current_session();
if (!current_session) {
session_snapshot_.reset();
UpdateAccessibleName();
return;
}
session_snapshot_ = current_session->GetSnapshot(base::Time::Now());
UpdateAccessibleName();
if (bubble_) {
if (auto* bubble_view = bubble_->GetBubbleView()) {
bubble_view->UpdateAccessibleName();
}
UpdateBubbleViews(session_snapshot_.value());
} else if (session_snapshot_->state == FocusModeSession::State::kEnding) {
bounce_in_animation_finished_ = false;
MaybePlayBounceInAnimation();
}
}
void FocusModeTray::OnTimerTick(
const FocusModeSession::Snapshot& session_snapshot) {
session_snapshot_ = session_snapshot;
UpdateAccessibleName();
if (bubble_ && bubble_->GetBubbleView()) {
bubble_->GetBubbleView()->UpdateAccessibleName();
}
// We only paint the progress ring if it has reached the next threshold of
// progress. This is to try and decrease power usage of Focus mode when the
// user is idling and there are no required paints in the display.
if (session_snapshot_->progress >= progress_ring_update_threshold_) {
UpdateProgressRing();
// Change the next progress step into a percentage threshold.
progress_ring_update_threshold_ =
(double)focus_mode_util::GetNextProgressStep(
session_snapshot_->progress) /
focus_mode_util::kProgressIndicatorSteps;
}
MaybeUpdateCountdownViewUI(session_snapshot);
}
void FocusModeTray::OnActiveSessionDurationChanged(
const FocusModeSession::Snapshot& session_snapshot) {
session_snapshot_ = session_snapshot;
UpdateAccessibleName();
if (bubble_ && bubble_->GetBubbleView()) {
bubble_->GetBubbleView()->UpdateAccessibleName();
}
UpdateProgressRing();
progress_ring_update_threshold_ = 0.0;
MaybeUpdateCountdownViewUI(session_snapshot);
}
void FocusModeTray::OnSelectedTaskChanged(
const std::optional<FocusModeTask>& task) {
if (!bubble_) {
return;
}
// Task was either completed or cleared.
if (!task) {
selected_task_.reset();
if (!task_item_view_) {
// Task view is already gone. Nothing to do.
return;
}
if (task_item_view_->GetWasCompleted()) {
// Task was already completed and is in the process of being deleted.
return;
}
// Task was deleted.
OnClearTask();
return;
}
// A new task was picked or updated. Update the UI.
const std::string& task_title = task->title;
if (task_title.empty()) {
// Can't create a task view for an empty title.
return;
}
selected_task_ = task->task_id;
if (task_item_view_) {
// Assume that the title changed and try to update it.
task_item_view_->UpdateTitle(base::UTF8ToUTF16(task_title));
return;
}
CreateTaskItemView(task_title);
// We need to update the bubble after creating the `task_item_view_` so the
// widget bounds are updated and shows the view.
bubble_->bubble_view()->UpdateBubble();
}
void FocusModeTray::OnTasksUpdated(const std::vector<FocusModeTask>& tasks) {}
void FocusModeTray::OnTaskCompleted(const FocusModeTask& completed_task) {
// Initiate UI update to indicate that the task was completed.
if (!task_item_view_ || task_item_view_->GetWasCompleted()) {
return;
}
task_item_view_->UpdateStyleToCompleted();
OnClearTask();
}
void FocusModeTray::Layout(PassKey) {
LayoutSuperclass<views::View>(this);
// Position the progress indicator based on the position of the image view.
// The centered position inside of the tray container changes based on shelf
// orientation and tablet mode, but there is already logic to keep the image
// view centered that we can use.
gfx::Rect progress_bounds = gfx::Rect(views::View::ConvertRectToTarget(
/*source=*/image_view_,
/*target=*/tray_container(), image_view_->GetImageBounds()));
progress_bounds.Inset(kProgressIndicatorInsets);
progress_indicator_->layer()->SetBounds(progress_bounds);
}
void FocusModeTray::MaybePlayBounceInAnimation() {
if (bubble_ || !FocusModeController::Get()->in_ending_moment()) {
return;
}
BounceInAnimation(/*scale_animation=*/false);
}
const views::ImageButton* FocusModeTray::GetRadioButtonForTesting() const {
return task_item_view_->GetRadioButton();
}
const views::Label* FocusModeTray::GetTaskTitleForTesting() const {
return task_item_view_->GetTaskTitle();
}
void FocusModeTray::CreateTaskItemView(const std::string& task_title) {
if (task_title.empty()) {
return;
}
task_item_view_ =
bubble_view_container_->AddChildView(std::make_unique<TaskItemView>(
base::UTF8ToUTF16(task_title),
base::BindRepeating(&FocusModeTray::HandleCompleteTaskButton,
weak_ptr_factory_.GetWeakPtr())));
task_item_view_->SetProperty(views::kBoxLayoutFlexKey,
views::BoxLayoutFlexSpecification());
}
void FocusModeTray::UpdateTrayIcon() {
SkColor color = GetColorProvider()->GetColor(
is_active() ? cros_tokens::kCrosSysSystemOnPrimaryContainer
: cros_tokens::kCrosSysOnSurface);
image_view_->SetImage(CreateVectorIcon(kFocusModeLampIcon, color));
}
void FocusModeTray::FocusModeIconActivated(const ui::Event& event) {
if (bubble_ && bubble_->bubble_view()->GetVisible()) {
CloseBubble();
return;
}
ShowBubble();
}
void FocusModeTray::UpdateBubbleViews(
const FocusModeSession::Snapshot& session_snapshot) {
const bool is_ending_moment =
session_snapshot.state == FocusModeSession::State::kEnding;
countdown_view_->SetVisible(!is_ending_moment);
ending_moment_view_->SetVisible(is_ending_moment);
if (is_ending_moment) {
MaybeUpdateEndingMomentViewUI(session_snapshot);
} else {
MaybeUpdateCountdownViewUI(session_snapshot);
}
}
void FocusModeTray::MaybeUpdateCountdownViewUI(
const FocusModeSession::Snapshot& session_snapshot) {
if (countdown_view_ && countdown_view_->GetVisible() &&
session_snapshot.state == FocusModeSession::State::kOn) {
countdown_view_->UpdateUI(session_snapshot);
}
}
void FocusModeTray::MaybeUpdateEndingMomentViewUI(
const FocusModeSession::Snapshot& session_snapshot) {
if (ending_moment_view_ && ending_moment_view_->GetVisible()) {
ending_moment_view_->ShowEndingMomentContents(
FocusModeController::CanExtendSessionDuration(session_snapshot));
}
}
void FocusModeTray::HandleCompleteTaskButton() {
// The user clicked on the task complete button. Notify the model. UI updates
// happen in the model events.
if (!selected_task_.has_value()) {
// If there is no selected id, `OnClearTask()` should have been triggered
// already either by `OnTaskCompleted()` or `OnSelectedTaskChanged()`, so
// we can just return.
return;
}
FocusModeController::Get()->tasks_model().UpdateTask(
FocusModeTasksModel::TaskUpdate::CompletedUpdate(*selected_task_));
}
void FocusModeTray::OnClearTask() {
if (!selected_task_.has_value()) {
return;
}
selected_task_.reset();
if (!task_item_view_) {
return;
}
// We want to show the check icon and a strikethrough on the label for
// `kStartAnimationDelay` before removing `task_item_view_` from the
// bubble.
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&FocusModeTray::AnimateBubbleResize,
weak_ptr_factory_.GetWeakPtr()),
kStartAnimationDelay);
}
void FocusModeTray::OnBubbleResizeAnimationStarted() {
if (bubble_ && task_item_view_) {
auto* ptr = task_item_view_.get();
task_item_view_ = nullptr;
bubble_view_container_->RemoveChildViewT(ptr);
}
}
void FocusModeTray::OnBubbleResizeAnimationEnded() {
if (bubble_) {
bubble_->bubble_view()->UpdateBubble();
}
}
void FocusModeTray::AnimateBubbleResize() {
// If there is no `task_item_view_` or it has already been cleared, we should
// skip the animation.
if (!bubble_ || !task_item_view_) {
return;
}
// `remove_height` is the height of the `task_item_view_` and the spacing
// above it.
const int remove_height = task_item_view_->bounds().height() + kBubbleInset;
auto target_bounds = bubble_->bubble_view()->layer()->bounds();
target_bounds.Inset(gfx::Insets::TLBR(remove_height, 0, 0, 0));
views::AnimationBuilder()
.SetPreemptionStrategy(ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET)
.OnStarted(base::BindOnce(&FocusModeTray::OnBubbleResizeAnimationStarted,
weak_ptr_factory_.GetWeakPtr()))
.OnEnded(base::BindOnce(&FocusModeTray::OnBubbleResizeAnimationEnded,
weak_ptr_factory_.GetWeakPtr()))
.Once()
.SetDuration(kTaskItemViewFadeOutDuration)
.SetBounds(bubble_->bubble_view()->layer(), target_bounds,
gfx::Tween::EASE_OUT);
}
void FocusModeTray::UpdateProgressRing() {
// Schedule a repaint of the indicator.
progress_indicator_->InvalidateLayer();
}
bool FocusModeTray::EventTargetsTray(const ui::LocatedEvent& event) const {
if (event.target() != GetWidget()->GetNativeWindow()) {
return false;
}
gfx::Point location_in_status_area = event.location();
views::View::ConvertPointFromWidget(this, &location_in_status_area);
return bounds().Contains(location_in_status_area);
}
void FocusModeTray::CloseBubbleAndMaybeReset(bool should_reset) {
if (!bubble_) {
return;
}
if (auto* bubble_view = bubble_->GetBubbleView()) {
bubble_view->ResetDelegate();
}
bubble_.reset();
countdown_view_ = nullptr;
ending_moment_view_ = nullptr;
task_item_view_ = nullptr;
bubble_view_container_ = nullptr;
SetIsActive(false);
progress_indicator_->layer()->SetOpacity(1);
UpdateProgressRing();
if (auto* controller = FocusModeController::Get();
!controller->in_focus_session() && should_reset) {
controller->ResetFocusSession();
}
}
void FocusModeTray::UpdateAccessibleName() {
if (!session_snapshot_) {
GetViewAccessibility().RemoveName();
image_view_->SetCachedTooltipText(std::u16string());
return;
}
std::u16string name;
if (session_snapshot_->state == FocusModeSession::State::kEnding) {
name = focus_mode_util::GetCongratulatoryTextAndEmoji(
FocusModeController::Get()->congratulatory_index());
} else {
const std::u16string duration_string =
session_snapshot_->remaining_time < base::Minutes(1)
? l10n_util::GetStringUTF16(
IDS_ASH_STATUS_TRAY_FOCUS_MODE_SESSION_LESS_THAN_ONE_MINUTE)
: focus_mode_util::GetDurationString(
session_snapshot_->remaining_time,
/*digital_format=*/false);
name = l10n_util::GetStringFUTF16(
IDS_ASH_STATUS_TRAY_FOCUS_MODE_TRAY_BUBBLE_ACCESSIBLE_NAME,
duration_string);
}
GetViewAccessibility().SetName(name);
if (image_view_) {
image_view_->SetTooltipText(name);
}
}
BEGIN_METADATA(FocusModeTray)
END_METADATA
} // namespace ash