summaryrefslogtreecommitdiffstats
path: root/ash/system/tray/tray_item_more.cc
blob: f16f97b7aad8d91bbd0d35aea13ce9c00ebfbdf8 (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
// 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 "ash/system/tray/tray_item_more.h"

#include "ash/system/tray/fixed_sized_image_view.h"
#include "ash/system/tray/system_tray_item.h"
#include "ash/system/tray/tray_constants.h"
#include "grit/ash_resources.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"

namespace ash {

TrayItemMore::TrayItemMore(SystemTrayItem* owner, bool show_more)
    : owner_(owner),
      show_more_(show_more),
      icon_(NULL),
      label_(NULL),
      more_(NULL) {
  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
      kTrayPopupPaddingHorizontal, 0, kTrayPopupPaddingBetweenItems));

  icon_ = new FixedSizedImageView(0, kTrayPopupItemHeight);
  AddChildView(icon_);

  label_ = new views::Label;
  label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  AddChildView(label_);

  if (show_more) {
    more_ = new views::ImageView;
    more_->EnableCanvasFlippingForRTLUI(true);
    more_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
        IDR_AURA_UBER_TRAY_MORE).ToImageSkia());
    AddChildView(more_);
  }
}

TrayItemMore::~TrayItemMore() {
}

void TrayItemMore::SetLabel(const base::string16& label) {
  label_->SetText(label);
  Layout();
  SchedulePaint();
}

void TrayItemMore::SetImage(const gfx::ImageSkia* image_skia) {
  icon_->SetImage(image_skia);
  SchedulePaint();
}

void TrayItemMore::SetAccessibleName(const base::string16& name) {
  accessible_name_ = name;
}

void TrayItemMore::ReplaceIcon(views::View* view) {
  delete icon_;
  icon_ = NULL;
  AddChildViewAt(view, 0);
}

bool TrayItemMore::PerformAction(const ui::Event& event) {
  if (!show_more_)
    return false;

  owner()->TransitionDetailedView();
  return true;
}

void TrayItemMore::Layout() {
  // Let the box-layout do the layout first. Then move the '>' arrow to right
  // align.
  views::View::Layout();

  if (!show_more_)
    return;

  // Make sure the chevron always has the full size.
  gfx::Size size = more_->GetPreferredSize();
  gfx::Rect bounds(size);
  bounds.set_x(width() - size.width() - kTrayPopupPaddingBetweenItems);
  bounds.set_y((height() - size.height()) / 2);
  more_->SetBoundsRect(bounds);

  // Adjust the label's bounds in case it got cut off by |more_|.
  if (label_->bounds().Intersects(more_->bounds())) {
    gfx::Rect bounds = label_->bounds();
    bounds.set_width(more_->x() - kTrayPopupPaddingBetweenItems - label_->x());
    label_->SetBoundsRect(bounds);
  }
}

void TrayItemMore::GetAccessibleState(ui::AXViewState* state) {
  ActionableView::GetAccessibleState(state);
  if (!accessible_name_.empty())
    state->name = accessible_name_;
}

}  // namespace ash