OLD | NEW |
| (Empty) |
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/common/system/tray/tray_event_filter.h" | |
6 | |
7 #include "ash/common/system/tray/tray_background_view.h" | |
8 #include "ash/common/system/tray/tray_bubble_wrapper.h" | |
9 #include "ash/common/wm/container_finder.h" | |
10 #include "ash/common/wm_shell.h" | |
11 #include "ash/common/wm_window.h" | |
12 #include "ash/public/cpp/shell_window_ids.h" | |
13 #include "ui/views/widget/widget.h" | |
14 | |
15 namespace ash { | |
16 | |
17 TrayEventFilter::TrayEventFilter() {} | |
18 | |
19 TrayEventFilter::~TrayEventFilter() { | |
20 DCHECK(wrappers_.empty()); | |
21 } | |
22 | |
23 void TrayEventFilter::AddWrapper(TrayBubbleWrapper* wrapper) { | |
24 bool was_empty = wrappers_.empty(); | |
25 wrappers_.insert(wrapper); | |
26 if (was_empty && !wrappers_.empty()) { | |
27 WmShell::Get()->AddPointerWatcher(this, | |
28 views::PointerWatcherEventTypes::BASIC); | |
29 } | |
30 } | |
31 | |
32 void TrayEventFilter::RemoveWrapper(TrayBubbleWrapper* wrapper) { | |
33 wrappers_.erase(wrapper); | |
34 if (wrappers_.empty()) | |
35 WmShell::Get()->RemovePointerWatcher(this); | |
36 } | |
37 | |
38 void TrayEventFilter::OnPointerEventObserved( | |
39 const ui::PointerEvent& event, | |
40 const gfx::Point& location_in_screen, | |
41 views::Widget* target) { | |
42 if (event.type() == ui::ET_POINTER_DOWN) | |
43 ProcessPressedEvent(location_in_screen, target); | |
44 } | |
45 | |
46 void TrayEventFilter::ProcessPressedEvent(const gfx::Point& location_in_screen, | |
47 views::Widget* target) { | |
48 if (target) { | |
49 WmWindow* window = WmWindow::Get(target->GetNativeWindow()); | |
50 int container_id = wm::GetContainerForWindow(window)->GetShellWindowId(); | |
51 // Don't process events that occurred inside an embedded menu, for example | |
52 // the right-click menu in a popup notification. | |
53 if (container_id == kShellWindowId_MenuContainer) | |
54 return; | |
55 // Don't process events that occurred inside a popup notification | |
56 // from message center. | |
57 if (container_id == kShellWindowId_StatusContainer && | |
58 window->GetType() == ui::wm::WINDOW_TYPE_POPUP && | |
59 target->IsAlwaysOnTop()) { | |
60 return; | |
61 } | |
62 } | |
63 | |
64 std::set<TrayBackgroundView*> trays; | |
65 // Check the boundary for all wrappers, and do not handle the event if it | |
66 // happens inside of any of those wrappers. | |
67 for (std::set<TrayBubbleWrapper*>::const_iterator iter = wrappers_.begin(); | |
68 iter != wrappers_.end(); ++iter) { | |
69 const TrayBubbleWrapper* wrapper = *iter; | |
70 const views::Widget* bubble_widget = wrapper->bubble_widget(); | |
71 if (!bubble_widget) | |
72 continue; | |
73 | |
74 gfx::Rect bounds = bubble_widget->GetWindowBoundsInScreen(); | |
75 bounds.Inset(wrapper->bubble_view()->GetBorderInsets()); | |
76 if (bounds.Contains(location_in_screen)) | |
77 continue; | |
78 if (wrapper->tray()) { | |
79 // If the user clicks on the parent tray, don't process the event here, | |
80 // let the tray logic handle the event and determine show/hide behavior. | |
81 bounds = wrapper->tray()->GetBoundsInScreen(); | |
82 if (bounds.Contains(location_in_screen)) | |
83 continue; | |
84 } | |
85 trays.insert((*iter)->tray()); | |
86 } | |
87 | |
88 // Close all bubbles other than the one a user clicked on the tray | |
89 // or its bubble. | |
90 for (std::set<TrayBackgroundView*>::iterator iter = trays.begin(); | |
91 iter != trays.end(); ++iter) { | |
92 (*iter)->ClickedOutsideBubble(); | |
93 } | |
94 } | |
95 | |
96 } // namespace ash | |
OLD | NEW |