Commit 54a963cd authored by Tommy Martino's avatar Tommy Martino Committed by Commit Bot

[Autofill Views] Setting max width

* Introduces a maximum width for the Autofill dropdown.
* Creates a subclass for Warnings with styles different from regular
  rows, and introduces logic to allow the long strings inside to wrap.
  This subclass also makes click events on the warning message a
  no-op, since the warning is not meant to be interactive.
* Changes the color of the text in warnings to use a Refresh value.

Bug: 860352
Change-Id: I320949e3965faf758286af8fbdf3487e0cb91794
Reviewed-on: https://chromium-review.googlesource.com/1130106
Commit-Queue: Tommy Martino <tmartino@chromium.org>
Reviewed-by: default avatarFabio Tirelo <ftirelo@chromium.org>
Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#575456}
parent d545b720
...@@ -24,6 +24,37 @@ namespace autofill { ...@@ -24,6 +24,37 @@ namespace autofill {
namespace { namespace {
// CalculateWidthValues below ultimately determines the maximum popup width
// given the constraints of the window and the element's position within it.
// Returning a struct allows callers to reuse intermediary values produced
// during this calculation which are also relevant to positioning and sizing.
struct WidthCalculationResults {
int right_growth_start; // Popup start coordinate when growing to the right.
int left_growth_end; // Popup end coordinate when growing to the left.
int right_available; // Amount of space available if drawing to the right.
int left_available; // Amount of space available if drawing to the left.
int max_popup_width; // The max of |right_available| and |left_available|.
};
WidthCalculationResults CalculateWidthValues(int leftmost_available_x,
int rightmost_available_x,
const gfx::Rect& element_bounds) {
WidthCalculationResults result;
result.right_growth_start =
std::max(leftmost_available_x,
std::min(rightmost_available_x, element_bounds.x()));
result.left_growth_end =
std::max(leftmost_available_x,
std::min(rightmost_available_x, element_bounds.right()));
result.right_available = rightmost_available_x - result.right_growth_start;
result.left_available = result.left_growth_end - leftmost_available_x;
result.max_popup_width =
std::max(result.right_available, result.left_available);
return result;
}
// Sets the |x| and |width| components of |popup_bounds| as the x-coordinate of // Sets the |x| and |width| components of |popup_bounds| as the x-coordinate of
// the starting point and the width of the popup, taking into account the // the starting point and the width of the popup, taking into account the
// direction it's supposed to grow (either to the left or to the right). // direction it's supposed to grow (either to the left or to the right).
...@@ -34,36 +65,26 @@ void CalculatePopupXAndWidth(int leftmost_available_x, ...@@ -34,36 +65,26 @@ void CalculatePopupXAndWidth(int leftmost_available_x,
const gfx::Rect& element_bounds, const gfx::Rect& element_bounds,
bool is_rtl, bool is_rtl,
gfx::Rect* popup_bounds) { gfx::Rect* popup_bounds) {
// Calculate the start coordinates for the popup if it is growing right or WidthCalculationResults result = CalculateWidthValues(
// the end position if it is growing to the left, capped to screen space. leftmost_available_x, rightmost_available_x, element_bounds);
int right_growth_start =
std::max(leftmost_available_x,
std::min(rightmost_available_x, element_bounds.x()));
int left_growth_end =
std::max(leftmost_available_x,
std::min(rightmost_available_x, element_bounds.right()));
int right_available = rightmost_available_x - right_growth_start; int popup_width = std::min(popup_required_width, result.max_popup_width);
int left_available = left_growth_end - leftmost_available_x;
int popup_width =
std::min(popup_required_width, std::max(right_available, left_available));
// Prefer to grow towards the end (right for LTR, left for RTL). But if there // Prefer to grow towards the end (right for LTR, left for RTL). But if there
// is not enough space available in the desired direction and more space in // is not enough space available in the desired direction and more space in
// the other direction, reverse it. // the other direction, reverse it.
bool grow_left = false; bool grow_left = false;
if (is_rtl) { if (is_rtl) {
grow_left = grow_left = result.left_available >= popup_width ||
left_available >= popup_width || left_available >= right_available; result.left_available >= result.right_available;
} else { } else {
grow_left = grow_left = result.right_available < popup_width &&
right_available < popup_width && right_available < left_available; result.right_available < result.left_available;
} }
popup_bounds->set_width(popup_width); popup_bounds->set_width(popup_width);
popup_bounds->set_x(grow_left ? left_growth_end - popup_width popup_bounds->set_x(grow_left ? result.left_growth_end - popup_width
: right_growth_start); : result.right_growth_start);
} }
// Sets the |y| and |height| components of |popup_bounds| as the y-coordinate of // Sets the |y| and |height| components of |popup_bounds| as the y-coordinate of
...@@ -171,4 +192,14 @@ gfx::Rect PopupViewCommon::GetWindowBounds(gfx::NativeView container_view) { ...@@ -171,4 +192,14 @@ gfx::Rect PopupViewCommon::GetWindowBounds(gfx::NativeView container_view) {
#endif #endif
} }
int PopupViewCommon::CalculateMaxWidth(const gfx::Rect& element_bounds,
gfx::NativeView container_view) {
const gfx::Rect window_bounds = GetWindowBounds(container_view);
int leftmost_available_x = window_bounds.x();
int rightmost_available_x = window_bounds.x() + window_bounds.width();
return CalculateWidthValues(leftmost_available_x, rightmost_available_x,
element_bounds)
.max_popup_width;
}
} // namespace autofill } // namespace autofill
...@@ -48,6 +48,12 @@ class PopupViewCommon { ...@@ -48,6 +48,12 @@ class PopupViewCommon {
// testing. // testing.
virtual gfx::Rect GetWindowBounds(gfx::NativeView container_view); virtual gfx::Rect GetWindowBounds(gfx::NativeView container_view);
// Returns the greatest possible width for the popup, based on the distances
// between the edges of the element and the respective far edges of the
// window.
int CalculateMaxWidth(const gfx::Rect& element_bounds,
gfx::NativeView container_view);
DISALLOW_COPY_AND_ASSIGN(PopupViewCommon); DISALLOW_COPY_AND_ASSIGN(PopupViewCommon);
}; };
......
...@@ -54,7 +54,6 @@ class AutofillPopupRowView : public views::View { ...@@ -54,7 +54,6 @@ class AutofillPopupRowView : public views::View {
AutofillPopupController* controller_; AutofillPopupController* controller_;
const int line_number_; const int line_number_;
bool is_warning_ = false; // overwritten in ctor
bool is_selected_ = false; bool is_selected_ = false;
}; };
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include "base/no_destructor.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/ui/autofill/autofill_popup_controller.h" #include "chrome/browser/ui/autofill/autofill_popup_controller.h"
...@@ -30,7 +31,7 @@ struct TypeClicks { ...@@ -30,7 +31,7 @@ struct TypeClicks {
const struct TypeClicks kClickTestCase[] = { const struct TypeClicks kClickTestCase[] = {
{autofill::POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY, 1}, {autofill::POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY, 1},
{autofill::POPUP_ITEM_ID_INSECURE_CONTEXT_PAYMENT_DISABLED_MESSAGE, 1}, {autofill::POPUP_ITEM_ID_INSECURE_CONTEXT_PAYMENT_DISABLED_MESSAGE, 0},
{autofill::POPUP_ITEM_ID_PASSWORD_ENTRY, 1}, {autofill::POPUP_ITEM_ID_PASSWORD_ENTRY, 1},
{autofill::POPUP_ITEM_ID_SEPARATOR, 0}, {autofill::POPUP_ITEM_ID_SEPARATOR, 0},
{autofill::POPUP_ITEM_ID_CLEAR_FORM, 1}, {autofill::POPUP_ITEM_ID_CLEAR_FORM, 1},
...@@ -61,7 +62,10 @@ class MockAutofillPopupController : public autofill::AutofillPopupController { ...@@ -61,7 +62,10 @@ class MockAutofillPopupController : public autofill::AutofillPopupController {
MOCK_CONST_METHOD0(HasSelection, bool()); MOCK_CONST_METHOD0(HasSelection, bool());
MOCK_CONST_METHOD0(popup_bounds, gfx::Rect()); MOCK_CONST_METHOD0(popup_bounds, gfx::Rect());
MOCK_METHOD0(container_view, gfx::NativeView()); MOCK_METHOD0(container_view, gfx::NativeView());
MOCK_CONST_METHOD0(element_bounds, const gfx::RectF&()); const gfx::RectF& element_bounds() const override {
static base::NoDestructor<gfx::RectF> bounds({100, 100, 250, 50});
return *bounds;
}
MOCK_CONST_METHOD0(IsRTL, bool()); MOCK_CONST_METHOD0(IsRTL, bool());
const std::vector<autofill::Suggestion> GetSuggestions() override { const std::vector<autofill::Suggestion> GetSuggestions() override {
return suggestions_; return suggestions_;
......
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