Commit 1890d5ff authored by Connie Wan's avatar Connie Wan Committed by Commit Bot

Allow Editor Bubble to take an anchor Rect (Editor Bubble refactor Part 2)

WebUI doesn't use TabGroupHeaders for tab groups, so Mohnstrudel needs another way to position the Editor Bubble. BubbleDialogDelegateView allows for an alternative way to set an anchor, which is to give it the anchor bounds as a Rect. This CL exposes that method of anchoring the bubble while still defaulting to using the TabGroupHeader in the desktop UI.

Bug: 1055537
Change-Id: I9d38971f867a362007abd626aa425515bef16af3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2071911
Commit-Queue: Connie Wan <connily@chromium.org>
Reviewed-by: default avatarCharlene Yan <cyan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#745542}
parent d8e981a9
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/metrics/user_metrics.h" #include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h" #include "base/metrics/user_metrics_action.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/optional.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
...@@ -48,11 +49,22 @@ ...@@ -48,11 +49,22 @@
// static // static
views::Widget* TabGroupEditorBubbleView::Show( views::Widget* TabGroupEditorBubbleView::Show(
TabGroupHeader* anchor_view,
const Browser* browser, const Browser* browser,
const tab_groups::TabGroupId& group) { const tab_groups::TabGroupId& group,
TabGroupHeader* anchor_view) {
views::Widget* const widget = BubbleDialogDelegateView::CreateBubble(
new TabGroupEditorBubbleView(browser, group, anchor_view, base::nullopt));
widget->Show();
return widget;
}
// static
views::Widget* TabGroupEditorBubbleView::ShowWithRect(
const Browser* browser,
const tab_groups::TabGroupId& group,
gfx::Rect anchor_rect) {
views::Widget* const widget = BubbleDialogDelegateView::CreateBubble( views::Widget* const widget = BubbleDialogDelegateView::CreateBubble(
new TabGroupEditorBubbleView(anchor_view, browser, group)); new TabGroupEditorBubbleView(browser, group, nullptr, anchor_rect));
widget->Show(); widget->Show();
return widget; return widget;
} }
...@@ -72,14 +84,22 @@ views::View* TabGroupEditorBubbleView::GetInitiallyFocusedView() { ...@@ -72,14 +84,22 @@ views::View* TabGroupEditorBubbleView::GetInitiallyFocusedView() {
} }
TabGroupEditorBubbleView::TabGroupEditorBubbleView( TabGroupEditorBubbleView::TabGroupEditorBubbleView(
TabGroupHeader* anchor_view,
const Browser* browser, const Browser* browser,
const tab_groups::TabGroupId& group) const tab_groups::TabGroupId& group,
TabGroupHeader* anchor_view,
base::Optional<gfx::Rect> anchor_rect)
: browser_(browser), : browser_(browser),
group_(group), group_(group),
title_field_controller_(this), title_field_controller_(this),
button_listener_(browser, anchor_view, group) { button_listener_(browser, group, anchor_view) {
SetAnchorView(anchor_view); // Either |anchor_view| or |anchor_rect| should be defined. |anchor_rect| is
// only used in situations were the available Views are different, e.g. WebUI.
DCHECK(anchor_view || anchor_rect.has_value());
if (anchor_view)
SetAnchorView(anchor_view);
else
SetAnchorRect(anchor_rect.value());
set_margins(gfx::Insets()); set_margins(gfx::Insets());
DialogDelegate::set_buttons(ui::DIALOG_BUTTON_NONE); DialogDelegate::set_buttons(ui::DIALOG_BUTTON_NONE);
...@@ -292,9 +312,9 @@ bool TabGroupEditorBubbleView::TitleFieldController::HandleKeyEvent( ...@@ -292,9 +312,9 @@ bool TabGroupEditorBubbleView::TitleFieldController::HandleKeyEvent(
TabGroupEditorBubbleView::ButtonListener::ButtonListener( TabGroupEditorBubbleView::ButtonListener::ButtonListener(
const Browser* browser, const Browser* browser,
TabGroupHeader* anchor_view, tab_groups::TabGroupId group,
tab_groups::TabGroupId group) TabGroupHeader* anchor_view)
: browser_(browser), anchor_view_(anchor_view), group_(group) {} : browser_(browser), group_(group), anchor_view_(anchor_view) {}
void TabGroupEditorBubbleView::ButtonListener::ButtonPressed( void TabGroupEditorBubbleView::ButtonListener::ButtonPressed(
views::Button* sender, views::Button* sender,
...@@ -312,7 +332,8 @@ void TabGroupEditorBubbleView::ButtonListener::ButtonPressed( ...@@ -312,7 +332,8 @@ void TabGroupEditorBubbleView::ButtonListener::ButtonPressed(
case TAB_GROUP_HEADER_CXMENU_UNGROUP: case TAB_GROUP_HEADER_CXMENU_UNGROUP:
base::RecordAction( base::RecordAction(
base::UserMetricsAction("TabGroups_TabGroupBubble_Ungroup")); base::UserMetricsAction("TabGroups_TabGroupBubble_Ungroup"));
anchor_view_->RemoveObserverFromWidget(sender->GetWidget()); if (anchor_view_)
anchor_view_->RemoveObserverFromWidget(sender->GetWidget());
model->RemoveFromGroup(tabs_in_group); model->RemoveFromGroup(tabs_in_group);
break; break;
case TAB_GROUP_HEADER_CXMENU_CLOSE_GROUP: case TAB_GROUP_HEADER_CXMENU_CLOSE_GROUP:
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_UI_VIEWS_TABS_TAB_GROUP_EDITOR_BUBBLE_VIEW_H_ #ifndef CHROME_BROWSER_UI_VIEWS_TABS_TAB_GROUP_EDITOR_BUBBLE_VIEW_H_
#define CHROME_BROWSER_UI_VIEWS_TABS_TAB_GROUP_EDITOR_BUBBLE_VIEW_H_ #define CHROME_BROWSER_UI_VIEWS_TABS_TAB_GROUP_EDITOR_BUBBLE_VIEW_H_
#include "base/optional.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "chrome/browser/ui/views/tabs/tab_group_header.h" #include "chrome/browser/ui/views/tabs/tab_group_header.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h" #include "ui/views/bubble/bubble_dialog_delegate_view.h"
...@@ -34,11 +35,19 @@ class TabGroupEditorBubbleView : public views::BubbleDialogDelegateView { ...@@ -34,11 +35,19 @@ class TabGroupEditorBubbleView : public views::BubbleDialogDelegateView {
static constexpr int TAB_GROUP_HEADER_CXMENU_CLOSE_GROUP = 15; static constexpr int TAB_GROUP_HEADER_CXMENU_CLOSE_GROUP = 15;
static constexpr int TAB_GROUP_HEADER_CXMENU_FEEDBACK = 16; static constexpr int TAB_GROUP_HEADER_CXMENU_FEEDBACK = 16;
// Shows the editor for |group|. Returns an *unowned* pointer to the // Shows the editor for |group| using a TabGroupHeader anchor. Should be used
// bubble's widget. // in most cases to allow focus handling between the header and the bubble.
static views::Widget* Show(TabGroupHeader* anchor_view, // Returns an *unowned* pointer to the bubble's widget.
const Browser* browser, static views::Widget* Show(const Browser* browser,
const tab_groups::TabGroupId& group); const tab_groups::TabGroupId& group,
TabGroupHeader* anchor_view);
// Shows the editor for |group| using a rect as an anchor. Should only be used
// if the TabGroupHeader is not available as an anchor, e.g. in WebUI. Returns
// an *unowned* pointer to the bubble's widget.
static views::Widget* ShowWithRect(const Browser* browser,
const tab_groups::TabGroupId& group,
gfx::Rect anchor_rect);
// views::BubbleDialogDelegateView: // views::BubbleDialogDelegateView:
gfx::Size CalculatePreferredSize() const override; gfx::Size CalculatePreferredSize() const override;
...@@ -46,9 +55,10 @@ class TabGroupEditorBubbleView : public views::BubbleDialogDelegateView { ...@@ -46,9 +55,10 @@ class TabGroupEditorBubbleView : public views::BubbleDialogDelegateView {
views::View* GetInitiallyFocusedView() override; views::View* GetInitiallyFocusedView() override;
private: private:
TabGroupEditorBubbleView(TabGroupHeader* anchor_view, TabGroupEditorBubbleView(const Browser* browser,
const Browser* browser, const tab_groups::TabGroupId& group,
const tab_groups::TabGroupId& group); TabGroupHeader* anchor_view,
base::Optional<gfx::Rect> anchor_rect);
~TabGroupEditorBubbleView() override; ~TabGroupEditorBubbleView() override;
void UpdateGroup(); void UpdateGroup();
...@@ -81,16 +91,16 @@ class TabGroupEditorBubbleView : public views::BubbleDialogDelegateView { ...@@ -81,16 +91,16 @@ class TabGroupEditorBubbleView : public views::BubbleDialogDelegateView {
class ButtonListener : public views::ButtonListener { class ButtonListener : public views::ButtonListener {
public: public:
explicit ButtonListener(const Browser* browser, explicit ButtonListener(const Browser* browser,
TabGroupHeader* anchor_view, tab_groups::TabGroupId group,
tab_groups::TabGroupId group); TabGroupHeader* anchor_view);
// views::ButtonListener: // views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override; void ButtonPressed(views::Button* sender, const ui::Event& event) override;
private: private:
const Browser* const browser_; const Browser* const browser_;
TabGroupHeader* anchor_view_;
const tab_groups::TabGroupId group_; const tab_groups::TabGroupId group_;
TabGroupHeader* anchor_view_;
}; };
ButtonListener button_listener_; ButtonListener button_listener_;
......
...@@ -106,7 +106,7 @@ bool TabGroupHeader::OnKeyPressed(const ui::KeyEvent& event) { ...@@ -106,7 +106,7 @@ bool TabGroupHeader::OnKeyPressed(const ui::KeyEvent& event) {
event.key_code() == ui::VKEY_RETURN) && event.key_code() == ui::VKEY_RETURN) &&
!editor_bubble_tracker_.is_open()) { !editor_bubble_tracker_.is_open()) {
editor_bubble_tracker_.Opened(TabGroupEditorBubbleView::Show( editor_bubble_tracker_.Opened(TabGroupEditorBubbleView::Show(
this, tab_strip_->controller()->GetBrowser(), group().value())); tab_strip_->controller()->GetBrowser(), group().value(), this));
return true; return true;
} }
...@@ -155,7 +155,7 @@ bool TabGroupHeader::OnMouseDragged(const ui::MouseEvent& event) { ...@@ -155,7 +155,7 @@ bool TabGroupHeader::OnMouseDragged(const ui::MouseEvent& event) {
void TabGroupHeader::OnMouseReleased(const ui::MouseEvent& event) { void TabGroupHeader::OnMouseReleased(const ui::MouseEvent& event) {
if (!dragging()) { if (!dragging()) {
editor_bubble_tracker_.Opened(TabGroupEditorBubbleView::Show( editor_bubble_tracker_.Opened(TabGroupEditorBubbleView::Show(
this, tab_strip_->controller()->GetBrowser(), group().value())); tab_strip_->controller()->GetBrowser(), group().value(), this));
} }
tab_strip_->EndDrag(END_DRAG_COMPLETE); tab_strip_->EndDrag(END_DRAG_COMPLETE);
} }
...@@ -175,7 +175,7 @@ void TabGroupHeader::OnGestureEvent(ui::GestureEvent* event) { ...@@ -175,7 +175,7 @@ void TabGroupHeader::OnGestureEvent(ui::GestureEvent* event) {
switch (event->type()) { switch (event->type()) {
case ui::ET_GESTURE_TAP: { case ui::ET_GESTURE_TAP: {
editor_bubble_tracker_.Opened(TabGroupEditorBubbleView::Show( editor_bubble_tracker_.Opened(TabGroupEditorBubbleView::Show(
this, tab_strip_->controller()->GetBrowser(), group().value())); tab_strip_->controller()->GetBrowser(), group().value(), this));
break; break;
} }
......
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