Commit 4a41c57a authored by Tommy C. Li's avatar Tommy C. Li Committed by Commit Bot

Omnibox: Add a context menu for suggestions (disabled by default)

This CL adds a context menu for suggestions. It currently only contains
a "Remove suggestion..." entry to mirror the Shift+Delete behavior,
but currently doesn't do anything.

In the future, it will be wired to a confirm bubble.

Bug: 929477
Change-Id: If12de2e04c41c4a639fd31a39a80efeac936883c
Reviewed-on: https://chromium-review.googlesource.com/c/1457234
Commit-Queue: Tommy Li <tommycli@chromium.org>
Reviewed-by: default avatarmanuk hovanesian <manukh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#630560}
parent 904ab8a3
......@@ -13,6 +13,7 @@
#include "base/macros.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/omnibox/omnibox_theme.h"
......@@ -35,6 +36,7 @@
#include "ui/base/theme_provider.h"
#include "ui/events/event.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/controls/menu/menu_runner.h"
#if defined(OS_WIN)
#include "base/win/atl.h"
......@@ -59,6 +61,14 @@ OmniboxResultView::OmniboxResultView(OmniboxPopupContentsView* model,
omnibox::kKeywordSearchIcon, GetLayoutConstant(LOCATION_BAR_ICON_SIZE),
GetColor(OmniboxPart::RESULTS_ICON)));
keyword_view_->icon()->SizeToPreferredSize();
if (base::FeatureList::IsEnabled(
omnibox::kOmniboxContextMenuForSuggestions)) {
// TODO(tommycli): Replace this with the real translated string from UX.
context_menu_contents_.AddItem(COMMAND_REMOVE_SUGGESTION,
base::ASCIIToUTF16("Remove suggestion..."));
set_context_menu_controller(this);
}
}
OmniboxResultView::~OmniboxResultView() {}
......@@ -387,6 +397,44 @@ void OmniboxResultView::OnNativeThemeChanged(const ui::NativeTheme* theme) {
SchedulePaint();
}
void OmniboxResultView::ShowContextMenuForView(views::View* source,
const gfx::Point& point,
ui::MenuSourceType source_type) {
// Deferred unhover of the result until the context menu is closed.
// If the mouse is still over the result when the context menu is closed, the
// View will receive an OnMouseMoved call anyways, which sets hover to true.
base::RepeatingClosure set_hovered_false = base::BindRepeating(
&OmniboxResultView::SetHovered, weak_factory_.GetWeakPtr(), false);
context_menu_runner_ = std::make_unique<views::MenuRunner>(
&context_menu_contents_,
views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU,
set_hovered_false);
context_menu_runner_->RunMenuAt(GetWidget(), nullptr,
gfx::Rect(point, gfx::Size()),
views::MENU_ANCHOR_TOPLEFT, source_type);
// Opening the context menu unsets the hover state, but we still want the
// result 'hovered' as long as the context menu is open.
SetHovered(true);
}
// ui::SimpleMenuModel::Delegate overrides:
bool OmniboxResultView::IsCommandIdChecked(int command_id) const {
return false;
}
bool OmniboxResultView::IsCommandIdEnabled(int command_id) const {
DCHECK_EQ(COMMAND_REMOVE_SUGGESTION, command_id);
return match_.SupportsDeletion();
}
void OmniboxResultView::ExecuteCommand(int command_id, int event_flags) {
DCHECK_EQ(COMMAND_REMOVE_SUGGESTION, command_id);
// TODO(tommycli): Launch modal bubble to confirm removing the suggestion.
}
void OmniboxResultView::ProvideButtonFocusHint() {
suggestion_tab_switch_button_->ProvideFocusHint();
}
......
......@@ -10,14 +10,17 @@
#include <utility>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/suggestion_answer.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/models/simple_menu_model.h"
#include "ui/base/window_open_disposition.h"
#include "ui/gfx/animation/animation_delegate.h"
#include "ui/gfx/animation/slide_animation.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/context_menu_controller.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/view.h"
......@@ -33,9 +36,15 @@ namespace gfx {
class Image;
}
namespace views {
class MenuRunner;
}
class OmniboxResultView : public views::View,
private gfx::AnimationDelegate,
public views::ButtonListener {
public views::ButtonListener,
public views::ContextMenuController,
public ui::SimpleMenuModel::Delegate {
public:
OmniboxResultView(OmniboxPopupContentsView* model, int model_index);
~OmniboxResultView() override;
......@@ -86,7 +95,20 @@ class OmniboxResultView : public views::View,
gfx::Size CalculatePreferredSize() const override;
void OnNativeThemeChanged(const ui::NativeTheme* theme) override;
// views::ContextMenuController:
void ShowContextMenuForView(views::View* source,
const gfx::Point& point,
ui::MenuSourceType source_type) override;
// ui::SimpleMenuModel::Delegate overrides:
bool IsCommandIdChecked(int command_id) const override;
bool IsCommandIdEnabled(int command_id) const override;
void ExecuteCommand(int command_id, int event_flags) override;
private:
// TODO(tommycli): This will be removed once we get final strings from UX.
enum CommandID { COMMAND_REMOVE_SUGGESTION };
// Returns the height of the text portion of the result view.
int GetTextHeight() const;
......@@ -120,11 +142,17 @@ class OmniboxResultView : public views::View,
// For sliding in the keyword search.
std::unique_ptr<gfx::SlideAnimation> animation_;
// Context menu related members.
ui::SimpleMenuModel context_menu_contents_{this};
std::unique_ptr<views::MenuRunner> context_menu_runner_;
// Weak pointers for easy reference.
OmniboxMatchCellView* suggestion_view_; // The leading (or left) view.
OmniboxMatchCellView* keyword_view_; // The trailing (or right) view.
std::unique_ptr<OmniboxTabSwitchButton> suggestion_tab_switch_button_;
base::WeakPtrFactory<OmniboxResultView> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(OmniboxResultView);
};
......
......@@ -153,6 +153,12 @@ constexpr base::FeatureParam<OmniboxFieldTrial::PedalSuggestionMode>
OmniboxFieldTrial::PedalSuggestionMode::DEDICATED,
&kPedalSuggestionModeOptions};
// Feature used to show a context menu for suggestions when the user
// right-clicks a suggestion in the omnibox dropdown. It's currently disabled
// by default during development, but will eventually be enabled by default.
const base::Feature kOmniboxContextMenuForSuggestions{
"OmniboxContextMenuForSuggestions", base::FEATURE_DISABLED_BY_DEFAULT};
// Feature used to enable clipboard provider, which provides the user with
// suggestions of the URL in the user's clipboard (if any) upon omnibox focus.
const base::Feature kEnableClipboardProvider {
......
......@@ -39,6 +39,7 @@ extern const base::Feature kOmniboxTabSwitchSuggestions;
extern const base::Feature kOmniboxReverseTabSwitchLogic;
extern const base::Feature kExperimentalKeywordMode;
extern const base::Feature kOmniboxPedalSuggestions;
extern const base::Feature kOmniboxContextMenuForSuggestions;
extern const base::Feature kEnableClipboardProvider;
extern const base::Feature kEnableClipboardProviderTextSuggestions;
extern const base::Feature kEnableClipboardProviderImageSuggestions;
......
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