Commit 6f06c4ad authored by Olesia Marukhno's avatar Olesia Marukhno Committed by Commit Bot

[omnibox] Refactoring OmniboxResultView to use flex layout

Based on: https://chromium-review.googlesource.com/c/chromium/src/+/1865014

Bug: 1005568
Change-Id: I8c04530c8a0582c2d7446dfcc42ec661d6efae2e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2398500Reviewed-by: default avatarAngela Yoeurng <yoangela@chromium.org>
Reviewed-by: default avatarBret Sepulveda <bsep@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarTommy Li <tommycli@chromium.org>
Commit-Queue: Olesia Marukhno <olesiamarukhno@google.com>
Cr-Commit-Position: refs/heads/master@{#821675}
parent 365bd152
......@@ -29,7 +29,7 @@ class OmniboxMatchCellView;
class OmniboxPopupContentsView;
class OmniboxSuggestionButtonRowView;
class OmniboxTabSwitchButton;
class OmniboxResultFocusBar;
class OmniboxResultSelectionIndicator;
enum class OmniboxPart;
enum class OmniboxPartState;
......@@ -96,14 +96,12 @@ class OmniboxResultView : public views::View,
void EmitTextChangedAccessiblityEvent();
// views::View:
void Layout() override;
bool OnMousePressed(const ui::MouseEvent& event) override;
bool OnMouseDragged(const ui::MouseEvent& event) override;
void OnMouseReleased(const ui::MouseEvent& event) override;
void OnMouseEntered(const ui::MouseEvent& event) override;
void OnMouseExited(const ui::MouseEvent& event) override;
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
gfx::Size CalculatePreferredSize() const override;
void OnThemeChanged() override;
private:
......@@ -127,6 +125,9 @@ class OmniboxResultView : public views::View,
// state.
void UpdateRemoveSuggestionVisibility();
// Sets the widths of the suggestion and keyword and calls Layout().
void SetWidths();
// views::View:
const char* GetClassName() const override;
void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
......@@ -149,23 +150,26 @@ class OmniboxResultView : public views::View,
// For sliding in the keyword search.
std::unique_ptr<gfx::SlideAnimation> keyword_slide_animation_;
// Container for the first row (for everything expect |button_row_|).
views::View* suggestion_container_;
// Weak pointers for easy reference.
OmniboxMatchCellView* suggestion_view_; // The leading (or left) view.
OmniboxMatchCellView* keyword_view_; // The trailing (or right) view.
OmniboxTabSwitchButton* suggestion_tab_switch_button_;
// The blue bar used to indicate focus. This is currently only used if
// The blue bar used to indicate selection. This is currently only used if
// omnibox-refined-focus-state flag is enabled.
OmniboxResultFocusBar* focus_bar_ = nullptr;
// The row of buttons, only assigned and used if OmniboxSuggestionButtonRow
// feature is enabled. It is owned by the base view, not this raw pointer.
OmniboxSuggestionButtonRowView* button_row_ = nullptr;
OmniboxResultSelectionIndicator* selection_indicator_ = nullptr;
// The "X" button at the end of the match cell, used to remove suggestions.
views::ImageButton* remove_suggestion_button_;
views::FocusRing* remove_suggestion_focus_ring_ = nullptr;
// The row of buttons, only assigned and used if OmniboxSuggestionButtonRow
// feature is enabled. It is owned by the base view, not this raw pointer.
OmniboxSuggestionButtonRowView* button_row_ = nullptr;
// Keeps track of mouse-enter and mouse-exit events of child Views.
OmniboxMouseEnterExitHandler mouse_enter_exit_handler_;
......
......@@ -72,6 +72,17 @@ OmniboxTabSwitchButton::OmniboxTabSwitchButton(
OmniboxTabSwitchButton::~OmniboxTabSwitchButton() = default;
void OmniboxTabSwitchButton::OnBoundsChanged(const gfx::Rect& previous_bounds) {
MdTextButton::OnBoundsChanged(previous_bounds);
base::string16 text = hint_;
if (width() <= icon_only_width_)
text = base::string16();
else if (width() <= short_text_width_)
text = hint_short_;
SetText(text);
}
void OmniboxTabSwitchButton::StateChanged(ButtonState old_state) {
MdTextButton::StateChanged(old_state);
if (GetState() == STATE_NORMAL && old_state == STATE_PRESSED) {
......@@ -98,15 +109,33 @@ void OmniboxTabSwitchButton::GetAccessibleNodeData(ui::AXNodeData* node_data) {
IsSelected());
}
void OmniboxTabSwitchButton::UpdateBackground() {
focus_ring()->SchedulePaint();
// static
views::FlexRule OmniboxTabSwitchButton::GetFlexRule() {
// The rule below snaps between full, short, icon-only, and zero widths.
return base::BindRepeating(
[](const views::View* view, const views::SizeBounds& maximum_size) {
gfx::Size preferred_size = view->GetPreferredSize();
int width;
if (!maximum_size.width().is_bounded()) {
// Until width is bounded, return 0 to allocate flex excess correctly.
width = 0;
} else if (full_text_width_ <= maximum_size.width()) {
width = full_text_width_;
} else if (short_text_width_ <= maximum_size.width()) {
width = short_text_width_;
} else if (icon_only_width_ < maximum_size.width()) {
width = icon_only_width_;
} else {
// Available width is too small to fit even the icon only. So don't
// show button at all.
width = 0;
}
return gfx::Size(width, preferred_size.height());
});
}
void OmniboxTabSwitchButton::ProvideWidthHint(int parent_width) {
base::string16 text;
int preferred_width = CalculateGoalWidth(parent_width, &text);
SetText(text);
SetPreferredSize({preferred_width, GetPreferredSize().height()});
void OmniboxTabSwitchButton::UpdateBackground() {
focus_ring()->SchedulePaint();
}
bool OmniboxTabSwitchButton::IsSelected() const {
......@@ -115,17 +144,3 @@ bool OmniboxTabSwitchButton::IsSelected() const {
popup_contents_view_->model()->selected_line_state() ==
OmniboxPopupModel::FOCUSED_BUTTON_TAB_SWITCH;
}
int OmniboxTabSwitchButton::CalculateGoalWidth(int parent_width,
base::string16* goal_text) {
if (full_text_width_ * 5 <= parent_width) {
*goal_text = hint_;
return full_text_width_;
}
if (short_text_width_ * 5 <= parent_width) {
*goal_text = hint_short_;
return short_text_width_;
}
*goal_text = base::string16();
return (icon_only_width_ * 5 <= parent_width) ? icon_only_width_ : 0;
}
......@@ -6,6 +6,7 @@
#define CHROME_BROWSER_UI_VIEWS_OMNIBOX_OMNIBOX_TAB_SWITCH_BUTTON_H_
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/layout/flex_layout_types.h"
class OmniboxPopupContentsView;
class OmniboxResultView;
......@@ -22,26 +23,22 @@ class OmniboxTabSwitchButton : public views::MdTextButton {
~OmniboxTabSwitchButton() override;
// views::MdTextButton:
void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
void StateChanged(ButtonState old_state) override;
void OnThemeChanged() override;
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
// Returns the FlexRule that should be used to size this button.
static views::FlexRule GetFlexRule();
// Called by parent views to change background on external (not mouse related)
// event (tab key).
void UpdateBackground();
// Called by parent view to provide the width of the surrounding area
// so the button can adjust its size or even presence.
void ProvideWidthHint(int width);
private:
// Consults the parent views to see if the button is selected.
bool IsSelected() const;
// Helper function to translate parent width into goal width, and
// pass back the text at that width.
int CalculateGoalWidth(int parent_width, base::string16* goal_text);
// The ancestor views.
OmniboxPopupContentsView* const popup_contents_view_;
OmniboxResultView* const result_view_;
......
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