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

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

Powered by Google App Engine
This is Rietveld 408576698