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
ash / glanceables / classroom / glanceables_classroom_student_view.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/glanceables/classroom/glanceables_classroom_student_view.h"
#include <array>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "ash/glanceables/classroom/glanceables_classroom_client.h"
#include "ash/glanceables/classroom/glanceables_classroom_item_view.h"
#include "ash/glanceables/classroom/glanceables_classroom_types.h"
#include "ash/glanceables/common/glanceables_contents_scroll_view.h"
#include "ash/glanceables/common/glanceables_list_footer_view.h"
#include "ash/glanceables/common/glanceables_progress_bar_view.h"
#include "ash/glanceables/common/glanceables_view_id.h"
#include "ash/glanceables/glanceables_controller.h"
#include "ash/glanceables/glanceables_metrics.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/combobox.h"
#include "ash/style/error_message_toast.h"
#include "ash/style/typography.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/metrics/user_metrics.h"
#include "base/ranges/algorithm.h"
#include "base/time/time.h"
#include "base/types/cxx23_to_underlying.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "ui/accessibility/ax_enums.mojom-shared.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/combobox_model.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "url/gurl.h"
namespace ash {
namespace {
// Helps to map `combobox_view_` selected index to the corresponding
// `StudentAssignmentsListType` value.
constexpr std::array<StudentAssignmentsListType, 4>
kStudentAssignmentsListTypeOrdered = {
StudentAssignmentsListType::kAssigned,
StudentAssignmentsListType::kNoDueDate,
StudentAssignmentsListType::kMissing,
StudentAssignmentsListType::kDone};
constexpr auto kStudentAssignmentsListTypeToLabel =
base::MakeFixedFlatMap<StudentAssignmentsListType, int>(
{{StudentAssignmentsListType::kAssigned,
IDS_GLANCEABLES_CLASSROOM_STUDENT_DUE_SOON_LIST_NAME},
{StudentAssignmentsListType::kNoDueDate,
IDS_GLANCEABLES_CLASSROOM_STUDENT_NO_DUE_DATE_LIST_NAME},
{StudentAssignmentsListType::kMissing,
IDS_GLANCEABLES_CLASSROOM_STUDENT_MISSING_LIST_NAME},
{StudentAssignmentsListType::kDone,
IDS_GLANCEABLES_CLASSROOM_STUDENT_DONE_LIST_NAME}});
constexpr char kClassroomHomePage[] = "https://classroom.google.com/u/0/h";
constexpr char kClassroomWebUIAssignedUrl[] =
"https://classroom.google.com/u/0/a/not-turned-in/all";
constexpr char kClassroomWebUIMissingUrl[] =
"https://classroom.google.com/u/0/a/missing/all";
constexpr char kClassroomWebUIDoneUrl[] =
"https://classroom.google.com/u/0/a/turned-in/all";
constexpr char kLastSelectedAssignmentsListPref[] =
"ash.glanceables.classroom.student.last_selected_assignments_list";
constexpr char kExpandAnimationSmoothnessHistogramName[] =
"Ash.Glanceables.TimeManagement.Classroom.Expand.AnimationSmoothness";
constexpr char kCollapseAnimationSmoothnessHistogramName[] =
"Ash.Glanceables.TimeManagement.Classroom.Collapse.AnimationSmoothness";
constexpr size_t kMaxAssignments = 100;
constexpr auto kEmptyListLabelMargins = gfx::Insets::TLBR(24, 0, 32, 0);
constexpr auto kFooterMargins = gfx::Insets::TLBR(12, 2, 0, 0);
std::u16string GetAssignmentListName(size_t index) {
CHECK(index >= 0 || index < kStudentAssignmentsListTypeOrdered.size());
const auto iter = kStudentAssignmentsListTypeToLabel.find(
kStudentAssignmentsListTypeOrdered[index]);
CHECK(iter != kStudentAssignmentsListTypeToLabel.end());
return l10n_util::GetStringUTF16(iter->second);
}
class ClassroomStudentComboboxModel : public ui::ComboboxModel {
public:
ClassroomStudentComboboxModel() = default;
ClassroomStudentComboboxModel(const ClassroomStudentComboboxModel&) = delete;
ClassroomStudentComboboxModel& operator=(
const ClassroomStudentComboboxModel&) = delete;
~ClassroomStudentComboboxModel() override = default;
size_t GetItemCount() const override {
return kStudentAssignmentsListTypeOrdered.size();
}
std::u16string GetItemAt(size_t index) const override {
return GetAssignmentListName(index);
}
std::optional<size_t> GetDefaultIndex() const override {
const auto selected_list_type = static_cast<StudentAssignmentsListType>(
Shell::Get()->session_controller()->GetActivePrefService()->GetInteger(
kLastSelectedAssignmentsListPref));
const auto iter = base::ranges::find(kStudentAssignmentsListTypeOrdered,
selected_list_type);
return iter != kStudentAssignmentsListTypeOrdered.end()
? iter - kStudentAssignmentsListTypeOrdered.begin()
: 0;
}
};
GlanceablesClassroomStudentView::InitParams CreateInitParamsForClassroom() {
GlanceablesClassroomStudentView::InitParams init_params;
init_params.context = GlanceablesClassroomStudentView::Context::kClassroom;
init_params.combobox_model =
std::make_unique<ClassroomStudentComboboxModel>();
init_params.combobox_tooltip = l10n_util::GetStringUTF16(
IDS_GLANCEABLES_CLASSROOM_DROPDOWN_ACCESSIBLE_NAME);
init_params.expand_button_tooltip_id =
IDS_GLANCEABLES_CLASSROOM_EXPAND_BUTTON_EXPAND_TOOLTIP;
init_params.collapse_button_tooltip_id =
IDS_GLANCEABLES_CLASSROOM_EXPAND_BUTTON_COLLAPSE_TOOLTIP;
init_params.footer_title = l10n_util::GetStringUTF16(
IDS_GLANCEABLES_LIST_FOOTER_SEE_ALL_ASSIGNMENTS_LABEL);
init_params.footer_tooltip = l10n_util::GetStringUTF16(
IDS_GLANCEABLES_CLASSROOM_SEE_ALL_BUTTON_ACCESSIBLE_NAME);
init_params.header_icon = &kGlanceablesClassroomIcon;
init_params.header_icon_tooltip_id =
IDS_GLANCEABLES_CLASSROOM_HEADER_ICON_ACCESSIBLE_NAME;
return init_params;
}
} // namespace
GlanceablesClassroomStudentView::GlanceablesClassroomStudentView()
: GlanceablesTimeManagementBubbleView(CreateInitParamsForClassroom()),
shown_time_(base::Time::Now()) {
const auto* const typography_provider = TypographyProvider::Get();
empty_list_label_ = content_scroll_view()->contents()->AddChildView(
views::Builder<views::Label>()
.SetProperty(views::kMarginsKey, kEmptyListLabelMargins)
.SetEnabledColorId(cros_tokens::kCrosSysOnSurface)
.SetFontList(typography_provider->ResolveTypographyToken(
TypographyToken::kCrosButton2))
.SetLineHeight(typography_provider->ResolveLineHeight(
TypographyToken::kCrosButton2))
.SetID(base::to_underlying(
GlanceablesViewId::kClassroomBubbleEmptyListLabel))
.Build());
SelectedAssignmentListChanged(/*initial_update=*/true);
}
GlanceablesClassroomStudentView::~GlanceablesClassroomStudentView() {
if (list_shown_start_time_.has_value()) {
RecordStudentAssignmentListShowTime(
selected_list_type_,
base::TimeTicks::Now() - list_shown_start_time_.value(),
/*default_list=*/selected_list_change_count_ == 0);
}
if (first_assignment_list_shown_) {
RecordStudentSelectedListChangeCount(selected_list_change_count_);
}
RecordTotalShowTimeForClassroom(base::Time::Now() - shown_time_);
}
// static
void GlanceablesClassroomStudentView::RegisterUserProfilePrefs(
PrefRegistrySimple* registry) {
registry->RegisterIntegerPref(
kLastSelectedAssignmentsListPref,
base::to_underlying(StudentAssignmentsListType::kAssigned));
}
// static
void GlanceablesClassroomStudentView::ClearUserStatePrefs(
PrefService* pref_service) {
pref_service->ClearPref(kLastSelectedAssignmentsListPref);
}
void GlanceablesClassroomStudentView::CancelUpdates() {
weak_ptr_factory_.InvalidateWeakPtrs();
}
void GlanceablesClassroomStudentView::OnHeaderIconPressed() {
RecordClassroomHeaderIconPressed();
OpenUrl(GURL(kClassroomHomePage));
}
void GlanceablesClassroomStudentView::OnFooterButtonPressed() {
base::RecordAction(
base::UserMetricsAction("Glanceables_Classroom_SeeAllPressed"));
CHECK(combobox_view()->GetSelectedIndex());
switch (selected_list_type_) {
case StudentAssignmentsListType::kAssigned:
case StudentAssignmentsListType::kNoDueDate:
return OpenUrl(GURL(kClassroomWebUIAssignedUrl));
case StudentAssignmentsListType::kMissing:
return OpenUrl(GURL(kClassroomWebUIMissingUrl));
case StudentAssignmentsListType::kDone:
return OpenUrl(GURL(kClassroomWebUIDoneUrl));
}
}
void GlanceablesClassroomStudentView::SelectedListChanged() {
SelectedAssignmentListChanged(/*initial_update=*/false);
}
void GlanceablesClassroomStudentView::AnimateResize(
ResizeAnimation::Type resize_type) {
const int current_height = size().height();
if (current_height == 0) {
return;
}
resize_animation_.reset();
if (!ui::ScopedAnimationDurationScaleMode::duration_multiplier()) {
PreferredSizeChanged();
return;
}
// Check if the available height is large enough for the preferred height, so
// that the target height for the animation is correctly bounded.
const views::SizeBound available_height =
parent()->GetAvailableSize(this).height();
const int preferred_height = GetPreferredSize().height();
const int target_height =
available_height.is_bounded()
? std::min(available_height.value(), preferred_height)
: preferred_height;
if (current_height == target_height) {
return;
}
SetUpResizeThroughputTracker(target_height > current_height
? kExpandAnimationSmoothnessHistogramName
: kCollapseAnimationSmoothnessHistogramName);
resize_animation_ = std::make_unique<ResizeAnimation>(
current_height, target_height, this,
ResizeAnimation::Type::kContainerExpandStateChanged);
resize_animation_->Start();
}
void GlanceablesClassroomStudentView::OpenUrl(const GURL& url) const {
NewWindowDelegate::GetPrimary()->OpenUrl(
url, NewWindowDelegate::OpenUrlFrom::kUserInteraction,
NewWindowDelegate::Disposition::kNewForegroundTab);
}
void GlanceablesClassroomStudentView::OnItemViewPressed(
bool initial_list_selected,
const GURL& url) {
RecordStudentAssignmentPressed(/*default_list=*/initial_list_selected);
OpenUrl(url);
}
void GlanceablesClassroomStudentView::SelectedAssignmentListChanged(
bool initial_update) {
auto* const client =
Shell::Get()->glanceables_controller()->GetClassroomClient();
if (!client) {
// Hide this bubble when no classroom client exists.
SetVisible(false);
return;
}
const auto prev_selected_list_type = selected_list_type_;
const auto selected_index = GetComboboxSelectedIndex();
CHECK(selected_index >= 0 ||
selected_index < kStudentAssignmentsListTypeOrdered.size());
selected_list_type_ = kStudentAssignmentsListTypeOrdered[selected_index];
UpdateComboboxReplacementLabelText();
if (!initial_update) {
base::RecordAction(
base::UserMetricsAction("Glanceables_Classroom_SelectedListChanged"));
if (list_shown_start_time_.has_value()) {
RecordStudentAssignmentListShowTime(
prev_selected_list_type,
base::TimeTicks::Now() - list_shown_start_time_.value(),
/*default_list=*/selected_list_change_count_ == 0);
}
RecordStudentAssignmentListSelected(selected_list_type_);
selected_list_change_count_++;
}
list_shown_start_time_.reset();
Shell::Get()->session_controller()->GetActivePrefService()->SetInteger(
kLastSelectedAssignmentsListPref,
base::to_underlying(selected_list_type_));
// Cancel any old pending assignment requests.
CancelUpdates();
assignments_requested_time_ = base::TimeTicks::Now();
progress_bar()->UpdateProgressBarVisibility(/*visible=*/true);
combobox_view()->GetViewAccessibility().SetDescription(u"");
auto callback =
base::BindOnce(&GlanceablesClassroomStudentView::OnGetAssignments,
weak_ptr_factory_.GetWeakPtr(),
GetAssignmentListName(selected_index), initial_update);
switch (selected_list_type_) {
case StudentAssignmentsListType::kAssigned:
empty_list_label_->SetText(l10n_util::GetStringUTF16(
IDS_GLANCEABLES_CLASSROOM_STUDENT_EMPTY_ITEM_DUE_LIST));
return client->GetStudentAssignmentsWithApproachingDueDate(
std::move(callback));
case StudentAssignmentsListType::kNoDueDate:
empty_list_label_->SetText(l10n_util::GetStringUTF16(
IDS_GLANCEABLES_CLASSROOM_STUDENT_EMPTY_ITEM_DUE_LIST));
return client->GetStudentAssignmentsWithoutDueDate(std::move(callback));
case StudentAssignmentsListType::kMissing:
empty_list_label_->SetText(l10n_util::GetStringUTF16(
IDS_GLANCEABLES_CLASSROOM_STUDENT_EMPTY_ITEM_MISSING_LIST));
return client->GetStudentAssignmentsWithMissedDueDate(
std::move(callback));
case StudentAssignmentsListType::kDone:
empty_list_label_->SetText(l10n_util::GetStringUTF16(
IDS_GLANCEABLES_CLASSROOM_STUDENT_EMPTY_ITEM_DONE_LIST));
return client->GetCompletedStudentAssignments(std::move(callback));
}
}
void GlanceablesClassroomStudentView::OnGetAssignments(
const std::u16string& list_name,
bool initial_update,
bool success,
std::vector<std::unique_ptr<GlanceablesClassroomAssignment>> assignments) {
const gfx::Size old_preferred_size = GetPreferredSize();
progress_bar()->UpdateProgressBarVisibility(/*visible=*/false);
items_container_view()->RemoveAllChildViews();
total_assignments_ = assignments.size();
const size_t num_assignments = std::min(kMaxAssignments, assignments.size());
for (size_t i = 0; i < num_assignments; ++i) {
items_container_view()->AddChildView(
std::make_unique<GlanceablesClassroomItemView>(
assignments[i].get(),
base::BindRepeating(
&GlanceablesClassroomStudentView::OnItemViewPressed,
base::Unretained(this), initial_update, assignments[i]->link)));
}
const size_t shown_assignments = items_container_view()->children().size();
expand_button()->UpdateCounter(shown_assignments);
const bool is_list_empty = shown_assignments == 0;
empty_list_label_->SetVisible(is_list_empty);
bool should_show_footer_view;
should_show_footer_view = assignments.size() >= kMaxAssignments;
list_footer_view()->SetVisible(should_show_footer_view);
list_footer_view()->SetProperty(views::kMarginsKey, kFooterMargins);
items_container_view()->GetViewAccessibility().SetName(
l10n_util::GetStringFUTF16(
IDS_GLANCEABLES_CLASSROOM_SELECTED_LIST_ACCESSIBLE_NAME, list_name));
items_container_view()->NotifyAccessibilityEvent(
ax::mojom::Event::kChildrenChanged,
/*send_native_event=*/true);
if (old_preferred_size != GetPreferredSize()) {
PreferredSizeChanged();
if (!initial_update) {
GetWidget()->LayoutRootViewIfNecessary();
ScrollViewToVisible();
}
}
// Reset the position of the scroll view after the new data is fetched.
content_scroll_view()->ScrollToOffset(gfx::PointF(0, 0));
auto* controller = Shell::Get()->glanceables_controller();
if (initial_update) {
RecordClassromInitialLoadTime(
/*first_occurrence=*/controller->bubble_shown_count() == 1,
base::TimeTicks::Now() - controller->last_bubble_show_time());
} else {
RecordClassroomChangeLoadTime(
success, base::TimeTicks::Now() - assignments_requested_time_);
}
list_shown_start_time_ = base::TimeTicks::Now();
first_assignment_list_shown_ = true;
if (success) {
MaybeDismissErrorMessage();
} else {
ShowErrorMessage(
l10n_util::GetStringUTF16(IDS_GLANCEABLES_CLASSROOM_FETCH_ERROR),
base::BindRepeating(
&GlanceablesClassroomStudentView::MaybeDismissErrorMessage,
base::Unretained(this)),
ErrorMessageToast::ButtonActionType::kDismiss);
}
}
BEGIN_METADATA(GlanceablesClassroomStudentView)
END_METADATA
} // namespace ash