Home | History | Annotate | Download | only in widget
      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 "ui/views/widget/root_view.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "base/logging.h"
     10 #include "base/message_loop/message_loop.h"
     11 #include "ui/base/accessibility/accessible_view_state.h"
     12 #include "ui/base/dragdrop/drag_drop_types.h"
     13 #include "ui/base/events/event.h"
     14 #include "ui/base/keycodes/keyboard_codes.h"
     15 #include "ui/compositor/layer.h"
     16 #include "ui/gfx/canvas.h"
     17 #include "ui/views/focus/view_storage.h"
     18 #include "ui/views/layout/fill_layout.h"
     19 #include "ui/views/widget/widget.h"
     20 #include "ui/views/widget/widget_delegate.h"
     21 #include "ui/views/widget/widget_deletion_observer.h"
     22 
     23 namespace views {
     24 namespace internal {
     25 
     26 namespace {
     27 
     28 enum EventType {
     29   EVENT_ENTER,
     30   EVENT_EXIT
     31 };
     32 
     33 class MouseEnterExitEvent : public ui::MouseEvent {
     34  public:
     35   MouseEnterExitEvent(const ui::MouseEvent& event, ui::EventType type)
     36       : ui::MouseEvent(event,
     37                        static_cast<View*>(NULL),
     38                        static_cast<View*>(NULL)) {
     39     DCHECK(type == ui::ET_MOUSE_ENTERED ||
     40            type == ui::ET_MOUSE_EXITED);
     41     SetType(type);
     42   }
     43 
     44   virtual ~MouseEnterExitEvent() {}
     45 };
     46 
     47 }  // namespace
     48 
     49 // static
     50 const char RootView::kViewClassName[] = "RootView";
     51 
     52 ////////////////////////////////////////////////////////////////////////////////
     53 // RootView, public:
     54 
     55 // Creation and lifetime -------------------------------------------------------
     56 
     57 RootView::RootView(Widget* widget)
     58     : widget_(widget),
     59       mouse_pressed_handler_(NULL),
     60       mouse_move_handler_(NULL),
     61       last_click_handler_(NULL),
     62       explicit_mouse_handler_(false),
     63       last_mouse_event_flags_(0),
     64       last_mouse_event_x_(-1),
     65       last_mouse_event_y_(-1),
     66       touch_pressed_handler_(NULL),
     67       gesture_handler_(NULL),
     68       scroll_gesture_handler_(NULL),
     69       focus_search_(this, false, false),
     70       focus_traversable_parent_(NULL),
     71       focus_traversable_parent_view_(NULL),
     72       event_dispatch_target_(NULL) {
     73 }
     74 
     75 RootView::~RootView() {
     76   // If we have children remove them explicitly so to make sure a remove
     77   // notification is sent for each one of them.
     78   if (has_children())
     79     RemoveAllChildViews(true);
     80 }
     81 
     82 // Tree operations -------------------------------------------------------------
     83 
     84 void RootView::SetContentsView(View* contents_view) {
     85   DCHECK(contents_view && GetWidget()->native_widget()) <<
     86       "Can't be called until after the native widget is created!";
     87   // The ContentsView must be set up _after_ the window is created so that its
     88   // Widget pointer is valid.
     89   SetLayoutManager(new FillLayout);
     90   if (has_children())
     91     RemoveAllChildViews(true);
     92   AddChildView(contents_view);
     93 
     94   // Force a layout now, since the attached hierarchy won't be ready for the
     95   // containing window's bounds. Note that we call Layout directly rather than
     96   // calling the widget's size changed handler, since the RootView's bounds may
     97   // not have changed, which will cause the Layout not to be done otherwise.
     98   Layout();
     99 }
    100 
    101 View* RootView::GetContentsView() {
    102   return child_count() > 0 ? child_at(0) : NULL;
    103 }
    104 
    105 void RootView::NotifyNativeViewHierarchyChanged(bool attached,
    106                                                 gfx::NativeView native_view) {
    107   PropagateNativeViewHierarchyChanged(attached, native_view, this);
    108 }
    109 
    110 // Input -----------------------------------------------------------------------
    111 
    112 void RootView::DispatchKeyEvent(ui::KeyEvent* event) {
    113   View* v = NULL;
    114   if (GetFocusManager())  // NULL in unittests.
    115     v = GetFocusManager()->GetFocusedView();
    116   // Special case to handle right-click context menus triggered by the
    117   // keyboard.
    118   if (v && v->enabled() && ((event->key_code() == ui::VKEY_APPS) ||
    119      (event->key_code() == ui::VKEY_F10 && event->IsShiftDown()))) {
    120     v->ShowContextMenu(v->GetKeyboardContextMenuLocation(),
    121                        ui::MENU_SOURCE_KEYBOARD);
    122     event->StopPropagation();
    123     return;
    124   }
    125 
    126   for (; v && v != this && !event->handled(); v = v->parent())
    127     DispatchEventToTarget(v, event);
    128 }
    129 
    130 void RootView::DispatchScrollEvent(ui::ScrollEvent* event) {
    131   for (View* v = GetEventHandlerForPoint(event->location());
    132        v && v != this && !event->stopped_propagation(); v = v->parent()) {
    133     DispatchEventToTarget(v, event);
    134   }
    135 
    136   if (event->handled() || event->type() != ui::ET_SCROLL)
    137     return;
    138 
    139   // Convert unprocessed scroll events into mouse-wheel events.
    140   ui::MouseWheelEvent wheel(*event);
    141   if (OnMouseWheel(wheel))
    142     event->SetHandled();
    143 }
    144 
    145 void RootView::DispatchTouchEvent(ui::TouchEvent* event) {
    146   // TODO: this looks all wrong. On a TOUCH_PRESSED we should figure out the
    147   // view and target that view with all touches with the same id until the
    148   // release (or keep it if captured).
    149 
    150   // If touch_pressed_handler_ is non null, we are currently processing
    151   // a touch down on the screen situation. In that case we send the
    152   // event to touch_pressed_handler_
    153 
    154   if (touch_pressed_handler_) {
    155     ui::TouchEvent touch_event(*event, static_cast<View*>(this),
    156                                touch_pressed_handler_);
    157     DispatchEventToTarget(touch_pressed_handler_, &touch_event);
    158     if (touch_event.handled())
    159       event->SetHandled();
    160     if (touch_event.stopped_propagation())
    161       event->StopPropagation();
    162     return;
    163   }
    164 
    165   // Walk up the tree until we find a view that wants the touch event.
    166   for (touch_pressed_handler_ = GetEventHandlerForPoint(event->location());
    167        touch_pressed_handler_ && (touch_pressed_handler_ != this);
    168        touch_pressed_handler_ = touch_pressed_handler_->parent()) {
    169     if (!touch_pressed_handler_->enabled()) {
    170       // Disabled views eat events but are treated as not handled.
    171       break;
    172     }
    173 
    174     // See if this view wants to handle the touch
    175     ui::TouchEvent touch_event(*event, static_cast<View*>(this),
    176                                touch_pressed_handler_);
    177     DispatchEventToTarget(touch_pressed_handler_, &touch_event);
    178     if (touch_event.handled())
    179       event->SetHandled();
    180     if (touch_event.stopped_propagation())
    181       event->StopPropagation();
    182 
    183     // The view could have removed itself from the tree when handling
    184     // OnTouchEvent(). So handle as per OnMousePressed. NB: we
    185     // assume that the RootView itself cannot be so removed.
    186     if (!touch_pressed_handler_)
    187       break;
    188 
    189     // The touch event wasn't processed. Go up the view hierarchy and dispatch
    190     // the touch event.
    191     if (!event->handled())
    192       continue;
    193 
    194     // If a View consumed the event, that means future touch-events should go to
    195     // that View. If the event wasn't consumed, then reset the handler.
    196     if (!event->stopped_propagation())
    197       touch_pressed_handler_ = NULL;
    198 
    199     return;
    200   }
    201 
    202   // Reset touch_pressed_handler_ to indicate that no processing is occurring.
    203   touch_pressed_handler_ = NULL;
    204 
    205   return;
    206 }
    207 
    208 void RootView::DispatchGestureEvent(ui::GestureEvent* event) {
    209   if (gesture_handler_) {
    210     // |gesture_handler_| (or |scroll_gesture_handler_|) can be deleted during
    211     // processing.
    212     View* handler = scroll_gesture_handler_ &&
    213         (event->IsScrollGestureEvent() || event->IsFlingScrollEvent())  ?
    214             scroll_gesture_handler_ : gesture_handler_;
    215     ui::GestureEvent handler_event(*event, static_cast<View*>(this), handler);
    216     DispatchEventToTarget(handler, &handler_event);
    217 
    218     if (event->type() == ui::ET_GESTURE_END &&
    219         event->details().touch_points() <= 1) {
    220       // In case a drag was in progress, reset all the handlers. Otherwise, just
    221       // reset the gesture handler.
    222       if (gesture_handler_ == mouse_pressed_handler_)
    223         SetMouseHandler(NULL);
    224       else
    225         gesture_handler_ = NULL;
    226     }
    227 
    228     if (scroll_gesture_handler_ &&
    229         (event->type() == ui::ET_GESTURE_SCROLL_END ||
    230          event->type() == ui::ET_SCROLL_FLING_START)) {
    231       scroll_gesture_handler_ = NULL;
    232     }
    233 
    234     if (handler_event.stopped_propagation()) {
    235       event->StopPropagation();
    236       return;
    237     } else if (handler_event.handled()) {
    238       event->SetHandled();
    239       return;
    240     }
    241 
    242     if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN &&
    243         !scroll_gesture_handler_) {
    244       // Some view started processing gesture events, however it does not
    245       // process scroll-gesture events. In such case, we allow the event to
    246       // bubble up, and install a different scroll-gesture handler different
    247       // from the default gesture handler.
    248       for (scroll_gesture_handler_ = gesture_handler_->parent();
    249           scroll_gesture_handler_ && scroll_gesture_handler_ != this;
    250           scroll_gesture_handler_ = scroll_gesture_handler_->parent()) {
    251         ui::GestureEvent gesture_event(*event, static_cast<View*>(this),
    252                                        scroll_gesture_handler_);
    253         DispatchEventToTarget(scroll_gesture_handler_, &gesture_event);
    254         if (gesture_event.stopped_propagation()) {
    255           event->StopPropagation();
    256           return;
    257         } else if (gesture_event.handled()) {
    258           event->SetHandled();
    259           return;
    260         }
    261       }
    262       scroll_gesture_handler_ = NULL;
    263     }
    264 
    265     return;
    266   }
    267 
    268   // If there was no handler for a SCROLL_BEGIN event, then subsequent scroll
    269   // events are not dispatched to any views.
    270   switch (event->type()) {
    271     case ui::ET_GESTURE_SCROLL_UPDATE:
    272     case ui::ET_GESTURE_SCROLL_END:
    273     case ui::ET_SCROLL_FLING_START:
    274       return;
    275     default:
    276       break;
    277   }
    278 
    279   // Walk up the tree until we find a view that wants the gesture event.
    280   for (gesture_handler_ = GetEventHandlerForPoint(event->location());
    281       gesture_handler_ && (gesture_handler_ != this);
    282       gesture_handler_ = gesture_handler_->parent()) {
    283     if (!gesture_handler_->enabled()) {
    284       // Disabled views eat events but are treated as not handled.
    285       return;
    286     }
    287 
    288     // See if this view wants to handle the Gesture.
    289     ui::GestureEvent gesture_event(*event, static_cast<View*>(this),
    290                                    gesture_handler_);
    291     DispatchEventToTarget(gesture_handler_, &gesture_event);
    292 
    293     // The view could have removed itself from the tree when handling
    294     // OnGestureEvent(). So handle as per OnMousePressed. NB: we
    295     // assume that the RootView itself cannot be so removed.
    296     if (!gesture_handler_)
    297       return;
    298 
    299     if (gesture_event.handled()) {
    300       if (gesture_event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
    301         scroll_gesture_handler_ = gesture_handler_;
    302       if (gesture_event.stopped_propagation())
    303         event->StopPropagation();
    304       else
    305         event->SetHandled();
    306       return;
    307     }
    308 
    309     // The gesture event wasn't processed. Go up the view hierarchy and
    310     // dispatch the gesture event.
    311   }
    312 
    313   gesture_handler_ = NULL;
    314 }
    315 
    316 // Focus -----------------------------------------------------------------------
    317 
    318 void RootView::SetFocusTraversableParent(FocusTraversable* focus_traversable) {
    319   DCHECK(focus_traversable != this);
    320   focus_traversable_parent_ = focus_traversable;
    321 }
    322 
    323 void RootView::SetFocusTraversableParentView(View* view) {
    324   focus_traversable_parent_view_ = view;
    325 }
    326 
    327 // System events ---------------------------------------------------------------
    328 
    329 void RootView::ThemeChanged() {
    330   View::PropagateThemeChanged();
    331 }
    332 
    333 void RootView::LocaleChanged() {
    334   View::PropagateLocaleChanged();
    335 }
    336 
    337 ////////////////////////////////////////////////////////////////////////////////
    338 // RootView, FocusTraversable implementation:
    339 
    340 FocusSearch* RootView::GetFocusSearch() {
    341   return &focus_search_;
    342 }
    343 
    344 FocusTraversable* RootView::GetFocusTraversableParent() {
    345   return focus_traversable_parent_;
    346 }
    347 
    348 View* RootView::GetFocusTraversableParentView() {
    349   return focus_traversable_parent_view_;
    350 }
    351 
    352 ////////////////////////////////////////////////////////////////////////////////
    353 // RootView, View overrides:
    354 
    355 const Widget* RootView::GetWidget() const {
    356   return widget_;
    357 }
    358 
    359 Widget* RootView::GetWidget() {
    360   return const_cast<Widget*>(const_cast<const RootView*>(this)->GetWidget());
    361 }
    362 
    363 bool RootView::IsDrawn() const {
    364   return visible();
    365 }
    366 
    367 const char* RootView::GetClassName() const {
    368   return kViewClassName;
    369 }
    370 
    371 void RootView::SchedulePaintInRect(const gfx::Rect& rect) {
    372   if (layer()) {
    373     layer()->SchedulePaint(rect);
    374   } else {
    375     gfx::Rect xrect = ConvertRectToParent(rect);
    376     gfx::Rect invalid_rect = gfx::IntersectRects(GetLocalBounds(), xrect);
    377     if (!invalid_rect.IsEmpty())
    378       widget_->SchedulePaintInRect(invalid_rect);
    379   }
    380 }
    381 
    382 bool RootView::OnMousePressed(const ui::MouseEvent& event) {
    383   UpdateCursor(event);
    384   SetMouseLocationAndFlags(event);
    385 
    386   // If mouse_pressed_handler_ is non null, we are currently processing
    387   // a pressed -> drag -> released session. In that case we send the
    388   // event to mouse_pressed_handler_
    389   if (mouse_pressed_handler_) {
    390     ui::MouseEvent mouse_pressed_event(event, static_cast<View*>(this),
    391                                        mouse_pressed_handler_);
    392     drag_info_.Reset();
    393     DispatchEventToTarget(mouse_pressed_handler_, &mouse_pressed_event);
    394     return true;
    395   }
    396   DCHECK(!explicit_mouse_handler_);
    397 
    398   bool hit_disabled_view = false;
    399   // Walk up the tree until we find a view that wants the mouse event.
    400   for (mouse_pressed_handler_ = GetEventHandlerForPoint(event.location());
    401        mouse_pressed_handler_ && (mouse_pressed_handler_ != this);
    402        mouse_pressed_handler_ = mouse_pressed_handler_->parent()) {
    403     DVLOG(1) << "OnMousePressed testing "
    404         << mouse_pressed_handler_->GetClassName();
    405     if (!mouse_pressed_handler_->enabled()) {
    406       // Disabled views should eat events instead of propagating them upwards.
    407       hit_disabled_view = true;
    408       break;
    409     }
    410 
    411     // See if this view wants to handle the mouse press.
    412     ui::MouseEvent mouse_pressed_event(event, static_cast<View*>(this),
    413                                        mouse_pressed_handler_);
    414 
    415     // Remove the double-click flag if the handler is different than the
    416     // one which got the first click part of the double-click.
    417     if (mouse_pressed_handler_ != last_click_handler_)
    418       mouse_pressed_event.set_flags(event.flags() & ~ui::EF_IS_DOUBLE_CLICK);
    419 
    420     drag_info_.Reset();
    421     {
    422       WidgetDeletionObserver widget_deletion_observer(widget_);
    423       DispatchEventToTarget(mouse_pressed_handler_, &mouse_pressed_event);
    424       if (!widget_deletion_observer.IsWidgetAlive())
    425         return mouse_pressed_event.handled();
    426     }
    427 
    428     // The view could have removed itself from the tree when handling
    429     // OnMousePressed().  In this case, the removal notification will have
    430     // reset mouse_pressed_handler_ to NULL out from under us.  Detect this
    431     // case and stop.  (See comments in view.h.)
    432     //
    433     // NOTE: Don't return true here, because we don't want the frame to
    434     // forward future events to us when there's no handler.
    435     if (!mouse_pressed_handler_)
    436       break;
    437 
    438     // If the view handled the event, leave mouse_pressed_handler_ set and
    439     // return true, which will cause subsequent drag/release events to get
    440     // forwarded to that view.
    441     if (mouse_pressed_event.handled()) {
    442       last_click_handler_ = mouse_pressed_handler_;
    443       DVLOG(1) << "OnMousePressed handled by "
    444           << mouse_pressed_handler_->GetClassName();
    445       return true;
    446     }
    447   }
    448 
    449   // Reset mouse_pressed_handler_ to indicate that no processing is occurring.
    450   mouse_pressed_handler_ = NULL;
    451 
    452   // In the event that a double-click is not handled after traversing the
    453   // entire hierarchy (even as a single-click when sent to a different view),
    454   // it must be marked as handled to avoid anything happening from default
    455   // processing if it the first click-part was handled by us.
    456   if (last_click_handler_ && (event.flags() & ui::EF_IS_DOUBLE_CLICK))
    457     hit_disabled_view = true;
    458 
    459   last_click_handler_ = NULL;
    460   return hit_disabled_view;
    461 }
    462 
    463 bool RootView::OnMouseDragged(const ui::MouseEvent& event) {
    464   if (mouse_pressed_handler_) {
    465     SetMouseLocationAndFlags(event);
    466 
    467     ui::MouseEvent mouse_event(event, static_cast<View*>(this),
    468                                mouse_pressed_handler_);
    469     DispatchEventToTarget(mouse_pressed_handler_, &mouse_event);
    470   }
    471   return false;
    472 }
    473 
    474 void RootView::OnMouseReleased(const ui::MouseEvent& event) {
    475   UpdateCursor(event);
    476 
    477   if (mouse_pressed_handler_) {
    478     ui::MouseEvent mouse_released(event, static_cast<View*>(this),
    479                                   mouse_pressed_handler_);
    480     // We allow the view to delete us from the event dispatch callback. As such,
    481     // configure state such that we're done first, then call View.
    482     View* mouse_pressed_handler = mouse_pressed_handler_;
    483     SetMouseHandler(NULL);
    484     DispatchEventToTarget(mouse_pressed_handler, &mouse_released);
    485     // WARNING: we may have been deleted.
    486   }
    487 }
    488 
    489 void RootView::OnMouseCaptureLost() {
    490   // TODO: this likely needs to reset touch handler too.
    491 
    492   if (mouse_pressed_handler_ || gesture_handler_) {
    493     // Synthesize a release event for UpdateCursor.
    494     if (mouse_pressed_handler_) {
    495       gfx::Point last_point(last_mouse_event_x_, last_mouse_event_y_);
    496       ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
    497                                    last_point, last_point,
    498                                    last_mouse_event_flags_);
    499       UpdateCursor(release_event);
    500     }
    501     // We allow the view to delete us from OnMouseCaptureLost. As such,
    502     // configure state such that we're done first, then call View.
    503     View* mouse_pressed_handler = mouse_pressed_handler_;
    504     View* gesture_handler = gesture_handler_;
    505     SetMouseHandler(NULL);
    506     if (mouse_pressed_handler)
    507       mouse_pressed_handler->OnMouseCaptureLost();
    508     else
    509       gesture_handler->OnMouseCaptureLost();
    510     // WARNING: we may have been deleted.
    511   }
    512 }
    513 
    514 void RootView::OnMouseMoved(const ui::MouseEvent& event) {
    515   View* v = GetEventHandlerForPoint(event.location());
    516   // Find the first enabled view, or the existing move handler, whichever comes
    517   // first.  The check for the existing handler is because if a view becomes
    518   // disabled while handling moves, it's wrong to suddenly send ET_MOUSE_EXITED
    519   // and ET_MOUSE_ENTERED events, because the mouse hasn't actually exited yet.
    520   while (v && !v->enabled() && (v != mouse_move_handler_))
    521     v = v->parent();
    522   if (v && v != this) {
    523     if (v != mouse_move_handler_) {
    524       if (mouse_move_handler_ != NULL &&
    525           (!mouse_move_handler_->notify_enter_exit_on_child() ||
    526            !mouse_move_handler_->Contains(v))) {
    527         MouseEnterExitEvent exit(event, ui::ET_MOUSE_EXITED);
    528         DispatchEventToTarget(mouse_move_handler_, &exit);
    529         NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
    530             mouse_move_handler_, v);
    531       }
    532       View* old_handler = mouse_move_handler_;
    533       mouse_move_handler_ = v;
    534       if (!mouse_move_handler_->notify_enter_exit_on_child() ||
    535           !mouse_move_handler_->Contains(old_handler)) {
    536         MouseEnterExitEvent entered(event, ui::ET_MOUSE_ENTERED);
    537         entered.ConvertLocationToTarget(static_cast<View*>(this),
    538                                         mouse_move_handler_);
    539         DispatchEventToTarget(mouse_move_handler_, &entered);
    540         NotifyEnterExitOfDescendant(entered, ui::ET_MOUSE_ENTERED, v,
    541             old_handler);
    542       }
    543     }
    544     ui::MouseEvent moved_event(event, static_cast<View*>(this),
    545                                mouse_move_handler_);
    546     mouse_move_handler_->OnMouseMoved(moved_event);
    547     if (!(moved_event.flags() & ui::EF_IS_NON_CLIENT))
    548       widget_->SetCursor(mouse_move_handler_->GetCursor(moved_event));
    549   } else if (mouse_move_handler_ != NULL) {
    550     MouseEnterExitEvent exited(event, ui::ET_MOUSE_EXITED);
    551     DispatchEventToTarget(mouse_move_handler_, &exited);
    552     NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
    553         mouse_move_handler_, v);
    554     // On Aura the non-client area extends slightly outside the root view for
    555     // some windows.  Let the non-client cursor handling code set the cursor
    556     // as we do above.
    557     if (!(event.flags() & ui::EF_IS_NON_CLIENT))
    558       widget_->SetCursor(gfx::kNullCursor);
    559     mouse_move_handler_ = NULL;
    560   }
    561 }
    562 
    563 void RootView::OnMouseExited(const ui::MouseEvent& event) {
    564   if (mouse_move_handler_ != NULL) {
    565     MouseEnterExitEvent exited(event, ui::ET_MOUSE_EXITED);
    566     DispatchEventToTarget(mouse_move_handler_, &exited);
    567     NotifyEnterExitOfDescendant(event, ui::ET_MOUSE_EXITED,
    568         mouse_move_handler_, NULL);
    569     mouse_move_handler_ = NULL;
    570   }
    571 }
    572 
    573 bool RootView::OnMouseWheel(const ui::MouseWheelEvent& event) {
    574   for (View* v = GetEventHandlerForPoint(event.location());
    575        v && v != this && !event.handled(); v = v->parent())
    576     DispatchEventToTarget(v, const_cast<ui::MouseWheelEvent*>(&event));
    577   return event.handled();
    578 }
    579 
    580 void RootView::SetMouseHandler(View* new_mh) {
    581   // If we're clearing the mouse handler, clear explicit_mouse_handler_ as well.
    582   explicit_mouse_handler_ = (new_mh != NULL);
    583   mouse_pressed_handler_ = new_mh;
    584   gesture_handler_ = new_mh;
    585   scroll_gesture_handler_ = new_mh;
    586   drag_info_.Reset();
    587 }
    588 
    589 void RootView::GetAccessibleState(ui::AccessibleViewState* state) {
    590   state->name = widget_->widget_delegate()->GetAccessibleWindowTitle();
    591   state->role = widget_->widget_delegate()->GetAccessibleWindowRole();
    592 }
    593 
    594 void RootView::UpdateParentLayer() {
    595   if (layer())
    596     ReparentLayer(gfx::Vector2d(GetMirroredX(), y()), widget_->GetLayer());
    597 }
    598 
    599 ////////////////////////////////////////////////////////////////////////////////
    600 // RootView, protected:
    601 
    602 void RootView::ViewHierarchyChanged(
    603     const ViewHierarchyChangedDetails& details) {
    604   widget_->ViewHierarchyChanged(details);
    605 
    606   if (!details.is_add) {
    607     if (!explicit_mouse_handler_ && mouse_pressed_handler_ == details.child)
    608       mouse_pressed_handler_ = NULL;
    609     if (mouse_move_handler_ == details.child)
    610       mouse_move_handler_ = NULL;
    611     if (touch_pressed_handler_ == details.child)
    612       touch_pressed_handler_ = NULL;
    613     if (gesture_handler_ == details.child)
    614       gesture_handler_ = NULL;
    615     if (scroll_gesture_handler_ == details.child)
    616       scroll_gesture_handler_ = NULL;
    617     if (event_dispatch_target_ == details.child)
    618       event_dispatch_target_ = NULL;
    619   }
    620 }
    621 
    622 void RootView::VisibilityChanged(View* /*starting_from*/, bool is_visible) {
    623   if (!is_visible) {
    624     // When the root view is being hidden (e.g. when widget is minimized)
    625     // handlers are reset, so that after it is reshown, events are not captured
    626     // by old handlers.
    627     mouse_pressed_handler_ = NULL;
    628     mouse_move_handler_ = NULL;
    629     touch_pressed_handler_ = NULL;
    630     gesture_handler_ = NULL;
    631     scroll_gesture_handler_ = NULL;
    632     event_dispatch_target_ = NULL;
    633   }
    634 }
    635 
    636 void RootView::OnPaint(gfx::Canvas* canvas) {
    637   if (!layer() || !layer()->fills_bounds_opaquely())
    638     canvas->DrawColor(SK_ColorBLACK, SkXfermode::kClear_Mode);
    639 
    640   // TODO (pkotwicz): Remove this once we switch over to Aura desktop.
    641   // This is needed so that we can set the background behind the RWHV when the
    642   // RWHV is not visible. Not needed once there is a view between the RootView
    643   // and RWHV.
    644   View::OnPaint(canvas);
    645 }
    646 
    647 gfx::Vector2d RootView::CalculateOffsetToAncestorWithLayer(
    648     ui::Layer** layer_parent) {
    649   gfx::Vector2d offset(View::CalculateOffsetToAncestorWithLayer(layer_parent));
    650   if (!layer() && layer_parent)
    651     *layer_parent = widget_->GetLayer();
    652   return offset;
    653 }
    654 
    655 View::DragInfo* RootView::GetDragInfo() {
    656   return &drag_info_;
    657 }
    658 
    659 ////////////////////////////////////////////////////////////////////////////////
    660 // RootView, private:
    661 
    662 // Input -----------------------------------------------------------------------
    663 
    664 void RootView::UpdateCursor(const ui::MouseEvent& event) {
    665   if (!(event.flags() & ui::EF_IS_NON_CLIENT)) {
    666     View* v = GetEventHandlerForPoint(event.location());
    667     ui::MouseEvent me(event, static_cast<View*>(this), v);
    668     widget_->SetCursor(v->GetCursor(me));
    669   }
    670 }
    671 
    672 void RootView::SetMouseLocationAndFlags(const ui::MouseEvent& event) {
    673   last_mouse_event_flags_ = event.flags();
    674   last_mouse_event_x_ = event.x();
    675   last_mouse_event_y_ = event.y();
    676 }
    677 
    678 void RootView::DispatchEventToTarget(View* target, ui::Event* event) {
    679   View* old_target = event_dispatch_target_;
    680   event_dispatch_target_ = target;
    681   if (DispatchEvent(target, event))
    682     event_dispatch_target_ = old_target;
    683 }
    684 
    685 void RootView::NotifyEnterExitOfDescendant(const ui::MouseEvent& event,
    686                                            ui::EventType type,
    687                                            View* view,
    688                                            View* sibling) {
    689   for (View* p = view->parent(); p; p = p->parent()) {
    690     if (!p->notify_enter_exit_on_child())
    691       continue;
    692     if (sibling && p->Contains(sibling))
    693       break;
    694     // It is necessary to recreate the notify-event for each dispatch, since one
    695     // of the callbacks can mark the event as handled, and that would cause
    696     // incorrect event dispatch.
    697     MouseEnterExitEvent notify_event(event, type);
    698     DispatchEventToTarget(p, &notify_event);
    699   }
    700 }
    701 
    702 bool RootView::CanDispatchToTarget(ui::EventTarget* target) {
    703   return event_dispatch_target_ == target;
    704 }
    705 
    706 }  // namespace internal
    707 }  // namespace views
    708