Skip to content

Commit

Permalink
Move KeyboardLayoutManager class to keyboard_layout_manager.*
Browse files Browse the repository at this point in the history
BUG=NONE

Review URL: https://codereview.chromium.org/195193003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@258745 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
bshe@chromium.org committed Mar 22, 2014
1 parent 36829c3 commit 3d6b28e
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 80 deletions.
2 changes: 2 additions & 0 deletions ui/keyboard/keyboard.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
'keyboard_controller_observer.h',
'keyboard_controller_proxy.cc',
'keyboard_controller_proxy.h',
'keyboard_layout_manager.h',
'keyboard_layout_manager.cc',
'keyboard_export.h',
'keyboard_switches.cc',
'keyboard_switches.h',
Expand Down
82 changes: 3 additions & 79 deletions ui/keyboard/keyboard_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "base/bind.h"
#include "base/command_line.h"
#include "ui/aura/layout_manager.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/base/cursor/cursor.h"
Expand All @@ -20,6 +19,7 @@
#include "ui/gfx/skia_util.h"
#include "ui/keyboard/keyboard_controller_observer.h"
#include "ui/keyboard/keyboard_controller_proxy.h"
#include "ui/keyboard/keyboard_layout_manager.h"
#include "ui/keyboard/keyboard_switches.h"
#include "ui/keyboard/keyboard_util.h"
#include "ui/wm/core/masked_window_targeter.h"
Expand All @@ -40,34 +40,6 @@ const int kAnimationDurationMs = 200;
// hide animation finishes.
const float kAnimationStartOrAfterHideOpacity = 0.2f;

// The ratio between the height of the keyboard and the screen when using the
// usability keyboard.
const float kUsabilityKeyboardHeightRatio = 1.0f;

// The default ratio between the height of the keyboard and the screen.
const float kDefaultKeyboardHeightRatio = 0.3f;

// The ratio between the height of the keyboard and the screen when using the
// accessibility keyboard.
const float kAccessibilityKeyboardHeightRatio = 0.3f;

float GetKeyboardHeightRatio(){
if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
return kUsabilityKeyboardHeightRatio;
} else if (keyboard::GetAccessibilityKeyboardEnabled()) {
return kAccessibilityKeyboardHeightRatio;
}
return kDefaultKeyboardHeightRatio;
}
gfx::Rect KeyboardBoundsFromWindowBounds(const gfx::Rect& window_bounds) {
const float kKeyboardHeightRatio = GetKeyboardHeightRatio();
return gfx::Rect(
window_bounds.x(),
window_bounds.y() + window_bounds.height() * (1 - kKeyboardHeightRatio),
window_bounds.width(),
window_bounds.height() * kKeyboardHeightRatio);
}

