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"
14 #include "ui/views/resources/grit/views_resources.h"
19 const int kBarImagesActive
[] = {
20 IDR_SLIDER_ACTIVE_LEFT
,
21 IDR_SLIDER_ACTIVE_CENTER
,
22 IDR_SLIDER_ACTIVE_RIGHT
,
25 const int kBarImagesDisabled
[] = {
26 IDR_SLIDER_DISABLED_LEFT
,
27 IDR_SLIDER_DISABLED_CENTER
,
28 IDR_SLIDER_DISABLED_RIGHT
,
33 class TrayBarButtonWithTitle::TrayBarButton
: public views::View
{
35 TrayBarButton(const int bar_active_images
[], const int bar_disabled_images
[])
37 bar_active_images_(bar_active_images
),
38 bar_disabled_images_(bar_disabled_images
),
39 painter_(new views::HorizontalPainter(bar_active_images_
)){
41 ~TrayBarButton() override
{}
43 // Overriden from views::View:
44 void OnPaint(gfx::Canvas
* canvas
) override
{
45 painter_
->Paint(canvas
, size());
48 void Update(bool control_on
) {
49 painter_
.reset(new views::HorizontalPainter(
50 control_on
? bar_active_images_
: bar_disabled_images_
));
55 const int* bar_active_images_
;
56 const int* bar_disabled_images_
;
57 scoped_ptr
<views::HorizontalPainter
> painter_
;
59 DISALLOW_COPY_AND_ASSIGN(TrayBarButton
);
62 TrayBarButtonWithTitle::TrayBarButtonWithTitle(views::ButtonListener
* listener
,
65 : views::CustomButton(listener
),
66 image_(new TrayBarButton(kBarImagesActive
, kBarImagesDisabled
)),
71 title_
= new views::Label
;
72 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
73 base::string16 text
= rb
.GetLocalizedString(title_id
);
74 title_
->SetText(text
);
78 image_height_
= ui::ResourceBundle::GetSharedInstance().GetImageNamed(
79 kBarImagesActive
[0]).ToImageSkia()->height();
82 TrayBarButtonWithTitle::~TrayBarButtonWithTitle() {}
84 void TrayBarButtonWithTitle::UpdateButton(bool control_on
) {
85 image_
->Update(control_on
);
88 gfx::Size
TrayBarButtonWithTitle::GetPreferredSize() const {
89 return gfx::Size(width_
, kTrayPopupItemHeight
);
92 void TrayBarButtonWithTitle::Layout() {
93 gfx::Rect
rect(GetContentsBounds());
94 int bar_image_y
= rect
.height() / 2 - image_height_
/ 2;
95 gfx::Rect
bar_image_rect(rect
.x(),
99 image_
->SetBoundsRect(bar_image_rect
);
101 // The image_ has some empty space below the bar image, move the title
102 // a little bit up to look closer to the bar.
103 gfx::Size title_size
= title_
->GetPreferredSize();
104 title_
->SetBounds(rect
.x(),
105 bar_image_y
+ image_height_
- 3,
107 title_size
.height());