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"
15 const int kStopButtonRightPadding
= 18;
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
) {
26 image_view()->SetImage(ui::ResourceBundle::GetSharedInstance()
27 .GetImageNamed(icon_id
).ToImageSkia());
32 ScreenTrayView::~ScreenTrayView() {
35 void ScreenTrayView::Update() {
36 SetVisible(screen_tray_item_
->is_started());
40 // ScreenStatusView implementations.
41 ScreenStatusView::ScreenStatusView(ScreenTrayItem
* screen_tray_item
,
43 const base::string16
& label_text
,
44 const base::string16
& stop_button_text
)
45 : screen_tray_item_(screen_tray_item
),
50 label_text_(label_text
),
51 stop_button_text_(stop_button_text
) {
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());
95 label_
= new views::Label
;
96 label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
97 label_
->SetMultiLine(true);
98 label_
->SetText(label_text_
);
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::Display() {
122 void ScreenNotificationDelegate::Error() {
125 void ScreenNotificationDelegate::Close(bool by_user
) {
128 void ScreenNotificationDelegate::Click() {
131 void ScreenNotificationDelegate::ButtonClick(int button_index
) {
132 DCHECK_EQ(0, button_index
);
133 screen_tray_
->Stop();
138 ScreenTrayItem::ScreenTrayItem(SystemTray
* system_tray
)
139 : SystemTrayItem(system_tray
),
143 stop_callback_(base::Bind(&base::DoNothing
)) {
146 ScreenTrayItem::~ScreenTrayItem() {}
148 void ScreenTrayItem::Update() {
150 tray_view_
->Update();
152 default_view_
->Update();
154 CreateOrUpdateNotification();
156 message_center::MessageCenter::Get()->RemoveNotification(
157 GetNotificationId(), false /* by_user */);
161 void ScreenTrayItem::Start(const base::Closure
& stop_callback
) {
162 stop_callback_
= stop_callback
;
166 tray_view_
->Update();
169 default_view_
->Update();
171 if (!system_tray()->HasSystemBubbleType(
172 SystemTrayBubble::BUBBLE_TYPE_DEFAULT
)) {
173 CreateOrUpdateNotification();
177 void ScreenTrayItem::Stop() {
181 if (stop_callback_
.is_null())
184 base::Closure callback
= stop_callback_
;
185 stop_callback_
.Reset();
189 void ScreenTrayItem::DestroyTrayView() {
193 void ScreenTrayItem::DestroyDefaultView() {
194 default_view_
= NULL
;
197 void ScreenTrayItem::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment
) {
201 // Center the item dependent on the orientation of the shelf.
202 views::BoxLayout::Orientation layout
=
203 (alignment
== ash::SHELF_ALIGNMENT_BOTTOM
||
204 alignment
== ash::SHELF_ALIGNMENT_TOP
)
205 ? views::BoxLayout::kHorizontal
206 : views::BoxLayout::kVertical
;
207 tray_view_
->SetLayoutManager(new views::BoxLayout(layout
, 0, 0, 0));
208 tray_view_
->Layout();