Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / user / button_from_view.cc
blobb3c434d47f770be421c88be7d51bcaad44781e13
1 // Copyright 2014 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.
5 #include "ash/system/user/button_from_view.h"
7 #include "ash/ash_constants.h"
8 #include "ash/system/tray/tray_constants.h"
9 #include "ash/system/tray/tray_utils.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "ui/accessibility/ax_view_state.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/views/background.h"
15 #include "ui/views/border.h"
16 #include "ui/views/layout/box_layout.h"
18 namespace ash {
20 namespace {
22 // The border color of the user button.
23 const SkColor kBorderColor = 0xffdcdcdc;
25 } // namespace
27 namespace tray {
29 ButtonFromView::ButtonFromView(views::View* content,
30 views::ButtonListener* listener,
31 bool highlight_on_hover,
32 const gfx::Insets& tab_frame_inset)
33 : CustomButton(listener),
34 content_(content),
35 highlight_on_hover_(highlight_on_hover),
36 button_hovered_(false),
37 show_border_(false),
38 tab_frame_inset_(tab_frame_inset) {
39 set_notify_enter_exit_on_child(true);
40 SetLayoutManager(
41 new views::BoxLayout(views::BoxLayout::kHorizontal, 1, 1, 0));
42 AddChildView(content_);
43 ShowActive();
44 // Only make it focusable when we are active/interested in clicks.
45 if (listener)
46 SetFocusable(true);
49 ButtonFromView::~ButtonFromView() {}
51 void ButtonFromView::ForceBorderVisible(bool show) {
52 show_border_ = show;
53 ShowActive();
56 void ButtonFromView::OnMouseEntered(const ui::MouseEvent& event) {
57 button_hovered_ = true;
58 ShowActive();
61 void ButtonFromView::OnMouseExited(const ui::MouseEvent& event) {
62 button_hovered_ = false;
63 ShowActive();
66 void ButtonFromView::OnPaint(gfx::Canvas* canvas) {
67 View::OnPaint(canvas);
68 if (HasFocus()) {
69 gfx::Rect rect(GetLocalBounds());
70 rect.Inset(tab_frame_inset_);
71 canvas->DrawSolidFocusRect(rect, kFocusBorderColor);
75 void ButtonFromView::OnFocus() {
76 View::OnFocus();
77 // Adding focus frame.
78 SchedulePaint();
81 void ButtonFromView::OnBlur() {
82 View::OnBlur();
83 // Removing focus frame.
84 SchedulePaint();
87 void ButtonFromView::GetAccessibleState(ui::AXViewState* state) {
88 state->role = ui::AX_ROLE_BUTTON;
89 std::vector<base::string16> labels;
90 for (int i = 0; i < child_count(); ++i)
91 GetAccessibleLabelFromDescendantViews(child_at(i), labels);
92 state->name = base::JoinString(labels, base::ASCIIToUTF16(" "));
95 void ButtonFromView::ShowActive() {
96 bool border_visible =
97 (button_hovered_ && highlight_on_hover_) || show_border_;
98 SkColor border_color = border_visible ? kBorderColor : SK_ColorTRANSPARENT;
99 SetBorder(views::Border::CreateSolidBorder(1, border_color));
100 if (highlight_on_hover_) {
101 SkColor background_color =
102 button_hovered_ ? kHoverBackgroundColor : kBackgroundColor;
103 content_->set_background(
104 views::Background::CreateSolidBackground(background_color));
105 set_background(views::Background::CreateSolidBackground(background_color));
107 SchedulePaint();
110 } // namespace tray
111 } // namespace ash