| 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 "chrome/browser/ui/views/message_center/message_center_widget_delegate.
h" |
| 6 |
| 7 #include <complex> |
| 8 |
| 9 #include "chrome/browser/ui/views/message_center/message_center_frame_view.h" |
| 10 #include "content/public/browser/user_metrics.h" |
| 11 #include "ui/base/accessibility/accessible_view_state.h" |
| 12 #include "ui/gfx/screen.h" |
| 13 #include "ui/message_center/message_center_style.h" |
| 14 #include "ui/message_center/message_center_util.h" |
| 15 #include "ui/message_center/views/message_center_view.h" |
| 16 #include "ui/native_theme/native_theme.h" |
| 17 #include "ui/views/border.h" |
| 18 #include "ui/views/layout/box_layout.h" |
| 19 #include "ui/views/widget/widget.h" |
| 20 |
| 21 #if defined(OS_WIN) |
| 22 #include "ui/views/win/hwnd_util.h" |
| 23 #endif |
| 24 |
| 25 namespace message_center { |
| 26 |
| 27 MessageCenterWidgetDelegate::MessageCenterWidgetDelegate( |
| 28 WebNotificationTray* tray, |
| 29 MessageCenterTray* mc_tray, |
| 30 bool initially_settings_visible, |
| 31 const PositionInfo& pos_info) |
| 32 : MessageCenterView(tray->message_center(), |
| 33 mc_tray, |
| 34 pos_info.max_height, |
| 35 initially_settings_visible, |
| 36 pos_info.message_center_alignment & |
| 37 ALIGNMENT_TOP /* Show buttons on top if |
| 38 message center is |
| 39 top aligned */), |
| 40 preferred_width_(kNotificationWidth), |
| 41 pos_info_(pos_info), |
| 42 tray_(tray) { |
| 43 |
| 44 views::BoxLayout* layout = |
| 45 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); |
| 46 layout->set_spread_blank_space(true); |
| 47 SetLayoutManager(layout); |
| 48 |
| 49 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); |
| 50 |
| 51 if (get_use_acceleration_when_possible()) { |
| 52 SetPaintToLayer(true); |
| 53 SetFillsBoundsOpaquely(true); |
| 54 } |
| 55 |
| 56 InitWidget(); |
| 57 } |
| 58 |
| 59 MessageCenterWidgetDelegate::~MessageCenterWidgetDelegate() { |
| 60 views::Widget* widget = GetWidget(); |
| 61 if (widget) { |
| 62 widget->RemoveObserver(this); |
| 63 } |
| 64 } |
| 65 |
| 66 views::View* MessageCenterWidgetDelegate::GetContentsView() { |
| 67 return this; |
| 68 } |
| 69 |
| 70 views::NonClientFrameView* |
| 71 MessageCenterWidgetDelegate::CreateNonClientFrameView(views::Widget* widget) { |
| 72 return new MessageCenterFrameView(); |
| 73 } |
| 74 |
| 75 views::Widget* MessageCenterWidgetDelegate::GetWidget() { |
| 76 return View::GetWidget(); |
| 77 } |
| 78 |
| 79 const views::Widget* MessageCenterWidgetDelegate::GetWidget() const { |
| 80 return View::GetWidget(); |
| 81 } |
| 82 |
| 83 void MessageCenterWidgetDelegate::OnWidgetActivationChanged( |
| 84 views::Widget* widget, |
| 85 bool active) { |
| 86 if (!active) { |
| 87 tray_->HideBubble(); |
| 88 } |
| 89 } |
| 90 |
| 91 void MessageCenterWidgetDelegate::OnWidgetClosing(views::Widget* widget) { |
| 92 tray_->SetMessageCenterHidden(); |
| 93 } |
| 94 |
| 95 void MessageCenterWidgetDelegate::PreferredSizeChanged() { |
| 96 GetWidget()->SetBounds(GetBubbleBounds()); |
| 97 views::View::PreferredSizeChanged(); |
| 98 } |
| 99 |
| 100 gfx::Size MessageCenterWidgetDelegate::GetPreferredSize() { |
| 101 gfx::Size size = |
| 102 gfx::Size(preferred_width_, GetHeightForWidth(preferred_width_)); |
| 103 |
| 104 // Make space for borders on sides. |
| 105 gfx::Insets frame_insets = MessageCenterFrameView::GetBorderInsets(); |
| 106 size.Enlarge(frame_insets.width(), frame_insets.height()); |
| 107 return size; |
| 108 } |
| 109 |
| 110 gfx::Size MessageCenterWidgetDelegate::GetMaximumSize() { |
| 111 gfx::Size size = GetPreferredSize(); |
| 112 return size; |
| 113 } |
| 114 |
| 115 int MessageCenterWidgetDelegate::GetHeightForWidth(int width) { |
| 116 int height = MessageCenterView::GetHeightForWidth(width); |
| 117 return (pos_info_.max_height != 0) ? std::min(height, pos_info_.max_height) |
| 118 : height; |
| 119 } |
| 120 bool MessageCenterWidgetDelegate::AcceleratorPressed( |
| 121 const ui::Accelerator& accelerator) { |
| 122 if (accelerator.key_code() != ui::VKEY_ESCAPE) |
| 123 return false; |
| 124 tray_->HideBubble(); |
| 125 return true; |
| 126 } |
| 127 |
| 128 void MessageCenterWidgetDelegate::InitWidget() { |
| 129 views::Widget* widget = new views::Widget(); |
| 130 views::Widget::InitParams params(views::Widget::InitParams::TYPE_BUBBLE); |
| 131 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 132 params.delegate = this; |
| 133 params.keep_on_top = true; |
| 134 params.top_level = true; |
| 135 widget->Init(params); |
| 136 |
| 137 #if defined(OS_WIN) |
| 138 // Remove the bubbles for Notifications and Notification Center from taskbar |
| 139 // and alt-tab rotation. |
| 140 HWND hwnd = views::HWNDForWidget(widget); |
| 141 LONG_PTR ex_styles = ::GetWindowLongPtr(hwnd, GWL_EXSTYLE); |
| 142 ex_styles |= WS_EX_TOOLWINDOW; |
| 143 ::SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_styles); |
| 144 #endif |
| 145 |
| 146 widget->AddObserver(this); |
| 147 widget->StackAtTop(); |
| 148 |
| 149 // Message Center is always |
| 150 // shown as a result of user action so it can be activated here. |
| 151 widget->SetAlwaysOnTop(true); |
| 152 |
| 153 const NotificationList::Notifications& notifications = |
| 154 tray_->message_center()->GetNotifications(); |
| 155 SetNotifications(notifications); |
| 156 |
| 157 widget->SetBounds(GetBubbleBounds()); |
| 158 widget->Show(); |
| 159 widget->Activate(); |
| 160 } |
| 161 |
| 162 gfx::Point MessageCenterWidgetDelegate::GetCorrectedAnchor( |
| 163 gfx::Size calculated_size) { |
| 164 gfx::Point corrected_anchor = pos_info_.inital_anchor_point; |
| 165 |
| 166 // Inset the width slightly so that the click point is not exactly on the edge |
| 167 // of the message center but somewhere within the middle 60 %. |
| 168 int insetted_width = (calculated_size.width() * 4) / 5; |
| 169 |
| 170 if (pos_info_.systray_alignment == ALIGNMENT_TOP || |
| 171 pos_info_.systray_alignment == ALIGNMENT_BOTTOM) { |
| 172 int click_point_x = tray_->mouse_click_point().x(); |
| 173 |
| 174 if (pos_info_.message_center_alignment & ALIGNMENT_RIGHT) { |
| 175 int opposite_x_corner = |
| 176 pos_info_.inital_anchor_point.x() - insetted_width; |
| 177 |
| 178 // If the click point is outside the x axis length of the message center, |
| 179 // push the message center towards the LEFT to align with the click point. |
| 180 if (opposite_x_corner > click_point_x) |
| 181 corrected_anchor.set_x(pos_info_.inital_anchor_point.x() - |
| 182 (opposite_x_corner - click_point_x)); |
| 183 } else { |
| 184 int opposite_x_corner = |
| 185 pos_info_.inital_anchor_point.x() + insetted_width; |
| 186 |
| 187 if (opposite_x_corner < click_point_x) |
| 188 corrected_anchor.set_x(pos_info_.inital_anchor_point.x() + |
| 189 (click_point_x - opposite_x_corner)); |
| 190 } |
| 191 } else if (pos_info_.systray_alignment == ALIGNMENT_LEFT || |
| 192 pos_info_.systray_alignment == ALIGNMENT_RIGHT) { |
| 193 int click_point_y = tray_->mouse_click_point().y(); |
| 194 |
| 195 if (pos_info_.message_center_alignment & ALIGNMENT_BOTTOM) { |
| 196 int opposite_y_corner = |
| 197 pos_info_.inital_anchor_point.y() - insetted_width; |
| 198 |
| 199 // If the click point is outside the y axis length of the message center, |
| 200 // push the message center UPWARDS to align with the click point. |
| 201 if (opposite_y_corner > click_point_y) |
| 202 corrected_anchor.set_y(pos_info_.inital_anchor_point.y() - |
| 203 (opposite_y_corner - click_point_y)); |
| 204 } else { |
| 205 int opposite_y_corner = |
| 206 pos_info_.inital_anchor_point.y() + insetted_width; |
| 207 |
| 208 if (opposite_y_corner < click_point_y) |
| 209 corrected_anchor.set_y(pos_info_.inital_anchor_point.y() + |
| 210 (click_point_y - opposite_y_corner)); |
| 211 } |
| 212 } |
| 213 return corrected_anchor; |
| 214 } |
| 215 |
| 216 gfx::Rect MessageCenterWidgetDelegate::GetBubbleBounds() { |
| 217 const gfx::Size size = GetPreferredSize(); |
| 218 gfx::Rect bounds(size); |
| 219 |
| 220 gfx::Point corrected_anchor = GetCorrectedAnchor(size); |
| 221 |
| 222 if (pos_info_.message_center_alignment & ALIGNMENT_TOP) |
| 223 bounds.set_y(corrected_anchor.y()); |
| 224 if (pos_info_.message_center_alignment & ALIGNMENT_BOTTOM) |
| 225 bounds.set_y(corrected_anchor.y() - size.height()); |
| 226 if (pos_info_.message_center_alignment & ALIGNMENT_LEFT) |
| 227 bounds.set_x(corrected_anchor.x()); |
| 228 if (pos_info_.message_center_alignment & ALIGNMENT_RIGHT) |
| 229 bounds.set_x(corrected_anchor.x() - size.width()); |
| 230 |
| 231 return bounds; |
| 232 } |
| 233 |
| 234 } // namespace message_center |
| OLD | NEW |