File: calendar_event_list_view.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (371 lines) | stat: -rw-r--r-- 15,018 bytes parent folder | download | duplicates (7)
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
// Copyright 2021 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/time/calendar_event_list_view.h"

#include <memory>

#include "ash/bubble/bubble_constants.h"
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/ash_typography.h"
#include "ash/public/cpp/system_tray_client.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/style/icon_button.h"
#include "ash/style/pill_button.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/time/calendar_event_list_item_view.h"
#include "ash/system/time/calendar_metrics.h"
#include "ash/system/time/calendar_utils.h"
#include "ash/system/time/calendar_view_controller.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/constants/chromeos_features.h"
#include "google_apis/calendar/calendar_api_response_types.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/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/vector_icons.h"
#include "ui/views/view.h"

namespace ash {
namespace {

// The paddings in `close_button_container_`.
const auto kCloseButtonContainerInsets = gfx::Insets::VH(8, 16);

// The paddings in `CalendarEventListView`.
constexpr auto kContentInsets = gfx::Insets::TLBR(0, 16, 16, 16);

// The insets for `CalendarEmptyEventListView`.
constexpr auto kOpenGoogleCalendarContainerInsets = gfx::Insets::VH(20, 60);

// Border thickness for `CalendarEmptyEventListView`.
constexpr int kOpenGoogleCalendarBorderThickness = 1;

constexpr auto kEventListViewCornerRadius = gfx::RoundedCornersF(24);

constexpr int kScrollViewGradientSize = 16;

// The spacing between the child lists where we separate multi-day and non
// multi-day events into two separate child list views.
constexpr int kEventListViewBetweenChildSpacing = 8;

// The between child spacing within the child event lists.
constexpr int kChildEventListBetweenChildSpacing = 2;

}  // namespace

// A view that's displayed when the user selects a day cell from the calendar
// month view that has no events. Clicking on it opens Google calendar.
class CalendarEmptyEventListView : public PillButton {
  METADATA_HEADER(CalendarEmptyEventListView, PillButton)

 public:
  explicit CalendarEmptyEventListView(CalendarViewController* controller)
      : PillButton(views::Button::PressedCallback(base::BindRepeating(
                       &CalendarEmptyEventListView::OpenCalendarDefault,
                       base::Unretained(this))),
                   l10n_util::GetStringUTF16(IDS_ASH_CALENDAR_NO_EVENTS),
                   PillButton::Type::kSecondaryWithoutIcon,
                   /*icon=*/nullptr),
        controller_(controller) {
    SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_CENTER);

    SetBorder(views::CreateRoundedRectBorder(kOpenGoogleCalendarBorderThickness,
                                             GetPreferredSize().height() / 2,
                                             kColorAshHairlineBorderColor));
    SetTooltipText(
        l10n_util::GetStringUTF16(IDS_ASH_CALENDAR_NO_EVENT_BUTTON_TOOL_TIP));
  }
  CalendarEmptyEventListView(const CalendarEmptyEventListView& other) = delete;
  CalendarEmptyEventListView& operator=(
      const CalendarEmptyEventListView& other) = delete;
  ~CalendarEmptyEventListView() override = default;

  // Callback that's invoked when the user clicks on "Open in Google calendar"
  // in an empty event list.
  void OpenCalendarDefault() {
    controller_->OnCalendarEventWillLaunch();

    calendar_metrics::RecordCalendarLaunchedFromEmptyEventList();

    GURL finalized_url;
    bool opened_pwa = false;
    DCHECK(controller_->selected_date().has_value());

    // Open Google calendar and land on the local day/month/year.
    Shell::Get()->system_tray_model()->client()->ShowCalendarEvent(
        std::nullopt, controller_->selected_date_midnight(), opened_pwa,
        finalized_url);
  }

 private:
  // Owned by the parent view. Guaranteed to outlive this.
  const raw_ptr<CalendarViewController> controller_;
};

BEGIN_METADATA(CalendarEmptyEventListView)
END_METADATA

