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
|
// 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.
#ifndef ASH_SYSTEM_MAHI_MAHI_UI_UPDATE_H_
#define ASH_SYSTEM_MAHI_MAHI_UI_UPDATE_H_
#include <functional>
#include <optional>
#include <string>
#include <variant>
#include <vector>
#include "ash/ash_export.h"
#include "ui/gfx/geometry/rect.h"
namespace chromeos {
struct MahiOutline;
enum class MahiResponseStatus;
} // namespace chromeos
namespace ash {
enum class VisibilityState {
// The state that shows the view displaying errors from user actions.
// NOTE: Not all errors should be displayed in this state.
kError,
// The state that shows the view displaying questions and answers.
kQuestionAndAnswer,
// The state that shows the view displaying summary or elucidation or outlines
// (not implemented yet).
kSummaryAndOutlinesAndElucidation,
};
enum class MahiUiUpdateType {
// An answer is loaded successfully.
kAnswerLoaded,
// A request to refresh the panel contents is initiated.
kContentsRefreshInitiated,
// An error is received.
kErrorReceived,
// Outlines are loaded successfully.
kOutlinesLoaded,
// Panel bounds have changed (i.e. due to a resize)
kPanelBoundsChanged,
// The question and answer view is requested to show.
kQuestionAndAnswerViewNavigated,
// A question is posted by user.
kQuestionPosted,
// A question is re-asked by user.
kQuestionReAsked,
// The content refresh availability changes.
kRefreshAvailabilityUpdated,
// The summary and outlines section is requested to show.
kSummaryAndOutlinesSectionNavigated,
// A summary is loaded with a success.
kSummaryLoaded,
// The summary and outlines are requested to reload.
kSummaryAndOutlinesReloaded,
// An elucidation for selected text is requested.
kElucidationRequested,
// An elucidation is loaded with a success.
kElucidationLoaded,
};
// Contains the params required to send a question to the Mahi backend.
struct MahiQuestionParams {
MahiQuestionParams(const std::u16string& question,
bool current_panel_content);
MahiQuestionParams(const MahiQuestionParams&) = delete;
MahiQuestionParams& operator=(const MahiQuestionParams&) = delete;
~MahiQuestionParams();
const std::u16string question;
// Determines if the `question` is regarding the current content displayed on
// the panel.
const bool current_panel_content;
};
// Describes a Mahi UI error, including its origin and status.
struct MahiUiError {
MahiUiError(chromeos::MahiResponseStatus status,
VisibilityState origin_state);
MahiUiError(const MahiUiError&) = delete;
MahiUiError& operator=(const MahiUiError&) = delete;
~MahiUiError();
// The error status. NOTE: `status` should not be
// `chromeos::MahiResponseStatus::kSuccess` or
// `chromeos::MahiResponseStatus::kLowQuota`.
const chromeos::MahiResponseStatus status;
// Indicates the `VisibilityState` where `status` comes from.
const VisibilityState origin_state;
};
// Indicates a change that triggers a visible update on the Mahi UI.
class ASH_EXPORT MahiUiUpdate {
public:
explicit MahiUiUpdate(MahiUiUpdateType type);
MahiUiUpdate(MahiUiUpdateType type, bool payload);
// NOTE: `MahiUiUpdate` caches the const reference to `payload`, not a copy.
// The class user has the duty to ensure the original `payload` object
// outlives the `MahiUiUpdate` instance.
MahiUiUpdate(MahiUiUpdateType type, const MahiUiError& payload);
MahiUiUpdate(MahiUiUpdateType type, const MahiQuestionParams& payload);
MahiUiUpdate(MahiUiUpdateType type, const std::u16string& payload);
MahiUiUpdate(MahiUiUpdateType type,
const std::vector<chromeos::MahiOutline>& payload);
MahiUiUpdate(MahiUiUpdateType type, const gfx::Rect& payload);
MahiUiUpdate(const MahiUiUpdate&) = delete;
MahiUiUpdate& operator=(const MahiUiUpdate&) = delete;
~MahiUiUpdate();
// Returns the answer from `payload`.
// NOTE: This function should be called only if `type` is `kAnswerLoaded`.
const std::u16string& GetAnswer() const;
// Returns the error from `payload`.
// NOTE: This function should be called only if `type` is `kErrorReceived`.
const MahiUiError& GetError() const;
// Returns the outlines from `payload`.
// NOTE: This function should be called only if `type` is `kOutlinesLoaded`.
const std::vector<chromeos::MahiOutline>& GetOutlines() const;
// Returns the outlines from `payload`.
// NOTE: This function should be called only if `type` is
// `kPanelBoundsChanged`.
const gfx::Rect& GetPanelBounds() const;
// Returns the question from `payload`.
// NOTE: This function should be called only if `type` is `kQuestionPosted`.
const std::u16string& GetQuestion() const;
// Returns the params required to re-ask a question.
// NOTE: This function should be called only if `type` is `kQuestionReAsked`.
const MahiQuestionParams& GetReAskQuestionParams() const;
// Returns the refresh availability from `payload`.
// NOTE: This function should be called only if `type` is
// `kRefreshAvailabilityUpdated`.
bool GetRefreshAvailability() const;
// Returns the summary from `payload`.
// NOTE: This function should be called only if `type` is `kSummaryLoaded`.
const std::u16string& GetSummary() const;
// Returns the elucidation/simplication from `payload`.
// NOTE: This function should be called only if `type` is
// `kElucidationLoaded`.
const std::u16string& GetElucidation() const;
MahiUiUpdateType type() const { return type_; }
private:
// Check that `type_` matches the actual type of `payload_`.
void CheckTypeMatchesPayload();
const MahiUiUpdateType type_;
// Provides more details on the update. Its actual data depends on `type`:
// For `kAnswerLoaded`, `payload` is an answer;
// For `kContentsRefreshInitiated`, `payload` is `std::nullopt`;
// For `kErrorReceived`, `payload` is an error;
// For `kOutlinesLoaded`, `payload` is an array of outlines;
// For `kQuestionAndAnswerViewNavigated`, `payload` is `std::nullopt`;
// For `kQuestionPosted`, `payload` is a question;
// For `kQuestionReAsked`, `payload` is a question params struct;
// For `kRefreshAvailabilityUpdated`, `payload` is a boolean;
// For `kSummaryAndOutlinesSectionNavigated`, `payload` is `std::nullopt`;
// For `kSummaryLoaded`, `payload` is a summary;
// For `kSummaryAndOutlinesReloaded`, `payload` is `std::nullopt`.
// For `kElucidationLoaded`, `payload` is an elucidation;
// For `kElucidationRequested`, `payload` is `std::nullopt`;
using PayloadType = std::variant<
std::reference_wrapper<const std::u16string>,
std::reference_wrapper<const MahiQuestionParams>,
std::reference_wrapper<const MahiUiError>,
std::reference_wrapper<const std::vector<chromeos::MahiOutline>>,
std::reference_wrapper<const gfx::Rect>,
bool>;
const std::optional<PayloadType> payload_;
};
} // namespace ash
#endif // ASH_SYSTEM_MAHI_MAHI_UI_UPDATE_H_
|