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.
4
5#include "ash/system/user/button_from_view.h"
6
7#include "ash/ash_constants.h"
8#include "ash/system/tray/tray_constants.h"
9#include "ui/gfx/canvas.h"
10#include "ui/views/background.h"
11#include "ui/views/border.h"
12#include "ui/views/layout/box_layout.h"
13
14namespace ash {
15
16namespace {
17
18// The border color of the user button.
19const SkColor kBorderColor = 0xffdcdcdc;
20
21}  // namespace
22
23namespace tray {
24
25ButtonFromView::ButtonFromView(views::View* content,
26                               views::ButtonListener* listener,
27                               bool highlight_on_hover,
28                               const gfx::Insets& tab_frame_inset)
29    : CustomButton(listener),
30      content_(content),
31      highlight_on_hover_(highlight_on_hover),
32      button_hovered_(false),
33      show_border_(false),
34      tab_frame_inset_(tab_frame_inset) {
35  set_notify_enter_exit_on_child(true);
36  SetLayoutManager(
37      new views::BoxLayout(views::BoxLayout::kHorizontal, 1, 1, 0));
38  AddChildView(content_);
39  ShowActive();
40  // Only make it focusable when we are active/interested in clicks.
41  if (listener)
42    SetFocusable(true);
43}
44
45ButtonFromView::~ButtonFromView() {}
46
47void ButtonFromView::ForceBorderVisible(bool show) {
48  show_border_ = show;
49  ShowActive();
50}
51
52void ButtonFromView::OnMouseEntered(const ui::MouseEvent& event) {
53  button_hovered_ = true;
54  ShowActive();
55}
56
57void ButtonFromView::OnMouseExited(const ui::MouseEvent& event) {
58  button_hovered_ = false;
59  ShowActive();
60}
61
62void ButtonFromView::OnPaint(gfx::Canvas* canvas) {
63  View::OnPaint(canvas);
64  if (HasFocus()) {
65    gfx::Rect rect(GetLocalBounds());
66    rect.Inset(tab_frame_inset_);
67    canvas->DrawSolidFocusRect(rect, kFocusBorderColor);
68  }
69}
70
71void ButtonFromView::OnFocus() {
72  View::OnFocus();
73  // Adding focus frame.
74  SchedulePaint();
75}
76
77void ButtonFromView::OnBlur() {
78  View::OnBlur();
79  // Removing focus frame.
80  SchedulePaint();
81}
82
83void ButtonFromView::ShowActive() {
84  bool border_visible =
85      (button_hovered_ && highlight_on_hover_) || show_border_;
86  SkColor border_color = border_visible ? kBorderColor : SK_ColorTRANSPARENT;
87  SetBorder(views::Border::CreateSolidBorder(1, border_color));
88  if (highlight_on_hover_) {
89    SkColor background_color =
90        button_hovered_ ? kHoverBackgroundColor : kBackgroundColor;
91    content_->set_background(
92        views::Background::CreateSolidBackground(background_color));
93    set_background(views::Background::CreateSolidBackground(background_color));
94  }
95  SchedulePaint();
96}
97
98}  // namespace tray
99}  // namespace ash
100