Commit 3fd25671 authored by Tommy Li's avatar Tommy Li Committed by Commit Bot

[omnibox] Implement a rudimentary views::Label for header text

This CL uses a rudimentary views::Label to display header text for
AutocompleteResults that contain matches with headers.

Screenshot: https://i.imgur.com/f61GxWR.png

Bug: 1052522
Change-Id: I042c21a60a8416e243498793cee5c3dabeaac4f4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2137673
Commit-Queue: Tommy Li <tommycli@chromium.org>
Reviewed-by: default avatarJustin Donnelly <jdonnelly@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756863}
parent d11c8e73
...@@ -299,10 +299,11 @@ void OmniboxPopupContentsView::UpdatePopupAppearance() { ...@@ -299,10 +299,11 @@ void OmniboxPopupContentsView::UpdatePopupAppearance() {
location_bar_view_->profile())); location_bar_view_->profile()));
} }
} else { } else {
base::Optional<int> previous_row_group_id = base::nullopt;
for (size_t i = 0; i < result_size; ++i) { for (size_t i = 0; i < result_size; ++i) {
// Create child views lazily. Since especially the first result view may // Create child views lazily. Since especially the first result view may
// be expensive to create due to loading font data, this saves time and // be expensive to create due to loading font data, this saves time and
// memory during browser startup. // memory during browser startup. https://crbug.com/1021323
if (children().size() == i) { if (children().size() == i) {
AddChildView(std::make_unique<OmniboxRowView>( AddChildView(std::make_unique<OmniboxRowView>(
std::make_unique<OmniboxResultView>(this, i))); std::make_unique<OmniboxResultView>(this, i)));
...@@ -312,8 +313,19 @@ void OmniboxPopupContentsView::UpdatePopupAppearance() { ...@@ -312,8 +313,19 @@ void OmniboxPopupContentsView::UpdatePopupAppearance() {
static_cast<OmniboxRowView*>(children()[i]); static_cast<OmniboxRowView*>(children()[i]);
row_view->SetVisible(true); row_view->SetVisible(true);
OmniboxResultView* const result_view = row_view->result_view(); // Show the header if it's distinct from the previous match's header.
const AutocompleteMatch& match = GetMatchAtIndex(i); const AutocompleteMatch& match = GetMatchAtIndex(i);
if (match.suggestion_group_id.has_value() &&
match.suggestion_group_id != previous_row_group_id) {
row_view->ShowHeader(match.suggestion_group_id,
model_->result().GetHeaderForGroupId(
match.suggestion_group_id.value()));
} else {
row_view->HideHeader();
}
previous_row_group_id = match.suggestion_group_id;
OmniboxResultView* const result_view = row_view->result_view();
result_view->SetMatch(match); result_view->SetMatch(match);
const SkBitmap* bitmap = model_->RichSuggestionBitmapAt(i); const SkBitmap* bitmap = model_->RichSuggestionBitmapAt(i);
......
...@@ -5,8 +5,27 @@ ...@@ -5,8 +5,27 @@
#include "chrome/browser/ui/views/omnibox/omnibox_row_view.h" #include "chrome/browser/ui/views/omnibox/omnibox_row_view.h"
#include "chrome/browser/ui/views/omnibox/omnibox_result_view.h" #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h" #include "ui/views/layout/box_layout.h"
class OmniboxRowView::HeaderView : public views::Label {
public:
HeaderView() {
SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal));
header_text_ = AddChildView(std::make_unique<views::Label>());
}
void SetHeaderText(const base::string16& header_text) {
header_text_->SetText(header_text);
}
private:
// The Label containing the header text. This is never nullptr.
views::Label* header_text_;
};
OmniboxRowView::OmniboxRowView(std::unique_ptr<OmniboxResultView> result_view) { OmniboxRowView::OmniboxRowView(std::unique_ptr<OmniboxResultView> result_view) {
DCHECK(result_view); DCHECK(result_view);
...@@ -16,7 +35,17 @@ OmniboxRowView::OmniboxRowView(std::unique_ptr<OmniboxResultView> result_view) { ...@@ -16,7 +35,17 @@ OmniboxRowView::OmniboxRowView(std::unique_ptr<OmniboxResultView> result_view) {
result_view_ = AddChildView(std::move(result_view)); result_view_ = AddChildView(std::move(result_view));
} }
void OmniboxRowView::SetHeader(base::Optional<int> suggestion_group_id, void OmniboxRowView::ShowHeader(base::Optional<int> suggestion_group_id,
const base::string16& header_text) { const base::string16& header_text) {
// TODO(tommycli): Create a child header view here. Not implemented yet. // Create the header (at index 0) if it doesn't exist.
if (header_view_ == nullptr)
header_view_ = AddChildViewAt(std::make_unique<HeaderView>(), 0);
header_view_->SetHeaderText(header_text);
header_view_->SetVisible(true);
}
void OmniboxRowView::HideHeader() {
if (header_view_)
header_view_->SetVisible(false);
} }
...@@ -22,15 +22,24 @@ class OmniboxRowView : public views::View { ...@@ -22,15 +22,24 @@ class OmniboxRowView : public views::View {
public: public:
explicit OmniboxRowView(std::unique_ptr<OmniboxResultView> result_view); explicit OmniboxRowView(std::unique_ptr<OmniboxResultView> result_view);
// Sets the header that appears above this row. |suggestion_group_id| can be // Sets the header that appears above this row. Also shows the header.
// base::nullopt, and that will hide the header. void ShowHeader(base::Optional<int> suggestion_group_id,
void SetHeader(base::Optional<int> suggestion_group_id,
const base::string16& header_text); const base::string16& header_text);
// Hides the header.
void HideHeader();
// The result view associated with this row. // The result view associated with this row.
OmniboxResultView* result_view() const { return result_view_; } OmniboxResultView* result_view() const { return result_view_; }
private: private:
class HeaderView;
// Non-owning pointer to the header view for this row. This is initially
// nullptr, and lazily created when a header is first set for this row.
// Lazily creating these speeds up browser startup: https://crbug.com/1021323
HeaderView* header_view_ = nullptr;
// Non-owning pointer to the result view for this row. This is never nullptr. // Non-owning pointer to the result view for this row. This is never nullptr.
OmniboxResultView* result_view_; OmniboxResultView* result_view_;
}; };
......
...@@ -728,7 +728,7 @@ AutocompleteResult::GetMatchDedupComparators() const { ...@@ -728,7 +728,7 @@ AutocompleteResult::GetMatchDedupComparators() const {
} }
base::string16 AutocompleteResult::GetHeaderForGroupId( base::string16 AutocompleteResult::GetHeaderForGroupId(
int suggestion_group_id) { int suggestion_group_id) const {
const auto& it = headers_map_.find(suggestion_group_id); const auto& it = headers_map_.find(suggestion_group_id);
if (it != headers_map_.end()) if (it != headers_map_.end())
return it->second; return it->second;
......
...@@ -155,7 +155,9 @@ class AutocompleteResult { ...@@ -155,7 +155,9 @@ class AutocompleteResult {
// Get a list of comparators used for deduping for the matches in this result. // Get a list of comparators used for deduping for the matches in this result.
std::vector<MatchDedupComparator> GetMatchDedupComparators() const; std::vector<MatchDedupComparator> GetMatchDedupComparators() const;
base::string16 GetHeaderForGroupId(int suggestion_group_id); // Gets the header string associated with |suggestion_group_id|. Returns an
// empty string if no header is found.
base::string16 GetHeaderForGroupId(int suggestion_group_id) const;
// Logs metrics for when |new_result| replaces |old_result| asynchronously. // Logs metrics for when |new_result| replaces |old_result| asynchronously.
// |old_result| a list of the comparators for the old matches. // |old_result| a list of the comparators for the old matches.
......
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