Chromium Code Reviews| 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 message | |
| 38 // center is top aligned | |
| 39 preferred_width_(kNotificationWidth), | |
| 40 pos_info_(pos_info), | |
| 41 tray_(tray) { | |
| 42 // A WidgetDelegate should be deleted on DeleteDelegate. | |
| 43 set_owned_by_client(); | |
| 44 | |
| 45 views::BoxLayout* layout = | |
| 46 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0); | |
| 47 layout->set_spread_blank_space(true); | |
| 48 SetLayoutManager(layout); | |
| 49 | |
| 50 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE)); | |
| 51 | |
| 52 if (get_use_acceleration_when_possible()) { | |
| 53 SetPaintToLayer(true); | |
| 54 SetFillsBoundsOpaquely(true); | |
| 55 } | |
| 56 | |
| 57 InitWidget(); | |
| 58 } | |
| 59 | |
| 60 MessageCenterWidgetDelegate::~MessageCenterWidgetDelegate() { | |
| 61 views::Widget* widget = GetWidget(); | |
| 62 if (widget) { | |
| 63 widget->RemoveObserver(this); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 views::View* MessageCenterWidgetDelegate::GetContentsView() { | |
| 68 return this; | |
| 69 } | |
| 70 | |
| 71 views::NonClientFrameView* | |
| 72 MessageCenterWidgetDelegate::CreateNonClientFrameView(views::Widget* widget) { | |
| 73 MessageCenterFrameView* frame_view = new MessageCenterFrameView(); | |
| 74 border_insets_ = frame_view->GetInsets(); | |
| 75 return frame_view; | |
| 76 } | |
| 77 | |
| 78 void MessageCenterWidgetDelegate::DeleteDelegate() { | |
| 79 delete this; | |
| 80 } | |
| 81 | |
| 82 views::Widget* MessageCenterWidgetDelegate::GetWidget() { | |
| 83 return View::GetWidget(); | |
| 84 } | |
| 85 | |
| 86 const views::Widget* MessageCenterWidgetDelegate::GetWidget() const { | |
| 87 return View::GetWidget(); | |
| 88 } | |
| 89 | |
| 90 void MessageCenterWidgetDelegate::OnWidgetActivationChanged( | |
| 91 views::Widget* widget, | |
| 92 bool active) { | |
| 93 if (!active) { | |
| 94 tray_->SendHideMessageCenter(); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 void MessageCenterWidgetDelegate::OnWidgetClosing(views::Widget* widget) { | |
| 99 tray_->MarkMessageCenterHidden(); | |
| 100 } | |
| 101 | |
| 102 void MessageCenterWidgetDelegate::PreferredSizeChanged() { | |
| 103 GetWidget()->SetBounds(GetMessageCenterBounds()); | |
| 104 views::View::PreferredSizeChanged(); | |
| 105 } | |
| 106 | |
| 107 gfx::Size MessageCenterWidgetDelegate::GetPreferredSize() { | |
| 108 return gfx::Size(preferred_width_, GetHeightForWidth(preferred_width_)); | |
|
dewittj
2013/07/16 22:00:24
This size is wrong - the preferred size is 380px w
sidharthms
2013/07/16 22:44:13
Done.
| |
| 109 } | |
| 110 | |
| 111 gfx::Size MessageCenterWidgetDelegate::GetMaximumSize() { | |
| 112 gfx::Size size = GetPreferredSize(); | |
| 113 return size; | |
| 114 } | |
| 115 | |
| 116 int MessageCenterWidgetDelegate::GetHeightForWidth(int width) { | |
| 117 int height = MessageCenterView::GetHeightForWidth(width); | |
| 118 return (pos_info_.max_height != 0) ? std::min(height, pos_info_.max_height) | |
| 119 : height; | |
| 120 } | |
| 121 | |
| 122 bool MessageCenterWidgetDelegate::AcceleratorPressed( | |
| 123 const ui::Accelerator& accelerator) { | |
| 124 if (accelerator.key_code() != ui::VKEY_ESCAPE) | |
| 125 return false; | |
| 126 tray_->SendHideMessageCenter(); | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 void MessageCenterWidgetDelegate::InitWidget() { | |
| 131 views::Widget* widget = new views::Widget(); | |
| 132 views::Widget::InitParams params(views::Widget::InitParams::TYPE_BUBBLE); | |
| 133 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
| 134 params.delegate = this; | |
| 135 params.keep_on_top = true; | |
| 136 params.top_level = true; | |
| 137 widget->Init(params); | |
| 138 | |
| 139 #if defined(OS_WIN) | |
| 140 // Remove the Message Center from taskbar and alt-tab rotation. | |
| 141 HWND hwnd = views::HWNDForWidget(widget); | |
| 142 LONG_PTR ex_styles = ::GetWindowLongPtr(hwnd, GWL_EXSTYLE); | |
| 143 ex_styles |= WS_EX_TOOLWINDOW; | |
| 144 ::SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_styles); | |
| 145 #endif | |
| 146 | |
| 147 widget->AddObserver(this); | |
| 148 widget->StackAtTop(); | |
| 149 widget->SetAlwaysOnTop(true); | |
| 150 | |
| 151 const NotificationList::Notifications& notifications = | |
| 152 tray_->message_center()->GetNotifications(); | |
| 153 SetNotifications(notifications); | |
| 154 | |
| 155 widget->SetBounds(GetMessageCenterBounds()); | |
| 156 widget->Show(); | |
| 157 widget->Activate(); | |
| 158 } | |
| 159 | |
| 160 gfx::Point MessageCenterWidgetDelegate::GetCorrectedAnchor( | |
| 161 gfx::Size calculated_size) { | |
| 162 gfx::Point corrected_anchor = pos_info_.inital_anchor_point; | |
| 163 | |
| 164 // Inset the width slightly so that the click point is not exactly on the edge | |
| 165 // of the message center but somewhere within the middle 60 %. | |
| 166 int insetted_width = (calculated_size.width() * 4) / 5; | |
| 167 | |
| 168 if (pos_info_.taskbar_alignment == ALIGNMENT_TOP || | |
| 169 pos_info_.taskbar_alignment == ALIGNMENT_BOTTOM) { | |
| 170 int click_point_x = tray_->mouse_click_point().x(); | |
| 171 | |
| 172 if (pos_info_.message_center_alignment & ALIGNMENT_RIGHT) { | |
| 173 int opposite_x_corner = | |
| 174 pos_info_.inital_anchor_point.x() - insetted_width; | |
| 175 | |
| 176 // If the click point is outside the x axis length of the message center, | |
| 177 // push the message center towards the left to align with the click point. | |
| 178 if (opposite_x_corner > click_point_x) | |
| 179 corrected_anchor.set_x(pos_info_.inital_anchor_point.x() - | |
| 180 (opposite_x_corner - click_point_x)); | |
| 181 } else { | |
| 182 int opposite_x_corner = | |
| 183 pos_info_.inital_anchor_point.x() + insetted_width; | |
| 184 | |
| 185 if (opposite_x_corner < click_point_x) | |
| 186 corrected_anchor.set_x(pos_info_.inital_anchor_point.x() + | |
| 187 (click_point_x - opposite_x_corner)); | |
| 188 } | |
| 189 } else if (pos_info_.taskbar_alignment == ALIGNMENT_LEFT || | |
| 190 pos_info_.taskbar_alignment == ALIGNMENT_RIGHT) { | |
| 191 int click_point_y = tray_->mouse_click_point().y(); | |
| 192 | |
| 193 if (pos_info_.message_center_alignment & ALIGNMENT_BOTTOM) { | |
| 194 int opposite_y_corner = | |
| 195 pos_info_.inital_anchor_point.y() - insetted_width; | |
| 196 | |
| 197 // If the click point is outside the y axis length of the message center, | |
| 198 // push the message center upwards to align with the click point. | |
| 199 if (opposite_y_corner > click_point_y) | |
| 200 corrected_anchor.set_y(pos_info_.inital_anchor_point.y() - | |
| 201 (opposite_y_corner - click_point_y)); | |
| 202 } else { | |
| 203 int opposite_y_corner = | |
| 204 pos_info_.inital_anchor_point.y() + insetted_width; | |
| 205 | |
| 206 if (opposite_y_corner < click_point_y) | |
| 207 corrected_anchor.set_y(pos_info_.inital_anchor_point.y() + | |
| 208 (click_point_y - opposite_y_corner)); | |
| 209 } | |
| 210 } | |
| 211 return corrected_anchor; | |
| 212 } | |
| 213 | |
| 214 gfx::Rect MessageCenterWidgetDelegate::GetMessageCenterBounds() { | |
| 215 gfx::Size size = GetPreferredSize(); | |
|
dewittj
2013/07/16 21:42:10
I wonder if GetWindowBoundsForClientBounds(GetPref
dewittj
2013/07/16 22:00:24
Fine to leave this for later.
| |
| 216 | |
| 217 // Make space for borders on sides. | |
| 218 size.Enlarge(border_insets_.width(), border_insets_.height()); | |
| 219 gfx::Rect bounds(size); | |
| 220 | |
| 221 gfx::Point corrected_anchor = GetCorrectedAnchor(size); | |
| 222 | |
| 223 if (pos_info_.message_center_alignment & ALIGNMENT_TOP) | |
| 224 bounds.set_y(corrected_anchor.y()); | |
| 225 if (pos_info_.message_center_alignment & ALIGNMENT_BOTTOM) | |
| 226 bounds.set_y(corrected_anchor.y() - size.height()); | |
| 227 if (pos_info_.message_center_alignment & ALIGNMENT_LEFT) | |
| 228 bounds.set_x(corrected_anchor.x()); | |
| 229 if (pos_info_.message_center_alignment & ALIGNMENT_RIGHT) | |
| 230 bounds.set_x(corrected_anchor.x() - size.width()); | |
| 231 | |
| 232 return bounds; | |
| 233 } | |
| 234 | |
| 235 } // namespace message_center | |
| OLD | NEW |