Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "ash/keyboard_overlay/keyboard_overlay_delegate.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | |
| 9 | #include "ash/shell.h" |
| 10 | #include "base/bind.h" |
| 11 | #include "base/memory/weak_ptr.h" |
| 12 | #include "base/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" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 16 | #include "ui/aura/root_window.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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" |
| 21 | |
| 22 | using content::WebContents; |
| 23 | using content::WebUIMessageHandler; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | const int kBaseWidth = 1252; |
| 28 | const int kBaseHeight = 516; |
| 29 | const int kHorizontalMargin = 28; |
| 30 | |
| 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> { |
| 35 | public: |
| 36 | explicit PaintMessageHandler(views::Widget* widget) : widget_(widget) {} |
| 37 | virtual ~PaintMessageHandler() {} |
| 38 | |
| 39 | // WebUIMessageHandler implementation. |
| 40 | virtual void RegisterMessages() OVERRIDE; |
| 41 | |
| 42 | private: |
| 43 | void DidPaint(const ListValue* args); |
| 44 | |
| 45 | views::Widget* widget_; |
| 46 | |
| 47 | DISALLOW_COPY_AND_ASSIGN(PaintMessageHandler); |
| 48 | }; |
| 49 | |
| 50 | void PaintMessageHandler::RegisterMessages() { |
| 51 | web_ui()->RegisterMessageCallback( |
| 52 | "didPaint", |
| 53 | base::Bind(&PaintMessageHandler::DidPaint, base::Unretained(this))); |
| 54 | } |
| 55 | |
| 56 | void PaintMessageHandler::DidPaint(const ListValue* args) { |
| 57 | // Show the widget after the web content has been painted. |
| 58 | widget_->Show(); |
| 59 | } |
| 60 | |
| 61 | } // namespace |
| 62 | |
| 63 | namespace ash { |
| 64 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame^] | 65 | KeyboardOverlayDelegate::KeyboardOverlayDelegate(const base::string16& title, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 66 | const GURL& url) |
| 67 | : title_(title), |
| 68 | url_(url), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 69 | widget_(NULL) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | KeyboardOverlayDelegate::~KeyboardOverlayDelegate() { |
| 73 | } |
| 74 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 75 | views::Widget* KeyboardOverlayDelegate::Show(views::WebDialogView* view) { |
| 76 | widget_ = new views::Widget; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 77 | views::Widget::InitParams params( |
| 78 | views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 79 | params.context = Shell::GetPrimaryRootWindow(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 80 | params.delegate = view; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 81 | widget_->Init(params); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 82 | |
| 83 | // Show the widget at the bottom of the work area. |
| 84 | gfx::Size size; |
| 85 | GetDialogSize(&size); |
| 86 | const gfx::Rect& rect = Shell::GetScreen()->GetDisplayNearestWindow( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 87 | widget_->GetNativeView()).work_area(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 88 | gfx::Rect bounds((rect.width() - size.width()) / 2, |
| 89 | rect.height() - size.height(), |
| 90 | size.width(), |
| 91 | size.height()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 92 | widget_->SetBounds(bounds); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 93 | |
| 94 | // The widget will be shown when the web contents gets ready to display. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 95 | return widget_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | ui::ModalType KeyboardOverlayDelegate::GetDialogModalType() const { |
| 99 | return ui::MODAL_TYPE_SYSTEM; |
| 100 | } |
| 101 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame^] | 102 | base::string16 KeyboardOverlayDelegate::GetDialogTitle() const { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 103 | return title_; |
| 104 | } |
| 105 | |
| 106 | GURL KeyboardOverlayDelegate::GetDialogContentURL() const { |
| 107 | return url_; |
| 108 | } |
| 109 | |
| 110 | void KeyboardOverlayDelegate::GetWebUIMessageHandlers( |
| 111 | std::vector<WebUIMessageHandler*>* handlers) const { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 112 | handlers->push_back(new PaintMessageHandler(widget_)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void KeyboardOverlayDelegate::GetDialogSize( |
| 116 | gfx::Size* size) const { |
| 117 | using std::min; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 118 | DCHECK(widget_); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 119 | gfx::Rect rect = ash::Shell::GetScreen()->GetDisplayNearestWindow( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 120 | widget_->GetNativeView()).bounds(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 121 | const int width = min(kBaseWidth, rect.width() - kHorizontalMargin); |
| 122 | const int height = width * kBaseHeight / kBaseWidth; |
| 123 | size->SetSize(width, height); |
| 124 | } |
| 125 | |
| 126 | std::string KeyboardOverlayDelegate::GetDialogArgs() const { |
| 127 | return "[]"; |
| 128 | } |
| 129 | |
| 130 | void KeyboardOverlayDelegate::OnDialogClosed( |
| 131 | const std::string& json_retval) { |
| 132 | delete this; |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | void KeyboardOverlayDelegate::OnCloseContents(WebContents* source, |
| 137 | bool* out_close_dialog) { |
| 138 | } |
| 139 | |
| 140 | bool KeyboardOverlayDelegate::ShouldShowDialogTitle() const { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | bool KeyboardOverlayDelegate::HandleContextMenu( |
| 145 | const content::ContextMenuParams& params) { |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | } // namespace ash |