Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / ash / keyboard_overlay / keyboard_overlay_delegate.cc
blob76563f5b80efc90fde9224d13c2123cf36f9241f
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"
7 #include <algorithm>
9 #include "ash/shell.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/window_event_dispatcher.h"
17 #include "ui/gfx/screen.h"
18 #include "ui/views/controls/webview/web_dialog_view.h"
19 #include "ui/views/widget/widget.h"
21 using content::WebContents;
22 using content::WebUIMessageHandler;
24 namespace {
26 const int kBaseWidth = 1252;
27 const int kBaseHeight = 516;
28 const int kHorizontalMargin = 28;
30 // A message handler for detecting the timing when the web contents is painted.
31 class PaintMessageHandler
32 : public WebUIMessageHandler,
33 public base::SupportsWeakPtr<PaintMessageHandler> {
34 public:
35 explicit PaintMessageHandler(views::Widget* widget) : widget_(widget) {}
36 ~PaintMessageHandler() override {}
38 // WebUIMessageHandler implementation.
39 void RegisterMessages() override;
41 private:
42 void DidPaint(const base::ListValue* args);
44 views::Widget* widget_;
46 DISALLOW_COPY_AND_ASSIGN(PaintMessageHandler);
49 void PaintMessageHandler::RegisterMessages() {
50 web_ui()->RegisterMessageCallback(
51 "didPaint",
52 base::Bind(&PaintMessageHandler::DidPaint, AsWeakPtr()));
55 void PaintMessageHandler::DidPaint(const base::ListValue* args) {
56 // Show the widget after the web content has been painted.
57 widget_->Show();
60 } // namespace
62 namespace ash {
64 KeyboardOverlayDelegate::KeyboardOverlayDelegate(const base::string16& title,
65 const GURL& url)
66 : title_(title),
67 url_(url),
68 widget_(NULL) {
71 KeyboardOverlayDelegate::~KeyboardOverlayDelegate() {
74 views::Widget* KeyboardOverlayDelegate::Show(views::WebDialogView* view) {
75 widget_ = new views::Widget;
76 views::Widget::InitParams params(
77 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
78 params.context = Shell::GetPrimaryRootWindow();
79 params.delegate = view;
80 widget_->Init(params);
82 // Show the widget at the bottom of the work area.
83 gfx::Size size;
84 GetDialogSize(&size);
85 const gfx::Rect& rect = Shell::GetScreen()->GetDisplayNearestWindow(
86 widget_->GetNativeView()).work_area();
87 gfx::Rect bounds(rect.x() + (rect.width() - size.width()) / 2,
88 rect.bottom() - size.height(),
89 size.width(),
90 size.height());
91 widget_->SetBounds(bounds);
93 // The widget will be shown when the web contents gets ready to display.
94 return widget_;
97 ui::ModalType KeyboardOverlayDelegate::GetDialogModalType() const {
98 return ui::MODAL_TYPE_SYSTEM;
101 base::string16 KeyboardOverlayDelegate::GetDialogTitle() const {
102 return title_;
105 GURL KeyboardOverlayDelegate::GetDialogContentURL() const {
106 return url_;
109 void KeyboardOverlayDelegate::GetWebUIMessageHandlers(
110 std::vector<WebUIMessageHandler*>* handlers) const {
111 handlers->push_back(new PaintMessageHandler(widget_));
114 void KeyboardOverlayDelegate::GetDialogSize(
115 gfx::Size* size) const {
116 using std::min;
117 DCHECK(widget_);
118 gfx::Rect rect = ash::Shell::GetScreen()->GetDisplayNearestWindow(
119 widget_->GetNativeView()).work_area();
120 const int width = min(kBaseWidth, rect.width() - kHorizontalMargin);
121 const int height = width * kBaseHeight / kBaseWidth;
122 size->SetSize(width, height);
125 std::string KeyboardOverlayDelegate::GetDialogArgs() const {
126 return "[]";
129 void KeyboardOverlayDelegate::OnDialogClosed(
130 const std::string& json_retval) {
131 delete this;
132 return;
135 void KeyboardOverlayDelegate::OnCloseContents(WebContents* source,
136 bool* out_close_dialog) {
139 bool KeyboardOverlayDelegate::ShouldShowDialogTitle() const {
140 return false;
143 bool KeyboardOverlayDelegate::HandleContextMenu(
144 const content::ContextMenuParams& params) {
145 return true;
148 } // namespace ash