File: mahi_question_answer_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (374 lines) | stat: -rw-r--r-- 15,494 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 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/mahi/mahi_question_answer_view.h"

#include <memory>
#include <string>
#include <utility>

#include "ash/public/cpp/ash_view_ids.h"
#include "ash/public/cpp/style/color_provider.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/icon_button.h"
#include "ash/style/system_textfield.h"
#include "ash/style/typography.h"
#include "ash/system/mahi/mahi_animation_utils.h"
#include "ash/system/mahi/mahi_constants.h"
#include "ash/system/mahi/mahi_ui_controller.h"
#include "ash/system/mahi/mahi_ui_update.h"
#include "ash/system/mahi/mahi_utils.h"
#include "ash/system/mahi/resources/grit/mahi_resources.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "chromeos/components/mahi/public/cpp/mahi_manager.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/vector_icons/vector_icons.h"
#include "ui/base/l10n/l10n_util.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/compositor/layer.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/animated_image_view.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/flex_layout_types.h"
#include "ui/views/layout/flex_layout_view.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/metadata/view_factory.h"
#include "ui/views/view.h"

namespace ash {

namespace {

// Constants -------------------------------------------------------------------

// ErrorBubble
constexpr auto kErrorBubbleInteriorMargin = gfx::Insets::TLBR(/*top=*/4,
                                                              /*left=*/4,
                                                              /*bottom=*/0,
                                                              /*right=*/8);
constexpr int kErrorIconSize = 16;
constexpr auto kErrorLabelInteriorMargin =
    gfx::Insets::TLBR(/*top=*/0, /*left=*/8, /*bottom=*/0, /*right=*/0);
constexpr int kErrorLabelMaximumWidth = 276;

// MahiQuestionAnswerView
constexpr gfx::Insets kQuestionAnswerInteriorMargin(/*all=*/8);
constexpr auto kTextBubbleInteriorMargin =
    gfx::Insets::VH(/*vertical=*/8, /*horizontal=*/12);
constexpr int kBetweenChildSpacing = 8;
constexpr int kTextBubbleCornerRadius = 12;

constexpr int kTextBubbleLabelDefaultMaximumWidth =
    mahi_constants::kScrollViewDefaultWidth -
    kQuestionAnswerInteriorMargin.width() - kTextBubbleInteriorMargin.width();

// ErrorBubble -----------------------------------------------------------------

// A bubble presenting the error introduced by answering a question.
class ErrorBubble : public views::FlexLayoutView {
  METADATA_HEADER(ErrorBubble, views::FlexLayoutView)
 public:
  explicit ErrorBubble(int error_text_id) {
    views::Builder<views::FlexLayoutView>(this)
        .SetBorder(views::CreateEmptyBorder(kErrorBubbleInteriorMargin))
        .SetOrientation(views::LayoutOrientation::kHorizontal)
        .AddChildren(
            views::Builder<views::ImageView>()
                .SetID(mahi_constants::ViewId::kQuestionAnswerErrorImage)
                .SetImage(ui::ImageModel::FromVectorIcon(
                    vector_icons::kErrorIcon, cros_tokens::kCrosSysSecondary,
                    kErrorIconSize)),
            views::Builder<views::Label>()
                .SetBorder(views::CreateEmptyBorder(kErrorLabelInteriorMargin))
                .SetFontList(TypographyProvider::Get()->ResolveTypographyToken(
                    TypographyToken::kCrosAnnotation1))
                .SetID(mahi_constants::ViewId::kQuestionAnswerErrorLabel)
                .SetMultiLine(true)
                .SetMaximumWidth(kErrorLabelMaximumWidth)
                .SetText(l10n_util::GetStringUTF16(error_text_id)))
        .BuildChildren();
  }
};

BEGIN_VIEW_BUILDER(ASH_EXPORT, ErrorBubble, views::FlexLayoutView)
END_VIEW_BUILDER

BEGIN_METADATA(ErrorBubble)
END_METADATA

// Creates a text bubble that will be populated with `text` and styled
// to be a question or answer based on `is_question`.
views::Builder<views::FlexLayoutView> CreateTextBubbleBuilder(
    const std::u16string& text,
    bool is_question) {
  return views::Builder<views::FlexLayoutView>()
      .SetInteriorMargin(kTextBubbleInteriorMargin)
      .SetBackground(views::CreateRoundedRectBackground(
          is_question ? cros_tokens::kCrosSysSystemPrimaryContainer
                      : cros_tokens::kCrosSysSystemOnBase,
          gfx::RoundedCornersF(kTextBubbleCornerRadius)))
      .SetMainAxisAlignment(is_question ? views::LayoutAlignment::kEnd
                                        : views::LayoutAlignment::kStart)
      .CustomConfigure(base::BindOnce([](views::FlexLayoutView* layout) {
        layout->SetProperty(
            views::kFlexBehaviorKey,
            views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToZero,
                                     views::MaximumFlexSizeRule::kPreferred,
                                     /*adjust_height_for_width=*/true));
      }))
      .AddChildren(
          views::Builder<views::Label>()
              // Since every text bubble label has this ID, the view lookup will
              // only be performed from one parent above.
              .SetID(mahi_constants::ViewId::kQuestionAnswerTextBubbleLabel)
              .SetSelectable(true)
              .SetMultiLine(true)
              .CustomConfigure(base::BindOnce([](views::Label* label) {
                label->SetProperty(views::kFlexBehaviorKey,
                                   views::FlexSpecification(
                                       views::MinimumFlexSizeRule::kScaleToZero,
                                       views::MaximumFlexSizeRule::kPreferred,
                                       /*adjust_height_for_width=*/true));

                // TODO(crbug.com/40233803): Multiline label right now doesn't
                // work well with `FlexLayout`. The size constraint is not
                // passed down from the views tree in the first round of layout,
                // so we impose a maximum width constraint so that the first
                // layout handle the width and height constraint correctly.
                label->SetMaximumWidth(kTextBubbleLabelDefaultMaximumWidth);
              }))
              .SetText(text)
              .SetTooltipText(text)
              .SetHorizontalAlignment(is_question ? gfx::ALIGN_RIGHT
                                                  : gfx::ALIGN_LEFT)
              .SetEnabledColor(
                  is_question ? cros_tokens::kCrosSysSystemOnPrimaryContainer
                              : cros_tokens::kCrosSysOnSurface)
              .SetAutoColorReadabilityEnabled(false)
              .SetSubpixelRenderingEnabled(false)
              .SetFontList(TypographyProvider::Get()->ResolveTypographyToken(
                  TypographyToken::kCrosBody2)));
}

// Create a row within the `MahiQuestionAnswerView`, corresponding to a question
// or an answer.
std::unique_ptr<views::View> CreateQuestionAnswerRow(const std::u16string& text,
                                                     bool is_question) {
  views::Builder<views::FlexLayoutView> row_builder =
      views::Builder<views::FlexLayoutView>()
          .SetOrientation(views::LayoutOrientation::kHorizontal)
          .CustomConfigure(base::BindOnce([](views::FlexLayoutView* layout) {
            // TODO(crbug.com/377582486): When respecting size constraints, the
            // layout manager has no layout cache, so multiple levels of
            // FlexLayoutView nesting have performance issues here (see
            // crbug.com/40232718). Only needs to be disabled if resizing is
            // enabled.
            layout->SetLayoutManagerUseConstrainedSpace(
                !base::FeatureList::IsEnabled(
                    chromeos::features::kMahiPanelResizable));
          }));

  views::Builder<views::FlexLayoutView> spacer =
      views::Builder<views::FlexLayoutView>().CustomConfigure(
          base::BindOnce([](views::FlexLayoutView* layout) {
            layout->SetProperty(views::kFlexBehaviorKey,
                                views::FlexSpecification(
                                    views::LayoutOrientation::kHorizontal,
                                    views::MinimumFlexSizeRule::kScaleToZero,
                                    views::MaximumFlexSizeRule::kUnbounded,
                                    /*adjust_height_for_width=*/true));
          }));

  if (is_question) {
    // Add a `FlexLayoutView` that is stretched the remaining space to the
    // left of the text bubble.
    return std::move(row_builder)
        .AddChildren(spacer, CreateTextBubbleBuilder(text, is_question))
        .Build();
  }

  // Add a `FlexLayoutView` that is stretched the remaining space to the right
  // of the text bubble.
  return std::move(row_builder)
      .AddChildren(CreateTextBubbleBuilder(text, is_question), spacer)
      .Build();
}

}  // namespace

}  // namespace ash

