Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / system / tray / tray_event_filter.cc
1 // Copyright (c) 2012 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 "ash/system/tray/tray_event_filter.h"
6
7 #include "ash/root_window_controller.h"
8 #include "ash/shelf/shelf_layout_manager.h"
9 #include "ash/shell.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/system/tray/tray_background_view.h"
12 #include "ash/system/tray/tray_bubble_wrapper.h"
13 #include "ash/system/tray/tray_constants.h"
14 #include "ash/system/tray/tray_event_filter.h"
15 #include "ui/aura/client/screen_position_client.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/views/widget/widget.h"
19
20 namespace ash {
21
22 TrayEventFilter::TrayEventFilter() {
23 }
24
25 TrayEventFilter::~TrayEventFilter() {
26   DCHECK(wrappers_.empty());
27 }
28
29 void TrayEventFilter::AddWrapper(TrayBubbleWrapper* wrapper) {
30   bool was_empty = wrappers_.empty();
31   wrappers_.insert(wrapper);
32   if (was_empty && !wrappers_.empty())
33     ash::Shell::GetInstance()->AddPreTargetHandler(this);
34 }
35
36 void TrayEventFilter::RemoveWrapper(TrayBubbleWrapper* wrapper) {
37   wrappers_.erase(wrapper);
38   if (wrappers_.empty())
39     ash::Shell::GetInstance()->RemovePreTargetHandler(this);
40 }
41
42 void TrayEventFilter::OnMouseEvent(ui::MouseEvent* event) {
43   if (event->type() == ui::ET_MOUSE_PRESSED &&
44       ProcessLocatedEvent(event)) {
45     event->StopPropagation();
46   }
47 }
48
49 void TrayEventFilter::OnTouchEvent(ui::TouchEvent* event) {
50   if (event->type() == ui::ET_TOUCH_PRESSED && ProcessLocatedEvent(event))
51     event->StopPropagation();
52 }
53
54 bool TrayEventFilter::ProcessLocatedEvent(ui::LocatedEvent* event) {
55   if (event->target()) {
56     aura::Window* target = static_cast<aura::Window*>(event->target());
57     // Don't process events that occurred inside an embedded menu.
58     RootWindowController* root_controller =
59         GetRootWindowController(target->GetRootWindow());
60     if (root_controller &&
61         root_controller->GetContainer(kShellWindowId_MenuContainer)
62             ->Contains(target)) {
63       return false;
64     }
65     // Don't process events that occurred inside the status area widget and
66     // a popup notification from message center.
67     if (root_controller &&
68         root_controller->GetContainer(kShellWindowId_StatusContainer)
69             ->Contains(target)) {
70       return false;
71     }
72   }
73
74   // Check the boundary for all wrappers, and do not handle the event if it
75   // happens inside of any of those wrappers.
76   for (std::set<TrayBubbleWrapper*>::const_iterator iter = wrappers_.begin();
77        iter != wrappers_.end(); ++iter) {
78     const TrayBubbleWrapper* wrapper = *iter;
79     const views::Widget* bubble_widget = wrapper->bubble_widget();
80     if (!bubble_widget)
81       continue;
82
83     gfx::Rect bounds = bubble_widget->GetWindowBoundsInScreen();
84     bounds.Inset(wrapper->bubble_view()->GetBorderInsets());
85     aura::Window* root = bubble_widget->GetNativeView()->GetRootWindow();
86     aura::client::ScreenPositionClient* screen_position_client =
87         aura::client::GetScreenPositionClient(root);
88     gfx::Point screen_point(event->root_location());
89     screen_position_client->ConvertPointToScreen(root, &screen_point);
90
91     if (bounds.Contains(screen_point))
92       return false;
93     if (wrapper->tray()) {
94       // If the user clicks on the parent tray, don't process the event here,
95       // let the tray logic handle the event and determine show/hide behavior.
96       bounds = wrapper->tray()->GetBoundsInScreen();
97       if (bounds.Contains(screen_point))
98         return false;
99     }
100   }
101
102   // Handle clicking outside the bubble and tray and return true if the
103   // event was handled.
104   // Cannot iterate |wrappers_| directly, because clicking outside will remove
105   // the wrapper, which shrinks |wrappers_| unsafely.
106   std::set<TrayBackgroundView*> trays;
107   for (std::set<TrayBubbleWrapper*>::iterator iter = wrappers_.begin();
108        iter != wrappers_.end(); ++iter) {
109     trays.insert((*iter)->tray());
110   }
111   bool handled = false;
112   for (std::set<TrayBackgroundView*>::iterator iter = trays.begin();
113        iter != trays.end(); ++iter) {
114     handled |= (*iter)->ClickedOutsideBubble();
115   }
116   return handled;
117 }
118
119 }  // namespace ash