#include "ui/wm/core/ime_util_chromeos.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/base/ui_base_switches.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/wm/core/coordinate_conversion.h"
namespace wm {
namespace {
void SetWindowBoundsInScreen(aura::Window* window,
const gfx::Rect& bounds_in_screen) {
window->SetBoundsInScreen(
bounds_in_screen, display::Screen::Get()->GetDisplayNearestView(window));
}
void MoveWindowToEnsureCaretNotInRect(aura::Window* window,
const gfx::Rect& rect_in_screen) {
gfx::Rect original_window_bounds = window->GetBoundsInScreen();
if (window->GetProperty(kVirtualKeyboardRestoreBoundsKey)) {
original_window_bounds =
*window->GetProperty(kVirtualKeyboardRestoreBoundsKey);
}
const int top_y = std::max(
rect_in_screen.y() - original_window_bounds.height(),
display::Screen::Get()->GetDisplayNearestView(window).work_area().y());
if (top_y >= original_window_bounds.y())
return;
window->SetProperty(kVirtualKeyboardRestoreBoundsKey,
std::make_unique<gfx::Rect>(original_window_bounds));
gfx::Rect new_bounds_in_screen = original_window_bounds;
new_bounds_in_screen.set_y(top_y);
SetWindowBoundsInScreen(window, new_bounds_in_screen);
}
}
DEFINE_OWNED_UI_CLASS_PROPERTY_KEY(gfx::Rect, kVirtualKeyboardRestoreBoundsKey)
void RestoreWindowBoundsOnClientFocusLost(aura::Window* window) {
gfx::Rect* vk_restore_bounds =
window->GetProperty(kVirtualKeyboardRestoreBoundsKey);
if (!vk_restore_bounds)
return;
if (window->GetBoundsInScreen() != *vk_restore_bounds) {
SetWindowBoundsInScreen(window, *vk_restore_bounds);
}
window->ClearProperty(wm::kVirtualKeyboardRestoreBoundsKey);
}
void EnsureWindowNotInRect(aura::Window* window,
const gfx::Rect& rect_in_screen) {
gfx::Rect original_window_bounds = window->GetBoundsInScreen();
if (window->GetProperty(wm::kVirtualKeyboardRestoreBoundsKey)) {
original_window_bounds =
*window->GetProperty(wm::kVirtualKeyboardRestoreBoundsKey);
}
gfx::Rect hidden_window_bounds_in_screen =
gfx::IntersectRects(rect_in_screen, original_window_bounds);
if (hidden_window_bounds_in_screen.IsEmpty()) {
RestoreWindowBoundsOnClientFocusLost(window);
return;
}
MoveWindowToEnsureCaretNotInRect(window, rect_in_screen);
}
}