Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / chromeos / screen_security / screen_tray_item.cc
blob2b3667d7bd8fa9645e3d718d73fe174e55ec5b80
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/chromeos/screen_security/screen_tray_item.h"
7 #include "ash/system/tray/fixed_sized_image_view.h"
8 #include "ash/system/tray/tray_constants.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/message_center/message_center.h"
11 #include "ui/views/controls/label.h"
12 #include "ui/views/layout/box_layout.h"
14 namespace {
15 const int kStopButtonRightPadding = 18;
16 } // namespace
18 namespace ash {
19 namespace tray {
21 // ScreenTrayView implementations.
22 ScreenTrayView::ScreenTrayView(ScreenTrayItem* screen_tray_item, int icon_id)
23 : TrayItemView(screen_tray_item),
24 screen_tray_item_(screen_tray_item) {
25 CreateImageView();
26 image_view()->SetImage(ui::ResourceBundle::GetSharedInstance()
27 .GetImageNamed(icon_id).ToImageSkia());
29 Update();
32 ScreenTrayView::~ScreenTrayView() {
35 void ScreenTrayView::Update() {
36 SetVisible(screen_tray_item_->is_started());
40 // ScreenStatusView implementations.
41 ScreenStatusView::ScreenStatusView(ScreenTrayItem* screen_tray_item,
42 int icon_id,
43 const base::string16& label_text,
44 const base::string16& stop_button_text)
45 : screen_tray_item_(screen_tray_item),
46 icon_(NULL),
47 label_(NULL),
48 stop_button_(NULL),
49 icon_id_(icon_id),
50 label_text_(label_text),
51 stop_button_text_(stop_button_text) {
52 CreateItems();
53 Update();
56 ScreenStatusView::~ScreenStatusView() {
59 void ScreenStatusView::Layout() {
60 views::View::Layout();
62 // Give the stop button the space it requests.
63 gfx::Size stop_size = stop_button_->GetPreferredSize();
64 gfx::Rect stop_bounds(stop_size);
65 stop_bounds.set_x(width() - stop_size.width() - kStopButtonRightPadding);
66 stop_bounds.set_y((height() - stop_size.height()) / 2);
67 stop_button_->SetBoundsRect(stop_bounds);
69 // Adjust the label's bounds in case it got cut off by |stop_button_|.
70 if (label_->bounds().Intersects(stop_button_->bounds())) {
71 gfx::Rect label_bounds = label_->bounds();
72 label_bounds.set_width(
73 stop_button_->x() - kTrayPopupPaddingBetweenItems - label_->x());
74 label_->SetBoundsRect(label_bounds);
78 void ScreenStatusView::ButtonPressed(
79 views::Button* sender,
80 const ui::Event& event) {
81 DCHECK(sender == stop_button_);
82 screen_tray_item_->Stop();
85 void ScreenStatusView::CreateItems() {
86 set_background(views::Background::CreateSolidBackground(kBackgroundColor));
87 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
88 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
89 kTrayPopupPaddingHorizontal,
91 kTrayPopupPaddingBetweenItems));
92 icon_ = new FixedSizedImageView(0, kTrayPopupItemHeight);
93 icon_->SetImage(bundle.GetImageNamed(icon_id_).ToImageSkia());
94 AddChildView(icon_);
95 label_ = new views::Label;
96 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
97 label_->SetMultiLine(true);
98 label_->SetText(label_text_);
99 AddChildView(label_);
101 stop_button_ = new TrayPopupLabelButton(this, stop_button_text_);
102 AddChildView(stop_button_);
105 void ScreenStatusView::Update() {
106 // Hide the notification bubble when the ash tray bubble opens.
107 screen_tray_item_->HideNotificationView();
108 SetVisible(screen_tray_item_->is_started());
111 ScreenNotificationDelegate::ScreenNotificationDelegate(
112 ScreenTrayItem* screen_tray)
113 : screen_tray_(screen_tray) {
116 ScreenNotificationDelegate::~ScreenNotificationDelegate() {
119 void ScreenNotificationDelegate::ButtonClick(int button_index) {
120 DCHECK_EQ(0, button_index);
121 screen_tray_->Stop();
124 } // namespace tray
126 ScreenTrayItem::ScreenTrayItem(SystemTray* system_tray)
127 : SystemTrayItem(system_tray),
128 tray_view_(NULL),
129 default_view_(NULL),
130 is_started_(false),
131 stop_callback_(base::Bind(&base::DoNothing)) {
134 ScreenTrayItem::~ScreenTrayItem() {}
136 void ScreenTrayItem::Update() {
137 if (tray_view_)
138 tray_view_->Update();
139 if (default_view_)
140 default_view_->Update();
141 if (is_started_) {
142 CreateOrUpdateNotification();
143 } else {
144 message_center::MessageCenter::Get()->RemoveNotification(
145 GetNotificationId(), false /* by_user */);
149 void ScreenTrayItem::Start(const base::Closure& stop_callback) {
150 stop_callback_ = stop_callback;
151 is_started_ = true;
153 if (tray_view_)
154 tray_view_->Update();
156 if (default_view_)
157 default_view_->Update();
159 if (!system_tray()->HasSystemBubbleType(
160 SystemTrayBubble::BUBBLE_TYPE_DEFAULT)) {
161 CreateOrUpdateNotification();
165 void ScreenTrayItem::Stop() {
166 is_started_ = false;
167 Update();
169 if (stop_callback_.is_null())
170 return;
172 base::Closure callback = stop_callback_;
173 stop_callback_.Reset();
174 callback.Run();
177 void ScreenTrayItem::DestroyTrayView() {
178 tray_view_ = NULL;
181 void ScreenTrayItem::DestroyDefaultView() {
182 default_view_ = NULL;
185 void ScreenTrayItem::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
186 if (!tray_view_)
187 return;
189 // Center the item dependent on the orientation of the shelf.
190 views::BoxLayout::Orientation layout =
191 (alignment == ash::SHELF_ALIGNMENT_BOTTOM ||
192 alignment == ash::SHELF_ALIGNMENT_TOP)
193 ? views::BoxLayout::kHorizontal
194 : views::BoxLayout::kVertical;
195 tray_view_->SetLayoutManager(new views::BoxLayout(layout, 0, 0, 0));
196 tray_view_->Layout();
199 } // namespace ash