CalendarEventListView::CalendarEventListView(
    CalendarViewController* calendar_view_controller)
    : calendar_view_controller_(calendar_view_controller),
      close_button_container_(AddChildView(std::make_unique<views::View>())),
      scroll_view_(AddChildView(std::make_unique<views::ScrollView>())),
      content_view_(
          scroll_view_->SetContents(std::make_unique<views::View>())) {
  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical));

  SetPaintToLayer();
  layer()->SetFillsBoundsOpaquely(false);
  // Set the bottom corners to be rounded so that `CalendarEventListView` is
  // contained in `CalendarView`.
  layer()->SetRoundedCornerRadius(kEventListViewCornerRadius);

  views::BoxLayout* button_layout = close_button_container_->SetLayoutManager(
      std::make_unique<views::BoxLayout>(
          views::BoxLayout::Orientation::kHorizontal));
  button_layout->set_main_axis_alignment(
      views::BoxLayout::MainAxisAlignment::kEnd);
  close_button_container_->SetBorder(
      views::CreateEmptyBorder(kCloseButtonContainerInsets));

  close_button_ =
      close_button_container_->AddChildView(std::make_unique<IconButton>(
          views::Button::PressedCallback(
              base::BindRepeating(&CalendarViewController::CloseEventListView,
                                  base::Unretained(calendar_view_controller))),
          IconButton::Type::kMediumFloating, &views::kIcCloseIcon,
          IDS_ASH_CLOSE_BUTTON_ACCESSIBLE_DESCRIPTION));

  scroll_view_->SetAllowKeyboardScrolling(false);
  scroll_view_->SetBackgroundColor(std::nullopt);
  // Gives a min height so the background color can be filled to all the spaces
  // in the available expanded area.
  scroll_view_->ClipHeightTo(
      INT_MAX - close_button_container_->GetPreferredSize().height(), INT_MAX);
  scroll_view_->SetDrawOverflowIndicator(false);
  scroll_view_->SetVerticalScrollBarMode(
      views::ScrollView::ScrollBarMode::kHiddenButEnabled);

  // Set up fade in/fade out gradients at top/bottom of scroll view.
  scroll_view_->SetPaintToLayer(ui::LAYER_NOT_DRAWN);
  gradient_helper_ = std::make_unique<ScrollViewGradientHelper>(
      scroll_view_, kScrollViewGradientSize);

  content_view_->SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical, gfx::Insets(),
      kEventListViewBetweenChildSpacing));
  content_view_->SetBorder(views::CreateEmptyBorder(kContentInsets));

  UpdateListItems();

  scoped_calendar_view_controller_observer_.Observe(
      calendar_view_controller_.get());
  scoped_calendar_model_observer_.Observe(
      Shell::Get()->system_tray_model()->calendar_model());
}

CalendarEventListView::~CalendarEventListView() = default;

void CalendarEventListView::OnThemeChanged() {
  views::View::OnThemeChanged();
  SetBackground(views::CreateSolidBackground(
      GetColorProvider()->GetColor(cros_tokens::kCrosSysSystemOnBaseOpaque)));
}

void CalendarEventListView::Layout(PassKey) {
  LayoutSuperclass<views::View>(this);

  if (gradient_helper_) {
    gradient_helper_->UpdateGradientMask();
  }

  // If `CalendarEventListItemView` or the join button is focused, do not scroll
  // to the current or next event. Otherwise `scroll_view_` won't scroll with
  // the focus change.
  if (GetFocusManager() && GetFocusManager()->GetFocusedView()) {
    if (const auto focused_view_class_name =
            GetFocusManager()->GetFocusedView()->GetClassName();
        focused_view_class_name == CalendarEventListItemView::kViewClassName ||
        focused_view_class_name == PillButton::kViewClassName) {
      return;
    }
  }

  const std::optional<base::Time> selected_date =
      calendar_view_controller_->selected_date();

  // If the selected date is not today, do not auto scroll and reset the
  // `scroll_view_` position. Otherwise the previous position will be preserved.
  if (!calendar_utils::IsToday(selected_date.value())) {
    scroll_view_->ScrollToPosition(scroll_view_->vertical_scroll_bar(), 0);
    return;
  }

  // Scrolls to the top of `current_or_next_event_view_`. Ignores the multi-day
  // events on the top if exists.
  if (current_or_next_event_view_) {
    auto* multi_day_events_container =
        GetViewByID(kEventListMultiDayEventsContainer);

    scroll_view_->ScrollToPosition(
        scroll_view_->vertical_scroll_bar(),
        (multi_day_events_container
             ? multi_day_events_container->GetPreferredSize().height() +
                   kEventListViewBetweenChildSpacing
             : 0) +
            (current_or_next_event_view_->GetPreferredSize().height() +
             kChildEventListBetweenChildSpacing) *
                current_or_next_event_index_);
  } else {
    // If there's no current or next event because there's no single-day event
    // for today or all events have passed, scroll to the end of the list if
    // selected date is today.
    scroll_view_->ScrollToPosition(
        scroll_view_->vertical_scroll_bar(),
        scroll_view_->GetVisibleRect().bottom() + kContentInsets.bottom());
  }
}

void CalendarEventListView::RequestCloseButtonFocus() {
  close_button_->RequestFocus();
}

void CalendarEventListView::OnSelectedDateUpdated() {
  UpdateListItems();
}

void CalendarEventListView::OnEventsFetched(
    const CalendarModel::FetchingStatus status,
    const base::Time start_time) {
  if (status == CalendarModel::kSuccess &&
      start_time == calendar_utils::GetStartOfMonthUTC(
                        calendar_view_controller_->selected_date_midnight())) {
    UpdateListItems();
  }
}

