Commit 5318ec14 authored by David Black's avatar David Black Committed by Commit Bot

Adds InteractionState to AssistantInteractionModel.

State is simplified to {kInactive, kActive} for now but will need more
granularity later on.

Bug: b:78913434
Change-Id: I558c4f2c9b2b266a621a36fc67ecd5af1d28eed6
Reviewed-on: https://chromium-review.googlesource.com/1036729Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Commit-Queue: David Black <dmblack@google.com>
Cr-Commit-Position: refs/heads/master@{#555283}
parent 096b5311
......@@ -24,9 +24,12 @@ AshAssistantController::AshAssistantController()
: assistant_controller_binding_(this),
assistant_event_subscriber_binding_(this),
assistant_bubble_(std::make_unique<AssistantBubble>(this)) {
AddInteractionModelObserver(this);
}
AshAssistantController::~AshAssistantController() {
RemoveInteractionModelObserver(this);
assistant_controller_binding_.Close();
assistant_event_subscriber_binding_.Close();
}
......@@ -95,32 +98,39 @@ void AshAssistantController::RemoveInteractionModelObserver(
void AshAssistantController::StartInteraction() {
// TODO(dmblack): Instruct underlying service to start listening if current
// input modality is VOICE. Also modify to check against state in
// AssistantInteractionModel when available.
if (!assistant_bubble_->IsShowing())
// input modality is VOICE.
if (assistant_interaction_model_.interaction_state() ==
InteractionState::kInactive) {
OnInteractionStarted();
}
}
void AshAssistantController::StopInteraction() {
// TODO(dmblack): Instruct underlying service to stop listening. Also modify
// to check against state in AssistantInteractionModel when available.
if (assistant_bubble_->IsShowing())
// TODO(dmblack): Instruct underlying service to stop listening.
if (assistant_interaction_model_.interaction_state() !=
InteractionState::kInactive) {
OnInteractionDismissed();
}
}
void AshAssistantController::ToggleInteraction() {
// TODO(dmblack): Add interaction state to AssistantInteractionModel. Bubble
// should observe interaction model state changes and show/hide itself
// appropriately.
if (!assistant_bubble_->IsShowing())
if (assistant_interaction_model_.interaction_state() ==
InteractionState::kInactive) {
StartInteraction();
else
} else {
StopInteraction();
}
}
void AshAssistantController::OnInteractionStateChanged(
InteractionState interaction_state) {
if (interaction_state == InteractionState::kInactive)
assistant_interaction_model_.ClearInteraction();
}
void AshAssistantController::OnInteractionStarted() {
assistant_bubble_timer_.Stop();
assistant_bubble_->Show();
assistant_interaction_model_.SetInteractionState(InteractionState::kActive);
}
void AshAssistantController::OnInteractionFinished(
......@@ -132,8 +142,7 @@ void AshAssistantController::OnInteractionFinished(
void AshAssistantController::OnInteractionDismissed() {
assistant_bubble_timer_.Stop();
assistant_bubble_->Dismiss();
assistant_interaction_model_.ClearInteraction();
assistant_interaction_model_.SetInteractionState(InteractionState::kInactive);
}
void AshAssistantController::OnHtmlResponse(const std::string& response) {
......
......@@ -10,6 +10,7 @@
#include <vector>
#include "ash/assistant/model/assistant_interaction_model.h"
#include "ash/assistant/model/assistant_interaction_model_observer.h"
#include "ash/public/interfaces/ash_assistant_controller.mojom.h"
#include "ash/public/interfaces/assistant_card_renderer.mojom.h"
#include "base/macros.h"
......@@ -28,7 +29,8 @@ class AssistantInteractionModelObserver;
class AshAssistantController
: public mojom::AshAssistantController,
public chromeos::assistant::mojom::AssistantEventSubscriber {
public chromeos::assistant::mojom::AssistantEventSubscriber,
public AssistantInteractionModelObserver {
public:
AshAssistantController();
~AshAssistantController() override;
......@@ -70,6 +72,9 @@ class AshAssistantController
// Invoked on suggestion chip pressed event.
void OnSuggestionChipPressed(const std::string& text);
// AssistantInteractionModelObserver:
void OnInteractionStateChanged(InteractionState interaction_state) override;
// chromeos::assistant::mojom::AssistantEventSubscriber:
void OnInteractionStarted() override;
void OnInteractionFinished(
......
......@@ -32,6 +32,15 @@ void AssistantInteractionModel::ClearInteraction() {
ClearSuggestions();
}
void AssistantInteractionModel::SetInteractionState(
InteractionState interaction_state) {
if (interaction_state == interaction_state_)
return;
interaction_state_ = interaction_state;
NotifyInteractionStateChanged();
}
void AssistantInteractionModel::SetInputModality(InputModality input_modality) {
if (input_modality == input_modality_)
return;
......@@ -74,6 +83,11 @@ void AssistantInteractionModel::ClearSuggestions() {
NotifySuggestionsCleared();
}
void AssistantInteractionModel::NotifyInteractionStateChanged() {
for (AssistantInteractionModelObserver& observer : observers_)
observer.OnInteractionStateChanged(interaction_state_);
}
void AssistantInteractionModel::NotifyInputModalityChanged() {
for (AssistantInteractionModelObserver& observer : observers_) {
observer.OnInputModalityChanged(input_modality_);
......
......@@ -24,6 +24,15 @@ enum class InputModality {
kVoice,
};
// TODO(dmblack): This is an oversimplification. We will eventually want to
// distinctly represent listening/thinking/etc. states explicitly so they can
// be adequately represented in the UI.
// Enumeration of interaction states.
enum class InteractionState {
kActive,
kInactive,
};
// Models the state of the query. For a text query, only the high confidence
// text portion will be populated. At start of a voice query, both the high and
// low confidence text portions will be empty. As speech recognition continues,
......@@ -54,6 +63,12 @@ class AssistantInteractionModel {
void AddObserver(AssistantInteractionModelObserver* observer);
void RemoveObserver(AssistantInteractionModelObserver* observer);
// Sets the interaction state.
void SetInteractionState(InteractionState interaction_state);
// Returns the interaction state.
InteractionState interaction_state() const { return interaction_state_; }
// Resets the interaction to its initial state.
void ClearInteraction();
......@@ -87,6 +102,7 @@ class AssistantInteractionModel {
void ClearSuggestions();
private:
void NotifyInteractionStateChanged();
void NotifyInputModalityChanged();
void NotifyUiElementAdded(const AssistantUiElement* ui_element);
void NotifyUiElementsCleared();
......@@ -95,6 +111,7 @@ class AssistantInteractionModel {
void NotifySuggestionsAdded(const std::vector<std::string>& suggestions);
void NotifySuggestionsCleared();
InteractionState interaction_state_ = InteractionState::kInactive;
InputModality input_modality_;
Query query_;
std::vector<std::string> suggestions_list_;
......
......@@ -14,12 +14,16 @@ namespace ash {
class AssistantUiElement;
enum class InputModality;
enum class InteractionState;
struct Query;
// An observer which receives notification of changes to an Assistant
// interaction.
class AssistantInteractionModelObserver {
public:
// Invoked when the interaction state is changed.
virtual void OnInteractionStateChanged(InteractionState interaction_state) {}
// Invoked when the input modality associated with the interaction is changed.
virtual void OnInputModalityChanged(InputModality input_modality) {}
......
......@@ -99,9 +99,13 @@ class AssistantContainerView : public views::BubbleDialogDelegateView {
// AssistantBubble -------------------------------------------------------------
AssistantBubble::AssistantBubble(AshAssistantController* assistant_controller)
: assistant_controller_(assistant_controller) {}
: assistant_controller_(assistant_controller) {
assistant_controller_->AddInteractionModelObserver(this);
}
AssistantBubble::~AssistantBubble() {
assistant_controller_->RemoveInteractionModelObserver(this);
if (container_view_)
container_view_->GetWidget()->RemoveObserver(this);
}
......@@ -111,6 +115,18 @@ void AssistantBubble::OnWidgetClosing(views::Widget* widget) {
container_view_ = nullptr;
}
void AssistantBubble::OnInteractionStateChanged(
InteractionState interaction_state) {
switch (interaction_state) {
case InteractionState::kActive:
Show();
break;
case InteractionState::kInactive:
Dismiss();
break;
}
}
void AssistantBubble::Show() {
if (!container_view_)
container_view_ = new AssistantContainerView(assistant_controller_);
......@@ -124,8 +140,4 @@ void AssistantBubble::Dismiss() {
container_view_->GetWidget()->Close();
}
bool AssistantBubble::IsShowing() const {
return container_view_ && container_view_->GetWidget()->IsVisible();
}
} // namespace ash
......@@ -5,6 +5,7 @@
#ifndef ASH_ASSISTANT_UI_ASSISTANT_BUBBLE_H_
#define ASH_ASSISTANT_UI_ASSISTANT_BUBBLE_H_
#include "ash/assistant/model/assistant_interaction_model_observer.h"
#include "base/macros.h"
#include "ui/views/widget/widget_observer.h"
......@@ -20,7 +21,8 @@ namespace {
class AssistantContainerView;
} // namespace
class AssistantBubble : public views::WidgetObserver {
class AssistantBubble : public views::WidgetObserver,
public AssistantInteractionModelObserver {
public:
explicit AssistantBubble(AshAssistantController* assistant_controller);
~AssistantBubble() override;
......@@ -28,12 +30,13 @@ class AssistantBubble : public views::WidgetObserver {
// views::WidgetObserver:
void OnWidgetClosing(views::Widget* widget) override;
// AssistantInteractionModelObserver:
void OnInteractionStateChanged(InteractionState interaction_state) override;
private:
void Show();
void Dismiss();
bool IsShowing() const;
private:
AshAssistantController* const assistant_controller_; // Owned by Shell.
// Owned by view hierarchy.
......
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