summaryrefslogtreecommitdiffstats
path: root/ash/system/tray/tray_bubble_wrapper.cc
blob: c57cc7a009240ab1571250ca66ef94e7fc372a25 (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
// 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/tray/tray_bubble_wrapper.h"

#include "ash/system/tray/tray_background_view.h"
#include "ash/system/tray/tray_event_filter.h"
#include "ash/wm/window_properties.h"
#include "base/bind.h"
#include "base/message_loop.h"
#include "base/time.h"
#include "ui/views/bubble/tray_bubble_view.h"
#include "ui/views/widget/widget.h"

namespace ash {
namespace internal {

TrayBubbleWrapper::TrayBubbleWrapper(TrayBackgroundView* tray,
                                     views::TrayBubbleView* bubble_view)
    : tray_(tray),
      bubble_view_(bubble_view) {
  DCHECK(tray_);
  bubble_widget_ = views::BubbleDelegateView::CreateBubble(bubble_view_);
  bubble_widget_->AddObserver(this);
  bubble_widget_->GetNativeView()->
      SetProperty(internal::kStayInSameRootWindowKey, true);

  tray_->InitializeBubbleAnimations(bubble_widget_);
  tray_->UpdateBubbleViewArrow(bubble_view_);
  bubble_view_->InitializeAndShowBubble();

  tray->tray_event_filter()->AddWrapper(this);
}

TrayBubbleWrapper::~TrayBubbleWrapper() {
  tray_->tray_event_filter()->RemoveWrapper(this);
  if (bubble_widget_) {
    bubble_widget_->RemoveObserver(this);
    bubble_widget_->Close();
  }
}

void TrayBubbleWrapper::OnWidgetDestroying(views::Widget* widget) {
  CHECK_EQ(bubble_widget_, widget);
  bubble_widget_ = NULL;

  // Do not call HideBubbleWithView directly but post the task to ensure that
  // HideBubbleWithView is called after the click event on the tray button is
  // handled. See crbug.com/177075 and crbug.com/169940
  MessageLoopForUI::current()->PostTask(
      FROM_HERE,
      base::Bind(&TrayBackgroundView::HideBubbleWithView,
                 base::Unretained(tray_), base::Unretained(bubble_view_)));
}

}  // namespace internal
}  // namespace ash