1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_ui.h"
#include <string>
#include <utility>
#include "ash/keyboard/ui/keyboard_ui_controller.h"
#include "ash/shell.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_bounds_observer.h"
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_controller_client.h"
#include "chrome/browser/ui/ash/keyboard/chrome_keyboard_web_contents.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ime/ash/ime_bridge.h"
#include "ui/compositor/layer.h"
#include "ui/compositor_extra/shadow.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/wm/core/shadow_types.h"
namespace {
const int kShadowElevationVirtualKeyboard = 2;
} // namespace
ChromeKeyboardUI::ChromeKeyboardUI(content::BrowserContext* context)
: browser_context_(context) {}
ChromeKeyboardUI::~ChromeKeyboardUI() {
DCHECK(!keyboard_controller());
}
// keyboard::KeyboardUI:
aura::Window* ChromeKeyboardUI::LoadKeyboardWindow(LoadCallback callback) {
DCHECK(!keyboard_contents_);
keyboard_contents_ = std::make_unique<ChromeKeyboardWebContents>(
browser_context_,
ChromeKeyboardControllerClient::Get()->GetVirtualKeyboardUrl(),
base::BindOnce(
[](LoadCallback callback) {
ChromeKeyboardControllerClient::Get()->NotifyKeyboardLoaded();
std::move(callback).Run();
},
std::move(callback)),
base::NullCallback());
aura::Window* keyboard_window =
keyboard_contents_->web_contents()->GetNativeView();
keyboard_window->AddObserver(this);
return keyboard_window;
}
aura::Window* ChromeKeyboardUI::GetKeyboardWindow() const {
return keyboard_contents_
? keyboard_contents_->web_contents()->GetNativeView()
: nullptr;
}
ui::GestureConsumer* ChromeKeyboardUI::GetGestureConsumer() const {
return keyboard_contents_
? keyboard_contents_->web_contents()->GetContentNativeView()
: nullptr;
}
ui::InputMethod* ChromeKeyboardUI::GetInputMethod() {
ash::IMEBridge* bridge = ash::IMEBridge::Get();
if (!bridge || !bridge->GetInputContextHandler()) {
// Needed by a handful of browser tests that use MockInputMethod.
return ash::Shell::GetRootWindowForNewWindows()
->GetHost()
->GetInputMethod();
}
return bridge->GetInputContextHandler()->GetInputMethod();
}
void ChromeKeyboardUI::ReloadKeyboardIfNeeded() {
VLOG(1) << "ReloadKeyboardIfNeeded";
DCHECK(keyboard_contents_);
keyboard_contents_->SetKeyboardUrl(
ChromeKeyboardControllerClient::Get()->GetVirtualKeyboardUrl());
}
// aura::WindowObserver:
void ChromeKeyboardUI::OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds,
ui::PropertyChangeReason reason) {
SetShadowAroundKeyboard();
}
void ChromeKeyboardUI::OnWindowDestroyed(aura::Window* window) {
window->RemoveObserver(this);
}
void ChromeKeyboardUI::OnWindowParentChanged(aura::Window* window,
aura::Window* parent) {
SetShadowAroundKeyboard();
}
// private methods:
void ChromeKeyboardUI::SetShadowAroundKeyboard() {
aura::Window* contents_window = GetKeyboardWindow();
DCHECK(contents_window);
if (!shadow_) {
shadow_ = std::make_unique<ui::Shadow>();
shadow_->Init(kShadowElevationVirtualKeyboard);
shadow_->layer()->SetVisible(true);
contents_window->layer()->Add(shadow_->layer());
}
shadow_->SetContentBounds(gfx::Rect(contents_window->bounds().size()));
// In floating mode, make the shadow layer invisible because the shadows are
// drawn manually by the IME extension.
// TODO(https://crbug.com/856195): Remove this when we figure out how ChromeOS
// can draw custom shaped shadows, or how overscrolling can account for
// shadows drawn by IME.
shadow_->layer()->SetVisible(
keyboard_controller()->GetActiveContainerType() ==
keyboard::ContainerType::kFullWidth);
}
|