Commit e8202284 authored by Blake O'Hare's avatar Blake O'Hare Committed by Commit Bot

Refactor keyboard animations into ContainerBehavior

This is a small first step towards the floating keyboard, although the
changes here are pre-emptive refactorings independent of any floating
keyboard functionality or UX (and are probably reasonable de-couplings
to do anyway).

Unit tests that are coupled to the current animation behavior are still
valid and pass, although those should be decoupled when more container
behaviors are added.

Bug: 
Change-Id: Ifbd8e4125fe4be89e03a2fec813714edc0036996
Reviewed-on: https://chromium-review.googlesource.com/720541
Commit-Queue: Blake O'Hare <blakeo@chromium.org>
Reviewed-by: default avatarYuichiro Hanada <yhanada@chromium.org>
Reviewed-by: default avatarKeigo Oka <oka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#509311}
parent f0b3b477
...@@ -10,6 +10,9 @@ import("//tools/grit/grit_rule.gni") ...@@ -10,6 +10,9 @@ import("//tools/grit/grit_rule.gni")
component("keyboard") { component("keyboard") {
sources = [ sources = [
"container_behavior.h",
"container_full_width_behavior.cc",
"container_full_width_behavior.h",
"keyboard_controller.cc", "keyboard_controller.cc",
"keyboard_controller.h", "keyboard_controller.h",
"keyboard_controller_observer.h", "keyboard_controller_observer.h",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_KEYBOARD_CONTAINER_BEHAVIOR_H_
#define UI_KEYBOARD_CONTAINER_BEHAVIOR_H_
#include "ui/aura/window.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/wm/core/window_animations.h"
namespace keyboard {
// Represents and encapsulates how the keyboard container should visually behave
// within the workspace window.
class ContainerBehavior {
public:
virtual ~ContainerBehavior(){};
// Apply changes to the animation settings to animate the keyboard container
// showing.
virtual void DoShowingAnimation(
aura::Window* window,
ui::ScopedLayerAnimationSettings* animation_settings) = 0;
// Apply changes to the animation settings to animate the keyboard container
// hiding.
virtual void DoHidingAnimation(
aura::Window* window,
::wm::ScopedHidingAnimationSettings* animation_settings) = 0;
// Initialize the starting state of the keyboard container for the showing
// animation.
virtual void InitializeShowAnimationStartingState(
aura::Window* container) = 0;
};
} // namespace keyboard
#endif // UI_KEYBOARD_CONTAINER_BEHAVIOR_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/keyboard/container_full_width_behavior.h"
#include "ui/aura/window.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/wm/core/window_animations.h"
namespace keyboard {
// The virtual keyboard show/hide animation duration.
constexpr int kFullWidthKeyboardAnimationDurationMs = 100;
// The opacity of virtual keyboard container when show animation starts or
// hide animation finishes. This cannot be zero because we call Show() on the
// keyboard window before setting the opacity back to 1.0. Since windows are not
// allowed to be shown with zero opacity, we always animate to 0.01 instead.
constexpr float kAnimationStartOrAfterHideOpacity = 0.01f;
ContainerFullWidthBehavior::~ContainerFullWidthBehavior() {}
void ContainerFullWidthBehavior::DoHidingAnimation(
aura::Window* container,
::wm::ScopedHidingAnimationSettings* animation_settings) {
animation_settings->layer_animation_settings()->SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kFullWidthKeyboardAnimationDurationMs));
gfx::Transform transform;
transform.Translate(0, kFullWidthKeyboardAnimationDistance);
container->SetTransform(transform);
container->layer()->SetOpacity(0.f);
}
void ContainerFullWidthBehavior::DoShowingAnimation(
aura::Window* container,
ui::ScopedLayerAnimationSettings* animation_settings) {
animation_settings->SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN);
animation_settings->SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kFullWidthKeyboardAnimationDurationMs));
container->SetTransform(gfx::Transform());
container->layer()->SetOpacity(1.0);
}
void ContainerFullWidthBehavior::InitializeShowAnimationStartingState(
aura::Window* container) {
gfx::Transform transform;
transform.Translate(0, kFullWidthKeyboardAnimationDistance);
container->SetTransform(transform);
container->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
}
} // namespace keyboard
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_KEYBOARD_CONTAINER_FULL_WIDTH_BEHAVIOR_H_
#define UI_KEYBOARD_CONTAINER_FULL_WIDTH_BEHAVIOR_H_
#include "ui/aura/window.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/keyboard/container_behavior.h"
#include "ui/wm/core/window_animations.h"
namespace keyboard {
// Relative distance from the parent window, from which show animation starts
// or hide animation finishes.
constexpr int kFullWidthKeyboardAnimationDistance = 30;
class ContainerFullWidthBehavior : public ContainerBehavior {
public:
~ContainerFullWidthBehavior() override;
// ContainerBehavior overrides
void DoHidingAnimation(
aura::Window* window,
::wm::ScopedHidingAnimationSettings* animation_settings) override;
void DoShowingAnimation(
aura::Window* window,
ui::ScopedLayerAnimationSettings* animation_settings) override;
void InitializeShowAnimationStartingState(aura::Window* container) override;
};
} // namespace keyboard
#endif // UI_KEYBOARD_CONTAINER_FULL_WIDTH_BEHAVIOR_H_
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "ui/display/types/display_constants.h" #include "ui/display/types/display_constants.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#include "ui/gfx/path.h" #include "ui/gfx/path.h"
#include "ui/keyboard/container_full_width_behavior.h"
#include "ui/keyboard/keyboard_controller_observer.h" #include "ui/keyboard/keyboard_controller_observer.h"
#include "ui/keyboard/keyboard_layout_manager.h" #include "ui/keyboard/keyboard_layout_manager.h"
#include "ui/keyboard/keyboard_ui.h" #include "ui/keyboard/keyboard_ui.h"
...@@ -41,19 +42,10 @@ namespace { ...@@ -41,19 +42,10 @@ namespace {
constexpr int kHideKeyboardDelayMs = 100; constexpr int kHideKeyboardDelayMs = 100;
// The virtual keyboard show/hide animation duration.
constexpr int kAnimationDurationMs = 100;
// Reports an error histogram if the keyboard state is lingering in an // Reports an error histogram if the keyboard state is lingering in an
// intermediate state for more than 5 seconds. // intermediate state for more than 5 seconds.
constexpr int kReportLingeringStateDelayMs = 5000; constexpr int kReportLingeringStateDelayMs = 5000;
// The opacity of virtual keyboard container when show animation starts or
// hide animation finishes. This cannot be zero because we call Show() on the
// keyboard window before setting the opacity back to 1.0. Since windows are not
// allowed to be shown with zero opacity, we always animate to 0.01 instead.
constexpr float kAnimationStartOrAfterHideOpacity = 0.01f;
// State transition diagram (document linked from crbug.com/719905) // State transition diagram (document linked from crbug.com/719905)
bool isAllowedStateTransition(keyboard::KeyboardControllerState from, bool isAllowedStateTransition(keyboard::KeyboardControllerState from,
keyboard::KeyboardControllerState to) { keyboard::KeyboardControllerState to) {
...@@ -226,6 +218,7 @@ KeyboardController::KeyboardController(std::unique_ptr<KeyboardUI> ui, ...@@ -226,6 +218,7 @@ KeyboardController::KeyboardController(std::unique_ptr<KeyboardUI> ui,
weak_factory_will_hide_(this) { weak_factory_will_hide_(this) {
ui_->GetInputMethod()->AddObserver(this); ui_->GetInputMethod()->AddObserver(this);
ui_->SetController(this); ui_->SetController(this);
container_behavior_ = std::make_unique<ContainerFullWidthBehavior>();
ChangeState(KeyboardControllerState::INITIAL); ChangeState(KeyboardControllerState::INITIAL);
} }
...@@ -367,19 +360,13 @@ void KeyboardController::HideKeyboard(HideReason reason) { ...@@ -367,19 +360,13 @@ void KeyboardController::HideKeyboard(HideReason reason) {
aura::Window* window = container_.get(); aura::Window* window = container_.get();
// Scoped settings go into effect when scope ends.
{ {
// Scoped settings go into effect when scope ends.
::wm::ScopedHidingAnimationSettings hiding_settings(window); ::wm::ScopedHidingAnimationSettings hiding_settings(window);
container_behavior_->DoHidingAnimation(window, &hiding_settings);
hiding_settings.layer_animation_settings()->SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kAnimationDurationMs));
gfx::Transform transform;
transform.Translate(0, kAnimationDistance);
window->SetTransform(transform);
window->layer()->SetOpacity(0.f);
} }
ui_->HideKeyboardContainer(container_.get()); ui_->HideKeyboardContainer(window);
ChangeState(KeyboardControllerState::HIDDEN); ChangeState(KeyboardControllerState::HIDDEN);
for (KeyboardControllerObserver& observer : observer_list_) for (KeyboardControllerObserver& observer : observer_list_)
...@@ -586,10 +573,8 @@ void KeyboardController::PopulateKeyboardContent(int64_t display_id, ...@@ -586,10 +573,8 @@ void KeyboardController::PopulateKeyboardContent(int64_t display_id,
case KeyboardControllerState::HIDDEN: { case KeyboardControllerState::HIDDEN: {
// If the container is not animating, makes sure the position and opacity // If the container is not animating, makes sure the position and opacity
// are at begin states for animation. // are at begin states for animation.
gfx::Transform transform; container_behavior_->InitializeShowAnimationStartingState(
transform.Translate(0, kAnimationDistance); container_.get());
container_->SetTransform(transform);
container_->layer()->SetOpacity(kAnimationStartOrAfterHideOpacity);
break; break;
} }
default: default:
...@@ -612,12 +597,8 @@ void KeyboardController::PopulateKeyboardContent(int64_t display_id, ...@@ -612,12 +597,8 @@ void KeyboardController::PopulateKeyboardContent(int64_t display_id,
ui_->ShowKeyboardContainer(container_.get()); ui_->ShowKeyboardContainer(container_.get());
ui::ScopedLayerAnimationSettings settings(container_animator); ui::ScopedLayerAnimationSettings settings(container_animator);
settings.SetTweenType(gfx::Tween::LINEAR_OUT_SLOW_IN);
settings.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kAnimationDurationMs));
container_->SetTransform(gfx::Transform()); container_behavior_->DoShowingAnimation(container_.get(), &settings);
container_->layer()->SetOpacity(1.0);
ChangeState(KeyboardControllerState::SHOWN); ChangeState(KeyboardControllerState::SHOWN);
NotifyKeyboardBoundsChangingAndEnsureCaretInWorkArea(); NotifyKeyboardBoundsChangingAndEnsureCaretInWorkArea();
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "ui/base/ime/input_method_observer.h" #include "ui/base/ime/input_method_observer.h"
#include "ui/base/ime/text_input_type.h" #include "ui/base/ime/text_input_type.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#include "ui/keyboard/container_behavior.h"
#include "ui/keyboard/keyboard_event_filter.h" #include "ui/keyboard/keyboard_event_filter.h"
#include "ui/keyboard/keyboard_export.h" #include "ui/keyboard/keyboard_export.h"
#include "ui/keyboard/keyboard_layout_delegate.h" #include "ui/keyboard/keyboard_layout_delegate.h"
...@@ -32,10 +33,6 @@ class CallbackAnimationObserver; ...@@ -32,10 +33,6 @@ class CallbackAnimationObserver;
class KeyboardControllerObserver; class KeyboardControllerObserver;
class KeyboardUI; class KeyboardUI;
// Relative distance from the parent window, from which show animation starts
// or hide animation finishes.
constexpr int kAnimationDistance = 30;
// Represents the current state of the keyboard managed by the controller. // Represents the current state of the keyboard managed by the controller.
// Don't change the numeric value of the members because they are used in UMA // Don't change the numeric value of the members because they are used in UMA
// - VirtualKeyboard.ControllerStateTransition. // - VirtualKeyboard.ControllerStateTransition.
...@@ -214,6 +211,9 @@ class KEYBOARD_EXPORT KeyboardController : public ui::InputMethodObserver, ...@@ -214,6 +211,9 @@ class KEYBOARD_EXPORT KeyboardController : public ui::InputMethodObserver,
// uses container_'s animator. // uses container_'s animator.
std::unique_ptr<CallbackAnimationObserver> animation_observer_; std::unique_ptr<CallbackAnimationObserver> animation_observer_;
// Current active visual behavior for the keyboard container.
std::unique_ptr<ContainerBehavior> container_behavior_;
// If true, show the keyboard window when keyboard UI content updates. // If true, show the keyboard window when keyboard UI content updates.
bool show_on_content_update_; bool show_on_content_update_;
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "ui/compositor/test/layer_animator_test_controller.h" #include "ui/compositor/test/layer_animator_test_controller.h"
#include "ui/events/test/event_generator.h" #include "ui/events/test/event_generator.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#include "ui/keyboard/container_full_width_behavior.h"
#include "ui/keyboard/keyboard_controller_observer.h" #include "ui/keyboard/keyboard_controller_observer.h"
#include "ui/keyboard/keyboard_test_util.h" #include "ui/keyboard/keyboard_test_util.h"
#include "ui/keyboard/keyboard_ui.h" #include "ui/keyboard/keyboard_ui.h"
...@@ -606,7 +607,7 @@ TEST_F(KeyboardControllerAnimationTest, ContainerAnimation) { ...@@ -606,7 +607,7 @@ TEST_F(KeyboardControllerAnimationTest, ContainerAnimation) {
EXPECT_TRUE(contents_window()->IsVisible()); EXPECT_TRUE(contents_window()->IsVisible());
float show_start_opacity = layer->opacity(); float show_start_opacity = layer->opacity();
gfx::Transform transform; gfx::Transform transform;
transform.Translate(0, kAnimationDistance); transform.Translate(0, keyboard::kFullWidthKeyboardAnimationDistance);
EXPECT_EQ(transform, layer->transform()); EXPECT_EQ(transform, layer->transform());
// animation occurs in a cloned layer, so the actual final bounds should // animation occurs in a cloned layer, so the actual final bounds should
// already be applied to the container. // already be applied to the container.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment