Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: ui/views/widget/root_view.cc

Issue 404213003: [WIP] Allow scroll events to permanently change the default gesture handler in RootView (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: friend test Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/widget/root_view.h" 5 #include "ui/views/widget/root_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 RootView::RootView(Widget* widget) 152 RootView::RootView(Widget* widget)
153 : widget_(widget), 153 : widget_(widget),
154 mouse_pressed_handler_(NULL), 154 mouse_pressed_handler_(NULL),
155 mouse_move_handler_(NULL), 155 mouse_move_handler_(NULL),
156 last_click_handler_(NULL), 156 last_click_handler_(NULL),
157 explicit_mouse_handler_(false), 157 explicit_mouse_handler_(false),
158 last_mouse_event_flags_(0), 158 last_mouse_event_flags_(0),
159 last_mouse_event_x_(-1), 159 last_mouse_event_x_(-1),
160 last_mouse_event_y_(-1), 160 last_mouse_event_y_(-1),
161 gesture_handler_(NULL), 161 gesture_handler_(NULL),
162 scroll_gesture_handler_(NULL),
163 pre_dispatch_handler_(new internal::PreEventDispatchHandler(this)), 162 pre_dispatch_handler_(new internal::PreEventDispatchHandler(this)),
164 post_dispatch_handler_(new internal::PostEventDispatchHandler), 163 post_dispatch_handler_(new internal::PostEventDispatchHandler),
165 focus_search_(this, false, false), 164 focus_search_(this, false, false),
166 focus_traversable_parent_(NULL), 165 focus_traversable_parent_(NULL),
167 focus_traversable_parent_view_(NULL), 166 focus_traversable_parent_view_(NULL),
168 event_dispatch_target_(NULL), 167 event_dispatch_target_(NULL),
169 old_dispatch_target_(NULL) { 168 old_dispatch_target_(NULL) {
170 AddPreTargetHandler(pre_dispatch_handler_.get()); 169 AddPreTargetHandler(pre_dispatch_handler_.get());
171 AddPostTargetHandler(post_dispatch_handler_.get()); 170 AddPostTargetHandler(post_dispatch_handler_.get());
172 SetEventTargeter(scoped_ptr<ViewTargeter>(new ViewTargeter(this))); 171 SetEventTargeter(scoped_ptr<ViewTargeter>(new ViewTargeter(this)));
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 // eventually remove this function altogether. See 255 // eventually remove this function altogether. See
257 // crbug.com/348083. 256 // crbug.com/348083.
258 257
259 if (event->IsKeyEvent()) 258 if (event->IsKeyEvent())
260 return EventProcessor::OnEventFromSource(event); 259 return EventProcessor::OnEventFromSource(event);
261 260
262 if (event->IsScrollEvent()) 261 if (event->IsScrollEvent())
263 return EventProcessor::OnEventFromSource(event); 262 return EventProcessor::OnEventFromSource(event);
264 263
265 if (event->IsGestureEvent()) { 264 if (event->IsGestureEvent()) {
266 // Ignore subsequent gesture scroll events if no handler was set for a 265 ui::GestureEvent* gesture_event = event->AsGestureEvent();
267 // ui::ET_GESTURE_SCROLL_BEGIN event. 266
268 if (!gesture_handler_ && 267 // Do not dispatch ui::ET_GESTURE_BEGIN events.
269 (event->type() == ui::ET_GESTURE_SCROLL_UPDATE || 268 if (gesture_event->type() == ui::ET_GESTURE_BEGIN)
270 event->type() == ui::ET_GESTURE_SCROLL_END || 269 return DispatchDetails();
271 event->type() == ui::ET_SCROLL_FLING_START)) { 270
271 // Do not dispatch ui::ET_GESTURE_END events, but reset the gesture handler
272 // if this ui::ET_GESTURE_END event corresponds to the removal of the final
273 // touch point.
274 if (gesture_event->type() == ui::ET_GESTURE_END) {
275 if (gesture_event->details().touch_points() <= 1) {
276 // In case a drag was in progress, reset all the handlers. Otherwise,
277 // just reset the gesture handler.
278 if (gesture_handler_ && gesture_handler_ == mouse_pressed_handler_)
279 SetMouseHandler(NULL);
280 else
281 gesture_handler_ = NULL;
282 }
283
272 return DispatchDetails(); 284 return DispatchDetails();
273 } 285 }
274 286
275 DispatchGestureEvent(event->AsGestureEvent()); 287 // Do not dispatch subsequent gesture scroll events if no handler was set
288 // for a ui::ET_GESTURE_SCROLL_BEGIN event.
289 if (!gesture_handler_ &&
290 (gesture_event->type() == ui::ET_GESTURE_SCROLL_UPDATE ||
291 gesture_event->type() == ui::ET_GESTURE_SCROLL_END ||
292 gesture_event->type() == ui::ET_SCROLL_FLING_START)) {
293 return DispatchDetails();
294 }
295
296 DispatchGestureEvent(gesture_event);
276 return DispatchDetails(); 297 return DispatchDetails();
277 } 298 }
278 299
279 if (event->IsTouchEvent()) 300 if (event->IsTouchEvent())
280 NOTREACHED() << "Touch events should not be sent to RootView."; 301 NOTREACHED() << "Touch events should not be sent to RootView.";
281 302
282 if (event->IsMouseEvent()) 303 if (event->IsMouseEvent())
283 NOTREACHED() << "Should not be called with a MouseEvent."; 304 NOTREACHED() << "Should not be called with a MouseEvent.";
284 305
285 return DispatchDetails(); 306 return DispatchDetails();
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 } 567 }
547 } 568 }
548 return event.handled(); 569 return event.handled();
549 } 570 }
550 571
551 void RootView::SetMouseHandler(View* new_mh) { 572 void RootView::SetMouseHandler(View* new_mh) {
552 // If we're clearing the mouse handler, clear explicit_mouse_handler_ as well. 573 // If we're clearing the mouse handler, clear explicit_mouse_handler_ as well.
553 explicit_mouse_handler_ = (new_mh != NULL); 574 explicit_mouse_handler_ = (new_mh != NULL);
554 mouse_pressed_handler_ = new_mh; 575 mouse_pressed_handler_ = new_mh;
555 gesture_handler_ = new_mh; 576 gesture_handler_ = new_mh;
556 scroll_gesture_handler_ = new_mh;
557 drag_info_.Reset(); 577 drag_info_.Reset();
558 } 578 }
559 579
560 void RootView::GetAccessibleState(ui::AXViewState* state) { 580 void RootView::GetAccessibleState(ui::AXViewState* state) {
561 state->name = widget_->widget_delegate()->GetAccessibleWindowTitle(); 581 state->name = widget_->widget_delegate()->GetAccessibleWindowTitle();
562 state->role = widget_->widget_delegate()->GetAccessibleWindowRole(); 582 state->role = widget_->widget_delegate()->GetAccessibleWindowRole();
563 } 583 }
564 584
565 void RootView::UpdateParentLayer() { 585 void RootView::UpdateParentLayer() {
566 if (layer()) 586 if (layer())
567 ReparentLayer(gfx::Vector2d(GetMirroredX(), y()), widget_->GetLayer()); 587 ReparentLayer(gfx::Vector2d(GetMirroredX(), y()), widget_->GetLayer());
568 } 588 }
569 589
570 //////////////////////////////////////////////////////////////////////////////// 590 ////////////////////////////////////////////////////////////////////////////////
571 // RootView, protected: 591 // RootView, protected:
572 592
573 void RootView::ViewHierarchyChanged( 593 void RootView::ViewHierarchyChanged(
574 const ViewHierarchyChangedDetails& details) { 594 const ViewHierarchyChangedDetails& details) {
575 widget_->ViewHierarchyChanged(details); 595 widget_->ViewHierarchyChanged(details);
576 596
577 if (!details.is_add) { 597 if (!details.is_add) {
578 if (!explicit_mouse_handler_ && mouse_pressed_handler_ == details.child) 598 if (!explicit_mouse_handler_ && mouse_pressed_handler_ == details.child)
579 mouse_pressed_handler_ = NULL; 599 mouse_pressed_handler_ = NULL;
580 if (mouse_move_handler_ == details.child) 600 if (mouse_move_handler_ == details.child)
581 mouse_move_handler_ = NULL; 601 mouse_move_handler_ = NULL;
582 if (gesture_handler_ == details.child) 602 if (gesture_handler_ == details.child) {
583 gesture_handler_ = NULL; 603 gesture_handler_ = NULL;
584 if (scroll_gesture_handler_ == details.child) 604 }
585 scroll_gesture_handler_ = NULL;
586 if (event_dispatch_target_ == details.child) 605 if (event_dispatch_target_ == details.child)
587 event_dispatch_target_ = NULL; 606 event_dispatch_target_ = NULL;
588 if (old_dispatch_target_ == details.child) 607 if (old_dispatch_target_ == details.child)
589 old_dispatch_target_ = NULL; 608 old_dispatch_target_ = NULL;
590 } 609 }
591 } 610 }
592 611
593 void RootView::VisibilityChanged(View* /*starting_from*/, bool is_visible) { 612 void RootView::VisibilityChanged(View* /*starting_from*/, bool is_visible) {
594 if (!is_visible) { 613 if (!is_visible) {
595 // When the root view is being hidden (e.g. when widget is minimized) 614 // When the root view is being hidden (e.g. when widget is minimized)
596 // handlers are reset, so that after it is reshown, events are not captured 615 // handlers are reset, so that after it is reshown, events are not captured
597 // by old handlers. 616 // by old handlers.
598 explicit_mouse_handler_ = false; 617 explicit_mouse_handler_ = false;
599 mouse_pressed_handler_ = NULL; 618 mouse_pressed_handler_ = NULL;
600 mouse_move_handler_ = NULL; 619 mouse_move_handler_ = NULL;
601 gesture_handler_ = NULL; 620 gesture_handler_ = NULL;
602 scroll_gesture_handler_ = NULL;
603 event_dispatch_target_ = NULL; 621 event_dispatch_target_ = NULL;
604 old_dispatch_target_ = NULL; 622 old_dispatch_target_ = NULL;
605 } 623 }
606 } 624 }
607 625
608 void RootView::OnPaint(gfx::Canvas* canvas) { 626 void RootView::OnPaint(gfx::Canvas* canvas) {
609 if (!layer() || !layer()->fills_bounds_opaquely()) 627 if (!layer() || !layer()->fills_bounds_opaquely())
610 canvas->DrawColor(SK_ColorBLACK, SkXfermode::kClear_Mode); 628 canvas->DrawColor(SK_ColorBLACK, SkXfermode::kClear_Mode);
611 629
612 View::OnPaint(canvas); 630 View::OnPaint(canvas);
(...skipping 11 matching lines...) Expand all
624 return &drag_info_; 642 return &drag_info_;
625 } 643 }
626 644
627 //////////////////////////////////////////////////////////////////////////////// 645 ////////////////////////////////////////////////////////////////////////////////
628 // RootView, private: 646 // RootView, private:
629 647
630 // Input ----------------------------------------------------------------------- 648 // Input -----------------------------------------------------------------------
631 649
632 void RootView::DispatchGestureEvent(ui::GestureEvent* event) { 650 void RootView::DispatchGestureEvent(ui::GestureEvent* event) {
633 if (gesture_handler_) { 651 if (gesture_handler_) {
634 // |gesture_handler_| (or |scroll_gesture_handler_|) can be deleted during 652 // |gesture_handler_| can be deleted during processing.
635 // processing. 653 ui::GestureEvent handler_event(*event,
636 View* handler = scroll_gesture_handler_ && 654 static_cast<View*>(this),
637 (event->IsScrollGestureEvent() || event->IsFlingScrollEvent()) ? 655 gesture_handler_);
638 scroll_gesture_handler_ : gesture_handler_;
639 ui::GestureEvent handler_event(*event, static_cast<View*>(this), handler);
640 ui::EventDispatchDetails dispatch_details = 656 ui::EventDispatchDetails dispatch_details =
641 DispatchEvent(handler, &handler_event); 657 DispatchEvent(gesture_handler_, &handler_event);
642 if (dispatch_details.dispatcher_destroyed) 658 if (dispatch_details.dispatcher_destroyed)
643 return; 659 return;
644 660
645 if (event->type() == ui::ET_GESTURE_END &&
646 event->details().touch_points() <= 1) {
647 // In case a drag was in progress, reset all the handlers. Otherwise, just
648 // reset the gesture handler.
649 if (gesture_handler_ == mouse_pressed_handler_)
650 SetMouseHandler(NULL);
651 else
652 gesture_handler_ = NULL;
653 }
654
655 if (scroll_gesture_handler_ &&
656 (event->type() == ui::ET_GESTURE_SCROLL_END ||
657 event->type() == ui::ET_SCROLL_FLING_START)) {
658 scroll_gesture_handler_ = NULL;
659 }
660
661 if (handler_event.stopped_propagation()) { 661 if (handler_event.stopped_propagation()) {
662 event->StopPropagation(); 662 event->StopPropagation();
663 return; 663 return;
664 } else if (handler_event.handled()) { 664 } else if (handler_event.handled()) {
665 event->SetHandled(); 665 event->SetHandled();
666 return; 666 return;
667 } 667 }
668 668
669 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN && 669 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN) {
670 !scroll_gesture_handler_) {
671 // Some view started processing gesture events, however it does not 670 // Some view started processing gesture events, however it does not
672 // process scroll-gesture events. In such case, we allow the event to 671 // process scroll-gesture events. In such case, we allow the event to
673 // bubble up, and install a different scroll-gesture handler different 672 // bubble up. |gesture_handler_| is changed to its nearest ancestor
674 // from the default gesture handler. 673 // that handles scroll-gesture events.
675 for (scroll_gesture_handler_ = gesture_handler_->parent(); 674 for (gesture_handler_ = gesture_handler_->parent();
676 scroll_gesture_handler_ && scroll_gesture_handler_ != this; 675 gesture_handler_ && gesture_handler_ != this;
677 scroll_gesture_handler_ = scroll_gesture_handler_->parent()) { 676 gesture_handler_ = gesture_handler_->parent()) {
678 ui::GestureEvent gesture_event(*event, static_cast<View*>(this), 677 ui::GestureEvent gesture_event(*event, static_cast<View*>(this),
679 scroll_gesture_handler_); 678 gesture_handler_);
680 ui::EventDispatchDetails dispatch_details = 679 ui::EventDispatchDetails dispatch_details =
681 DispatchEvent(scroll_gesture_handler_, &gesture_event); 680 DispatchEvent(gesture_handler_, &gesture_event);
682 if (gesture_event.stopped_propagation()) { 681 if (gesture_event.stopped_propagation()) {
683 event->StopPropagation(); 682 event->StopPropagation();
684 return; 683 return;
685 } else if (gesture_event.handled()) { 684 } else if (gesture_event.handled()) {
686 event->SetHandled(); 685 event->SetHandled();
687 return; 686 return;
688 } else if (dispatch_details.dispatcher_destroyed || 687 } else if (dispatch_details.dispatcher_destroyed ||
689 dispatch_details.target_destroyed) { 688 dispatch_details.target_destroyed) {
690 return; 689 return;
691 } 690 }
692 } 691 }
693 scroll_gesture_handler_ = NULL; 692 gesture_handler_ = NULL;
694 } 693 }
695 694
696 return; 695 return;
697 } 696 }
698 697
699 View* gesture_handler = NULL; 698 View* gesture_handler = NULL;
700 if (views::switches::IsRectBasedTargetingEnabled() && 699 if (views::switches::IsRectBasedTargetingEnabled() &&
701 !event->details().bounding_box().IsEmpty()) { 700 !event->details().bounding_box().IsEmpty()) {
702 // TODO(tdanderson): Pass in the bounding box to GetEventHandlerForRect() 701 // TODO(tdanderson): Pass in the bounding box to GetEventHandlerForRect()
703 // once crbug.com/313392 is resolved. 702 // once crbug.com/313392 is resolved.
704 gfx::Rect touch_rect(event->details().bounding_box()); 703 gfx::Rect touch_rect(event->details().bounding_box());
705 touch_rect.set_origin(event->location()); 704 touch_rect.set_origin(event->location());
706 touch_rect.Offset(-touch_rect.width() / 2, -touch_rect.height() / 2); 705 touch_rect.Offset(-touch_rect.width() / 2, -touch_rect.height() / 2);
707 gesture_handler = GetEventHandlerForRect(touch_rect); 706 gesture_handler = GetEventHandlerForRect(touch_rect);
708 } else { 707 } else {
709 gesture_handler = GetEventHandlerForPoint(event->location()); 708 gesture_handler = GetEventHandlerForPoint(event->location());
710 } 709 }
711 710
712 // Walk up the tree until we find a view that wants the gesture event. 711 // Walk up the tree until we find a view that wants the gesture event.
713 for (gesture_handler_ = gesture_handler; 712 for (gesture_handler_ = gesture_handler;
714 gesture_handler_ && (gesture_handler_ != this); 713 gesture_handler_ && (gesture_handler_ != this);
715 gesture_handler_ = gesture_handler_->parent()) { 714 gesture_handler_ = gesture_handler_->parent()) {
716 if (!gesture_handler_->enabled()) { 715 // Disabled views eat events but are treated as not handled.
717 // Disabled views eat events but are treated as not handled. 716 if (!gesture_handler_->enabled())
718 return; 717 return;
719 }
720 718
721 // See if this view wants to handle the Gesture. 719 // See if this view wants to handle the Gesture.
722 ui::GestureEvent gesture_event(*event, static_cast<View*>(this), 720 ui::GestureEvent gesture_event(*event, static_cast<View*>(this),
723 gesture_handler_); 721 gesture_handler_);
724 ui::EventDispatchDetails dispatch_details = 722 ui::EventDispatchDetails dispatch_details =
725 DispatchEvent(gesture_handler_, &gesture_event); 723 DispatchEvent(gesture_handler_, &gesture_event);
726 if (dispatch_details.dispatcher_destroyed) 724 if (dispatch_details.dispatcher_destroyed)
727 return; 725 return;
728 726
729 // The view could have removed itself from the tree when handling 727 // The view could have removed itself from the tree when handling
730 // OnGestureEvent(). So handle as per OnMousePressed. NB: we 728 // OnGestureEvent(). So handle as per OnMousePressed. NB: we
731 // assume that the RootView itself cannot be so removed. 729 // assume that the RootView itself cannot be so removed.
732 if (!gesture_handler_) 730 if (!gesture_handler_)
733 return; 731 return;
734 732
735 if (gesture_event.handled()) { 733 if (gesture_event.handled()) {
736 if (gesture_event.type() == ui::ET_GESTURE_SCROLL_BEGIN)
737 scroll_gesture_handler_ = gesture_handler_;
738 if (gesture_event.stopped_propagation()) 734 if (gesture_event.stopped_propagation())
739 event->StopPropagation(); 735 event->StopPropagation();
740 else 736 else
741 event->SetHandled(); 737 event->SetHandled();
742 // Last ui::ET_GESTURE_END should not set the gesture_handler_.
743 if (gesture_event.type() == ui::ET_GESTURE_END &&
744 event->details().touch_points() <= 1) {
745 gesture_handler_ = NULL;
746 }
747 return; 738 return;
748 } 739 }
749 740
750 // The gesture event wasn't processed. Go up the view hierarchy and 741 // The gesture event wasn't processed. Go up the view hierarchy and
751 // dispatch the gesture event. 742 // dispatch the gesture event.
752 } 743 }
753 744
754 gesture_handler_ = NULL; 745 gesture_handler_ = NULL;
755 } 746 }
756 747
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
811 802
812 #ifndef NDEBUG 803 #ifndef NDEBUG
813 DCHECK(!event_dispatch_target_ || Contains(event_dispatch_target_)); 804 DCHECK(!event_dispatch_target_ || Contains(event_dispatch_target_));
814 #endif 805 #endif
815 806
816 return details; 807 return details;
817 } 808 }
818 809
819 } // namespace internal 810 } // namespace internal
820 } // namespace views 811 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698