DEFINE_VIEW_BUILDER(ASH_EXPORT, ash::ErrorBubble)

namespace ash {

// MahiQuestionAnswerView::QuestionCountReporter -------------------------------

MahiQuestionAnswerView::QuestionCountReporter::QuestionCountReporter() =
    default;

MahiQuestionAnswerView::QuestionCountReporter::~QuestionCountReporter() =
    default;

void MahiQuestionAnswerView::QuestionCountReporter::IncreaseQuestionCount() {
  ++question_count_;
}

void MahiQuestionAnswerView::QuestionCountReporter::ReportDataAndReset() {
  base::UmaHistogramCounts100(
      mahi_constants::kQuestionCountPerMahiSessionHistogramName,
      question_count_);
  question_count_ = 0;
}

// MahiQuestionAnswerView ------------------------------------------------------

MahiQuestionAnswerView::MahiQuestionAnswerView(MahiUiController* ui_controller)
    : MahiUiController::Delegate(ui_controller), ui_controller_(ui_controller) {
  CHECK(ui_controller);

  // TODO(crbug.com/377576663) add performance metrics for resizing.
  SetOrientation(views::LayoutOrientation::kVertical);
  SetInteriorMargin(kQuestionAnswerInteriorMargin);
  SetIgnoreDefaultMainAxisMargins(true);
  SetCollapseMargins(true);
  SetDefault(views::kMarginsKey, gfx::Insets::VH(kBetweenChildSpacing, 0));
  // TODO(crbug.com/377582486): When respecting size constraints, the
  // layout manager has no layout cache, so multiple levels of
  // FlexLayoutView nesting have performance issues here (see
  // crbug.com/40232718).
  // Only needs to be disabled if resizing is enabled.
  SetLayoutManagerUseConstrainedSpace(
      !base::FeatureList::IsEnabled(chromeos::features::kMahiPanelResizable));
}

MahiQuestionAnswerView::~MahiQuestionAnswerView() {
  question_count_reporter_.ReportDataAndReset();
}

views::View* MahiQuestionAnswerView::GetView() {
  return this;
}

bool MahiQuestionAnswerView::GetViewVisibility(VisibilityState state) const {
  switch (state) {
    case VisibilityState::kQuestionAndAnswer:
      return true;
    case VisibilityState::kError:
    case VisibilityState::kSummaryAndOutlinesAndElucidation:
      return false;
  }
}

void MahiQuestionAnswerView::OnUpdated(const MahiUiUpdate& update) {
  switch (update.type()) {
    case MahiUiUpdateType::kAnswerLoaded: {
      RemoveLoadingAnimatedImage();

      base::UmaHistogramTimes(
          mahi_constants::kAnswerLoadingTimeHistogramName,
          base::TimeTicks::Now() - answer_start_loading_time_);

      auto& answer = update.GetAnswer();

      AddChildView(CreateQuestionAnswerRow(answer, /*is_question=*/false));
      GetViewAccessibility().AnnounceText(answer);
      return;
    }
    case MahiUiUpdateType::kContentsRefreshInitiated:
      question_count_reporter_.ReportDataAndReset();
      RemoveAllChildViews();
      return;
    case MahiUiUpdateType::kErrorReceived: {
      RemoveLoadingAnimatedImage();

      // Creates `error_bubble_` if having an error.
      const MahiUiError& error = update.GetError();
      if (error.origin_state == VisibilityState::kQuestionAndAnswer) {
        AddChildView(
            views::Builder<ErrorBubble>(
                std::make_unique<ErrorBubble>(
                    mahi_utils::GetErrorStatusViewTextId(error.status)))
                .Build());
      }
      return;
    }
    case MahiUiUpdateType::kQuestionPosted: {
      question_count_reporter_.IncreaseQuestionCount();
      AddChildView(CreateQuestionAnswerRow(update.GetQuestion(),
                                           /*is_question=*/true));
      if (answer_loading_animated_image_) {
        LOG(ERROR) << "Loading animated image shouldn't be running when a "
                      "question can be asked";
        return;
      }

      auto* answer_loading_animated_image = AddChildView(
          views::Builder<views::AnimatedImageView>()
              .SetID(mahi_constants::ViewId::kAnswerLoadingAnimatedImage)
              .SetAccessibleName(l10n_util::GetStringUTF16(
                  IDS_ASH_MAHI_LOADING_ACCESSIBLE_NAME))
              .SetAnimatedImage(mahi_animation_utils::GetLottieAnimationData(
                  IDR_MAHI_LOADING_SUMMARY_ANIMATION))
              .SetHorizontalAlignment(views::ImageViewBase::Alignment::kLeading)
              .AfterBuild(base::BindOnce([](views::AnimatedImageView* self) {
                self->Play(mahi_animation_utils::GetLottiePlaybackConfig(
                    *self->animated_image()->skottie(),
                    IDR_MAHI_LOADING_SUMMARY_ANIMATION));
              }))
              .Build());

      answer_loading_animated_image_.SetView(answer_loading_animated_image);

      answer_start_loading_time_ = base::TimeTicks::Now();

      return;
    }
    case MahiUiUpdateType::kQuestionReAsked: {
      const MahiQuestionParams& question_params =
          update.GetReAskQuestionParams();
      ui_controller_->SendQuestion(question_params.question,
                                   question_params.current_panel_content,
                                   MahiUiController::QuestionSource::kRetry);
      return;
    }
    case MahiUiUpdateType::kOutlinesLoaded:
    case MahiUiUpdateType::kPanelBoundsChanged:
    case MahiUiUpdateType::kQuestionAndAnswerViewNavigated:
    case MahiUiUpdateType::kRefreshAvailabilityUpdated:
    case MahiUiUpdateType::kSummaryLoaded:
    case MahiUiUpdateType::kSummaryAndOutlinesSectionNavigated:
    case MahiUiUpdateType::kSummaryAndOutlinesReloaded:
    case MahiUiUpdateType::kElucidationRequested:
    case MahiUiUpdateType::kElucidationLoaded:
      return;
  }
}

void MahiQuestionAnswerView::RemoveLoadingAnimatedImage() {
  if (answer_loading_animated_image_) {
    RemoveChildViewT(answer_loading_animated_image_.view());
  }
}

BEGIN_METADATA(MahiQuestionAnswerView)
END_METADATA

}  // namespace ash