summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/status/status_area_button.cc
blob: c524046428a76298dc6a2e3a5cee1a01f68fcfca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) 2012 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 "chrome/browser/chromeos/status/status_area_button.h"

#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/skbitmap_operations.h"
#include "ui/views/border.h"
#include "ui/views/view.h"

namespace {

// Colors for different text styles.
const SkColor kWhitePlainTextColor = 0x99ffffff;
const SkColor kGrayPlainTextColor = 0x99666666;
const SkColor kWhiteHaloedTextColor = 0xb3ffffff;
const SkColor kWhiteHaloedHaloColor = 0xb3000000;
const SkColor kGrayEmbossedTextColor = 0xff4c4c4c;
const SkColor kGrayEmbossedShadowColor = 0x80ffffff;

// Status area font is bigger.
const int kFontSizeDelta = 3;

// Pad for status icons.
const int kIconHorizontalPad = 2;

}

////////////////////////////////////////////////////////////////////////////////
// StatusAreaButton

StatusAreaButton::StatusAreaButton(Delegate* button_delegate,
                                   views::ViewMenuDelegate* menu_delegate)
    : MenuButton(NULL, string16(), menu_delegate, false),
      menu_active_(true),
      delegate_(button_delegate) {
  set_border(NULL);

  gfx::Font font =
      ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
  font = font.DeriveFont(kFontSizeDelta);
  font = delegate_->GetStatusAreaFont(font);
  SetFont(font);

  SetShowMultipleIconStates(false);
  set_alignment(TextButton::ALIGN_CENTER);
  set_border(views::Border::CreateEmptyBorder(
      0, kIconHorizontalPad, 0, kIconHorizontalPad));

  // Use an offset that is top aligned with toolbar.
  set_menu_offset(0, 4);

  UpdateTextStyle();
}

void StatusAreaButton::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) {
  if (state() == BS_PUSHED) {
    // Apply 10% white when pushed down.
    canvas->FillRect(SkColorSetARGB(0x19, 0xFF, 0xFF, 0xFF), GetLocalBounds());
  }

  views::MenuButton::PaintButton(canvas, mode);
}

void StatusAreaButton::SetText(const string16& text) {
  // TextButtons normally remember the max text size, so the button's preferred
  // size will always be as large as the largest text ever put in it.
  // We clear that max text size, so we can adjust the size to fit the text.
  // The order is important.  ClearMaxTextSize sets the size to that of the
  // current text, so it must be called after SetText.
  views::MenuButton::SetText(text);
  ClearMaxTextSize();
  PreferredSizeChanged();
}

bool StatusAreaButton::Activate() {
  if (menu_active_)
    return views::MenuButton::Activate();
  else
    return true;
}

gfx::Size StatusAreaButton::GetPreferredSize() {
  gfx::Insets insets = views::MenuButton::GetInsets();
  gfx::Size prefsize(icon_width() + insets.width(),
                     icon_height() + insets.height());

  // Adjusts size when use menu button paint.
  gfx::Size menu_button_size = views::MenuButton::GetPreferredSize();
  prefsize.SetSize(std::max(prefsize.width(), menu_button_size.width()),
                   std::max(prefsize.height(), menu_button_size.height()));

  // Shift 1-pixel down for odd number of pixels in vertical space.
  if ((prefsize.height() - menu_button_size.height()) % 2) {
    insets_.Set(insets.top() + 1, insets.left(),
                insets.bottom(), insets.right());
  }

  // Add padding.
  prefsize.Enlarge(2 * horizontal_padding(), 0);

  return prefsize;
}

gfx::Insets StatusAreaButton::GetInsets() const {
  return insets_;
}

void StatusAreaButton::OnThemeChanged() {
  UpdateTextStyle();
}

void StatusAreaButton::SetVisible(bool is_visible) {
  if (is_visible != visible()) {
    views::MenuButton::SetVisible(is_visible);
    delegate_->ButtonVisibilityChanged(this);
  }
}

bool StatusAreaButton::HitTest(const gfx::Point& l) const {
  // BrowserFrameViewChromeos adds a few pixels of pad to the top of the
  // status area. For mouse events in this area to activate the button,
  // hit testing need to be successful. We do this by clamping y to the
  // position of the menu button view.
  gfx::Point point(l.x(), std::max(y(), l.y()));
  return MenuButton::HitTest(point);
}

void StatusAreaButton::SetMenuActive(bool active) {
  menu_active_ = active;
}

void StatusAreaButton::UpdateTextStyle() {
  ClearEmbellishing();
  switch (delegate_->GetStatusAreaTextStyle()) {
    case WHITE_PLAIN:
      SetEnabledColor(kWhitePlainTextColor);
      break;
    case GRAY_PLAIN:
      SetEnabledColor(kGrayPlainTextColor);
      break;
    case WHITE_HALOED:
      SetEnabledColor(kWhiteHaloedTextColor);
      SetTextHaloColor(kWhiteHaloedHaloColor);
      break;
    case GRAY_EMBOSSED:
      SetEnabledColor(kGrayEmbossedTextColor);
      SetTextShadowColors(kGrayEmbossedShadowColor, kGrayEmbossedShadowColor);
      SetTextShadowOffset(0, 1);
      break;
  }
  SchedulePaint();
}

int StatusAreaButton::icon_height() {
  return 24;
}

int StatusAreaButton::icon_width() {
  return 23;
}

int StatusAreaButton::horizontal_padding() {
  return 1;
}