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

Side by Side Diff: ash/common/system/tray/system_menu_toggle_button.cc

Issue 2509943002: NO LONGER FOR REVIEW - [ash-md] Implements a square focus rectangle for ToggleButton (Closed)
Patch Set: [ash-md] Implements a square focus rectangle for ToggleButton (toggles that work) 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 unified diff | Download patch
« no previous file with comments | « ash/common/system/tray/system_menu_toggle_button.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ash/common/system/tray/system_menu_toggle_button.h"
6
7 #include "ash/common/ash_constants.h"
8 #include "ash/common/system/tray/tray_constants.h"
9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/views/controls/button/toggle_button.h"
11 #include "ui/views/layout/box_layout.h"
12 #include "ui/views/painter.h"
13
14 namespace ash {
15
16 // TODO(tdanderson): Update the focus rect color, border thickness, and
17 // location for material design.
18 SystemMenuToggleButton::SystemMenuToggleButton(views::ButtonListener* listener,
19 int accessible_name_id)
20 : views::Button(nullptr),
21 focus_painter_(views::Painter::CreateSolidFocusPainter(kFocusBorderColor,
22 gfx::Insets(1))),
23 listener_(listener) {
24 SetTooltipText(l10n_util::GetStringUTF16(accessible_name_id));
25
26 // Center ToggleButton in this container.
27 views::BoxLayout* container_layout =
28 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0);
29 container_layout->set_main_axis_alignment(
30 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
31 container_layout->set_cross_axis_alignment(
32 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
33 SetLayoutManager(container_layout);
34
35 toggle_ = new views::ToggleButton(this);
36 toggle_->SetFocusForPlatform();
37 AddChildView(toggle_);
38 }
39
40 SystemMenuToggleButton::~SystemMenuToggleButton() {}
41
42 void SystemMenuToggleButton::SetIsOn(bool is_on, bool animate) {
43 toggle_->SetIsOn(is_on, animate);
44 }
45
46 bool SystemMenuToggleButton::is_on() const {
47 return toggle_->is_on();
48 }
49
50 void SystemMenuToggleButton::NativeViewHierarchyChanged() {
51 views::FocusManager* focus_manager = GetFocusManager();
52 if (focus_manager_ != focus_manager) {
53 UnregisterFocusObserver();
54 if (focus_manager)
55 RegisterFocusObserver();
56 }
57 views::Button::NativeViewHierarchyChanged();
58 }
59
60 void SystemMenuToggleButton::ViewHierarchyChanged(
61 const ViewHierarchyChangedDetails& details) {
62 if (details.is_add) {
63 RegisterFocusObserver();
64 } else if (details.child == this) {
65 UnregisterFocusObserver();
66 }
67 }
68
69 gfx::Size SystemMenuToggleButton::GetPreferredSize() const {
70 const int kButtonHorizontalPadding = 14;
71 return gfx::Size(toggle_->width() + 2 * kButtonHorizontalPadding,
72 kMenuButtonSize);
73 }
74
75 int SystemMenuToggleButton::GetHeightForWidth(int w) const {
76 // Make row height fixed avoiding layout manager adjustments.
77 return GetPreferredSize().height();
78 }
79
80 void SystemMenuToggleButton::OnPaint(gfx::Canvas* canvas) {
81 // Call the base class first to paint any background/borders.
82 views::Button::OnPaint(canvas);
83 if (toggle_->HasFocus()) {
84 views::Painter::PaintPainterAt(canvas, focus_painter(), GetLocalBounds());
85 }
86 }
87
88 void SystemMenuToggleButton::ButtonPressed(views::Button* sender,
89 const ui::Event& event) {
90 DCHECK_EQ(toggle_, sender);
91 if (listener_)
92 listener_->ButtonPressed(this, event);
93 }
94
95 void SystemMenuToggleButton::OnWillChangeFocus(views::View* focused_before,
96 views::View* focused_now) {}
97
98 void SystemMenuToggleButton::OnDidChangeFocus(views::View* focused_before,
99 views::View* focused_now) {
100 if (focused_now == toggle_ || focused_before == toggle_)
101 SchedulePaint();
102 }
103
104 void SystemMenuToggleButton::RegisterFocusObserver() {
105 if (!GetWidget())
106 return;
107 views::FocusManager* focus_manager = GetFocusManager();
108 if (focus_manager_ == focus_manager)
109 return;
110 focus_manager_ = focus_manager;
111 if (focus_manager_)
112 focus_manager_->AddFocusChangeListener(this);
113 }
114
115 void SystemMenuToggleButton::UnregisterFocusObserver() {
116 if (GetWidget() && focus_manager_) {
117 focus_manager_->RemoveFocusChangeListener(this);
118 focus_manager_ = NULL;
119 }
120 }
121
122 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/tray/system_menu_toggle_button.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698