Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: chrome/browser/ui/views/message_center/message_center_widget_delegate.cc

Issue 18003003: Message center re-organized (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // A WidgetDelegate should be deleted on DeleteDelegate.
44 set_owned_by_client();
45
46 views::BoxLayout* layout =
47 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0);
48 layout->set_spread_blank_space(true);
49 SetLayoutManager(layout);
50
51 AddAccelerator(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_NONE));
52
53 if (get_use_acceleration_when_possible()) {
54 SetPaintToLayer(true);
55 SetFillsBoundsOpaquely(true);
56 }
57
58 InitWidget();
59 }
60
61 MessageCenterWidgetDelegate::~MessageCenterWidgetDelegate() {
62 views::Widget* widget = GetWidget();
63 if (widget) {
64 widget->RemoveObserver(this);
65 }
66 }
67
68 views::View* MessageCenterWidgetDelegate::GetContentsView() {
69 return this;
70 }
71
72 views::NonClientFrameView*
73 MessageCenterWidgetDelegate::CreateNonClientFrameView(views::Widget* widget) {
74 MessageCenterFrameView* frame_view = new MessageCenterFrameView();
75 border_insets_ = frame_view->GetInsets();
76 return frame_view;
77 }
78
79 void MessageCenterWidgetDelegate::DeleteDelegate() {
80 delete this;
81 }
82
83 views::Widget* MessageCenterWidgetDelegate::GetWidget() {
84 return View::GetWidget();
85 }
86
87 const views::Widget* MessageCenterWidgetDelegate::GetWidget() const {
88 return View::GetWidget();
89 }
90
91 void MessageCenterWidgetDelegate::OnWidgetActivationChanged(
92 views::Widget* widget,
93 bool active) {
94 if (!active) {
95 tray_->SendHideMessageCenter();
96 }
97 }
98
99 void MessageCenterWidgetDelegate::OnWidgetClosing(views::Widget* widget) {
100 tray_->MarkMessageCenterHidden();
101 }
102
103 void MessageCenterWidgetDelegate::PreferredSizeChanged() {
104 GetWidget()->SetBounds(GetMessageCenterBounds());
105 views::View::PreferredSizeChanged();
106 }
107
108 gfx::Size MessageCenterWidgetDelegate::GetPreferredSize() {
109 gfx::Size size =
110 gfx::Size(preferred_width_, GetHeightForWidth(preferred_width_));
111
112 // Make space for borders on sides.
113 size.Enlarge(border_insets_.width(), border_insets_.height());
dewittj 2013/07/16 18:24:01 I am surprised you need to enlarge by the border s
sidharthms 2013/07/16 21:31:23 Done. Moved to GetMessageCenterBounds.
114 return size;
115 }
116
117 gfx::Size MessageCenterWidgetDelegate::GetMaximumSize() {
118 gfx::Size size = GetPreferredSize();
119 return size;
120 }
121
122 int MessageCenterWidgetDelegate::GetHeightForWidth(int width) {
123 int height = MessageCenterView::GetHeightForWidth(width);
124 return (pos_info_.max_height != 0) ? std::min(height, pos_info_.max_height)
125 : height;
126 }
127
128 bool MessageCenterWidgetDelegate::AcceleratorPressed(
129 const ui::Accelerator& accelerator) {
130 if (accelerator.key_code() != ui::VKEY_ESCAPE)
131 return false;
132 tray_->SendHideMessageCenter();
133 return true;
134 }
135
136 void MessageCenterWidgetDelegate::InitWidget() {
137 views::Widget* widget = new views::Widget();
138 views::Widget::InitParams params(views::Widget::InitParams::TYPE_BUBBLE);
139 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
140 params.delegate = this;
141 params.keep_on_top = true;
142 params.top_level = true;
143 widget->Init(params);
144
145 #if defined(OS_WIN)
146 // Remove the Message Center from taskbar and alt-tab rotation.
147 HWND hwnd = views::HWNDForWidget(widget);
148 LONG_PTR ex_styles = ::GetWindowLongPtr(hwnd, GWL_EXSTYLE);
149 ex_styles |= WS_EX_TOOLWINDOW;
150 ::SetWindowLongPtr(hwnd, GWL_EXSTYLE, ex_styles);
151 #endif
152
153 widget->AddObserver(this);
154 widget->StackAtTop();
155
156 // Message Center is always
157 // shown as a result of user action so it can be activated here.
158 widget->SetAlwaysOnTop(true);
159
160 const NotificationList::Notifications& notifications =
161 tray_->message_center()->GetNotifications();
162 SetNotifications(notifications);
163
164 widget->SetBounds(GetMessageCenterBounds());
165 widget->Show();
166 widget->Activate();
167 }
168
169 gfx::Point MessageCenterWidgetDelegate::GetCorrectedAnchor(
170 gfx::Size calculated_size) {
171 gfx::Point corrected_anchor = pos_info_.inital_anchor_point;
172
173 // Inset the width slightly so that the click point is not exactly on the edge
174 // of the message center but somewhere within the middle 60 %.
175 int insetted_width = (calculated_size.width() * 4) / 5;
176
177 if (pos_info_.taskbar_alignment == ALIGNMENT_TOP ||
178 pos_info_.taskbar_alignment == ALIGNMENT_BOTTOM) {
179 int click_point_x = tray_->mouse_click_point().x();
180
181 if (pos_info_.message_center_alignment & ALIGNMENT_RIGHT) {
182 int opposite_x_corner =
183 pos_info_.inital_anchor_point.x() - insetted_width;
184
185 // If the click point is outside the x axis length of the message center,
186 // push the message center towards the left to align with the click point.
187 if (opposite_x_corner > click_point_x)
188 corrected_anchor.set_x(pos_info_.inital_anchor_point.x() -
189 (opposite_x_corner - click_point_x));
190 } else {
191 int opposite_x_corner =
192 pos_info_.inital_anchor_point.x() + insetted_width;
193
194 if (opposite_x_corner < click_point_x)
195 corrected_anchor.set_x(pos_info_.inital_anchor_point.x() +
196 (click_point_x - opposite_x_corner));
197 }
198 } else if (pos_info_.taskbar_alignment == ALIGNMENT_LEFT ||
199 pos_info_.taskbar_alignment == ALIGNMENT_RIGHT) {
200 int click_point_y = tray_->mouse_click_point().y();
201
202 if (pos_info_.message_center_alignment & ALIGNMENT_BOTTOM) {
203 int opposite_y_corner =
204 pos_info_.inital_anchor_point.y() - insetted_width;
205
206 // If the click point is outside the y axis length of the message center,
207 // push the message center upwards to align with the click point.
208 if (opposite_y_corner > click_point_y)
209 corrected_anchor.set_y(pos_info_.inital_anchor_point.y() -
210 (opposite_y_corner - click_point_y));
211 } else {
212 int opposite_y_corner =
213 pos_info_.inital_anchor_point.y() + insetted_width;
214
215 if (opposite_y_corner < click_point_y)
216 corrected_anchor.set_y(pos_info_.inital_anchor_point.y() +
217 (click_point_y - opposite_y_corner));
218 }
219 }
220 return corrected_anchor;
221 }
222
223 gfx::Rect MessageCenterWidgetDelegate::GetMessageCenterBounds() {
224 const gfx::Size size = GetPreferredSize();
225 gfx::Rect bounds(size);
226
227 gfx::Point corrected_anchor = GetCorrectedAnchor(size);
228
229 if (pos_info_.message_center_alignment & ALIGNMENT_TOP)
230 bounds.set_y(corrected_anchor.y());
231 if (pos_info_.message_center_alignment & ALIGNMENT_BOTTOM)
232 bounds.set_y(corrected_anchor.y() - size.height());
233 if (pos_info_.message_center_alignment & ALIGNMENT_LEFT)
234 bounds.set_x(corrected_anchor.x());
235 if (pos_info_.message_center_alignment & ALIGNMENT_RIGHT)
236 bounds.set_x(corrected_anchor.x() - size.width());
237
238 return bounds;
239 }
240
241 } // namespace message_center
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698