Skip to content

Commit

Permalink
Quick Answers V2: Update MainView logic
Browse files Browse the repository at this point in the history
- Use MainView as the button insead of the whole QuickAnswersView
- Migrate highlight logics to MainView

Bug: b/186906279
Test: Manual Test
Change-Id: Ifb705b50cc78fbceecd76e417dda14f48f19c9ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3044254
Commit-Queue: Yue Li <updowndota@chromium.org>
Reviewed-by: Tao Wu <wutao@chromium.org>
Reviewed-by: Xiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#905574}
  • Loading branch information
Yue Li authored and Chromium LUCI CQ committed Jul 27, 2021
1 parent 35908e9 commit 2a4be9d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 52 deletions.
95 changes: 55 additions & 40 deletions ash/quick_answers/ui/quick_answers_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,53 @@ View* AddHorizontalUiElements(
return labels_container;
}

class MainView : public views::Button {
public:
METADATA_HEADER(MainView);

MainView(PressedCallback callback) : Button(std::move(callback)) {
SetInstallFocusRingOnFocus(false);

// This is because waiting for mouse-release to fire buttons would be too
// late, since mouse-press dismisses the menu.
button_controller()->set_notify_action(
views::ButtonController::NotifyAction::kOnPress);
}

// Disallow copy and assign.
MainView(const MainView&) = delete;
MainView& operator=(const MainView&) = delete;

~MainView() override = default;

private:
// views::View:
void OnFocus() override { SetBackgroundState(true); }
void OnBlur() override { SetBackgroundState(false); }

// views::Button:
void StateChanged(views::Button::ButtonState old_state) override {
Button::StateChanged(old_state);
const bool hovered = GetState() == Button::STATE_HOVERED;
if (hovered || (GetState() == Button::STATE_NORMAL))
SetBackgroundState(hovered);
}

void SetBackgroundState(bool highlight) {
if (highlight) {
SetBackground(views::CreateBackgroundFromPainter(
views::Painter::CreateSolidRoundRectPainter(
SkColorSetA(SK_ColorBLACK, kHoverStateAlpha * 0xFF),
/*radius=*/0, kMainViewInsets)));
} else {
SetBackground(views::CreateSolidBackground(SK_ColorWHITE));
}
}
};

BEGIN_METADATA(MainView, views::Button)
END_METADATA

class ReportQueryView : public views::Button {
public:
METADATA_HEADER(ReportQueryView);
Expand Down Expand Up @@ -199,9 +246,7 @@ QuickAnswersView::QuickAnswersView(const gfx::Rect& anchor_view_bounds,
const std::string& title,
bool is_internal,
QuickAnswersUiController* controller)
: Button(base::BindRepeating(&QuickAnswersView::SendQuickAnswersQuery,
base::Unretained(this))),
anchor_view_bounds_(anchor_view_bounds),
: anchor_view_bounds_(anchor_view_bounds),
controller_(controller),
title_(title),
is_internal_(is_internal),
Expand All @@ -216,11 +261,6 @@ QuickAnswersView::QuickAnswersView(const gfx::Rect& anchor_view_bounds,

// Focus.
SetFocusBehavior(views::View::FocusBehavior::ALWAYS);
SetInstallFocusRingOnFocus(false);

// This is because waiting for mouse-release to fire buttons would be too
// late, since mouse-press dismisses the menu.
SetButtonNotifyActionToOnPress(this);

// Allow tooltips to be shown despite menu-controller owning capture.
GetWidget()->SetNativeWindowProperty(
Expand All @@ -235,7 +275,6 @@ const char* QuickAnswersView::GetClassName() const {
}

void QuickAnswersView::OnFocus() {
SetBackgroundState(true);
View* wants_focus = focus_search_->FindNextFocusableView(
nullptr, views::FocusSearch::SearchDirection::kForwards,
views::FocusSearch::TraversalDirection::kDown,
Expand All @@ -248,10 +287,6 @@ void QuickAnswersView::OnFocus() {
NotifyAccessibilityEvent(ax::mojom::Event::kFocus, true);
}

void QuickAnswersView::OnBlur() {
SetBackgroundState(false);
}

views::FocusTraversable* QuickAnswersView::GetPaneFocusTraversable() {
return focus_search_.get();
}
Expand All @@ -271,13 +306,6 @@ void QuickAnswersView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
l10n_util::GetStringUTF8(IDS_ASH_QUICK_ANSWERS_VIEW_A11Y_NAME_TEXT));
}

void QuickAnswersView::StateChanged(views::Button::ButtonState old_state) {
Button::StateChanged(old_state);
const bool hovered = GetState() == Button::STATE_HOVERED;
if (hovered || (GetState() == Button::STATE_NORMAL))
SetBackgroundState(hovered);
}

void QuickAnswersView::SendQuickAnswersQuery() {
controller_->OnQuickAnswersViewPressed();
}
Expand Down Expand Up @@ -324,7 +352,8 @@ void QuickAnswersView::ShowRetryView() {
l10n_util::GetStringUTF16(IDS_ASH_QUICK_ANSWERS_VIEW_RETRY)));
retry_label_->SetEnabledTextColors(gfx::kGoogleBlue600);
retry_label_->SetRequestFocusOnPress(true);
SetButtonNotifyActionToOnPress(retry_label_);
retry_label_->button_controller()->set_notify_action(
views::ButtonController::NotifyAction::kOnPress);
retry_label_->SetAccessibleName(l10n_util::GetStringFUTF16(
IDS_ASH_QUICK_ANSWERS_VIEW_A11Y_RETRY_LABEL_NAME_TEMPLATE,
l10n_util::GetStringUTF16(IDS_ASH_QUICK_ANSWERS_VIEW_A11Y_NAME_TEXT)));
Expand All @@ -343,7 +372,9 @@ void QuickAnswersView::InitLayout() {
base_layout->SetOrientation(views::LayoutOrientation::kVertical)
.SetCrossAxisAlignment(views::LayoutAlignment::kStretch);

main_view_ = base_view_->AddChildView(std::make_unique<View>());
main_view_ =
base_view_->AddChildView(std::make_unique<MainView>(base::BindRepeating(
&QuickAnswersView::SendQuickAnswersQuery, base::Unretained(this))));
auto* layout =
main_view_->SetLayoutManager(std::make_unique<views::FlexLayout>());
layout->SetOrientation(views::LayoutOrientation::kHorizontal)
Expand Down Expand Up @@ -429,7 +460,8 @@ void QuickAnswersView::AddDogfoodButton() {
kDogfoodButtonColor));
dogfood_button_->SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_QUICK_ANSWERS_DOGFOOD_BUTTON_TOOLTIP_TEXT));
SetButtonNotifyActionToOnPress(dogfood_button_);
dogfood_button_->button_controller()->set_notify_action(
views::ButtonController::NotifyAction::kOnPress);
}

