summaryrefslogtreecommitdiffstats
path: root/ash/system/web_notification/web_notification_tray.cc
blob: 7ea81aabd9159a6519b0b1980aa7303078efa568 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/web_notification/web_notification_tray.h"

#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/tray/tray_background_view.h"
#include "ash/system/tray/tray_bubble_wrapper.h"
#include "ash/wm/shelf_layout_manager.h"
#include "grit/ash_resources.h"
#include "grit/ash_strings.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/screen.h"
#include "ui/message_center/message_bubble_base.h"
#include "ui/message_center/message_center_bubble.h"
#include "ui/message_center/message_center_tray_delegate.h"
#include "ui/message_center/message_center_util.h"
#include "ui/message_center/message_popup_bubble.h"
#include "ui/message_center/quiet_mode_bubble.h"
#include "ui/views/bubble/tray_bubble_view.h"
#include "ui/views/widget/widget_observer.h"

#if defined(OS_CHROMEOS)

namespace message_center {

MessageCenterTrayDelegate* CreateMessageCenterTray() {
  // On Windows+Ash the Tray will not be hosted in ash::Shell.
  NOTREACHED();
  return NULL;
}

}  // namespace message_center

#endif  // defined(OS_CHROMEOS)

namespace ash {

namespace internal {

// Class to initialize and manage the WebNotificationBubble and
// TrayBubbleWrapper instances for a bubble.

class WebNotificationBubbleWrapper {
 public:
  // Takes ownership of |bubble| and creates |bubble_wrapper_|.
  WebNotificationBubbleWrapper(WebNotificationTray* tray,
                               message_center::MessageBubbleBase* bubble) {
    bubble_.reset(bubble);
    views::TrayBubbleView::AnchorAlignment anchor_alignment =
        tray->GetAnchorAlignment();
    views::TrayBubbleView::InitParams init_params =
        bubble->GetInitParams(anchor_alignment);
    init_params.close_on_deactivate = false;
    views::View* anchor = tray->tray_container();
    if (anchor_alignment == views::TrayBubbleView::ANCHOR_ALIGNMENT_BOTTOM) {
      gfx::Point bounds(anchor->width() / 2, 0);
      views::View::ConvertPointToWidget(anchor, &bounds);
      init_params.arrow_offset = bounds.x();
    }
    views::TrayBubbleView* bubble_view = views::TrayBubbleView::Create(
        tray->GetBubbleWindowContainer(), anchor, tray, &init_params);
    bubble_wrapper_.reset(new TrayBubbleWrapper(tray, bubble_view));
    bubble->InitializeContents(bubble_view);
  }

  message_center::MessageBubbleBase* bubble() const { return bubble_.get(); }

  // Convenience accessors.
  views::TrayBubbleView* bubble_view() const { return bubble_->bubble_view(); }

