OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/keyboard_overlay_delegate.h" | 5 #include "chrome/browser/ui/views/keyboard_overlay_delegate.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/memory/weak_ptr.h" |
9 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" |
10 #include "chrome/browser/ui/views/web_dialog_view.h" | 13 #include "chrome/browser/ui/views/web_dialog_view.h" |
11 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "content/public/browser/web_ui.h" |
| 16 #include "content/public/browser/web_ui_message_handler.h" |
12 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
13 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
14 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
15 #include "ui/gfx/screen.h" | 20 #include "ui/gfx/screen.h" |
16 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
17 | 22 |
18 using content::WebContents; | 23 using content::WebContents; |
19 using content::WebUIMessageHandler; | 24 using content::WebUIMessageHandler; |
20 | 25 |
21 namespace { | 26 namespace { |
22 | 27 |
23 const int kBaseWidth = 1252; | 28 const int kBaseWidth = 1252; |
24 const int kBaseHeight = 516; | 29 const int kBaseHeight = 516; |
25 const int kHorizontalMargin = 28; | 30 const int kHorizontalMargin = 28; |
26 | 31 |
| 32 // A message handler for detecting the timing when the web contents is painted. |
| 33 class PaintMessageHandler |
| 34 : public WebUIMessageHandler, |
| 35 public base::SupportsWeakPtr<PaintMessageHandler> { |
| 36 public: |
| 37 explicit PaintMessageHandler(views::Widget* widget) : widget_(widget) {} |
| 38 virtual ~PaintMessageHandler() {} |
| 39 |
| 40 // WebUIMessageHandler implementation. |
| 41 virtual void RegisterMessages() OVERRIDE; |
| 42 |
| 43 private: |
| 44 void DidPaint(const ListValue* args); |
| 45 |
| 46 views::Widget* widget_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(PaintMessageHandler); |
| 49 }; |
| 50 |
| 51 void PaintMessageHandler::RegisterMessages() { |
| 52 web_ui()->RegisterMessageCallback( |
| 53 "didPaint", |
| 54 base::Bind(&PaintMessageHandler::DidPaint, base::Unretained(this))); |
| 55 } |
| 56 |
| 57 void PaintMessageHandler::DidPaint(const ListValue* args) { |
| 58 // Show the widget after the web content has been painted. |
| 59 widget_->Show(); |
| 60 } |
| 61 |
27 } // namespace | 62 } // namespace |
28 | 63 |
29 KeyboardOverlayDelegate::KeyboardOverlayDelegate(const string16& title) | 64 KeyboardOverlayDelegate::KeyboardOverlayDelegate(const string16& title) |
30 : title_(title), | 65 : title_(title), |
31 view_(NULL) { | 66 view_(NULL) { |
32 } | 67 } |
33 | 68 |
34 KeyboardOverlayDelegate::~KeyboardOverlayDelegate() { | 69 KeyboardOverlayDelegate::~KeyboardOverlayDelegate() { |
35 } | 70 } |
36 | 71 |
| 72 void KeyboardOverlayDelegate::Show(WebDialogView* view) { |
| 73 view_ = view; |
| 74 |
| 75 views::Widget* widget = new views::Widget; |
| 76 views::Widget::InitParams params( |
| 77 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 78 params.delegate = view; |
| 79 widget->Init(params); |
| 80 |
| 81 // Show the widget at the bottom of the work area. |
| 82 gfx::Size size; |
| 83 GetDialogSize(&size); |
| 84 const gfx::Rect& rect = gfx::Screen::GetMonitorNearestWindow( |
| 85 widget->GetNativeView()).work_area(); |
| 86 gfx::Rect bounds((rect.width() - size.width()) / 2, |
| 87 rect.height() - size.height(), |
| 88 size.width(), |
| 89 size.height()); |
| 90 widget->SetBounds(bounds); |
| 91 |
| 92 // The widget will be shown when the web contents gets ready to display. |
| 93 } |
| 94 |
37 ui::ModalType KeyboardOverlayDelegate::GetDialogModalType() const { | 95 ui::ModalType KeyboardOverlayDelegate::GetDialogModalType() const { |
38 return ui::MODAL_TYPE_SYSTEM; | 96 return ui::MODAL_TYPE_SYSTEM; |
39 } | 97 } |
40 | 98 |
41 string16 KeyboardOverlayDelegate::GetDialogTitle() const { | 99 string16 KeyboardOverlayDelegate::GetDialogTitle() const { |
42 return title_; | 100 return title_; |
43 } | 101 } |
44 | 102 |
45 GURL KeyboardOverlayDelegate::GetDialogContentURL() const { | 103 GURL KeyboardOverlayDelegate::GetDialogContentURL() const { |
46 std::string url_string(chrome::kChromeUIKeyboardOverlayURL); | 104 std::string url_string(chrome::kChromeUIKeyboardOverlayURL); |
47 return GURL(url_string); | 105 return GURL(url_string); |
48 } | 106 } |
49 | 107 |
50 void KeyboardOverlayDelegate::GetWebUIMessageHandlers( | 108 void KeyboardOverlayDelegate::GetWebUIMessageHandlers( |
51 std::vector<WebUIMessageHandler*>* handlers) const { | 109 std::vector<WebUIMessageHandler*>* handlers) const { |
| 110 handlers->push_back(new PaintMessageHandler(view_->GetWidget())); |
52 } | 111 } |
53 | 112 |
54 void KeyboardOverlayDelegate::GetDialogSize( | 113 void KeyboardOverlayDelegate::GetDialogSize( |
55 gfx::Size* size) const { | 114 gfx::Size* size) const { |
56 using std::min; | 115 using std::min; |
57 DCHECK(view_); | 116 DCHECK(view_); |
58 gfx::Rect rect = gfx::Screen::GetMonitorNearestWindow( | 117 gfx::Rect rect = gfx::Screen::GetMonitorNearestWindow( |
59 view_->GetWidget()->GetNativeView()).bounds(); | 118 view_->GetWidget()->GetNativeView()).bounds(); |
60 const int width = min(kBaseWidth, rect.width() - kHorizontalMargin); | 119 const int width = min(kBaseWidth, rect.width() - kHorizontalMargin); |
61 const int height = width * kBaseHeight / kBaseWidth; | 120 const int height = width * kBaseHeight / kBaseWidth; |
(...skipping 15 matching lines...) Expand all Loading... |
77 } | 136 } |
78 | 137 |
79 bool KeyboardOverlayDelegate::ShouldShowDialogTitle() const { | 138 bool KeyboardOverlayDelegate::ShouldShowDialogTitle() const { |
80 return false; | 139 return false; |
81 } | 140 } |
82 | 141 |
83 bool KeyboardOverlayDelegate::HandleContextMenu( | 142 bool KeyboardOverlayDelegate::HandleContextMenu( |
84 const content::ContextMenuParams& params) { | 143 const content::ContextMenuParams& params) { |
85 return true; | 144 return true; |
86 } | 145 } |
OLD | NEW |