Commit 59d2b8a2 authored by David Black's avatar David Black Committed by Commit Bot

Handle suggestion action url.

Suggestions containing an action url should launch the url in the
browser. Otherwise we continue to use the suggestion text to submit
a simple query.

See bug for before/after demo.

Bug: b:79597028
Change-Id: I73b02c22e3fbdcef70f20a35100116ebd77261e9
Reviewed-on: https://chromium-review.googlesource.com/1056226Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Commit-Queue: David Black <dmblack@google.com>
Cr-Commit-Position: refs/heads/master@{#558456}
parent dfca5aad
......@@ -193,7 +193,22 @@ void AssistantController::OnHtmlResponse(const std::string& response) {
std::make_unique<AssistantCardElement>(response));
}
void AssistantController::OnSuggestionChipPressed(const std::string& text) {
void AssistantController::OnSuggestionChipPressed(int id) {
const AssistantSuggestion* suggestion =
assistant_interaction_model_.GetSuggestionById(id);
DCHECK(suggestion);
// If the suggestion contains a non-empty action url, we will handle the
// suggestion chip pressed event by launching the action url in the browser.
if (!suggestion->action_url.is_empty()) {
OnOpenUrlResponse(suggestion->action_url);
return;
}
// Otherwise, we will submit a simple text query using the suggestion text.
const std::string text = suggestion->text;
assistant_interaction_model_.ClearInteraction();
assistant_interaction_model_.SetQuery(
std::make_unique<AssistantTextQuery>(text));
......
......@@ -33,6 +33,7 @@ class AssistantController
public AssistantInteractionModelObserver,
public HighlighterController::Observer {
public:
using AssistantSuggestion = chromeos::assistant::mojom::AssistantSuggestion;
using AssistantSuggestionPtr =
chromeos::assistant::mojom::AssistantSuggestionPtr;
using AssistantInteractionResolution =
......@@ -82,7 +83,7 @@ class AssistantController
void OnDialogPlateContentsCommitted(const std::string& text);
// Invoked on suggestion chip pressed event.
void OnSuggestionChipPressed(const std::string& text);
void OnSuggestionChipPressed(int id);
// AssistantInteractionModelObserver:
void OnInteractionStateChanged(InteractionState interaction_state) override;
......
......@@ -85,18 +85,29 @@ void AssistantInteractionModel::ClearQuery() {
void AssistantInteractionModel::AddSuggestions(
std::vector<AssistantSuggestionPtr> suggestions) {
std::vector<AssistantSuggestion*> ptrs;
std::map<int, AssistantSuggestion*> ptrs;
// We use vector index to uniquely identify a given suggestion. This means
// that suggestion ids will reset with each call to |ClearSuggestions|, but
// that is acceptable.
for (AssistantSuggestionPtr& suggestion : suggestions) {
suggestions_list_.push_back(std::move(suggestion));
ptrs.push_back(suggestions_list_.back().get());
int id = suggestions_.size();
suggestions_.push_back(std::move(suggestion));
ptrs[id] = suggestions_.back().get();
}
NotifySuggestionsAdded(ptrs);
}
const AssistantInteractionModel::AssistantSuggestion*
AssistantInteractionModel::GetSuggestionById(int id) const {
return id >= 0 && id < static_cast<int>(suggestions_.size())
? suggestions_.at(id).get()
: nullptr;
}
void AssistantInteractionModel::ClearSuggestions() {
suggestions_list_.clear();
suggestions_.clear();
NotifySuggestionsCleared();
}
......@@ -137,7 +148,7 @@ void AssistantInteractionModel::NotifyQueryCleared() {
}
void AssistantInteractionModel::NotifySuggestionsAdded(
const std::vector<AssistantSuggestion*> suggestions) {
const std::map<int, AssistantSuggestion*>& suggestions) {
for (AssistantInteractionModelObserver& observer : observers_)
observer.OnSuggestionsAdded(suggestions);
}
......
......@@ -97,6 +97,10 @@ class AssistantInteractionModel {
// interaction.
void AddSuggestions(std::vector<AssistantSuggestionPtr> suggestions);
// Returns the suggestion uniquely identified by the specified |id|, or
// |nullptr| if no matching suggestion is found.
const AssistantSuggestion* GetSuggestionById(int id) const;
// Clears all suggestions for the interaction.
void ClearSuggestions();
......@@ -109,14 +113,14 @@ class AssistantInteractionModel {
void NotifyQueryChanged();
void NotifyQueryCleared();
void NotifySuggestionsAdded(
const std::vector<AssistantSuggestion*> suggestions);
const std::map<int, AssistantSuggestion*>& suggestions);
void NotifySuggestionsCleared();
InteractionState interaction_state_ = InteractionState::kInactive;
InputModality input_modality_;
MicState mic_state_ = MicState::kClosed;
std::unique_ptr<AssistantQuery> query_;
std::vector<AssistantSuggestionPtr> suggestions_list_;
std::vector<AssistantSuggestionPtr> suggestions_;
std::vector<std::unique_ptr<AssistantUiElement>> ui_element_list_;
base::ObserverList<AssistantInteractionModelObserver> observers_;
......
......@@ -5,6 +5,7 @@
#ifndef ASH_ASSISTANT_MODEL_ASSISTANT_INTERACTION_MODEL_OBSERVER_H_
#define ASH_ASSISTANT_MODEL_ASSISTANT_INTERACTION_MODEL_OBSERVER_H_
#include <map>
#include <string>
#include <vector>
......@@ -54,9 +55,11 @@ class AssistantInteractionModelObserver {
virtual void OnQueryCleared() {}
// Invoked when the specified |suggestions| are added to the associated
// interaction.
// interaction. The key for the map is the unique identifier by which the
// interaction model identifies each suggestion before the next
// |OnSuggestionsCleared| call.
virtual void OnSuggestionsAdded(
const std::vector<AssistantSuggestion*>& suggestions) {}
const std::map<int, AssistantSuggestion*>& suggestions) {}
// Invoked when all suggestions associated with the interaction are cleared.
virtual void OnSuggestionsCleared() {}
......
......@@ -363,10 +363,16 @@ class SuggestionsContainer : public views::View {
}
}
void AddSuggestions(const std::vector<AssistantSuggestion*>& suggestions) {
for (const AssistantSuggestion* suggestion : suggestions) {
AddChildView(new app_list::SuggestionChipView(
base::UTF8ToUTF16(suggestion->text), suggestion_chip_listener_));
void AddSuggestions(const std::map<int, AssistantSuggestion*>& suggestions) {
// When adding a SuggestionChipView, we give the view the same id by which
// the interaction model identifies the corresponding suggestion. This
// allows us to look up the suggestion for the view during event handling.
for (const std::pair<int, AssistantSuggestion*>& suggestion : suggestions) {
views::View* suggestion_chip_view = new app_list::SuggestionChipView(
base::UTF8ToUTF16(suggestion.second->text),
suggestion_chip_listener_);
suggestion_chip_view->set_id(suggestion.first);
AddChildView(suggestion_chip_view);
}
PreferredSizeChanged();
}
......@@ -580,7 +586,7 @@ void AssistantBubbleView::OnQueryCleared() {
}
void AssistantBubbleView::OnSuggestionsAdded(
const std::vector<AssistantSuggestion*>& suggestions) {
const std::map<int, AssistantSuggestion*>& suggestions) {
suggestions_container_->AddSuggestions(suggestions);
suggestions_container_->SetVisible(true);
}
......@@ -592,8 +598,7 @@ void AssistantBubbleView::OnSuggestionsCleared() {
void AssistantBubbleView::OnSuggestionChipPressed(
app_list::SuggestionChipView* suggestion_chip_view) {
assistant_controller_->OnSuggestionChipPressed(
base::UTF16ToUTF8(suggestion_chip_view->GetText()));
assistant_controller_->OnSuggestionChipPressed(suggestion_chip_view->id());
}
void AssistantBubbleView::OnTextAdded(
......
......@@ -55,7 +55,7 @@ class AssistantBubbleView : public views::View,
void OnQueryChanged(const AssistantQuery& query) override;
void OnQueryCleared() override;
void OnSuggestionsAdded(
const std::vector<AssistantSuggestion*>& suggestions) override;
const std::map<int, AssistantSuggestion*>& suggestions) override;
void OnSuggestionsCleared() override;
// app_list::SuggestionChipListener:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment