1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/keyboard_overlay/keyboard_overlay_delegate.h"
10 #include "base/bind.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "content/public/browser/web_ui.h"
15 #include "content/public/browser/web_ui_message_handler.h"
16 #include "ui/aura/root_window.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/gfx/screen.h"
19 #include "ui/views/controls/webview/web_dialog_view.h"
20 #include "ui/views/widget/widget.h"
22 using content::WebContents
;
23 using content::WebUIMessageHandler
;
27 const int kBaseWidth
= 1252;
28 const int kBaseHeight
= 516;
29 const int kHorizontalMargin
= 28;
31 // A message handler for detecting the timing when the web contents is painted.
32 class PaintMessageHandler
33 : public WebUIMessageHandler
,
34 public base::SupportsWeakPtr
<PaintMessageHandler
> {
36 explicit PaintMessageHandler(views::Widget
* widget
) : widget_(widget
) {}
37 virtual ~PaintMessageHandler() {}
39 // WebUIMessageHandler implementation.
40 virtual void RegisterMessages() OVERRIDE
;
43 void DidPaint(const base::ListValue
* args
);
45 views::Widget
* widget_
;
47 DISALLOW_COPY_AND_ASSIGN(PaintMessageHandler
);
50 void PaintMessageHandler::RegisterMessages() {
51 web_ui()->RegisterMessageCallback(
53 base::Bind(&PaintMessageHandler::DidPaint
, base::Unretained(this)));
56 void PaintMessageHandler::DidPaint(const base::ListValue
* args
) {
57 // Show the widget after the web content has been painted.
65 KeyboardOverlayDelegate::KeyboardOverlayDelegate(const base::string16
& title
,
72 KeyboardOverlayDelegate::~KeyboardOverlayDelegate() {
75 views::Widget
* KeyboardOverlayDelegate::Show(views::WebDialogView
* view
) {
76 widget_
= new views::Widget
;
77 views::Widget::InitParams
params(
78 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS
);
79 params
.context
= Shell::GetPrimaryRootWindow();
80 params
.delegate
= view
;
81 widget_
->Init(params
);
83 // Show the widget at the bottom of the work area.
86 const gfx::Rect
& rect
= Shell::GetScreen()->GetDisplayNearestWindow(
87 widget_
->GetNativeView()).work_area();
88 gfx::Rect
bounds(rect
.x() + (rect
.width() - size
.width()) / 2,
89 rect
.bottom() - size
.height(),
92 widget_
->SetBounds(bounds
);
94 // The widget will be shown when the web contents gets ready to display.
98 ui::ModalType
KeyboardOverlayDelegate::GetDialogModalType() const {
99 return ui::MODAL_TYPE_SYSTEM
;
102 base::string16
KeyboardOverlayDelegate::GetDialogTitle() const {
106 GURL
KeyboardOverlayDelegate::GetDialogContentURL() const {
110 void KeyboardOverlayDelegate::GetWebUIMessageHandlers(
111 std::vector
<WebUIMessageHandler
*>* handlers
) const {
112 handlers
->push_back(new PaintMessageHandler(widget_
));
115 void KeyboardOverlayDelegate::GetDialogSize(
116 gfx::Size
* size
) const {
119 gfx::Rect rect
= ash::Shell::GetScreen()->GetDisplayNearestWindow(
120 widget_
->GetNativeView()).work_area();
121 const int width
= min(kBaseWidth
, rect
.width() - kHorizontalMargin
);
122 const int height
= width
* kBaseHeight
/ kBaseWidth
;
123 size
->SetSize(width
, height
);
126 std::string
KeyboardOverlayDelegate::GetDialogArgs() const {
130 void KeyboardOverlayDelegate::OnDialogClosed(
131 const std::string
& json_retval
) {
136 void KeyboardOverlayDelegate::OnCloseContents(WebContents
* source
,
137 bool* out_close_dialog
) {
140 bool KeyboardOverlayDelegate::ShouldShowDialogTitle() const {
144 bool KeyboardOverlayDelegate::HandleContextMenu(
145 const content::ContextMenuParams
& params
) {