Commit 49695d19 authored by David Black's avatar David Black Committed by Commit Bot

Create DialogPlateDelegate.

This is done in similar fashion to what was done with CaptionBar to
decouple the controller and event callbacks:
https://chromium-review.googlesource.com/c/chromium/src/+/1086508/6

Bug: b:80542452
Change-Id: I992d2f5d0ee63e4d28a383b4e4f7ceafeabfff80
Reviewed-on: https://chromium-review.googlesource.com/1088233Reviewed-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@{#565392}
parent 73c3c21a
......@@ -241,70 +241,6 @@ void AssistantController::OnCardPressed(const GURL& url) {
OpenUrl(url);
}
void AssistantController::OnDialogPlateActionPressed(const std::string& text) {
InputModality input_modality = assistant_interaction_model_.input_modality();
// When using keyboard input modality, pressing the dialog plate action is
// equivalent to a commit.
if (input_modality == InputModality::kKeyboard) {
OnDialogPlateContentsCommitted(text);
return;
}
DCHECK(assistant_);
// It should not be possible to press the dialog plate action when not using
// keyboard or voice input modality.
DCHECK(input_modality == InputModality::kVoice);
// When using voice input modality, pressing the dialog plate action will
// toggle the voice interaction state.
switch (assistant_interaction_model_.mic_state()) {
case MicState::kClosed:
assistant_->StartVoiceInteraction();
break;
case MicState::kOpen:
has_active_interaction_ = false;
assistant_->StopActiveInteraction();
break;
}
}
void AssistantController::OnDialogPlateContentsChanged(
const std::string& text) {
if (text.empty()) {
// Note: This does not open the mic. It only updates the input modality to
// voice so that we will show the mic icon in the UI.
assistant_interaction_model_.SetInputModality(InputModality::kVoice);
} else {
assistant_interaction_model_.SetInputModality(InputModality::kKeyboard);
assistant_interaction_model_.SetMicState(MicState::kClosed);
}
}
void AssistantController::OnDialogPlateContentsCommitted(
const std::string& text) {
// TODO(dmblack): Handle an empty text query more gracefully by showing a
// helpful message to the user. Currently we just reset state and pretend as
// if nothing happened.
if (text.empty()) {
assistant_interaction_model_.ClearInteraction();
assistant_interaction_model_.SetInputModality(InputModality::kVoice);
return;
}
assistant_interaction_model_.ClearInteraction();
assistant_interaction_model_.SetQuery(
std::make_unique<AssistantTextQuery>(text));
// Note: This does not open the mic. It only updates the input modality to
// voice so that we will show the mic icon in the UI.
assistant_interaction_model_.SetInputModality(InputModality::kVoice);
DCHECK(assistant_);
assistant_->SendTextQuery(text);
}
void AssistantController::OnHtmlResponse(const std::string& response) {
if (!has_active_interaction_)
return;
......@@ -389,6 +325,67 @@ void AssistantController::OnOpenUrlResponse(const GURL& url) {
OpenUrl(url);
}
void AssistantController::OnDialogPlateActionPressed(const std::string& text) {
InputModality input_modality = assistant_interaction_model_.input_modality();
// When using keyboard input modality, pressing the dialog plate action is
// equivalent to a commit.
if (input_modality == InputModality::kKeyboard) {
OnDialogPlateContentsCommitted(text);
return;
}
// It should not be possible to press the dialog plate action when not using
// keyboard or voice input modality.
DCHECK(input_modality == InputModality::kVoice);
// When using voice input modality, pressing the dialog plate action will
// toggle the voice interaction state.
switch (assistant_interaction_model_.mic_state()) {
case MicState::kClosed:
assistant_->StartVoiceInteraction();
break;
case MicState::kOpen:
has_active_interaction_ = false;
assistant_->StopActiveInteraction();
break;
}
}
void AssistantController::OnDialogPlateContentsChanged(
const std::string& text) {
if (text.empty()) {
// Note: This does not open the mic. It only updates the input modality to
// voice so that we will show the mic icon in the UI.
assistant_interaction_model_.SetInputModality(InputModality::kVoice);
} else {
assistant_interaction_model_.SetInputModality(InputModality::kKeyboard);
assistant_interaction_model_.SetMicState(MicState::kClosed);
}
}
void AssistantController::OnDialogPlateContentsCommitted(
const std::string& text) {
// TODO(dmblack): Handle an empty text query more gracefully by showing a
// helpful message to the user. Currently we just reset state and pretend as
// if nothing happened.
if (text.empty()) {
assistant_interaction_model_.ClearInteraction();
assistant_interaction_model_.SetInputModality(InputModality::kVoice);
return;
}
assistant_interaction_model_.ClearInteraction();
assistant_interaction_model_.SetQuery(
std::make_unique<AssistantTextQuery>(text));
// Note: This does not open the mic. It only updates the input modality to
// voice so that we will show the mic icon in the UI.
assistant_interaction_model_.SetInputModality(InputModality::kVoice);
assistant_->SendTextQuery(text);
}
void AssistantController::OpenUrl(const GURL& url) {
Shell::Get()->shell_delegate()->OpenUrlFromArc(url);
StopInteraction();
......
......@@ -11,6 +11,7 @@
#include "ash/assistant/model/assistant_interaction_model.h"
#include "ash/assistant/model/assistant_interaction_model_observer.h"
#include "ash/assistant/ui/dialog_plate/dialog_plate.h"
#include "ash/highlighter/highlighter_controller.h"
#include "ash/public/interfaces/assistant_card_renderer.mojom.h"
#include "ash/public/interfaces/assistant_controller.mojom.h"
......@@ -34,7 +35,8 @@ class AssistantController
: public mojom::AssistantController,
public chromeos::assistant::mojom::AssistantEventSubscriber,
public AssistantInteractionModelObserver,
public HighlighterController::Observer {
public HighlighterController::Observer,
public DialogPlateDelegate {
public:
using AssistantSuggestion = chromeos::assistant::mojom::AssistantSuggestion;
using AssistantSuggestionPtr =
......@@ -86,15 +88,6 @@ class AssistantController
void StopInteraction();
void ToggleInteraction();
// Invoked on dialog plate action pressed event.
void OnDialogPlateActionPressed(const std::string& text);
// Invoked on dialog plate contents changed event.
void OnDialogPlateContentsChanged(const std::string& text);
// Invoked on dialog plate contents committed event.
void OnDialogPlateContentsCommitted(const std::string& text);
// Invoked on suggestion chip pressed event.
void OnSuggestionChipPressed(int id);
......@@ -133,6 +126,11 @@ class AssistantController
RequestScreenshotCallback callback) override;
void OnCardPressed(const GURL& url) override;
// DialogPlateDelegate:
void OnDialogPlateActionPressed(const std::string& text) override;
void OnDialogPlateContentsChanged(const std::string& text) override;
void OnDialogPlateContentsCommitted(const std::string& text) override;
AssistantBubbleController* bubble_controller() {
DCHECK(assistant_bubble_controller_);
return assistant_bubble_controller_.get();
......
......@@ -28,13 +28,17 @@ constexpr int kMaxHeightDip = 640;
AssistantMainView::AssistantMainView(AssistantController* assistant_controller)
: assistant_controller_(assistant_controller),
caption_bar_(new CaptionBar(assistant_controller->bubble_controller())),
caption_bar_(new CaptionBar()),
ui_element_container_(new UiElementContainerView(assistant_controller)),
suggestions_container_(new SuggestionContainerView(assistant_controller)),
dialog_plate_(new DialogPlate(assistant_controller)),
min_height_dip_(kMinHeightDip) {
InitLayout();
// Set delegates.
caption_bar_->set_delegate(assistant_controller_->bubble_controller());
dialog_plate_->set_delegate(assistant_controller_);
// Observe changes to interaction model.
assistant_controller_->AddInteractionModelObserver(this);
}
......
......@@ -48,7 +48,7 @@ class CaptionButton : public views::ImageButton {
// CaptionBar ------------------------------------------------------------------
CaptionBar::CaptionBar(CaptionBarDelegate* delegate) : delegate_(delegate) {
CaptionBar::CaptionBar() {
InitLayout();
}
......
......@@ -34,7 +34,7 @@ class CaptionBarDelegate {
class CaptionBar : public views::View, views::ButtonListener {
public:
explicit CaptionBar(CaptionBarDelegate* delegate);
CaptionBar();
~CaptionBar() override;
// views::View:
......@@ -44,10 +44,12 @@ class CaptionBar : public views::View, views::ButtonListener {
// views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override;
void set_delegate(CaptionBarDelegate* delegate) { delegate_ = delegate; }
private:
void InitLayout();
CaptionBarDelegate* const delegate_;
CaptionBarDelegate* delegate_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(CaptionBar);
};
......
......@@ -106,16 +106,16 @@ void DialogPlate::OnActionPressed() {
const base::StringPiece16& trimmed_text =
base::TrimWhitespace(textfield_->text(), base::TrimPositions::TRIM_ALL);
assistant_controller_->OnDialogPlateActionPressed(
base::UTF16ToUTF8(trimmed_text));
if (delegate_)
delegate_->OnDialogPlateActionPressed(base::UTF16ToUTF8(trimmed_text));
textfield_->SetText(base::string16());
}
void DialogPlate::ContentsChanged(views::Textfield* textfield,
const base::string16& new_contents) {
assistant_controller_->OnDialogPlateContentsChanged(
base::UTF16ToUTF8(new_contents));
if (delegate_)
delegate_->OnDialogPlateContentsChanged(base::UTF16ToUTF8(new_contents));
}
bool DialogPlate::HandleKeyEvent(views::Textfield* textfield,
......@@ -138,8 +138,8 @@ bool DialogPlate::HandleKeyEvent(views::Textfield* textfield,
const base::StringPiece16& trimmed_text =
base::TrimWhitespace(text, base::TrimPositions::TRIM_ALL);
assistant_controller_->OnDialogPlateContentsCommitted(
base::UTF16ToUTF8(trimmed_text));
if (delegate_)
delegate_->OnDialogPlateContentsCommitted(base::UTF16ToUTF8(trimmed_text));
textfield_->SetText(base::string16());
......
......@@ -5,6 +5,8 @@
#ifndef ASH_ASSISTANT_UI_DIALOG_PLATE_DIALOG_PLATE_H_
#define ASH_ASSISTANT_UI_DIALOG_PLATE_DIALOG_PLATE_H_
#include <string>
#include "ash/assistant/model/assistant_interaction_model_observer.h"
#include "ash/assistant/ui/dialog_plate/action_view.h"
#include "base/macros.h"
......@@ -15,6 +17,25 @@ namespace ash {
class AssistantController;
// DialogPlateDelegate ---------------------------------------------------------
class DialogPlateDelegate {
public:
// Invoked on dialog plate action pressed event.
virtual void OnDialogPlateActionPressed(const std::string& text) {}
// Invoked on dialog plate contents changed event.
virtual void OnDialogPlateContentsChanged(const std::string& text) {}
// Invoked on dialog plate contents committed event.
virtual void OnDialogPlateContentsCommitted(const std::string& text) {}
protected:
virtual ~DialogPlateDelegate() = default;
};
// DialogPlate -----------------------------------------------------------------
// DialogPlate is the child of AssistantMainView concerned with providing the
// means by which a user converses with Assistant. To this end, DialogPlate
// provides a textfield for use with the keyboard input modality, and an
......@@ -45,6 +66,8 @@ class DialogPlate : public views::View,
// AssistantInteractionModelObserver:
void OnInteractionStateChanged(InteractionState interaction_state) override;
void set_delegate(DialogPlateDelegate* delegate) { delegate_ = delegate; }
private:
void InitLayout();
void UpdateIcon();
......@@ -53,6 +76,8 @@ class DialogPlate : public views::View,
views::Textfield* textfield_; // Owned by view hierarchy.
ActionView* action_view_; // Owned by view hierarchy.
DialogPlateDelegate* delegate_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(DialogPlate);
};
......
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