| Index: ash/common/system/tray/system_menu_toggle_button.cc
|
| diff --git a/ash/common/system/tray/system_menu_toggle_button.cc b/ash/common/system/tray/system_menu_toggle_button.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..992bb7fc813576c459a5f0e1090a6c74e32f54e2
|
| --- /dev/null
|
| +++ b/ash/common/system/tray/system_menu_toggle_button.cc
|
| @@ -0,0 +1,122 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "ash/common/system/tray/system_menu_toggle_button.h"
|
| +
|
| +#include "ash/common/ash_constants.h"
|
| +#include "ash/common/system/tray/tray_constants.h"
|
| +#include "ui/base/l10n/l10n_util.h"
|
| +#include "ui/views/controls/button/toggle_button.h"
|
| +#include "ui/views/layout/box_layout.h"
|
| +#include "ui/views/painter.h"
|
| +
|
| +namespace ash {
|
| +
|
| +// TODO(tdanderson): Update the focus rect color, border thickness, and
|
| +// location for material design.
|
| +SystemMenuToggleButton::SystemMenuToggleButton(views::ButtonListener* listener,
|
| + int accessible_name_id)
|
| + : views::Button(nullptr),
|
| + focus_painter_(views::Painter::CreateSolidFocusPainter(kFocusBorderColor,
|
| + gfx::Insets(1))),
|
| + listener_(listener) {
|
| + SetTooltipText(l10n_util::GetStringUTF16(accessible_name_id));
|
| +
|
| + // Center ToggleButton in this container.
|
| + views::BoxLayout* container_layout =
|
| + new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0);
|
| + container_layout->set_main_axis_alignment(
|
| + views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
|
| + container_layout->set_cross_axis_alignment(
|
| + views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
|
| + SetLayoutManager(container_layout);
|
| +
|
| + toggle_ = new views::ToggleButton(this);
|
| + toggle_->SetFocusForPlatform();
|
| + AddChildView(toggle_);
|
| +}
|
| +
|
| +SystemMenuToggleButton::~SystemMenuToggleButton() {}
|
| +
|
| +void SystemMenuToggleButton::SetIsOn(bool is_on, bool animate) {
|
| + toggle_->SetIsOn(is_on, animate);
|
| +}
|
| +
|
| +bool SystemMenuToggleButton::is_on() const {
|
| + return toggle_->is_on();
|
| +}
|
| +
|
| +void SystemMenuToggleButton::NativeViewHierarchyChanged() {
|
| + views::FocusManager* focus_manager = GetFocusManager();
|
| + if (focus_manager_ != focus_manager) {
|
| + UnregisterFocusObserver();
|
| + if (focus_manager)
|
| + RegisterFocusObserver();
|
| + }
|
| + views::Button::NativeViewHierarchyChanged();
|
| +}
|
| +
|
| +void SystemMenuToggleButton::ViewHierarchyChanged(
|
| + const ViewHierarchyChangedDetails& details) {
|
| + if (details.is_add) {
|
| + RegisterFocusObserver();
|
| + } else if (details.child == this) {
|
| + UnregisterFocusObserver();
|
| + }
|
| +}
|
| +
|
| +gfx::Size SystemMenuToggleButton::GetPreferredSize() const {
|
| + const int kButtonHorizontalPadding = 14;
|
| + return gfx::Size(toggle_->width() + 2 * kButtonHorizontalPadding,
|
| + kMenuButtonSize);
|
| +}
|
| +
|
| +int SystemMenuToggleButton::GetHeightForWidth(int w) const {
|
| + // Make row height fixed avoiding layout manager adjustments.
|
| + return GetPreferredSize().height();
|
| +}
|
| +
|
| +void SystemMenuToggleButton::OnPaint(gfx::Canvas* canvas) {
|
| + // Call the base class first to paint any background/borders.
|
| + views::Button::OnPaint(canvas);
|
| + if (toggle_->HasFocus()) {
|
| + views::Painter::PaintPainterAt(canvas, focus_painter(), GetLocalBounds());
|
| + }
|
| +}
|
| +
|
| +void SystemMenuToggleButton::ButtonPressed(views::Button* sender,
|
| + const ui::Event& event) {
|
| + DCHECK_EQ(toggle_, sender);
|
| + if (listener_)
|
| + listener_->ButtonPressed(this, event);
|
| +}
|
| +
|
| +void SystemMenuToggleButton::OnWillChangeFocus(views::View* focused_before,
|
| + views::View* focused_now) {}
|
| +
|
| +void SystemMenuToggleButton::OnDidChangeFocus(views::View* focused_before,
|
| + views::View* focused_now) {
|
| + if (focused_now == toggle_ || focused_before == toggle_)
|
| + SchedulePaint();
|
| +}
|
| +
|
| +void SystemMenuToggleButton::RegisterFocusObserver() {
|
| + if (!GetWidget())
|
| + return;
|
| + views::FocusManager* focus_manager = GetFocusManager();
|
| + if (focus_manager_ == focus_manager)
|
| + return;
|
| + focus_manager_ = focus_manager;
|
| + if (focus_manager_)
|
| + focus_manager_->AddFocusChangeListener(this);
|
| +}
|
| +
|
| +void SystemMenuToggleButton::UnregisterFocusObserver() {
|
| + if (GetWidget() && focus_manager_) {
|
| + focus_manager_->RemoveFocusChangeListener(this);
|
| + focus_manager_ = NULL;
|
| + }
|
| +}
|
| +
|
| +} // namespace ash
|
|
|