Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Unified Diff: ui/views/controls/button/toggle_button.cc

Issue 2506133003: [ash-md] Allows ToggleButton to have a border and adds focus rectangle (Closed)
Patch Set: [ash-md] Allows ToggleButton to have a border and adds focus rectangle (comments) Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/views/controls/button/toggle_button.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/controls/button/toggle_button.cc
diff --git a/ui/views/controls/button/toggle_button.cc b/ui/views/controls/button/toggle_button.cc
index 8d02d0b838aa732697a6f3e5a7e9d1e0b87d4a26..dab123631d0eb60d5631d821fbae151c8e445370 100644
--- a/ui/views/controls/button/toggle_button.cc
+++ b/ui/views/controls/button/toggle_button.cc
@@ -10,9 +10,11 @@
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
+#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/views/animation/ink_drop_impl.h"
#include "ui/views/animation/ink_drop_ripple.h"
#include "ui/views/border.h"
+#include "ui/views/painter.h"
namespace views {
@@ -24,11 +26,8 @@ const int kTrackWidth = 28;
// Margins from edge of track to edge of view.
const int kTrackVerticalMargin = 5;
const int kTrackHorizontalMargin = 6;
-// Margin from edge of thumb to closest edge of view. Note that the thumb
-// margins must be sufficiently large to allow space for the shadow.
-const int kThumbHorizontalMargin = 4;
-// Margin from top/bottom edge of thumb to top/bottom edge of view.
-const int kThumbVerticalMargin = 3;
+// Inset from the rounded edge of the thumb to the rounded edge of the track.
+const int kThumbInset = 2;
} // namespace
@@ -51,6 +50,13 @@ class ToggleButton::ThumbView : public InkDropHostView {
.Offset(gfx::Vector2d(kShadowOffsetX, kShadowOffsetY));
}
+ protected:
+ // views::View:
+ bool CanProcessEventsWithinSubtree() const override {
+ // Make the thumb behave as part of the parent for tooltip event handling.
+ return false;
+ }
+
private:
static const int kShadowOffsetX = 0;
static const int kShadowOffsetY = 1;
@@ -108,10 +114,9 @@ ToggleButton::ToggleButton(ButtonListener* listener)
thumb_view_(new ThumbView()) {
slide_animation_.SetSlideDuration(80 /* ms */);
slide_animation_.SetTweenType(gfx::Tween::LINEAR);
- SetBorder(CreateEmptyBorder(
- gfx::Insets(kTrackVerticalMargin, kTrackHorizontalMargin)));
AddChildView(thumb_view_);
SetInkDropMode(InkDropMode::ON);
+ SetFocusForPlatform();
set_has_ink_drop_action_on_click(true);
}
@@ -136,9 +141,31 @@ void ToggleButton::SetIsOn(bool is_on, bool animate) {
}
}
+void ToggleButton::set_focus_painter(std::unique_ptr<Painter> focus_painter) {
+ focus_painter_ = std::move(focus_painter);
+}
+
+void ToggleButton::OnFocus() {
+ CustomButton::OnFocus();
+ if (focus_painter_)
+ SchedulePaint();
+}
+
+void ToggleButton::OnBlur() {
+ CustomButton::OnBlur();
+ if (focus_painter_)
+ SchedulePaint();
+}
+
+gfx::Rect ToggleButton::GetTrackBounds() const {
+ gfx::Rect track_bounds(size());
Evan Stade 2016/11/17 23:00:43 I believe this should be GetContentsBounds() inste
varkha 2016/11/17 23:18:10 Good catch in case we do asymmetric borders and ve
+ track_bounds.ClampToCenteredSize(gfx::Size(kTrackWidth, kTrackHeight));
+ return track_bounds;
+}
+
gfx::Rect ToggleButton::GetThumbBounds() const {
- gfx::Rect thumb_bounds = GetLocalBounds();
- thumb_bounds.Inset(gfx::Insets(kThumbVerticalMargin, kThumbHorizontalMargin));
+ gfx::Rect thumb_bounds(GetTrackBounds());
+ thumb_bounds.Inset(gfx::Insets(-kThumbInset));
thumb_bounds.set_x(thumb_bounds.x() +
slide_animation_.GetCurrentValue() *
(thumb_bounds.width() - thumb_bounds.height()));
@@ -165,6 +192,7 @@ SkColor ToggleButton::GetTrackColor(bool is_on) const {
gfx::Size ToggleButton::GetPreferredSize() const {
gfx::Rect rect(0, 0, kTrackWidth, kTrackHeight);
+ rect.Inset(gfx::Insets(-kTrackVerticalMargin, -kTrackHorizontalMargin));
if (border())
rect.Inset(-border()->GetInsets());
return rect.size();
@@ -177,8 +205,9 @@ const char* ToggleButton::GetClassName() const {
void ToggleButton::OnPaint(gfx::Canvas* canvas) {
// Paint the toggle track. To look sharp even at fractional scale factors,
// round up to pixel boundaries.
+ canvas->Save();
float dsf = canvas->UndoDeviceScaleFactor();
- gfx::RectF track_rect(GetContentsBounds());
+ gfx::RectF track_rect(GetTrackBounds());
track_rect.Scale(dsf);
track_rect = gfx::RectF(gfx::ToEnclosingRect(track_rect));
SkPaint track_paint;
@@ -188,6 +217,9 @@ void ToggleButton::OnPaint(gfx::Canvas* canvas) {
GetTrackColor(true), GetTrackColor(false),
static_cast<SkAlpha>(SK_AlphaOPAQUE * color_ratio)));
canvas->DrawRoundRect(track_rect, track_rect.height() / 2, track_paint);
+ canvas->Restore();
+
+ views::Painter::PaintFocusPainter(this, canvas, focus_painter_.get());
}
void ToggleButton::NotifyClick(const ui::Event& event) {
« no previous file with comments | « ui/views/controls/button/toggle_button.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698