| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "ash/system/chromeos/screen_security/screen_tray_item.h" | |
| 6 | |
| 7 #include "ash/common/shelf/wm_shelf_util.h" | |
| 8 #include "ash/common/system/tray/fixed_sized_image_view.h" | |
| 9 #include "ash/common/system/tray/tray_constants.h" | |
| 10 #include "ui/base/resource/resource_bundle.h" | |
| 11 #include "ui/message_center/message_center.h" | |
| 12 #include "ui/views/controls/label.h" | |
| 13 #include "ui/views/layout/box_layout.h" | |
| 14 | |
| 15 namespace { | |
| 16 const int kStopButtonRightPadding = 18; | |
| 17 } // namespace | |
| 18 | |
| 19 namespace ash { | |
| 20 namespace tray { | |
| 21 | |
| 22 // ScreenTrayView implementations. | |
| 23 ScreenTrayView::ScreenTrayView(ScreenTrayItem* screen_tray_item, int icon_id) | |
| 24 : TrayItemView(screen_tray_item), screen_tray_item_(screen_tray_item) { | |
| 25 CreateImageView(); | |
| 26 image_view()->SetImage(ui::ResourceBundle::GetSharedInstance() | |
| 27 .GetImageNamed(icon_id) | |
| 28 .ToImageSkia()); | |
| 29 | |
| 30 Update(); | |
| 31 } | |
| 32 | |
| 33 ScreenTrayView::~ScreenTrayView() {} | |
| 34 | |
| 35 void ScreenTrayView::Update() { | |
| 36 SetVisible(screen_tray_item_->is_started()); | |
| 37 } | |
| 38 | |
| 39 // ScreenStatusView implementations. | |
| 40 ScreenStatusView::ScreenStatusView(ScreenTrayItem* screen_tray_item, | |
| 41 int icon_id, | |
| 42 const base::string16& label_text, | |
| 43 const base::string16& stop_button_text) | |
| 44 : screen_tray_item_(screen_tray_item), | |
| 45 icon_(NULL), | |
| 46 label_(NULL), | |
| 47 stop_button_(NULL), | |
| 48 icon_id_(icon_id), | |
| 49 label_text_(label_text), | |
| 50 stop_button_text_(stop_button_text) { | |
| 51 CreateItems(); | |
| 52 Update(); | |
| 53 } | |
| 54 | |
| 55 ScreenStatusView::~ScreenStatusView() {} | |
| 56 | |
| 57 void ScreenStatusView::Layout() { | |
| 58 views::View::Layout(); | |
| 59 | |
| 60 // Give the stop button the space it requests. | |
| 61 gfx::Size stop_size = stop_button_->GetPreferredSize(); | |
| 62 gfx::Rect stop_bounds(stop_size); | |
| 63 stop_bounds.set_x(width() - stop_size.width() - kStopButtonRightPadding); | |
| 64 stop_bounds.set_y((height() - stop_size.height()) / 2); | |
| 65 stop_button_->SetBoundsRect(stop_bounds); | |
| 66 | |
| 67 // Adjust the label's bounds in case it got cut off by |stop_button_|. | |
| 68 if (label_->bounds().Intersects(stop_button_->bounds())) { | |
| 69 gfx::Rect label_bounds = label_->bounds(); | |
| 70 label_bounds.set_width(stop_button_->x() - kTrayPopupPaddingBetweenItems - | |
| 71 label_->x()); | |
| 72 label_->SetBoundsRect(label_bounds); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 void ScreenStatusView::ButtonPressed(views::Button* sender, | |
| 77 const ui::Event& event) { | |
| 78 DCHECK(sender == stop_button_); | |
| 79 screen_tray_item_->Stop(); | |
| 80 } | |
| 81 | |
| 82 void ScreenStatusView::CreateItems() { | |
| 83 set_background(views::Background::CreateSolidBackground(kBackgroundColor)); | |
| 84 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 85 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, | |
| 86 kTrayPopupPaddingHorizontal, 0, | |
| 87 kTrayPopupPaddingBetweenItems)); | |
| 88 icon_ = new FixedSizedImageView(0, kTrayPopupItemHeight); | |
| 89 icon_->SetImage(bundle.GetImageNamed(icon_id_).ToImageSkia()); | |
| 90 AddChildView(icon_); | |
| 91 label_ = new views::Label; | |
| 92 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 93 label_->SetMultiLine(true); | |
| 94 label_->SetText(label_text_); | |
| 95 AddChildView(label_); | |
| 96 | |
| 97 stop_button_ = new TrayPopupLabelButton(this, stop_button_text_); | |
| 98 AddChildView(stop_button_); | |
| 99 } | |
| 100 | |
| 101 void ScreenStatusView::Update() { | |
| 102 // Hide the notification bubble when the ash tray bubble opens. | |
| 103 screen_tray_item_->HideNotificationView(); | |
| 104 SetVisible(screen_tray_item_->is_started()); | |
| 105 } | |
| 106 | |
| 107 ScreenNotificationDelegate::ScreenNotificationDelegate( | |
| 108 ScreenTrayItem* screen_tray) | |
| 109 : screen_tray_(screen_tray) {} | |
| 110 | |
| 111 ScreenNotificationDelegate::~ScreenNotificationDelegate() {} | |
| 112 | |
| 113 void ScreenNotificationDelegate::ButtonClick(int button_index) { | |
| 114 DCHECK_EQ(0, button_index); | |
| 115 screen_tray_->Stop(); | |
| 116 } | |
| 117 | |
| 118 } // namespace tray | |
| 119 | |
| 120 ScreenTrayItem::ScreenTrayItem(SystemTray* system_tray) | |
| 121 : SystemTrayItem(system_tray), | |
| 122 tray_view_(NULL), | |
| 123 default_view_(NULL), | |
| 124 is_started_(false), | |
| 125 stop_callback_(base::Bind(&base::DoNothing)) {} | |
| 126 | |
| 127 ScreenTrayItem::~ScreenTrayItem() {} | |
| 128 | |
| 129 void ScreenTrayItem::Update() { | |
| 130 if (tray_view_) | |
| 131 tray_view_->Update(); | |
| 132 if (default_view_) | |
| 133 default_view_->Update(); | |
| 134 if (is_started_) { | |
| 135 CreateOrUpdateNotification(); | |
| 136 } else { | |
| 137 message_center::MessageCenter::Get()->RemoveNotification( | |
| 138 GetNotificationId(), false /* by_user */); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void ScreenTrayItem::Start(const base::Closure& stop_callback) { | |
| 143 stop_callback_ = stop_callback; | |
| 144 is_started_ = true; | |
| 145 | |
| 146 if (tray_view_) | |
| 147 tray_view_->Update(); | |
| 148 | |
| 149 if (default_view_) | |
| 150 default_view_->Update(); | |
| 151 | |
| 152 if (!system_tray()->HasSystemBubbleType( | |
| 153 SystemTrayBubble::BUBBLE_TYPE_DEFAULT)) { | |
| 154 CreateOrUpdateNotification(); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 void ScreenTrayItem::Stop() { | |
| 159 is_started_ = false; | |
| 160 Update(); | |
| 161 | |
| 162 if (stop_callback_.is_null()) | |
| 163 return; | |
| 164 | |
| 165 base::Closure callback = stop_callback_; | |
| 166 stop_callback_.Reset(); | |
| 167 callback.Run(); | |
| 168 } | |
| 169 | |
| 170 void ScreenTrayItem::DestroyTrayView() { | |
| 171 tray_view_ = NULL; | |
| 172 } | |
| 173 | |
| 174 void ScreenTrayItem::DestroyDefaultView() { | |
| 175 default_view_ = NULL; | |
| 176 } | |
| 177 | |
| 178 void ScreenTrayItem::UpdateAfterShelfAlignmentChange(ShelfAlignment alignment) { | |
| 179 if (!tray_view_) | |
| 180 return; | |
| 181 | |
| 182 // Center the item dependent on the orientation of the shelf. | |
| 183 views::BoxLayout::Orientation layout = IsHorizontalAlignment(alignment) | |
| 184 ? views::BoxLayout::kHorizontal | |
| 185 : views::BoxLayout::kVertical; | |
| 186 tray_view_->SetLayoutManager(new views::BoxLayout(layout, 0, 0, 0)); | |
| 187 tray_view_->Layout(); | |
| 188 } | |
| 189 | |
| 190 } // namespace ash | |
| OLD | NEW |