gesture_provider_config_helper.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2014 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/events/gesture_detection/gesture_provider_config_helper.h"
  5. #include "ui/display/screen.h"
  6. #include "ui/events/gesture_detection/gesture_configuration.h"
  7. namespace ui {
  8. namespace {
  9. constexpr float kSlopScaleForVr = 3.0f;
  10. class GenericDesktopGestureConfiguration : public GestureConfiguration {
  11. public:
  12. // The default GestureConfiguration parameters are already tailored for a
  13. // desktop environment (Aura).
  14. GenericDesktopGestureConfiguration() {}
  15. ~GenericDesktopGestureConfiguration() override {}
  16. };
  17. GestureDetector::Config BuildGestureDetectorConfig(
  18. const GestureConfiguration& gesture_config,
  19. scoped_refptr<base::SequencedTaskRunner> task_runner) {
  20. GestureDetector::Config config;
  21. config.longpress_timeout =
  22. base::Milliseconds(gesture_config.long_press_time_in_ms());
  23. config.shortpress_timeout = gesture_config.short_press_time();
  24. config.showpress_timeout =
  25. base::Milliseconds(gesture_config.show_press_delay_in_ms());
  26. config.double_tap_timeout =
  27. base::Milliseconds(gesture_config.double_tap_timeout_in_ms());
  28. config.touch_slop = gesture_config.max_touch_move_in_pixels_for_click();
  29. config.double_tap_slop =
  30. gesture_config.max_distance_between_taps_for_double_tap();
  31. config.minimum_fling_velocity = gesture_config.min_fling_velocity();
  32. config.maximum_fling_velocity = gesture_config.max_fling_velocity();
  33. config.swipe_enabled = gesture_config.swipe_enabled();
  34. config.minimum_swipe_velocity = gesture_config.min_swipe_velocity();
  35. config.maximum_swipe_deviation_angle =
  36. gesture_config.max_swipe_deviation_angle();
  37. config.two_finger_tap_enabled = gesture_config.two_finger_tap_enabled();
  38. config.two_finger_tap_max_separation =
  39. gesture_config.max_distance_for_two_finger_tap_in_pixels();
  40. config.two_finger_tap_timeout = base::Milliseconds(
  41. gesture_config.max_touch_down_duration_for_click_in_ms());
  42. config.single_tap_repeat_interval = gesture_config.max_tap_count();
  43. config.velocity_tracker_strategy = gesture_config.velocity_tracker_strategy();
  44. config.task_runner = task_runner;
  45. return config;
  46. }
  47. ScaleGestureDetector::Config BuildScaleGestureDetectorConfig(
  48. const GestureConfiguration& gesture_config) {
  49. ScaleGestureDetector::Config config;
  50. config.span_slop = gesture_config.span_slop();
  51. config.min_scaling_span = gesture_config.min_scaling_span_in_pixels();
  52. config.min_pinch_update_span_delta =
  53. gesture_config.min_pinch_update_span_delta();
  54. config.stylus_scale_enabled = gesture_config.stylus_scale_enabled();
  55. return config;
  56. }
  57. GestureProvider::Config BuildGestureProviderConfig(
  58. const GestureConfiguration& gesture_config,
  59. scoped_refptr<base::SequencedTaskRunner> task_runner) {
  60. GestureProvider::Config config;
  61. config.gesture_detector_config =
  62. BuildGestureDetectorConfig(gesture_config, task_runner);
  63. config.scale_gesture_detector_config =
  64. BuildScaleGestureDetectorConfig(gesture_config);
  65. config.double_tap_support_for_platform_enabled =
  66. gesture_config.double_tap_enabled();
  67. config.gesture_begin_end_types_enabled =
  68. gesture_config.gesture_begin_end_types_enabled();
  69. config.min_gesture_bounds_length = gesture_config.min_gesture_bounds_length();
  70. config.max_gesture_bounds_length = gesture_config.max_gesture_bounds_length();
  71. return config;
  72. }
  73. void TuneGestureProviderConfigForVr(GestureProvider::Config* config) {
  74. config->gesture_detector_config.touch_slop *= kSlopScaleForVr;
  75. }
  76. } // namespace
  77. GestureProvider::Config GetGestureProviderConfig(
  78. GestureProviderConfigType type,
  79. scoped_refptr<base::SequencedTaskRunner> task_runner) {
  80. GestureProvider::Config config;
  81. switch (type) {
  82. case GestureProviderConfigType::CURRENT_PLATFORM:
  83. config = BuildGestureProviderConfig(*GestureConfiguration::GetInstance(),
  84. task_runner);
  85. break;
  86. case GestureProviderConfigType::CURRENT_PLATFORM_VR:
  87. config = BuildGestureProviderConfig(*GestureConfiguration::GetInstance(),
  88. task_runner);
  89. TuneGestureProviderConfigForVr(&config);
  90. break;
  91. case GestureProviderConfigType::GENERIC_DESKTOP:
  92. config = BuildGestureProviderConfig(GenericDesktopGestureConfiguration(),
  93. task_runner);
  94. break;
  95. case GestureProviderConfigType::GENERIC_MOBILE:
  96. // The default GestureProvider::Config embeds a mobile configuration.
  97. break;
  98. }
  99. display::Screen* screen = display::Screen::GetScreen();
  100. // |screen| is sometimes NULL during tests.
  101. if (screen)
  102. config.display = screen->GetPrimaryDisplay();
  103. return config;
  104. }
  105. } // namespace ui