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
136
137
138
139
140
|
// Copyright (c) 2012 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 "ash/content/keyboard_overlay/keyboard_overlay_delegate.h"
#include <algorithm>
#include "ash/shell.h"
#include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/gfx/screen.h"
#include "ui/views/controls/webview/web_dialog_view.h"
#include "ui/views/widget/widget.h"
using content::WebContents;
using content::WebUIMessageHandler;
namespace {
const int kBaseWidth = 1252;
const int kBaseHeight = 516;
const int kHorizontalMargin = 28;
// A message handler for detecting the timing when the web contents is painted.
class PaintMessageHandler : public WebUIMessageHandler,
public base::SupportsWeakPtr<PaintMessageHandler> {
public:
explicit PaintMessageHandler(views::Widget* widget) : widget_(widget) {}
~PaintMessageHandler() override {}
// WebUIMessageHandler implementation.
void RegisterMessages() override;
private:
void DidPaint(const base::ListValue* args);
views::Widget* widget_;
DISALLOW_COPY_AND_ASSIGN(PaintMessageHandler);
};
void PaintMessageHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"didPaint", base::Bind(&PaintMessageHandler::DidPaint, AsWeakPtr()));
}
void PaintMessageHandler::DidPaint(const base::ListValue* args) {
// Show the widget after the web content has been painted.
widget_->Show();
}
} // namespace
namespace ash {
KeyboardOverlayDelegate::KeyboardOverlayDelegate(const base::string16& title,
const GURL& url)
: title_(title), url_(url), widget_(NULL) {}
KeyboardOverlayDelegate::~KeyboardOverlayDelegate() {}
views::Widget* KeyboardOverlayDelegate::Show(views::WebDialogView* view) {
widget_ = new views::Widget;
views::Widget::InitParams params(
views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.context = Shell::GetPrimaryRootWindow();
params.delegate = view;
widget_->Init(params);
// Show the widget at the bottom of the work area.
gfx::Size size;
GetDialogSize(&size);
const gfx::Rect& rect =
Shell::GetScreen()
->GetDisplayNearestWindow(widget_->GetNativeView())
.work_area();
gfx::Rect bounds(rect.x() + (rect.width() - size.width()) / 2,
rect.bottom() - size.height(), size.width(), size.height());
widget_->SetBounds(bounds);
// The widget will be shown when the web contents gets ready to display.
return widget_;
}
ui::ModalType KeyboardOverlayDelegate::GetDialogModalType() const {
return ui::MODAL_TYPE_SYSTEM;
}
base::string16 KeyboardOverlayDelegate::GetDialogTitle() const {
return title_;
}
GURL KeyboardOverlayDelegate::GetDialogContentURL() const {
return url_;
}
void KeyboardOverlayDelegate::GetWebUIMessageHandlers(
std::vector<WebUIMessageHandler*>* handlers) const {
handlers->push_back(new PaintMessageHandler(widget_));
}
void KeyboardOverlayDelegate::GetDialogSize(gfx::Size* size) const {
using std::min;
DCHECK(widget_);
gfx::Rect rect = ash::Shell::GetScreen()
->GetDisplayNearestWindow(widget_->GetNativeView())
.work_area();
const int width = min(kBaseWidth, rect.width() - kHorizontalMargin);
const int height = width * kBaseHeight / kBaseWidth;
size->SetSize(width, height);
}
std::string KeyboardOverlayDelegate::GetDialogArgs() const {
return "[]";
}
void KeyboardOverlayDelegate::OnDialogClosed(const std::string& json_retval) {
delete this;
return;
}
void KeyboardOverlayDelegate::OnCloseContents(WebContents* source,
bool* out_close_dialog) {}
bool KeyboardOverlayDelegate::ShouldShowDialogTitle() const {
return false;
}
bool KeyboardOverlayDelegate::HandleContextMenu(
const content::ContextMenuParams& params) {
return true;
}
} // namespace ash
|