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"
22 // The border color of the user button.
23 const SkColor kBorderColor
= 0xffdcdcdc;
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
),
35 highlight_on_hover_(highlight_on_hover
),
36 button_hovered_(false),
38 tab_frame_inset_(tab_frame_inset
) {
39 set_notify_enter_exit_on_child(true);
41 new views::BoxLayout(views::BoxLayout::kHorizontal
, 1, 1, 0));
42 AddChildView(content_
);
44 // Only make it focusable when we are active/interested in clicks.
49 ButtonFromView::~ButtonFromView() {}
51 void ButtonFromView::ForceBorderVisible(bool show
) {
56 void ButtonFromView::OnMouseEntered(const ui::MouseEvent
& event
) {
57 button_hovered_
= true;
61 void ButtonFromView::OnMouseExited(const ui::MouseEvent
& event
) {
62 button_hovered_
= false;
66 void ButtonFromView::OnPaint(gfx::Canvas
* canvas
) {
67 View::OnPaint(canvas
);
69 gfx::Rect
rect(GetLocalBounds());
70 rect
.Inset(tab_frame_inset_
);
71 canvas
->DrawSolidFocusRect(rect
, kFocusBorderColor
);
75 void ButtonFromView::OnFocus() {
77 // Adding focus frame.
81 void ButtonFromView::OnBlur() {
83 // Removing focus frame.
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
= JoinString(labels
, base::ASCIIToUTF16(" "));
95 void ButtonFromView::ShowActive() {
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
));