Commit 40f664c5 authored by Dana Fried's avatar Dana Fried Committed by Commit Bot

Enhance separator logic to handle most use cases.

In https://chromium-review.googlesource.com/c/chromium/src/+/1880531, we
fixed the case where a separator would not properly fill its bounds.
However that broke functionality where a spacer was being used as e.g. a
vertical separator in a text flow with insets on either side (see
attached bug).

This solution does both:
 - when there is no inset on a particular side of the separator, the
   separator is guaranteed to draw flush with the edge of the view
 - when there is an inset, the inset is guaranteed to be preserved,
   with the old behavior of the separator being adjusted to draw only on
   whole pixels in DPI-scaled environments (minimum of 1px in either
   dimension)


Fixes: 1019503
Bug: 1019503
Change-Id: I97dd487ba2374000dcef76e43665ef44eb93acd8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1894253
Commit-Queue: Dana Fried <dfried@chromium.org>
Auto-Submit: Dana Fried <dfried@chromium.org>
Reviewed-by: default avatarCollin Baker <collinbaker@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#711842}
parent fa6e9eed
......@@ -7,6 +7,7 @@
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/native_theme/native_theme.h"
namespace views {
......@@ -56,8 +57,40 @@ void Separator::OnPaint(gfx::Canvas* canvas) {
? *overridden_color_
: GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_SeparatorColor);
canvas->DrawColor(color);
// Paint background and border, if any.
View::OnPaint(canvas);
gfx::ScopedCanvas scoped_canvas(canvas);
const gfx::Insets insets = GetInsets();
const gfx::Rect contents_bounds = GetContentsBounds();
const float dsf = canvas->UndoDeviceScaleFactor();
// This is a hybrid of gfx::ScaleToEnclosedRect() and
// gfx::ScaleToEnclosingRect() based on whether there are nonzero insets on
// any particular side of the separator. If there is no inset, the separator
// will paint all the way out to the edge of the view. If there is an inset,
// the extent of the separator will rounded inward so that it paints only
// full pixels, providing a sharper appearance and preserving the inset.
//
// This allows separators that entirely fill their area to do so, and those
// intended as spacers in a larger flow to do so as well. See
// crbug.com/1016760 and crbug.com/1019503 for examples of why we need to
// handle both cases.
const int x = insets.left() == 0 ? std::floor(contents_bounds.x() * dsf)
: std::ceil(contents_bounds.x() * dsf);
const int y = insets.top() == 0 ? std::floor(contents_bounds.y() * dsf)
: std::ceil(contents_bounds.y() * dsf);
const int r = insets.right() == 0 ? std::ceil(contents_bounds.right() * dsf)
: std::floor(contents_bounds.right() * dsf);
const int b = insets.bottom() == 0
? std::ceil(contents_bounds.bottom() * dsf)
: std::floor(contents_bounds.bottom() * dsf);
// Minimum separator size is 1 px.
const int w = std::max(1, r - x);
const int h = std::max(1, b - y);
canvas->FillRect({x, y, w, h}, color);
}
BEGIN_METADATA(Separator)
......
This diff is collapsed.
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