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

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 (rebase) 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 c8ff2d8fb5b8ffe4b18ff3a84a6d047e37816189..7ce350d8632392c64fd238aefc82bd80a9fdd8fe 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 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,27 @@ void ToggleButton::SetIsOn(bool is_on, bool animate) {
}
}
+void ToggleButton::SetFocusPainter(std::unique_ptr<Painter> focus_painter) {
+ focus_painter_ = std::move(focus_painter);
+}
+
+gfx::Size ToggleButton::GetPreferredSize() const {
+ gfx::Rect rect(kTrackWidth, kTrackHeight);
+ rect.Inset(gfx::Insets(-kTrackVerticalMargin, -kTrackHorizontalMargin));
+ if (border())
+ rect.Inset(-border()->GetInsets());
+ return rect.size();
+}
+
+gfx::Rect ToggleButton::GetTrackBounds() const {
+ gfx::Rect track_bounds(GetContentsBounds());
+ 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()));
@@ -161,13 +184,6 @@ SkColor ToggleButton::GetTrackColor(bool is_on) const {
return SkColorSetA(GetNativeTheme()->GetSystemColor(color_id), kTrackAlpha);
}
-gfx::Size ToggleButton::GetPreferredSize() const {
- gfx::Rect rect(0, 0, kTrackWidth, kTrackHeight);
- if (border())
- rect.Inset(-border()->GetInsets());
- return rect.size();
-}
-
const char* ToggleButton::GetClassName() const {
return kViewClassName;
}
@@ -175,8 +191,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;
@@ -186,11 +203,21 @@ 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();
+
+ Painter::PaintFocusPainter(this, canvas, focus_painter_.get());
}
-void ToggleButton::NotifyClick(const ui::Event& event) {
- SetIsOn(!is_on(), true);
- CustomButton::NotifyClick(event);
+void ToggleButton::OnFocus() {
+ CustomButton::OnFocus();
+ if (focus_painter_)
+ SchedulePaint();
+}
+
+void ToggleButton::OnBlur() {
+ CustomButton::OnBlur();
+ if (focus_painter_)
+ SchedulePaint();
}
void ToggleButton::OnBoundsChanged(const gfx::Rect& previous_bounds) {
@@ -209,6 +236,11 @@ void ToggleButton::GetAccessibleNodeData(ui::AXNodeData* node_data) {
node_data->AddStateFlag(ui::AX_STATE_CHECKED);
}
+void ToggleButton::NotifyClick(const ui::Event& event) {
+ SetIsOn(!is_on(), true);
+ CustomButton::NotifyClick(event);
+}
+
void ToggleButton::AddInkDropLayer(ui::Layer* ink_drop_layer) {
thumb_view_->AddInkDropLayer(ink_drop_layer);
}
« 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