Views Omnibox: tolerate minor click-to-select-all dragging.
[chromium-blink-merge.git] / ui / message_center / views / toast_contents_view.cc
blobd71c86b6836f0f29b7931e8c3590e96e9a8235c1
1 // Copyright (c) 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.
5 #include "ui/message_center/views/toast_contents_view.h"
7 #include "base/bind.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "ui/base/accessibility/accessible_view_state.h"
14 #include "ui/gfx/animation/animation_delegate.h"
15 #include "ui/gfx/animation/slide_animation.h"
16 #include "ui/gfx/display.h"
17 #include "ui/gfx/screen.h"
18 #include "ui/message_center/message_center_style.h"
19 #include "ui/message_center/notification.h"
20 #include "ui/message_center/views/message_popup_collection.h"
21 #include "ui/message_center/views/message_view.h"
22 #include "ui/views/background.h"
23 #include "ui/views/view.h"
24 #include "ui/views/widget/widget.h"
25 #include "ui/views/widget/widget_delegate.h"
27 #if defined(OS_WIN) && defined(USE_ASH)
28 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
29 #endif
31 namespace message_center {
32 namespace {
34 // The width of a toast before animated reveal and after closing.
35 const int kClosedToastWidth = 5;
37 // FadeIn/Out look a bit better if they are slightly longer then default slide.
38 const int kFadeInOutDuration = 200;
40 } // namespace.
42 // static
43 gfx::Size ToastContentsView::GetToastSizeForView(views::View* view) {
44 int width = kNotificationWidth + view->GetInsets().width();
45 return gfx::Size(width, view->GetHeightForWidth(width));
48 ToastContentsView::ToastContentsView(
49 const std::string& notification_id,
50 base::WeakPtr<MessagePopupCollection> collection)
51 : collection_(collection),
52 id_(notification_id),
53 is_animating_bounds_(false),
54 is_closing_(false),
55 closing_animation_(NULL) {
56 set_notify_enter_exit_on_child(true);
57 // Sets the transparent background. Then, when the message view is slid out,
58 // the whole toast seems to slide although the actual bound of the widget
59 // remains. This is hacky but easier to keep the consistency.
60 set_background(views::Background::CreateSolidBackground(0, 0, 0, 0));
62 fade_animation_.reset(new gfx::SlideAnimation(this));
63 fade_animation_->SetSlideDuration(kFadeInOutDuration);
65 CreateWidget(collection->parent());
68 // This is destroyed when the toast window closes.
69 ToastContentsView::~ToastContentsView() {
70 if (collection_)
71 collection_->ForgetToast(this);
74 void ToastContentsView::SetContents(MessageView* view,
75 bool a11y_feedback_for_updates) {
76 bool already_has_contents = child_count() > 0;
77 RemoveAllChildViews(true);
78 AddChildView(view);
79 preferred_size_ = GetToastSizeForView(view);
80 Layout();
82 // If it has the contents already, this invocation means an update of the
83 // popup toast, and the new contents should be read through a11y feature.
84 // The notification type should be ALERT, otherwise the accessibility message
85 // won't be read for this view which returns ROLE_WINDOW.
86 if (already_has_contents && a11y_feedback_for_updates)
87 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_ALERT, false);
90 void ToastContentsView::RevealWithAnimation(gfx::Point origin) {
91 // Place/move the toast widgets. Currently it stacks the widgets from the
92 // right-bottom of the work area.
93 // TODO(mukai): allow to specify the placement policy from outside of this
94 // class. The policy should be specified from preference on Windows, or
95 // the launcher alignment on ChromeOS.
96 origin_ = gfx::Point(origin.x() - preferred_size_.width(),
97 origin.y() - preferred_size_.height());
99 gfx::Rect stable_bounds(origin_, preferred_size_);
101 SetBoundsInstantly(GetClosedToastBounds(stable_bounds));
102 StartFadeIn();
103 SetBoundsWithAnimation(stable_bounds);
106 void ToastContentsView::CloseWithAnimation() {
107 if (is_closing_)
108 return;
109 is_closing_ = true;
110 StartFadeOut();
113 void ToastContentsView::SetBoundsInstantly(gfx::Rect new_bounds) {
114 if (new_bounds == bounds())
115 return;
117 origin_ = new_bounds.origin();
118 if (!GetWidget())
119 return;
120 GetWidget()->SetBounds(new_bounds);
123 void ToastContentsView::SetBoundsWithAnimation(gfx::Rect new_bounds) {
124 if (new_bounds == bounds())
125 return;
127 origin_ = new_bounds.origin();
128 if (!GetWidget())
129 return;
131 // This picks up the current bounds, so if there was a previous animation
132 // half-done, the next one will pick up from the current location.
133 // This is the only place that should query current location of the Widget
134 // on screen, the rest should refer to the bounds_.
135 animated_bounds_start_ = GetWidget()->GetWindowBoundsInScreen();
136 animated_bounds_end_ = new_bounds;
138 if (collection_)
139 collection_->IncrementDeferCounter();
141 if (bounds_animation_.get())
142 bounds_animation_->Stop();
144 bounds_animation_.reset(new gfx::SlideAnimation(this));
145 bounds_animation_->Show();
148 void ToastContentsView::StartFadeIn() {
149 // The decrement is done in OnBoundsAnimationEndedOrCancelled callback.
150 if (collection_)
151 collection_->IncrementDeferCounter();
152 fade_animation_->Stop();
154 GetWidget()->SetOpacity(0);
155 GetWidget()->ShowInactive();
156 fade_animation_->Reset(0);
157 fade_animation_->Show();
160 void ToastContentsView::StartFadeOut() {
161 // The decrement is done in OnBoundsAnimationEndedOrCancelled callback.
162 if (collection_)
163 collection_->IncrementDeferCounter();
164 fade_animation_->Stop();
166 closing_animation_ = (is_closing_ ? fade_animation_.get() : NULL);
167 fade_animation_->Reset(1);
168 fade_animation_->Hide();
171 void ToastContentsView::OnBoundsAnimationEndedOrCancelled(
172 const gfx::Animation* animation) {
173 if (is_closing_ && closing_animation_ == animation && GetWidget()) {
174 views::Widget* widget = GetWidget();
175 #if defined(USE_AURA)
176 // TODO(dewittj): This is a workaround to prevent a nasty bug where
177 // closing a transparent widget doesn't actually remove the window,
178 // causing entire areas of the screen to become unresponsive to clicks.
179 // See crbug.com/243469
180 widget->Hide();
181 # if defined(OS_WIN)
182 widget->SetOpacity(0xFF);
183 # endif
184 #endif
185 widget->Close();
188 // This cannot be called before GetWidget()->Close(). Decrementing defer count
189 // will invoke update, which may invoke another close animation with
190 // incrementing defer counter. Close() after such process will cause a
191 // mismatch between increment/decrement. See crbug.com/238477
192 if (collection_)
193 collection_->DecrementDeferCounter();
196 // gfx::AnimationDelegate
197 void ToastContentsView::AnimationProgressed(const gfx::Animation* animation) {
198 if (animation == bounds_animation_.get()) {
199 gfx::Rect current(animation->CurrentValueBetween(
200 animated_bounds_start_, animated_bounds_end_));
201 GetWidget()->SetBounds(current);
202 } else if (animation == fade_animation_.get()) {
203 unsigned char opacity =
204 static_cast<unsigned char>(fade_animation_->GetCurrentValue() * 255);
205 GetWidget()->SetOpacity(opacity);
209 void ToastContentsView::AnimationEnded(const gfx::Animation* animation) {
210 OnBoundsAnimationEndedOrCancelled(animation);
213 void ToastContentsView::AnimationCanceled(
214 const gfx::Animation* animation) {
215 OnBoundsAnimationEndedOrCancelled(animation);
218 // views::WidgetDelegate
219 views::View* ToastContentsView::GetContentsView() {
220 return this;
223 void ToastContentsView::WindowClosing() {
224 if (!is_closing_ && collection_.get())
225 collection_->ForgetToast(this);
228 bool ToastContentsView::CanActivate() const {
229 return false;
232 void ToastContentsView::OnDisplayChanged() {
233 views::Widget* widget = GetWidget();
234 if (!widget)
235 return;
237 gfx::NativeView native_view = widget->GetNativeView();
238 if (!native_view || !collection_.get())
239 return;
241 collection_->OnDisplayBoundsChanged(gfx::Screen::GetScreenFor(
242 native_view)->GetDisplayNearestWindow(native_view));
245 void ToastContentsView::OnWorkAreaChanged() {
246 views::Widget* widget = GetWidget();
247 if (!widget)
248 return;
250 gfx::NativeView native_view = widget->GetNativeView();
251 if (!native_view || !collection_.get())
252 return;
254 collection_->OnDisplayBoundsChanged(gfx::Screen::GetScreenFor(
255 native_view)->GetDisplayNearestWindow(native_view));
258 // views::View
259 void ToastContentsView::OnMouseEntered(const ui::MouseEvent& event) {
260 if (collection_)
261 collection_->OnMouseEntered(this);
264 void ToastContentsView::OnMouseExited(const ui::MouseEvent& event) {
265 if (collection_)
266 collection_->OnMouseExited(this);
269 void ToastContentsView::Layout() {
270 if (child_count() > 0) {
271 child_at(0)->SetBounds(
272 0, 0, preferred_size_.width(), preferred_size_.height());
276 gfx::Size ToastContentsView::GetPreferredSize() {
277 return child_count() ? GetToastSizeForView(child_at(0)) : gfx::Size();
280 void ToastContentsView::GetAccessibleState(ui::AccessibleViewState* state) {
281 if (child_count() > 0)
282 child_at(0)->GetAccessibleState(state);
283 state->role = ui::AccessibilityTypes::ROLE_WINDOW;
286 void ToastContentsView::ClickOnNotification(
287 const std::string& notification_id) {
288 if (collection_)
289 collection_->ClickOnNotification(notification_id);
292 void ToastContentsView::RemoveNotification(
293 const std::string& notification_id,
294 bool by_user) {
295 if (collection_)
296 collection_->RemoveNotification(notification_id, by_user);
299 scoped_ptr<ui::MenuModel> ToastContentsView::CreateMenuModel(
300 const NotifierId& notifier_id,
301 const base::string16& display_source) {
302 // Should not reach, the context menu should be handled in
303 // MessagePopupCollection.
304 NOTREACHED();
305 return scoped_ptr<ui::MenuModel>();
308 bool ToastContentsView::HasClickedListener(
309 const std::string& notification_id) {
310 if (!collection_)
311 return false;
312 return collection_->HasClickedListener(notification_id);
315 void ToastContentsView::ClickOnNotificationButton(
316 const std::string& notification_id,
317 int button_index) {
318 if (collection_)
319 collection_->ClickOnNotificationButton(notification_id, button_index);
322 void ToastContentsView::ExpandNotification(
323 const std::string& notification_id) {
324 if (collection_)
325 collection_->ExpandNotification(notification_id);
328 void ToastContentsView::CreateWidget(gfx::NativeView parent) {
329 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
330 params.keep_on_top = true;
331 if (parent)
332 params.parent = parent;
333 else
334 params.top_level = true;
335 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
336 params.delegate = this;
337 views::Widget* widget = new views::Widget();
338 widget->set_focus_on_creation(false);
340 #if defined(OS_WIN) && defined(USE_ASH)
341 // We want to ensure that this toast always goes to the native desktop,
342 // not the Ash desktop (since there is already another toast contents view
343 // there.
344 if (!params.parent)
345 params.native_widget = new views::DesktopNativeWidgetAura(widget);
346 #endif
348 widget->Init(params);
351 gfx::Rect ToastContentsView::GetClosedToastBounds(gfx::Rect bounds) {
352 return gfx::Rect(bounds.x() + bounds.width() - kClosedToastWidth,
353 bounds.y(),
354 kClosedToastWidth,
355 bounds.height());
358 } // namespace message_center