// Event targeter for the keyboard container.
class KeyboardContainerTargeter : public wm::MaskedWindowTargeter {
public:
Expand All @@ -84,7 +56,7 @@ class KeyboardContainerTargeter : public wm::MaskedWindowTargeter {
virtual bool GetHitTestMask(aura::Window* window,
gfx::Path* mask) const OVERRIDE {
gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
KeyboardBoundsFromWindowBounds(window->bounds());
keyboard::DefaultKeyboardBoundsFromWindowBounds(window->bounds());
mask->addRect(RectToSkRect(keyboard_bounds));
return true;
}
Expand Down Expand Up @@ -133,7 +105,7 @@ class KeyboardWindowDelegate : public aura::WindowDelegate {
virtual bool HasHitTestMask() const OVERRIDE { return true; }
virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {
gfx::Rect keyboard_bounds = proxy_ ? proxy_->GetKeyboardWindow()->bounds() :
KeyboardBoundsFromWindowBounds(bounds_);
keyboard::DefaultKeyboardBoundsFromWindowBounds(bounds_);
mask->addRect(RectToSkRect(keyboard_bounds));
}
virtual void DidRecreateLayer(ui::Layer* old_layer,
Expand Down Expand Up @@ -208,54 +180,6 @@ void CallbackAnimationObserver::OnLayerAnimationAborted(
animator_->RemoveObserver(this);
}

// LayoutManager for the virtual keyboard container. Manages a single window
// (the virtual keyboard) and keeps it positioned at the bottom of the
// owner window.
class KeyboardLayoutManager : public aura::LayoutManager {
public:
explicit KeyboardLayoutManager(KeyboardController* controller)
: controller_(controller), keyboard_(NULL) {
}

// Overridden from aura::LayoutManager
virtual void OnWindowResized() OVERRIDE {
if (keyboard_ && !controller_->proxy()->resizing_from_contents())
ResizeKeyboardToDefault(keyboard_);
}
virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
DCHECK(!keyboard_);
keyboard_ = child;
ResizeKeyboardToDefault(keyboard_);
}
virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
virtual void OnChildWindowVisibilityChanged(aura::Window* child,
bool visible) OVERRIDE {}
virtual void SetChildBounds(aura::Window* child,
const gfx::Rect& requested_bounds) OVERRIDE {
// SetChildBounds can be invoked by resizing from the container or by
// resizing from the contents (through window.resizeTo call in JS).
// The flag resizing_from_contents() is used to determine the keyboard is
// resizing from which.
if (controller_->proxy()->resizing_from_contents()) {
controller_->NotifyKeyboardBoundsChanging(requested_bounds);
SetChildBoundsDirect(child, requested_bounds);
}
}

private:
void ResizeKeyboardToDefault(aura::Window* child) {
gfx::Rect keyboard_bounds = KeyboardBoundsFromWindowBounds(
controller_->GetContainerWindow()->bounds());
SetChildBoundsDirect(child, keyboard_bounds);
}

KeyboardController* controller_;
aura::Window* keyboard_;

DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager);
};

KeyboardController::KeyboardController(KeyboardControllerProxy* proxy)
: proxy_(proxy),
input_method_(NULL),
Expand Down
1 change: 0 additions & 1 deletion ui/keyboard/keyboard_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace keyboard {
class CallbackAnimationObserver;
class KeyboardControllerObserver;
class KeyboardControllerProxy;
class KeyboardLayoutManager;

// Provides control of the virtual keyboard, including providing a container
// and controlling visibility.
Expand Down
43 changes: 43 additions & 0 deletions ui/keyboard/keyboard_layout_manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2014 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/keyboard_layout_manager.h"

#include "ui/keyboard/keyboard_controller.h"
#include "ui/keyboard/keyboard_controller_proxy.h"
#include "ui/keyboard/keyboard_util.h"

namespace keyboard {

// Overridden from aura::LayoutManager
void KeyboardLayoutManager::OnWindowResized() {
if (keyboard_ && !controller_->proxy()->resizing_from_contents())
ResizeKeyboardToDefault(keyboard_);
}

void KeyboardLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
DCHECK(!keyboard_);
keyboard_ = child;
ResizeKeyboardToDefault(keyboard_);
}

void KeyboardLayoutManager::SetChildBounds(aura::Window* child,
const gfx::Rect& requested_bounds) {
// SetChildBounds can be invoked by resizing from the container or by
// resizing from the contents (through window.resizeTo call in JS).
// The flag resizing_from_contents() is used to determine the source of the
// resize.
if (controller_->proxy()->resizing_from_contents()) {
controller_->NotifyKeyboardBoundsChanging(requested_bounds);
SetChildBoundsDirect(child, requested_bounds);
}
}

void KeyboardLayoutManager::ResizeKeyboardToDefault(aura::Window* child) {
gfx::Rect keyboard_bounds = DefaultKeyboardBoundsFromWindowBounds(
controller_->GetContainerWindow()->bounds());
SetChildBoundsDirect(child, keyboard_bounds);
}

} // namespace keyboard
45 changes: 45 additions & 0 deletions ui/keyboard/keyboard_layout_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2014 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_KEYBOARD_LAYOUT_MANAGER_H_
#define UI_KEYBOARD_KEYBOARD_LAYOUT_MANAGER_H_

#include "ui/aura/layout_manager.h"
#include "ui/aura/window.h"

namespace keyboard {

class KeyboardController;

// LayoutManager for the virtual keyboard container. Manages a single window
// (the virtual keyboard) and keeps it positioned at the bottom of the
// owner window.
class KeyboardLayoutManager : public aura::LayoutManager {
public:
explicit KeyboardLayoutManager(KeyboardController* controller)
: controller_(controller), keyboard_(NULL) {
}

// Overridden from aura::LayoutManager
virtual void OnWindowResized() OVERRIDE;
virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE;
virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
virtual void OnChildWindowVisibilityChanged(aura::Window* child,
bool visible) OVERRIDE {}
virtual void SetChildBounds(aura::Window* child,
const gfx::Rect& requested_bounds) OVERRIDE;

private:
void ResizeKeyboardToDefault(aura::Window* child);

KeyboardController* controller_;
aura::Window* keyboard_;

DISALLOW_COPY_AND_ASSIGN(KeyboardLayoutManager);
};

} // namespace keyboard

#endif // UI_KEYBOARD_KEYBOARD_LAYOUT_MANAGER_H_
30 changes: 30 additions & 0 deletions ui/keyboard/keyboard_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,40 @@ bool g_accessibility_keyboard_enabled = false;

base::LazyInstance<GURL> g_override_content_url = LAZY_INSTANCE_INITIALIZER;

// The ratio between the height of the keyboard and the screen when using the
// usability keyboard.
const float kUsabilityKeyboardHeightRatio = 1.0f;

// The default ratio between the height of the keyboard and the screen.
const float kDefaultKeyboardHeightRatio = 0.3f;

// The ratio between the height of the keyboard and the screen when using the
// accessibility keyboard.
const float kAccessibilityKeyboardHeightRatio = 0.3f;

float GetKeyboardHeightRatio(){
if (keyboard::IsKeyboardUsabilityExperimentEnabled()) {
return kUsabilityKeyboardHeightRatio;
} else if (keyboard::GetAccessibilityKeyboardEnabled()) {
return kAccessibilityKeyboardHeightRatio;
}
return kDefaultKeyboardHeightRatio;
}

} // namespace

namespace keyboard {

gfx::Rect DefaultKeyboardBoundsFromWindowBounds(
const gfx::Rect& window_bounds) {
const float kKeyboardHeightRatio = GetKeyboardHeightRatio();
return gfx::Rect(
window_bounds.x(),
window_bounds.y() + window_bounds.height() * (1 - kKeyboardHeightRatio),
window_bounds.width(),
window_bounds.height() * kKeyboardHeightRatio);
}

void SetAccessibilityKeyboardEnabled(bool enabled) {
g_accessibility_keyboard_enabled = enabled;
}
Expand Down
4 changes: 4 additions & 0 deletions ui/keyboard/keyboard_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ enum KeyboardControlEvent {
KEYBOARD_CONTROL_MAX,
};

// Gets the default keyboard bounds from |window_bounds|.
KEYBOARD_EXPORT gfx::Rect DefaultKeyboardBoundsFromWindowBounds(
const gfx::Rect& window_bounds);

// Sets the state of the a11y onscreen keyboard.
KEYBOARD_EXPORT void SetAccessibilityKeyboardEnabled(bool enabled);

Expand Down

0 comments on commit 3d6b28e

Please sign in to comment.