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

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: Comments applied 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 namespace {
22
23 const int kBorderWidth = 1;
24
25 } // namespace
26
27 namespace message_center {
28
29 MessageCenterWidgetDelegate::MessageCenterWidgetDelegate(
30 WebNotificationTray* tray,
31 MessageCenterTray* mc_tray,
32 bool initially_settings_visible,
33 PositionInfo pos_info)
34 : MessageCenterView(tray->message_center(),
35 mc_tray,
36 pos_info.max_height,
37 initially_settings_visible,
38 pos_info.bubble_alignment & ALIGNMENT_TOP /* Show buttons on top if
dewittj 2013/07/02 18:43:32 I might just change this constructor to accept bub
sidharthms 2013/07/02 21:01:16 But MessageCenterView is also used in other places
39 message center is
40 top aligned */),
41 pos_info_(pos_info),
42 tray_(tray) {
43 preferred_width_ = kNotificationWidth;
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 UpdateColorsFromTheme(GetNativeTheme());
dewittj 2013/07/02 18:43:32 Should this be called on Windows?
sidharthms 2013/07/02 21:01:16 Eh, wouldn't it be better to use native themes? O
52
53 if (get_use_acceleration_when_possible()) {
54 SetPaintToLayer(true);
55 SetFillsBoundsOpaquely(true);
56 }
57
58 InitWidget();
59 UpdateNotifications();
dewittj 2013/07/02 18:43:32 This method is at least misnamed (since it also sh
sidharthms 2013/07/02 21:01:16 Done.
60 }
61
62 MessageCenterWidgetDelegate::~MessageCenterWidgetDelegate() {
63 views::Widget* widget = GetWidget();
64 if (widget) {
65 widget->RemoveObserver(this);
66 }
67 }
68
69 views::View* MessageCenterWidgetDelegate::GetContentsView() {
70 return this;
71 }
72
73 views::NonClientFrameView*
74 MessageCenterWidgetDelegate::CreateNonClientFrameView(views::Widget* widget) {
75 return new MessageCenterFrameView(kBorderWidth);
76 }
77
78 views::Widget* MessageCenterWidgetDelegate::GetWidget() {
79 return View::GetWidget();
80 }
81
82 const views::Widget* MessageCenterWidgetDelegate::GetWidget() const {
83 return View::GetWidget();
84 }
85
86 void MessageCenterWidgetDelegate::OnWidgetActivationChanged(
87 views::Widget* widget,
88 bool active) {
89 if (!active)
90 tray_->HideBubble();
91 }
92
93 void MessageCenterWidgetDelegate::PreferredSizeChanged() {
94 GetWidget()->SetBounds(GetBubbleBounds());
95 views::View::PreferredSizeChanged();
96 }
97
98 gfx::Size MessageCenterWidgetDelegate::GetPreferredSize() {
99 gfx::Size size =
100 gfx::Size(preferred_width_, GetHeightForWidth(preferred_width_));
101
102 // Make space for borders on sides.
103 size.Enlarge(2 * kBorderWidth, 2 * kBorderWidth);
104 return size;
105 }
106
107 gfx::Size MessageCenterWidgetDelegate::GetMaximumSize() {
108 gfx::Size size = GetPreferredSize();
109 size.set_width(preferred_width_);
dewittj 2013/07/02 18:43:32 This is smaller than the preferred size above. Sh
sidharthms 2013/07/02 21:01:16 Done.
110 return size;
111 }
112
113 int MessageCenterWidgetDelegate::GetHeightForWidth(int width) {
114 int height = MessageCenterView::GetHeightForWidth(width);
115 return (pos_info_.max_height != 0) ? std::min(height, pos_info_.max_height)
116 : height;
117 }
118 bool MessageCenterWidgetDelegate::AcceleratorPressed(
119 const ui::Accelerator& accelerator) {
dewittj 2013/07/02 18:43:32 Not sure, do we need to pass these through to the
sidharthms 2013/07/02 21:01:16 MessageCenterView doesn't seem to handle these...
120 if (accelerator.key_code() != ui::VKEY_ESCAPE)
121 return false;
122 GetWidget()->Close();
123 return true;
124 }
125
126 void MessageCenterWidgetDelegate::OnNativeThemeChanged(
127 const ui::NativeTheme* theme) {
128 UpdateColorsFromTheme(theme);
129 }
130
131 void MessageCenterWidgetDelegate::InitWidget() {
132 views::Widget* widget = new views::Widget();
133 views::Widget::InitParams params(views::Widget::InitParams::TYPE_BUBBLE);
134 params.delegate = this;
135 params.keep_on_top = true;
136 params.top_level = true;
137 #if defined(OS_WIN) && !defined(USE_AURA)
138 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
139 #endif
140 widget->Init(params);
141
142 widget->AddObserver(this);
143 widget->StackAtTop();
144
145 // Popups should appear on top of everything, but not disturb the user's
146 // focus since they could appear at any time. Message Center is always
147 // shown as a result of user action so it can be activated here.
148 widget->SetAlwaysOnTop(true);
149 widget->Activate();
150 }
151
152 void MessageCenterWidgetDelegate::CloseWidget() {
153 GetWidget()->Close();
154 }
155
156 void MessageCenterWidgetDelegate::UpdateColorsFromTheme(
157 const ui::NativeTheme* theme) {
158 color_ = GetNativeTheme()->GetSystemColor(
159 ui::NativeTheme::kColorId_WindowBackground);
160 set_background(views::Background::CreateSolidBackground(color_));
161 }
162
163 void MessageCenterWidgetDelegate::UpdateNotifications() {
164 const NotificationList::Notifications& notifications =
165 tray_->message_center()->GetNotifications();
166 SetNotifications(notifications);
167 GetWidget()->Show();
168 GetWidget()->SetBounds(GetBubbleBounds());
dewittj 2013/07/02 18:43:32 Seems like it would make more sense to set the bou
sidharthms 2013/07/02 21:01:16 Done.
169 GetWidget()->GetRootView()->SchedulePaint();
dewittj 2013/07/02 18:43:32 Does it not paint the widget when you show it?
sidharthms 2013/07/02 21:01:16 Done.
170 }
171
172 MessageCenterFrameView* MessageCenterWidgetDelegate::GetBubbleFrameView()
173 const {
174 const views::Widget* widget = GetWidget();
175 const views::NonClientView* view = widget ? widget->non_client_view() : NULL;
176 return view ? static_cast<MessageCenterFrameView*>(view->frame_view()) : NULL;
177 }
178
179 gfx::Rect MessageCenterWidgetDelegate::GetBubbleBounds() {
180 const gfx::Size size = GetPreferredSize();
181 gfx::Rect bounds(size);
182
183 gfx::Point corrected_anchor = GetCorrectedAnchor(size);
184
185 if (pos_info_.bubble_alignment & ALIGNMENT_TOP)
186 bounds.set_y(corrected_anchor.y());
187 if (pos_info_.bubble_alignment & ALIGNMENT_BOTTOM)
188 bounds.set_y(corrected_anchor.y() - size.height());
189 if (pos_info_.bubble_alignment & ALIGNMENT_LEFT)
190 bounds.set_x(corrected_anchor.x());
191 if (pos_info_.bubble_alignment & ALIGNMENT_RIGHT)
192 bounds.set_x(corrected_anchor.x() - size.width());
193
194 return bounds;
195 }
196
197 gfx::Point MessageCenterWidgetDelegate::GetCorrectedAnchor(
198 gfx::Size calculated_size) {
199 gfx::Point corrected_anchor = pos_info_.inital_anchor_point;
200
201 // Inset the width slightly so that the click point is not exactly on the edge
202 // of the message center but somewhere within the middle 60 %.
203 int insetted_width = (calculated_size.width() * 4) / 5;
204
205 if (pos_info_.systray_alignment == ALIGNMENT_TOP ||
206 pos_info_.systray_alignment == ALIGNMENT_BOTTOM) {
207 int click_point_x = tray_->mouse_click_point().x();
208
209 if (pos_info_.bubble_alignment & ALIGNMENT_RIGHT) {
210 int opposite_x_corner =
211 pos_info_.inital_anchor_point.x() - insetted_width;
212
213 // If the click point is outside the x axis length of the message center,
214 // push the message center towards the LEFT to align with the click point.
215 if (opposite_x_corner > click_point_x)
216 corrected_anchor.set_x(pos_info_.inital_anchor_point.x() -
217 (opposite_x_corner - click_point_x));
218 } else {
219 int opposite_x_corner =
220 pos_info_.inital_anchor_point.x() + insetted_width;
221
222 if (opposite_x_corner < click_point_x)
223 corrected_anchor.set_x(pos_info_.inital_anchor_point.x() +
224 (click_point_x - opposite_x_corner));
225 }
226 } else if (pos_info_.systray_alignment == ALIGNMENT_LEFT ||
227 pos_info_.systray_alignment == ALIGNMENT_RIGHT) {
228 int click_point_y = tray_->mouse_click_point().y();
229
230 if (pos_info_.bubble_alignment & ALIGNMENT_BOTTOM) {
231 int opposite_y_corner =
232 pos_info_.inital_anchor_point.y() - insetted_width;
233
234 // If the click point is outside the y axis length of the message center,
235 // push the message center UPWARDS to align with the click point.
236 if (opposite_y_corner > click_point_y)
237 corrected_anchor.set_y(pos_info_.inital_anchor_point.y() -
238 (opposite_y_corner - click_point_y));
239 } else {
240 int opposite_y_corner =
241 pos_info_.inital_anchor_point.y() + insetted_width;
242
243 if (opposite_y_corner < click_point_y)
244 corrected_anchor.set_y(pos_info_.inital_anchor_point.y() +
245 (click_point_y - opposite_y_corner));
246 }
247 }
248 return corrected_anchor;
249 }
250
251 } // namespace message_center
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698