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.
5 #include "ash/system/tray/tray_event_filter.h"
7 #include "ash/root_window_controller.h"
8 #include "ash/shelf/shelf_layout_manager.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"
22 TrayEventFilter::TrayEventFilter() {
25 TrayEventFilter::~TrayEventFilter() {
26 DCHECK(wrappers_.empty());
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);
36 void TrayEventFilter::RemoveWrapper(TrayBubbleWrapper* wrapper) {
37 wrappers_.erase(wrapper);
38 if (wrappers_.empty())
39 ash::Shell::GetInstance()->RemovePreTargetHandler(this);
42 void TrayEventFilter::OnMouseEvent(ui::MouseEvent* event) {
43 if (event->type() == ui::ET_MOUSE_PRESSED &&
44 ProcessLocatedEvent(event)) {
45 event->StopPropagation();
49 void TrayEventFilter::OnTouchEvent(ui::TouchEvent* event) {
50 if (event->type() == ui::ET_TOUCH_PRESSED && ProcessLocatedEvent(event))
51 event->StopPropagation();
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)
67 // Check the boundary for all wrappers, and do not handle the event if it
68 // happens inside of any of those wrappers.
69 for (std::set<TrayBubbleWrapper*>::const_iterator iter = wrappers_.begin();
70 iter != wrappers_.end(); ++iter) {
71 const TrayBubbleWrapper* wrapper = *iter;
72 const views::Widget* bubble_widget = wrapper->bubble_widget();
76 gfx::Rect bounds = bubble_widget->GetWindowBoundsInScreen();
77 bounds.Inset(wrapper->bubble_view()->GetBorderInsets());
78 aura::Window* root = bubble_widget->GetNativeView()->GetRootWindow();
79 aura::client::ScreenPositionClient* screen_position_client =
80 aura::client::GetScreenPositionClient(root);
81 gfx::Point screen_point(event->root_location());
82 screen_position_client->ConvertPointToScreen(root, &screen_point);
84 if (bounds.Contains(screen_point))
86 if (wrapper->tray()) {
87 // If the user clicks on the parent tray, don't process the event here,
88 // let the tray logic handle the event and determine show/hide behavior.
89 bounds = wrapper->tray()->GetBoundsInScreen();
90 if (bounds.Contains(screen_point))
95 // Handle clicking outside the bubble and tray and return true if the
97 // Cannot iterate |wrappers_| directly, because clicking outside will remove
98 // the wrapper, which shrinks |wrappers_| unsafely.
99 std::set<TrayBackgroundView*> trays;
100 for (std::set<TrayBubbleWrapper*>::iterator iter = wrappers_.begin();
101 iter != wrappers_.end(); ++iter) {
102 trays.insert((*iter)->tray());
104 bool handled = false;
105 for (std::set<TrayBackgroundView*>::iterator iter = trays.begin();
106 iter != trays.end(); ++iter) {
107 handled |= (*iter)->ClickedOutsideBubble();