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