void QuickAnswersView::AddSettingsButton() {
Expand Down Expand Up @@ -480,17 +512,6 @@ void QuickAnswersView::ResetContentView() {
first_answer_label_ = nullptr;
}

void QuickAnswersView::SetBackgroundState(bool highlight) {
if (highlight && !retry_label_) {
main_view_->SetBackground(views::CreateBackgroundFromPainter(
views::Painter::CreateSolidRoundRectPainter(
SkColorSetA(SK_ColorBLACK, kHoverStateAlpha * 0xFF),
/*radius=*/0, kMainViewInsets)));
} else if (!highlight) {
main_view_->SetBackground(views::CreateSolidBackground(SK_ColorWHITE));
}
}

void QuickAnswersView::UpdateBounds() {
int desired_width = anchor_view_bounds_.width();

Expand Down Expand Up @@ -575,12 +596,6 @@ void QuickAnswersView::UpdateQuickAnswerResult(
}
}

void QuickAnswersView::SetButtonNotifyActionToOnPress(views::Button* button) {
DCHECK(button);
button->button_controller()->set_notify_action(
views::ButtonController::NotifyAction::kOnPress);
}

std::vector<views::View*> QuickAnswersView::GetFocusableViews() {
std::vector<views::View*> focusable_views;
// The view itself does not gain focus for retry-view and transfers it to the
Expand Down
15 changes: 3 additions & 12 deletions ash/quick_answers/ui/quick_answers_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "ash/ash_export.h"
#include "ash/quick_answers/ui/quick_answers_focus_search.h"
#include "ui/events/event_handler.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/focus/focus_manager.h"

namespace chromeos {
Expand All @@ -31,27 +30,24 @@ class QuickAnswersUiController;
class QuickAnswersPreTargetHandler;

// A bubble style view to show QuickAnswer.
class ASH_EXPORT QuickAnswersView : public views::Button {
class ASH_EXPORT QuickAnswersView : public views::View {
public:
QuickAnswersView(const gfx::Rect& anchor_view_bounds,
const std::string& title,
bool is_internal,
QuickAnswersUiController* controller);
~QuickAnswersView() override;

QuickAnswersView(const QuickAnswersView&) = delete;
QuickAnswersView& operator=(const QuickAnswersView&) = delete;

~QuickAnswersView() override;

// views::View:
const char* GetClassName() const override;
void OnFocus() override;
void OnBlur() override;
views::FocusTraversable* GetPaneFocusTraversable() override;
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;

// views::Button:
void StateChanged(views::Button::ButtonState old_state) override;

// Called when a click happens to trigger Assistant Query.
void SendQuickAnswersQuery();

Expand All @@ -72,15 +68,10 @@ class ASH_EXPORT QuickAnswersView : public views::Button {
void AddAssistantIcon();
void AddGoogleIcon();
void ResetContentView();
void SetBackgroundState(bool highlight);
void UpdateBounds();
void UpdateQuickAnswerResult(
const chromeos::quick_answers::QuickAnswer& quick_answer);

// Buttons should fire on mouse-press instead of default behavior (waiting for
// mouse-release), since events of former type dismiss the accompanying menu.
void SetButtonNotifyActionToOnPress(views::Button* button);

// QuickAnswersFocusSearch::GetFocusableViewsCallback to poll currently
// focusable views.
std::vector<views::View*> GetFocusableViews();
Expand Down

0 comments on commit 2a4be9d

Please sign in to comment.