std::unique_ptr<views::View> CalendarEventListView::CreateChildEventListView(
    std::list<google_apis::calendar::CalendarEvent> events,
    int parent_view_id) {
  auto container = std::make_unique<views::View>();
  container->SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical, gfx::Insets(),
      kChildEventListBetweenChildSpacing));
  container->SetID(parent_view_id);

  const int events_size = events.size();
  for (SingleDayEventList::iterator it = events.begin(); it != events.end();
       ++it) {
    const int event_index = std::distance(events.begin(), it) + 1;
    auto* event_list_item_view =
        container->AddChildView(std::make_unique<CalendarEventListItemView>(
            /*calendar_view_controller=*/calendar_view_controller_,
            /*selected_date_params=*/
            SelectedDateParams{
                calendar_view_controller_->selected_date().value(),
                calendar_view_controller_->selected_date_midnight(),
                calendar_view_controller_
                    ->selected_date_midnight_utc()}, /*event=*/
            *it,
            /*ui_params=*/
            UIParams{/*round_top_corners=*/it == events.begin(),
                     /*round_bottom_corners=*/it->id() == events.rbegin()->id(),
                     /*is_up_next_event_list_item=*/false,
                     /*show_event_list_dot=*/true,
                     /*fixed_width=*/0},
            /*event_list_item_index=*/
            EventListItemIndex{/*item_index=*/event_index,
                               /*total_count_of_events=*/events_size}));

    // The `current_or_next_event_view_` is the first event that is not an
    // all-day or multi-day event, and is the ongoing or the following event.
    if (!current_or_next_event_view_ &&
        event_list_item_view->is_current_or_next_single_day_event()) {
      current_or_next_event_view_ = event_list_item_view;
      current_or_next_event_index_ = event_index - 1;
    }
  }

  return container;
}

void CalendarEventListView::UpdateListItems() {
  // Resets `current_or_next_event_view_` and `current_or_next_event_index_`
  // since the `event_list_view_` has been updated. This has to be reset before
  // `RemoveAllChildViews()` is called otherwise it will become a dangling ptr.
  current_or_next_event_view_ = nullptr;
  current_or_next_event_index_ = 0;

  content_view_->RemoveAllChildViews();

  const auto [multi_day_events, all_other_events] =
      calendar_view_controller_->SelectedDateEventsSplitByMultiDayAndSameDay();

  // If we have some events to display, then add them to the `content_view_`
  // and early return (the following methods in `UpdateListItems` handle empty
  // state etc).
  if (!multi_day_events.empty()) {
    content_view_->AddChildView(CreateChildEventListView(
        multi_day_events, kEventListMultiDayEventsContainer));
  }
  if (!all_other_events.empty()) {
    content_view_->AddChildView(CreateChildEventListView(
        all_other_events, kEventListSameDayEventsContainer));
  }

  content_view_->InvalidateLayout();

  calendar_metrics::RecordEventListEventCount(multi_day_events.size() +
                                              all_other_events.size());

  if (!multi_day_events.empty() || !all_other_events.empty()) {
    return;
  }

  // Show "Open in Google calendar"
  auto empty_list_view_container = std::make_unique<views::View>();
  empty_list_view_container->SetLayoutManager(
      std::make_unique<views::BoxLayout>(
          views::BoxLayout::Orientation::kVertical));
  CalendarEmptyEventListView* empty_button =
      empty_list_view_container->AddChildView(
          std::make_unique<CalendarEmptyEventListView>(
              calendar_view_controller_));

  // There is a corner case when user closes the event list view before this
  // line of code is executed. Then `selected_date_` is std::nullopt and
  // getting its value leads to a crash. Only set accessible name when
  // `selected_date_` has value, since if `event_list_view_` is closed, there'll
  // be no need to set the accessible name.
  if (!calendar_view_controller_->selected_date().has_value()) {
    return;
  }
  empty_button->GetViewAccessibility().SetName(l10n_util::GetStringFUTF16(
      IDS_ASH_CALENDAR_NO_EVENT_BUTTON_ACCESSIBLE_DESCRIPTION,
      calendar_utils::GetMonthNameAndDayOfMonth(
          calendar_view_controller_->selected_date().value())));
  empty_list_view_container->SetBorder(
      views::CreateEmptyBorder(kOpenGoogleCalendarContainerInsets));
  views::View* empty_list_view =
      content_view_->AddChildView(std::move(empty_list_view_container));

  // Needs to repaint the `content_view_`'s children.
  empty_list_view->InvalidateLayout();
}

BEGIN_METADATA(CalendarEventListView);
END_METADATA

}  // namespace ash