Commit 1a18c3f0 authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

[VK] Use semantically-correct bounds when querying keyboard bounds.

There are several places where we query the keyboard bounds by
grabbing the keyboard container window and calling |bounds|. However,
it's unclear whether they mean the bounds relative to parent or bounds
relative to the screen, since both are currently the same for the
keyboard container.

This patch changes several callers to use |GetBoundsInScreen| since
they actually want the screen bounds. Note we don't change any behaviour
since currently keyboard container |bounds| == |GetBoundsInScreen|, but
this will change in future when we get rid of keyboard container.

Bug: 849980
Change-Id: I7211073a7697f168824c127319dfc40e6888195a
Reviewed-on: https://chromium-review.googlesource.com/1098417
Commit-Queue: James Cook <jamescook@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Reviewed-by: default avatarYuichiro Hanada <yhanada@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567253}
parent 54b4f8dc
...@@ -927,8 +927,9 @@ void MagnificationController::MoveMagnifierWindowCenterPoint( ...@@ -927,8 +927,9 @@ void MagnificationController::MoveMagnifierWindowCenterPoint(
// Reduce the viewport bounds if the keyboard is up. // Reduce the viewport bounds if the keyboard is up.
if (keyboard::KeyboardController::Get()->enabled()) { if (keyboard::KeyboardController::Get()->enabled()) {
gfx::Rect keyboard_rect = gfx::Rect keyboard_rect = keyboard::KeyboardController::Get()
keyboard::KeyboardController::Get()->GetContainerWindow()->bounds(); ->GetContainerWindow()
->GetBoundsInScreen();
window_rect.set_height(window_rect.height() - window_rect.set_height(window_rect.height() -
keyboard_rect.height() / scale_); keyboard_rect.height() / scale_);
} }
......
...@@ -294,8 +294,8 @@ void ChromeKeyboardUI::UpdateInsetsForWindow(aura::Window* window) { ...@@ -294,8 +294,8 @@ void ChromeKeyboardUI::UpdateInsetsForWindow(aura::Window* window) {
content::RenderWidgetHostView* view = widget->GetView(); content::RenderWidgetHostView* view = widget->GetView();
if (view && window->Contains(view->GetNativeView())) { if (view && window->Contains(view->GetNativeView())) {
gfx::Rect window_bounds = view->GetNativeView()->GetBoundsInScreen(); gfx::Rect window_bounds = view->GetNativeView()->GetBoundsInScreen();
gfx::Rect intersect = gfx::Rect intersect = gfx::IntersectRects(
gfx::IntersectRects(window_bounds, keyboard_container->bounds()); window_bounds, keyboard_container->GetBoundsInScreen());
int overlap = ShouldEnableInsets(window) ? intersect.height() : 0; int overlap = ShouldEnableInsets(window) ? intersect.height() : 0;
if (overlap > 0 && overlap < window_bounds.height()) if (overlap > 0 && overlap < window_bounds.height())
view->SetInsets(gfx::Insets(0, 0, overlap, 0)); view->SetInsets(gfx::Insets(0, 0, overlap, 0));
......
...@@ -61,7 +61,7 @@ class KEYBOARD_EXPORT ContainerBehavior { ...@@ -61,7 +61,7 @@ class KEYBOARD_EXPORT ContainerBehavior {
virtual bool IsDragHandle(const gfx::Vector2d& offset, virtual bool IsDragHandle(const gfx::Vector2d& offset,
const gfx::Size& keyboard_size) const = 0; const gfx::Size& keyboard_size) const = 0;
virtual void SavePosition(const gfx::Rect& keyboard_bounds, virtual void SavePosition(const gfx::Rect& keyboard_bounds_in_screen,
const gfx::Size& screen_size) = 0; const gfx::Size& screen_size) = 0;
virtual bool HandlePointerEvent(const ui::LocatedEvent& event, virtual bool HandlePointerEvent(const ui::LocatedEvent& event,
......
...@@ -67,70 +67,73 @@ void ContainerFloatingBehavior::InitializeShowAnimationStartingState( ...@@ -67,70 +67,73 @@ void ContainerFloatingBehavior::InitializeShowAnimationStartingState(
gfx::Rect ContainerFloatingBehavior::AdjustSetBoundsRequest( gfx::Rect ContainerFloatingBehavior::AdjustSetBoundsRequest(
const gfx::Rect& display_bounds, const gfx::Rect& display_bounds,
const gfx::Rect& requested_bounds) { const gfx::Rect& requested_bounds_in_screen) {
gfx::Rect keyboard_bounds = requested_bounds; gfx::Rect keyboard_bounds_in_screen = requested_bounds_in_screen;
if (!default_position_) { if (!default_position_in_screen_) {
// If the keyboard hasn't been shown yet, ignore the request and use // If the keyboard hasn't been shown yet, ignore the request and use
// default. // default.
gfx::Point default_location = gfx::Point default_location = GetPositionForShowingKeyboard(
GetPositionForShowingKeyboard(keyboard_bounds.size(), display_bounds); keyboard_bounds_in_screen.size(), display_bounds);
keyboard_bounds = gfx::Rect(default_location, keyboard_bounds.size()); keyboard_bounds_in_screen =
gfx::Rect(default_location, keyboard_bounds_in_screen.size());
} else { } else {
// Otherwise, simply make sure that the new bounds are not off the edge of // Otherwise, simply make sure that the new bounds are not off the edge of
// the screen. // the screen.
keyboard_bounds = keyboard_bounds_in_screen = ContainKeyboardToScreenBounds(
ContainKeyboardToScreenBounds(keyboard_bounds, display_bounds); keyboard_bounds_in_screen, display_bounds);
SavePosition(keyboard_bounds, display_bounds.size()); SavePosition(keyboard_bounds_in_screen, display_bounds.size());
} }
return keyboard_bounds; return keyboard_bounds_in_screen;
} }
void ContainerFloatingBehavior::SavePosition(const gfx::Rect& keyboard_bounds, void ContainerFloatingBehavior::SavePosition(
const gfx::Size& screen_size) { const gfx::Rect& keyboard_bounds_in_screen,
int left_distance = keyboard_bounds.x(); const gfx::Size& screen_size) {
int right_distance = screen_size.width() - (keyboard_bounds.right()); int left_distance = keyboard_bounds_in_screen.x();
int top_distance = keyboard_bounds.y(); int right_distance = screen_size.width() - keyboard_bounds_in_screen.right();
int bottom_distance = screen_size.height() - (keyboard_bounds.bottom()); int top_distance = keyboard_bounds_in_screen.y();
int bottom_distance =
screen_size.height() - keyboard_bounds_in_screen.bottom();
double available_width = left_distance + right_distance; double available_width = left_distance + right_distance;
double available_height = top_distance + bottom_distance; double available_height = top_distance + bottom_distance;
if (!default_position_) { if (!default_position_in_screen_) {
default_position_ = std::make_unique<KeyboardPosition>(); default_position_in_screen_ = std::make_unique<KeyboardPosition>();
} }
default_position_->left_padding_allotment_ratio = default_position_in_screen_->left_padding_allotment_ratio =
left_distance / available_width; left_distance / available_width;
default_position_->top_padding_allotment_ratio = default_position_in_screen_->top_padding_allotment_ratio =
top_distance / available_height; top_distance / available_height;
} }
gfx::Rect ContainerFloatingBehavior::ContainKeyboardToScreenBounds( gfx::Rect ContainerFloatingBehavior::ContainKeyboardToScreenBounds(
const gfx::Rect& keyboard_bounds, const gfx::Rect& keyboard_bounds_in_screen,
const gfx::Rect& display_bounds) const { const gfx::Rect& display_bounds) const {
int left = keyboard_bounds.x(); int left = keyboard_bounds_in_screen.x();
int top = keyboard_bounds.y(); int top = keyboard_bounds_in_screen.y();
int right = keyboard_bounds.right(); int right = keyboard_bounds_in_screen.right();
int bottom = keyboard_bounds.bottom(); int bottom = keyboard_bounds_in_screen.bottom();
// Prevent keyboard from appearing off screen or overlapping with the edge. // Prevent keyboard from appearing off screen or overlapping with the edge.
if (left < display_bounds.x()) { if (left < display_bounds.x()) {
left = display_bounds.x(); left = display_bounds.x();
right = left + keyboard_bounds.width(); right = left + keyboard_bounds_in_screen.width();
} }
if (right >= display_bounds.right()) { if (right >= display_bounds.right()) {
right = display_bounds.right(); right = display_bounds.right();
left = right - keyboard_bounds.width(); left = right - keyboard_bounds_in_screen.width();
} }
if (top < display_bounds.y()) { if (top < display_bounds.y()) {
top = display_bounds.y(); top = display_bounds.y();
bottom = top + keyboard_bounds.height(); bottom = top + keyboard_bounds_in_screen.height();
} }
if (bottom >= display_bounds.bottom()) { if (bottom >= display_bounds.bottom()) {
bottom = display_bounds.bottom(); bottom = display_bounds.bottom();
top = bottom - keyboard_bounds.height(); top = bottom - keyboard_bounds_in_screen.height();
} }
return gfx::Rect(left, top, right - left, bottom - top); return gfx::Rect(left, top, right - left, bottom - top);
...@@ -145,7 +148,7 @@ gfx::Point ContainerFloatingBehavior::GetPositionForShowingKeyboard( ...@@ -145,7 +148,7 @@ gfx::Point ContainerFloatingBehavior::GetPositionForShowingKeyboard(
const gfx::Rect& display_bounds) const { const gfx::Rect& display_bounds) const {
// Start with the last saved position // Start with the last saved position
gfx::Point top_left_offset; gfx::Point top_left_offset;
KeyboardPosition* position = default_position_.get(); KeyboardPosition* position = default_position_in_screen_.get();
if (position == nullptr) { if (position == nullptr) {
// If there is none, center the keyboard along the bottom of the screen. // If there is none, center the keyboard along the bottom of the screen.
top_left_offset.set_x(display_bounds.width() - keyboard_size.width() - top_left_offset.set_x(display_bounds.width() - keyboard_size.width() -
...@@ -189,10 +192,10 @@ bool ContainerFloatingBehavior::HandlePointerEvent( ...@@ -189,10 +192,10 @@ bool ContainerFloatingBehavior::HandlePointerEvent(
aura::Window* container = controller_->GetContainerWindow(); aura::Window* container = controller_->GetContainerWindow();
const gfx::Rect& keyboard_bounds = container->bounds(); const gfx::Rect& keyboard_bounds_in_screen = container->GetBoundsInScreen();
// Don't handle events if this runs in a partially initialized state. // Don't handle events if this runs in a partially initialized state.
if (keyboard_bounds.height() <= 0) if (keyboard_bounds_in_screen.height() <= 0)
return false; return false;
ui::PointerId pointer_id = -1; ui::PointerId pointer_id = -1;
...@@ -205,7 +208,7 @@ bool ContainerFloatingBehavior::HandlePointerEvent( ...@@ -205,7 +208,7 @@ bool ContainerFloatingBehavior::HandlePointerEvent(
switch (type) { switch (type) {
case ui::ET_TOUCH_PRESSED: case ui::ET_TOUCH_PRESSED:
case ui::ET_MOUSE_PRESSED: case ui::ET_MOUSE_PRESSED:
if (!IsDragHandle(kb_offset, keyboard_bounds.size())) { if (!IsDragHandle(kb_offset, keyboard_bounds_in_screen.size())) {
drag_descriptor_ = nullptr; drag_descriptor_ = nullptr;
} else if (type == ui::ET_MOUSE_PRESSED && } else if (type == ui::ET_MOUSE_PRESSED &&
!((const ui::MouseEvent*)&event)->IsOnlyLeftMouseButton()) { !((const ui::MouseEvent*)&event)->IsOnlyLeftMouseButton()) {
...@@ -215,7 +218,7 @@ bool ContainerFloatingBehavior::HandlePointerEvent( ...@@ -215,7 +218,7 @@ bool ContainerFloatingBehavior::HandlePointerEvent(
// If there is no active drag descriptor, start a new one. // If there is no active drag descriptor, start a new one.
bool drag_started_by_touch = (type == ui::ET_TOUCH_PRESSED); bool drag_started_by_touch = (type == ui::ET_TOUCH_PRESSED);
drag_descriptor_.reset( drag_descriptor_.reset(
new DragDescriptor{keyboard_bounds.origin(), kb_offset, new DragDescriptor{keyboard_bounds_in_screen.origin(), kb_offset,
drag_started_by_touch, pointer_id}); drag_started_by_touch, pointer_id});
} }
break; break;
...@@ -239,14 +242,14 @@ bool ContainerFloatingBehavior::HandlePointerEvent( ...@@ -239,14 +242,14 @@ bool ContainerFloatingBehavior::HandlePointerEvent(
drag_descriptor_->original_keyboard_location + drag_descriptor_->original_keyboard_location +
drag_descriptor_->original_click_offset; drag_descriptor_->original_click_offset;
const gfx::Point current_drag_location = const gfx::Point current_drag_location =
keyboard_bounds.origin() + kb_offset; keyboard_bounds_in_screen.origin() + kb_offset;
const gfx::Vector2d cumulative_drag_offset = const gfx::Vector2d cumulative_drag_offset =
current_drag_location - original_click_location; current_drag_location - original_click_location;
const gfx::Point new_keyboard_location = const gfx::Point new_keyboard_location =
drag_descriptor_->original_keyboard_location + drag_descriptor_->original_keyboard_location +
cumulative_drag_offset; cumulative_drag_offset;
gfx::Rect new_bounds_in_local = gfx::Rect new_bounds_in_local =
gfx::Rect(new_keyboard_location, keyboard_bounds.size()); gfx::Rect(new_keyboard_location, keyboard_bounds_in_screen.size());
DisplayUtil display_util; DisplayUtil display_util;
const display::Display& new_display = const display::Display& new_display =
...@@ -274,7 +277,7 @@ bool ContainerFloatingBehavior::HandlePointerEvent( ...@@ -274,7 +277,7 @@ bool ContainerFloatingBehavior::HandlePointerEvent(
controller_->MoveToDisplayWithTransition(new_display, controller_->MoveToDisplayWithTransition(new_display,
new_bounds_in_local); new_bounds_in_local);
} }
SavePosition(container->bounds(), new_display.size()); SavePosition(container->GetBoundsInScreen(), new_display.size());
return true; return true;
} }
break; break;
...@@ -291,10 +294,10 @@ void ContainerFloatingBehavior::SetCanonicalBounds( ...@@ -291,10 +294,10 @@ void ContainerFloatingBehavior::SetCanonicalBounds(
const gfx::Rect& display_bounds) { const gfx::Rect& display_bounds) {
gfx::Point keyboard_location = gfx::Point keyboard_location =
GetPositionForShowingKeyboard(container->bounds().size(), display_bounds); GetPositionForShowingKeyboard(container->bounds().size(), display_bounds);
gfx::Rect keyboard_bounds = gfx::Rect keyboard_bounds_in_screen =
gfx::Rect(keyboard_location, container->bounds().size()); gfx::Rect(keyboard_location, container->bounds().size());
SavePosition(keyboard_bounds, display_bounds.size()); SavePosition(keyboard_bounds_in_screen, display_bounds.size());
container->SetBounds(keyboard_bounds); container->SetBounds(keyboard_bounds_in_screen);
} }
bool ContainerFloatingBehavior::TextBlurHidesKeyboard() const { bool ContainerFloatingBehavior::TextBlurHidesKeyboard() const {
......
...@@ -47,7 +47,7 @@ class KEYBOARD_EXPORT ContainerFloatingBehavior : public ContainerBehavior { ...@@ -47,7 +47,7 @@ class KEYBOARD_EXPORT ContainerFloatingBehavior : public ContainerBehavior {
bool IsOverscrollAllowed() const override; bool IsOverscrollAllowed() const override;
bool IsDragHandle(const gfx::Vector2d& offset, bool IsDragHandle(const gfx::Vector2d& offset,
const gfx::Size& keyboard_size) const override; const gfx::Size& keyboard_size) const override;
void SavePosition(const gfx::Rect& keyboard_bounds, void SavePosition(const gfx::Rect& keyboard_bounds_in_screen,
const gfx::Size& screen_size) override; const gfx::Size& screen_size) override;
bool HandlePointerEvent(const ui::LocatedEvent& event, bool HandlePointerEvent(const ui::LocatedEvent& event,
const display::Display& current_display) override; const display::Display& current_display) override;
...@@ -69,7 +69,7 @@ class KEYBOARD_EXPORT ContainerFloatingBehavior : public ContainerBehavior { ...@@ -69,7 +69,7 @@ class KEYBOARD_EXPORT ContainerFloatingBehavior : public ContainerBehavior {
// Ensures that the keyboard is neither off the screen nor overlapping an // Ensures that the keyboard is neither off the screen nor overlapping an
// edge. // edge.
gfx::Rect ContainKeyboardToScreenBounds( gfx::Rect ContainKeyboardToScreenBounds(
const gfx::Rect& keyboard_bounds, const gfx::Rect& keyboard_bounds_in_screen,
const gfx::Rect& display_bounds) const; const gfx::Rect& display_bounds) const;
// Saves the current keyboard location for use the next time it is displayed. // Saves the current keyboard location for use the next time it is displayed.
...@@ -78,8 +78,8 @@ class KEYBOARD_EXPORT ContainerFloatingBehavior : public ContainerBehavior { ...@@ -78,8 +78,8 @@ class KEYBOARD_EXPORT ContainerFloatingBehavior : public ContainerBehavior {
KeyboardController* controller_; KeyboardController* controller_;
// TODO(blakeo): cache the default_position_ on a per-display basis. // TODO(blakeo): cache the default_position_ on a per-display basis.
std::unique_ptr<struct keyboard::KeyboardPosition> default_position_ = std::unique_ptr<struct keyboard::KeyboardPosition>
nullptr; default_position_in_screen_ = nullptr;
// Current state of a cursor drag to move the keyboard, if one exists. // Current state of a cursor drag to move the keyboard, if one exists.
// Otherwise nullptr. // Otherwise nullptr.
......
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