container_full_width_behavior.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2017 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 "ash/keyboard/ui/container_full_width_behavior.h"
  5. #include "ash/public/cpp/tablet_mode.h"
  6. #include "ui/aura/window.h"
  7. #include "ui/compositor/layer.h"
  8. #include "ui/compositor/scoped_layer_animation_settings.h"
  9. #include "ui/gfx/geometry/transform.h"
  10. #include "ui/wm/core/window_animations.h"
  11. namespace keyboard {
  12. // The virtual keyboard show/hide animation durations.
  13. constexpr auto kShowAnimationDuration = base::Milliseconds(200);
  14. constexpr auto kHideAnimationDuration = base::Milliseconds(100);
  15. // The height of the area from the bottom of the keyboard where the user can
  16. // swipe up to access the shelf. Manually calculated to be slightly below
  17. // the virtual keyboard's space bar to avoid accidental trigger.
  18. constexpr int kSwipeUpGestureAreaHeight = 8;
  19. ContainerFullWidthBehavior::ContainerFullWidthBehavior(Delegate* delegate)
  20. : ContainerBehavior(delegate) {}
  21. ContainerFullWidthBehavior::~ContainerFullWidthBehavior() = default;
  22. ContainerType ContainerFullWidthBehavior::GetType() const {
  23. return ContainerType::kFullWidth;
  24. }
  25. void ContainerFullWidthBehavior::DoHidingAnimation(
  26. aura::Window* container,
  27. ::wm::ScopedHidingAnimationSettings* animation_settings) {
  28. animation_settings->layer_animation_settings()->SetTransitionDuration(
  29. kHideAnimationDuration);
  30. gfx::Transform transform;
  31. transform.Translate(0, kFullWidthKeyboardAnimationDistance);
  32. container->SetTransform(transform);
  33. container->layer()->SetOpacity(0.f);
  34. }
  35. void ContainerFullWidthBehavior::DoShowingAnimation(
  36. aura::Window* container,
  37. ui::ScopedLayerAnimationSettings* animation_settings) {
  38. animation_settings->SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN);
  39. animation_settings->SetTransitionDuration(kShowAnimationDuration);
  40. container->SetTransform(gfx::Transform());
  41. container->layer()->SetOpacity(1.0);
  42. }
  43. void ContainerFullWidthBehavior::InitializeShowAnimationStartingState(
  44. aura::Window* container) {
  45. SetCanonicalBounds(container, container->GetRootWindow()->bounds());
  46. gfx::Transform transform;
  47. transform.Translate(0, kFullWidthKeyboardAnimationDistance);
  48. container->SetTransform(transform);
  49. container->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
  50. }
  51. gfx::Rect ContainerFullWidthBehavior::AdjustSetBoundsRequest(
  52. const gfx::Rect& display_bounds,
  53. const gfx::Rect& requested_bounds_in_screen_coords) {
  54. gfx::Rect new_bounds;
  55. // Honors only the height of the request bounds
  56. const int keyboard_height = requested_bounds_in_screen_coords.height();
  57. new_bounds.set_y(display_bounds.bottom() - keyboard_height);
  58. new_bounds.set_height(keyboard_height);
  59. // If shelf is positioned on the left side of screen, x is not 0. In
  60. // FULL_WIDTH mode, the virtual keyboard should always align with the left
  61. // edge of the screen. So manually set x to the left side of the screen.
  62. new_bounds.set_x(display_bounds.x());
  63. new_bounds.set_width(display_bounds.width());
  64. return new_bounds;
  65. }
  66. bool ContainerFullWidthBehavior::IsOverscrollAllowed() const {
  67. // TODO(blakeo): The locked keyboard is essentially its own behavior type and
  68. // should be refactored as such. Then this will simply return 'true'.
  69. return delegate_ && !delegate_->IsKeyboardLocked();
  70. }
  71. void ContainerFullWidthBehavior::SavePosition(const gfx::Rect& keyboard_bounds,
  72. const gfx::Size& screen_size) {
  73. // No-op. Nothing to save.
  74. }
  75. bool ContainerFullWidthBehavior::HandlePointerEvent(
  76. const ui::LocatedEvent& event,
  77. const display::Display& current_display) {
  78. // No-op. Nothing special to do for pointer events.
  79. return false;
  80. }
  81. bool ContainerFullWidthBehavior::HandleGestureEvent(
  82. const ui::GestureEvent& event,
  83. const gfx::Rect& bounds_in_screen) {
  84. if (!ash::TabletMode::Get()->InTabletMode())
  85. return false;
  86. if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN) {
  87. // Check that the user is swiping upwards near the bottom of the keyboard.
  88. // The coordinates of the |event| is relative to the window.
  89. const auto details = event.details();
  90. if (std::abs(details.scroll_y_hint()) > std::abs(details.scroll_x_hint()) &&
  91. details.scroll_y_hint() < 0 &&
  92. event.y() > bounds_in_screen.height() - kSwipeUpGestureAreaHeight) {
  93. delegate_->TransferGestureEventToShelf(event);
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. void ContainerFullWidthBehavior::SetCanonicalBounds(
  100. aura::Window* container,
  101. const gfx::Rect& display_bounds) {
  102. const gfx::Rect new_keyboard_bounds =
  103. AdjustSetBoundsRequest(display_bounds, container->bounds());
  104. container->SetBounds(new_keyboard_bounds);
  105. }
  106. bool ContainerFullWidthBehavior::TextBlurHidesKeyboard() const {
  107. return !delegate_->IsKeyboardLocked();
  108. }
  109. void ContainerFullWidthBehavior::SetOccludedBounds(
  110. const gfx::Rect& occluded_bounds_in_window) {
  111. occluded_bounds_in_window_ = occluded_bounds_in_window;
  112. }
  113. gfx::Rect ContainerFullWidthBehavior::GetOccludedBounds(
  114. const gfx::Rect& visual_bounds_in_window) const {
  115. DCHECK(visual_bounds_in_window.Contains(occluded_bounds_in_window_));
  116. return occluded_bounds_in_window_.IsEmpty() ? visual_bounds_in_window
  117. : occluded_bounds_in_window_;
  118. }
  119. bool ContainerFullWidthBehavior::OccludedBoundsAffectWorkspaceLayout() const {
  120. return delegate_->IsKeyboardLocked();
  121. }
  122. void ContainerFullWidthBehavior::SetDraggableArea(const gfx::Rect& rect) {
  123. // Allow extension to call this function but does nothing here.
  124. }
  125. void ContainerFullWidthBehavior::SetAreaToRemainOnScreen(
  126. const gfx::Rect& bounds) {
  127. // Allow extension to call this function but does nothing here.
  128. }
  129. } // namespace keyboard