1 // Copyright 2013 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/tray/tray_bar_button_with_title.h"
7 #include "ash/system/tray/tray_constants.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/image/image_skia.h"
11 #include "ui/resources/grit/ui_resources.h"
12 #include "ui/views/controls/label.h"
13 #include "ui/views/painter.h"
18 const int kBarImagesActive
[] = {
19 IDR_SLIDER_ACTIVE_LEFT
,
20 IDR_SLIDER_ACTIVE_CENTER
,
21 IDR_SLIDER_ACTIVE_RIGHT
,
24 const int kBarImagesDisabled
[] = {
25 IDR_SLIDER_DISABLED_LEFT
,
26 IDR_SLIDER_DISABLED_CENTER
,
27 IDR_SLIDER_DISABLED_RIGHT
,
32 class TrayBarButtonWithTitle::TrayBarButton
: public views::View
{
34 TrayBarButton(const int bar_active_images
[], const int bar_disabled_images
[])
36 bar_active_images_(bar_active_images
),
37 bar_disabled_images_(bar_disabled_images
),
38 painter_(new views::HorizontalPainter(bar_active_images_
)){
40 virtual ~TrayBarButton() {}
42 // Overriden from views::View:
43 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
44 painter_
->Paint(canvas
, size());
47 void Update(bool control_on
) {
48 painter_
.reset(new views::HorizontalPainter(
49 control_on
? bar_active_images_
: bar_disabled_images_
));
54 const int* bar_active_images_
;
55 const int* bar_disabled_images_
;
56 scoped_ptr
<views::HorizontalPainter
> painter_
;
58 DISALLOW_COPY_AND_ASSIGN(TrayBarButton
);
61 TrayBarButtonWithTitle::TrayBarButtonWithTitle(views::ButtonListener
* listener
,
64 : views::CustomButton(listener
),
65 image_(new TrayBarButton(kBarImagesActive
, kBarImagesDisabled
)),
70 title_
= new views::Label
;
71 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
72 base::string16 text
= rb
.GetLocalizedString(title_id
);
73 title_
->SetText(text
);
77 image_height_
= ui::ResourceBundle::GetSharedInstance().GetImageNamed(
78 kBarImagesActive
[0]).ToImageSkia()->height();
81 TrayBarButtonWithTitle::~TrayBarButtonWithTitle() {}
83 void TrayBarButtonWithTitle::UpdateButton(bool control_on
) {
84 image_
->Update(control_on
);
87 gfx::Size
TrayBarButtonWithTitle::GetPreferredSize() const {
88 return gfx::Size(width_
, kTrayPopupItemHeight
);
91 void TrayBarButtonWithTitle::Layout() {
92 gfx::Rect
rect(GetContentsBounds());
93 int bar_image_y
= rect
.height() / 2 - image_height_
/ 2;
94 gfx::Rect
bar_image_rect(rect
.x(),
98 image_
->SetBoundsRect(bar_image_rect
);
100 // The image_ has some empty space below the bar image, move the title
101 // a little bit up to look closer to the bar.
102 gfx::Size title_size
= title_
->GetPreferredSize();
103 title_
->SetBounds(rect
.x(),
104 bar_image_y
+ image_height_
- 3,
106 title_size
.height());