tray_item_more.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 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/tray/tray_item_more.h"
6
7#include "ash/system/tray/fixed_sized_image_view.h"
8#include "ash/system/tray/system_tray_item.h"
9#include "ash/system/tray/tray_constants.h"
10#include "ash/system/tray/tray_views.h"
11#include "grit/ash_resources.h"
12#include "ui/base/accessibility/accessible_view_state.h"
13#include "ui/base/resource/resource_bundle.h"
14#include "ui/gfx/image/image.h"
15#include "ui/views/controls/image_view.h"
16#include "ui/views/controls/label.h"
17#include "ui/views/layout/box_layout.h"
18
19namespace ash {
20namespace internal {
21
22TrayItemMore::TrayItemMore(SystemTrayItem* owner, bool show_more)
23    : owner_(owner),
24      show_more_(show_more),
25      icon_(NULL),
26      label_(NULL),
27      more_(NULL) {
28  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
29      kTrayPopupPaddingHorizontal, 0, kTrayPopupPaddingBetweenItems));
30
31  icon_ = new FixedSizedImageView(0, kTrayPopupItemHeight);
32  AddChildView(icon_);
33
34  label_ = new views::Label;
35  label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
36  AddChildView(label_);
37
38  if (show_more) {
39    more_ = new views::ImageView;
40    more_->EnableCanvasFlippingForRTLUI(true);
41    more_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
42        IDR_AURA_UBER_TRAY_MORE).ToImageSkia());
43    AddChildView(more_);
44  }
45}
46
47TrayItemMore::~TrayItemMore() {
48}
49
50void TrayItemMore::SetLabel(const string16& label) {
51  label_->SetText(label);
52  Layout();
53  SchedulePaint();
54}
55
56void TrayItemMore::SetImage(const gfx::ImageSkia* image_skia) {
57  icon_->SetImage(image_skia);
58  SchedulePaint();
59}
60
61void TrayItemMore::SetAccessibleName(const string16& name) {
62  accessible_name_ = name;
63}
64
65void TrayItemMore::ReplaceIcon(views::View* view) {
66  delete icon_;
67  icon_ = NULL;
68  AddChildViewAt(view, 0);
69}
70
71bool TrayItemMore::PerformAction(const ui::Event& event) {
72  if (!show_more_)
73    return false;
74
75  owner()->TransitionDetailedView();
76  return true;
77}
78
79void TrayItemMore::Layout() {
80  // Let the box-layout do the layout first. Then move the '>' arrow to right
81  // align.
82  views::View::Layout();
83
84  if (!show_more_)
85    return;
86
87  // Make sure the chevron always has the full size.
88  gfx::Size size = more_->GetPreferredSize();
89  gfx::Rect bounds(size);
90  bounds.set_x(width() - size.width() - kTrayPopupPaddingBetweenItems);
91  bounds.set_y((height() - size.height()) / 2);
92  more_->SetBoundsRect(bounds);
93
94  // Adjust the label's bounds in case it got cut off by |more_|.
95  if (label_->bounds().Intersects(more_->bounds())) {
96    gfx::Rect bounds = label_->bounds();
97    bounds.set_width(more_->x() - kTrayPopupPaddingBetweenItems - label_->x());
98    label_->SetBoundsRect(bounds);
99  }
100}
101
102void TrayItemMore::GetAccessibleState(ui::AccessibleViewState* state) {
103  state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
104  state->name = accessible_name_;
105}
106
107}  // namespace internal
108}  // namespace ash
109