scroll_input_handler.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2018 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. #include "ui/compositor/overscroll/scroll_input_handler.h"
  5. #include "ui/compositor/layer.h"
  6. #include "ui/events/event.h"
  7. #include "ui/events/types/scroll_input_type.h"
  8. namespace ui {
  9. namespace {
  10. // Creates a cc::ScrollState from a ui::ScrollEvent, populating fields general
  11. // to all event phases. Take care not to put deltas on beginning/end updates,
  12. // since InputHandler will DCHECK if they're present.
  13. cc::ScrollState CreateScrollState(const ScrollEvent& scroll, bool is_begin) {
  14. cc::ScrollStateData scroll_state_data;
  15. scroll_state_data.position_x = scroll.x();
  16. scroll_state_data.position_y = scroll.y();
  17. if (is_begin) {
  18. scroll_state_data.delta_x_hint = -scroll.x_offset_ordinal();
  19. scroll_state_data.delta_y_hint = -scroll.y_offset_ordinal();
  20. } else {
  21. scroll_state_data.delta_x = -scroll.x_offset_ordinal();
  22. scroll_state_data.delta_y = -scroll.y_offset_ordinal();
  23. }
  24. scroll_state_data.is_in_inertial_phase =
  25. scroll.momentum_phase() == EventMomentumPhase::INERTIAL_UPDATE;
  26. scroll_state_data.is_ending = false;
  27. scroll_state_data.is_beginning = is_begin;
  28. return cc::ScrollState(scroll_state_data);
  29. }
  30. } // namespace
  31. ScrollInputHandler::ScrollInputHandler(
  32. const base::WeakPtr<cc::InputHandler>& input_handler)
  33. : input_handler_weak_ptr_(input_handler) {
  34. DCHECK(input_handler_weak_ptr_);
  35. input_handler_weak_ptr_->BindToClient(this);
  36. }
  37. ScrollInputHandler::~ScrollInputHandler() {
  38. DCHECK(!input_handler_weak_ptr_)
  39. << "Pointer invalidated before WillShutdown() is called.";
  40. }
  41. bool ScrollInputHandler::OnScrollEvent(const ScrollEvent& event,
  42. Layer* layer_to_scroll) {
  43. if (!input_handler_weak_ptr_)
  44. return false;
  45. // TODO(bokan): This code would ideally call Begin once at the beginning of a
  46. // gesture and End once at the end of it. Unfortunately, the phase data is
  47. // incomplete (only momentum phase is set and in a reduced way, e.g. we get
  48. // an end when we transition to a fling). See the TODOs in events_mac.mm and
  49. // ui/events/event.cc. Until those are fixed, there's no harm in simply
  50. // treating each event as a gesture unto itself.
  51. cc::ScrollState scroll_state_begin = CreateScrollState(event, true);
  52. scroll_state_begin.data()->set_current_native_scrolling_element(
  53. layer_to_scroll->element_id());
  54. // Note: the WHEEL type covers both actual wheels as well as trackpad
  55. // scrolling.
  56. cc::InputHandler::ScrollStatus result = input_handler_weak_ptr_->ScrollBegin(
  57. &scroll_state_begin, ui::ScrollInputType::kWheel);
  58. // Falling back to the main thread should never be required when an explicit
  59. // ElementId is provided.
  60. DCHECK(!result.needs_main_thread_hit_test);
  61. cc::ScrollState scroll_state = CreateScrollState(event, false);
  62. input_handler_weak_ptr_->ScrollUpdate(&scroll_state, base::TimeDelta());
  63. input_handler_weak_ptr_->ScrollEnd(/*should_snap=*/false);
  64. return true;
  65. }
  66. void ScrollInputHandler::WillShutdown() {
  67. DCHECK(input_handler_weak_ptr_);
  68. input_handler_weak_ptr_.reset();
  69. }
  70. void ScrollInputHandler::Animate(base::TimeTicks time) {}
  71. void ScrollInputHandler::ReconcileElasticOverscrollAndRootScroll() {}
  72. void ScrollInputHandler::SetPrefersReducedMotion(bool prefers_reduced_motion) {}
  73. void ScrollInputHandler::UpdateRootLayerStateForSynchronousInputHandler(
  74. const gfx::PointF& total_scroll_offset,
  75. const gfx::PointF& max_scroll_offset,
  76. const gfx::SizeF& scrollable_size,
  77. float page_scale_factor,
  78. float min_page_scale_factor,
  79. float max_page_scale_factor) {}
  80. void ScrollInputHandler::DeliverInputForBeginFrame(
  81. const viz::BeginFrameArgs& args) {}
  82. void ScrollInputHandler::DeliverInputForHighLatencyMode() {}
  83. } // namespace ui