1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ash/system/chromeos/screen_security/screen_tray_item.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ash/system/tray/fixed_sized_image_view.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ash/system/tray/tray_constants.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
10bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch#include "ui/message_center/message_center.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/views/controls/label.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ui/views/layout/box_layout.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace {
15bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdochconst int kStopButtonRightPadding = 18;
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace ash {
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace tray {
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// ScreenTrayView implementations.
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)ScreenTrayView::ScreenTrayView(ScreenTrayItem* screen_tray_item, int icon_id)
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : TrayItemView(screen_tray_item),
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      screen_tray_item_(screen_tray_item) {
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CreateImageView();
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  image_view()->SetImage(ui::ResourceBundle::GetSharedInstance()
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      .GetImageNamed(icon_id).ToImageSkia());
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Update();
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)ScreenTrayView::~ScreenTrayView() {
33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ScreenTrayView::Update() {
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  SetVisible(screen_tray_item_->is_started());
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// ScreenStatusView implementations.
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)ScreenStatusView::ScreenStatusView(ScreenTrayItem* screen_tray_item,
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                   int icon_id,
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                   const base::string16& label_text,
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                   const base::string16& stop_button_text)
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : screen_tray_item_(screen_tray_item),
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      icon_(NULL),
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      label_(NULL),
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      stop_button_(NULL),
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      icon_id_(icon_id),
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      label_text_(label_text),
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      stop_button_text_(stop_button_text) {
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  CreateItems();
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Update();
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)ScreenStatusView::~ScreenStatusView() {
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ScreenStatusView::Layout() {
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  views::View::Layout();
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Give the stop button the space it requests.
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  gfx::Size stop_size = stop_button_->GetPreferredSize();
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  gfx::Rect stop_bounds(stop_size);
65bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  stop_bounds.set_x(width() - stop_size.width() - kStopButtonRightPadding);
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  stop_bounds.set_y((height() - stop_size.height()) / 2);
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  stop_button_->SetBoundsRect(stop_bounds);
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Adjust the label's bounds in case it got cut off by |stop_button_|.
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (label_->bounds().Intersects(stop_button_->bounds())) {
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    gfx::Rect label_bounds = label_->bounds();
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    label_bounds.set_width(
73bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch        stop_button_->x() - kTrayPopupPaddingBetweenItems - label_->x());
74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    label_->SetBoundsRect(label_bounds);
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ScreenStatusView::ButtonPressed(
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    views::Button* sender,
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const ui::Event& event) {
81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  DCHECK(sender == stop_button_);
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  screen_tray_item_->Stop();
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ScreenStatusView::CreateItems() {
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  set_background(views::Background::CreateSolidBackground(kBackgroundColor));
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
88bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
89bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch                                        kTrayPopupPaddingHorizontal,
90bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch                                        0,
91bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch                                        kTrayPopupPaddingBetweenItems));
92bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  icon_ = new FixedSizedImageView(0, kTrayPopupItemHeight);
93bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  icon_->SetImage(bundle.GetImageNamed(icon_id_).ToImageSkia());
94bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  AddChildView(icon_);
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  label_ = new views::Label;
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  label_->SetMultiLine(true);
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  label_->SetText(label_text_);
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AddChildView(label_);
100868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  stop_button_ = new TrayPopupLabelButton(this, stop_button_text_);
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AddChildView(stop_button_);
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
105868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ScreenStatusView::Update() {
106bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  // Hide the notification bubble when the ash tray bubble opens.
107bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  screen_tray_item_->HideNotificationView();
108bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  SetVisible(screen_tray_item_->is_started());
109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
111bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben MurdochScreenNotificationDelegate::ScreenNotificationDelegate(
112bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    ScreenTrayItem* screen_tray)
113bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  : screen_tray_(screen_tray) {
114bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch}
115868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
116bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben MurdochScreenNotificationDelegate::~ScreenNotificationDelegate() {
117bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch}
118bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
119bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdochvoid ScreenNotificationDelegate::Display() {
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
122bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdochvoid ScreenNotificationDelegate::Error() {
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
125bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdochvoid ScreenNotificationDelegate::Close(bool by_user) {
126bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch}
127bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
128bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdochvoid ScreenNotificationDelegate::Click() {
129bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch}
130bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
131bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdochvoid ScreenNotificationDelegate::ButtonClick(int button_index) {
132bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  DCHECK_EQ(0, button_index);
133bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  screen_tray_->Stop();
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace tray
137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)ScreenTrayItem::ScreenTrayItem(SystemTray* system_tray)
139868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    : SystemTrayItem(system_tray),
140868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      tray_view_(NULL),
141868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      default_view_(NULL),
142868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      is_started_(false),
143868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      stop_callback_(base::Bind(&base::DoNothing)) {
144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)ScreenTrayItem::~ScreenTrayItem() {}
147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ScreenTrayItem::Update() {
149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (tray_view_)
150868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    tray_view_->Update();
151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (default_view_)
152868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    default_view_->Update();
153bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  if (is_started_) {
154bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    CreateOrUpdateNotification();
155bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  } else {
156bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    message_center::MessageCenter::Get()->RemoveNotification(
157bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch        GetNotificationId(), false /* by_user */);
158bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  }
159868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
160868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
161868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ScreenTrayItem::Start(const base::Closure& stop_callback) {
162868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  stop_callback_ = stop_callback;
163868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  is_started_ = true;
164868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
165868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (tray_view_)
166868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    tray_view_->Update();
167868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
168868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (default_view_)
169868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    default_view_->Update();
170868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
171868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (!system_tray()->HasSystemBubbleType(
172868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      SystemTrayBubble::BUBBLE_TYPE_DEFAULT)) {
173bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    CreateOrUpdateNotification();
174868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
175868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
176868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
177868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ScreenTrayItem::Stop() {
178868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  is_started_ = false;
179868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  Update();
180868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
181868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (stop_callback_.is_null())
182868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return;
183868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
184868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  base::Closure callback = stop_callback_;
185868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  stop_callback_.Reset();
186868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  callback.Run();
187868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
188868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
189868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ScreenTrayItem::DestroyTrayView() {
190868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  tray_view_ = NULL;
191868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
192868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
193868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void ScreenTrayItem::DestroyDefaultView() {
194868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  default_view_ = NULL;
195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
196868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void ScreenTrayItem::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) {
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!tray_view_)
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Center the item dependent on the orientation of the shelf.
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  views::BoxLayout::Orientation layout =
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      (alignment == ash::SHELF_ALIGNMENT_BOTTOM ||
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)       alignment == ash::SHELF_ALIGNMENT_TOP)
2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          ? views::BoxLayout::kHorizontal
2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          : views::BoxLayout::kVertical;
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  tray_view_->SetLayoutManager(new views::BoxLayout(layout, 0, 0, 0));
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  tray_view_->Layout();
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
211868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace ash
212