 private:
  scoped_ptr<message_center::MessageBubbleBase> bubble_;
  scoped_ptr<internal::TrayBubbleWrapper> bubble_wrapper_;
};

}  // namespace internal

WebNotificationTray::WebNotificationTray(
    internal::StatusAreaWidget* status_area_widget)
    : TrayBackgroundView(status_area_widget),
      button_(NULL),
      show_message_center_on_unlock_(false) {
  button_ = new views::ImageButton(this);
  button_->set_triggerable_event_flags(
      ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON);
  tray_container()->AddChildView(button_);
  SetVisible(false);
  message_center_tray_.reset(new message_center::MessageCenterTray(
      this,
      Shell::GetInstance()->message_center()));
  OnMessageCenterTrayChanged();
}

WebNotificationTray::~WebNotificationTray() {
  // Release any child views that might have back pointers before ~View().
  message_center_bubble_.reset();
  popup_bubble_.reset();
  if (quiet_mode_bubble() && quiet_mode_bubble()->GetBubbleWidget())
    quiet_mode_bubble()->GetBubbleWidget()->RemoveObserver(this);
  quiet_mode_bubble_.reset();
}

// Public methods.

bool WebNotificationTray::ShowMessageCenter() {
  if (!ShouldShowMessageCenter())
    return false;

  message_center::MessageCenterBubble* message_center_bubble =
      new message_center::MessageCenterBubble(message_center());

  // TODO(mukai): move this to WebNotificationBubbleWrapper if it's safe
  // to set the height of the popup.
  int max_height = 0;
  if (GetShelfLayoutManager()->GetAlignment() == SHELF_ALIGNMENT_BOTTOM) {
    gfx::Rect shelf_bounds = GetShelfLayoutManager()->GetIdealBounds();
    max_height = shelf_bounds.y();
  } else {
    // Assume that the bottom line of the status area widget and the bubble are
    // aligned.
    aura::Window* status_area_window = status_area_widget()->GetNativeWindow();
    max_height = status_area_window->GetBoundsInRootWindow().bottom();
  }
  message_center_bubble->SetMaxHeight(max_height);
  message_center_bubble_.reset(
      new internal::WebNotificationBubbleWrapper(this, message_center_bubble));

  status_area_widget()->SetHideSystemNotifications(true);
  GetShelfLayoutManager()->UpdateAutoHideState();
  return true;
}

void WebNotificationTray::UpdateMessageCenter() {
  if (message_center_bubble())
    message_center_bubble()->bubble()->ScheduleUpdate();
}

void WebNotificationTray::HideMessageCenter() {
  if (!message_center_bubble())
    return;
  message_center_bubble_.reset();
  show_message_center_on_unlock_ = false;
  status_area_widget()->SetHideSystemNotifications(false);
  GetShelfLayoutManager()->UpdateAutoHideState();
}

void WebNotificationTray::SetHidePopupBubble(bool hide) {
  if (hide)
    message_center_tray_->HidePopupBubble();
  else
    message_center_tray_->ShowPopupBubble();
}

bool WebNotificationTray::ShowPopups() {
  if (status_area_widget()->login_status() == user::LOGGED_IN_LOCKED ||
      message_center_bubble() ||
      !status_area_widget()->ShouldShowWebNotifications()) {
    return false;
  }
  message_center::MessagePopupBubble* popup_bubble =
      new message_center::MessagePopupBubble(message_center());
  popup_bubble_.reset(new internal::WebNotificationBubbleWrapper(
      this, popup_bubble));
  return true;
}

void WebNotificationTray::UpdatePopups() {
  if (popup_bubble())
    popup_bubble()->bubble()->ScheduleUpdate();
};

void WebNotificationTray::HidePopups() {
  popup_bubble_.reset();
}

// Private methods.

bool WebNotificationTray::ShouldShowMessageCenter() {
  return status_area_widget()->login_status() != user::LOGGED_IN_LOCKED &&
      status_area_widget()->ShouldShowWebNotifications();
}

bool WebNotificationTray::ShouldShowQuietModeBubble(const ui::Event& event) {
  // TODO(mukai): Add keyboard event handler.
  if (!event.IsMouseEvent())
    return false;

  const ui::MouseEvent* mouse_event =
      static_cast<const ui::MouseEvent*>(&event);

  return mouse_event->IsRightMouseButton();
}

void WebNotificationTray::ShowQuietModeBubble() {
  aura::Window* parent = Shell::GetContainer(
      Shell::GetPrimaryRootWindow(),
      internal::kShellWindowId_SettingBubbleContainer);
  quiet_mode_bubble_.reset(new message_center::QuietModeBubble(
      button_,
      parent,
      message_center_tray_->message_center()->notification_list()));
  quiet_mode_bubble()->GetBubbleWidget()->StackAtTop();
  quiet_mode_bubble()->GetBubbleWidget()->AddObserver(this);
}

void WebNotificationTray::UpdateAfterLoginStatusChange(
    user::LoginStatus login_status) {
  if (message_center::IsRichNotificationEnabled()) {
    // The status icon should be always visible except for lock screen / login
    // screen, to allow quiet mode and settings. This is valid only when rich
    // notification is enabled, since old UI doesn't have settings.
    SetVisible((login_status != user::LOGGED_IN_NONE) &&
               (login_status != user::LOGGED_IN_LOCKED));
  }

  if (login_status == user::LOGGED_IN_LOCKED) {
    show_message_center_on_unlock_ =
        message_center_tray_->HideMessageCenterBubble();
    message_center_tray_->HidePopupBubble();
  } else {
    // Only try once to show the message center bubble on login status change,
    // so always set |show_message_center_on_unlock_| to false.
    if (show_message_center_on_unlock_)
      message_center_tray_->ShowMessageCenterBubble();
    show_message_center_on_unlock_ = false;
  }
}

bool WebNotificationTray::ShouldBlockLauncherAutoHide() const {
  return IsMessageCenterBubbleVisible() || quiet_mode_bubble() != NULL;
}

bool WebNotificationTray::IsMessageCenterBubbleVisible() const {
  return (message_center_bubble() &&
          message_center_bubble()->bubble()->IsVisible());
}

bool WebNotificationTray::IsMouseInNotificationBubble() const {
  if (!popup_bubble())
    return false;
  return popup_bubble()->bubble_view()->GetBoundsInScreen().Contains(
      Shell::GetScreen()->GetCursorScreenPoint());
}

void WebNotificationTray::ShowMessageCenterBubble() {
  if (!IsMessageCenterBubbleVisible())
    message_center_tray_->ShowMessageCenterBubble();
}

void WebNotificationTray::SetShelfAlignment(ShelfAlignment alignment) {
  if (alignment == shelf_alignment())
    return;
  internal::TrayBackgroundView::SetShelfAlignment(alignment);
  // Destroy any existing bubble so that it will be rebuilt correctly.
  message_center_tray_->HideMessageCenterBubble();
  message_center_tray_->HidePopupBubble();
}

void WebNotificationTray::AnchorUpdated() {
  if (popup_bubble()) {
    popup_bubble()->bubble_view()->UpdateBubble();
    // Ensure that the notification buble is above the launcher/status area.
    popup_bubble()->bubble_view()->GetWidget()->StackAtTop();
    UpdateBubbleViewArrow(popup_bubble()->bubble_view());
  }
  if (message_center_bubble()) {
    message_center_bubble()->bubble_view()->UpdateBubble();
    UpdateBubbleViewArrow(message_center_bubble()->bubble_view());
  }
  // Quiet mode settings bubble has to be on top.
  if (quiet_mode_bubble() && quiet_mode_bubble()->GetBubbleWidget())
    quiet_mode_bubble()->GetBubbleWidget()->StackAtTop();
}

string16 WebNotificationTray::GetAccessibleNameForTray() {
  return l10n_util::GetStringUTF16(
      IDS_ASH_WEB_NOTIFICATION_TRAY_ACCESSIBLE_NAME);
}

void WebNotificationTray::HideBubbleWithView(
    const views::TrayBubbleView* bubble_view) {
  if (message_center_bubble() &&
      bubble_view == message_center_bubble()->bubble_view()) {
    message_center_tray_->HideMessageCenterBubble();
  } else if (popup_bubble() && bubble_view == popup_bubble()->bubble_view()) {
    message_center_tray_->HidePopupBubble();
  }
}

bool WebNotificationTray::PerformAction(const ui::Event& event) {
  if (!quiet_mode_bubble() && ShouldShowQuietModeBubble(event)) {
    ShowQuietModeBubble();
    return true;
  }
  quiet_mode_bubble_.reset();
  if (message_center_bubble())
    message_center_tray_->HideMessageCenterBubble();
  else
    message_center_tray_->ShowMessageCenterBubble();
  return true;
}

void WebNotificationTray::BubbleViewDestroyed() {
  if (message_center_bubble())
    message_center_bubble()->bubble()->BubbleViewDestroyed();
  if (popup_bubble())
    popup_bubble()->bubble()->BubbleViewDestroyed();
}

void WebNotificationTray::OnMouseEnteredView() {
  if (popup_bubble())
    popup_bubble()->bubble()->OnMouseEnteredView();
}

void WebNotificationTray::OnMouseExitedView() {
  if (popup_bubble())
    popup_bubble()->bubble()->OnMouseExitedView();
}

string16 WebNotificationTray::GetAccessibleNameForBubble() {
  return GetAccessibleNameForTray();
}

gfx::Rect WebNotificationTray::GetAnchorRect(
    views::Widget* anchor_widget,
    views::TrayBubbleView::AnchorType anchor_type,
    views::TrayBubbleView::AnchorAlignment anchor_alignment) {
  return GetBubbleAnchorRect(anchor_widget, anchor_type, anchor_alignment);
}

void WebNotificationTray::HideBubble(const views::TrayBubbleView* bubble_view) {
  HideBubbleWithView(bubble_view);
}

void WebNotificationTray::ButtonPressed(views::Button* sender,
                                        const ui::Event& event) {
  DCHECK_EQ(button_, sender);
  PerformAction(event);
}

void WebNotificationTray::OnWidgetDestroying(views::Widget* widget) {
  if (quiet_mode_bubble() && quiet_mode_bubble()->GetBubbleWidget() == widget) {
    widget->RemoveObserver(this);
  }
  quiet_mode_bubble_.reset();
}

void WebNotificationTray::OnMessageCenterTrayChanged() {
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  message_center::MessageCenter* message_center =
      message_center_tray_->message_center();
  if (message_center->UnreadNotificationCount() > 0) {
    button_->SetImage(views::CustomButton::STATE_NORMAL, rb.GetImageSkiaNamed(
        IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_ACTIVE_NORMAL));
    button_->SetImage(views::CustomButton::STATE_HOVERED, rb.GetImageSkiaNamed(
        IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_ACTIVE_HOVER));
    button_->SetImage(views::CustomButton::STATE_PRESSED, rb.GetImageSkiaNamed(
        IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_ACTIVE_PRESSED));
  } else {
    button_->SetImage(views::CustomButton::STATE_NORMAL, rb.GetImageSkiaNamed(
        IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_INACTIVE_NORMAL));
    button_->SetImage(views::CustomButton::STATE_HOVERED, rb.GetImageSkiaNamed(
        IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_INACTIVE_HOVER));
    button_->SetImage(views::CustomButton::STATE_PRESSED, rb.GetImageSkiaNamed(
        IDR_AURA_UBER_TRAY_NOTIFY_BUTTON_INACTIVE_PRESSED));
  }
  if (IsMessageCenterBubbleVisible())
    button_->SetState(views::CustomButton::STATE_PRESSED);
  else
    button_->SetState(views::CustomButton::STATE_NORMAL);
  // Change the visibility of the buttons here when rich notifications are not
  // enabled. If rich notifications are enabled, the visibility is changed at
  // UpdateAfterLoginStatusChange() since the visibility won't depend on the
  // number of notifications.
  if (!message_center::IsRichNotificationEnabled()) {
    bool is_visible =
        (status_area_widget()->login_status() != user::LOGGED_IN_NONE) &&
        (status_area_widget()->login_status() != user::LOGGED_IN_LOCKED) &&
        (message_center->NotificationCount() > 0);
    SetVisible(is_visible);
  }
  Layout();
  SchedulePaint();
}

bool WebNotificationTray::ClickedOutsideBubble() {
  // Only hide the message center and quiet mode bubble.
  if (!message_center_bubble() && !quiet_mode_bubble())
    return false;
  quiet_mode_bubble_.reset();
  message_center_tray_->HideMessageCenterBubble();
  return true;
}

message_center::MessageCenter* WebNotificationTray::message_center() {
  return message_center_tray_->message_center();
}

// Methods for testing

bool WebNotificationTray::IsPopupVisible() const {
  return message_center_tray_->popups_visible();
}

message_center::MessageCenterBubble*
WebNotificationTray::GetMessageCenterBubbleForTest() {
  if (!message_center_bubble())
    return NULL;
  return static_cast<message_center::MessageCenterBubble*>(
      message_center_bubble()->bubble());
}

message_center::MessagePopupBubble*
WebNotificationTray::GetPopupBubbleForTest() {
  if (!popup_bubble())
    return NULL;
  return static_cast<message_center::MessagePopupBubble*>(
      popup_bubble()->bubble());
}

}  // namespace ash