OLD | NEW |
| (Empty) |
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 "chrome/browser/ui/views/keyboard_overlay_delegate.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/utf_string_conversions.h" | |
12 #include "base/values.h" | |
13 #include "chrome/common/url_constants.h" | |
14 #include "content/public/browser/web_ui.h" | |
15 #include "content/public/browser/web_ui_message_handler.h" | |
16 #include "googleurl/src/gurl.h" | |
17 #include "grit/generated_resources.h" | |
18 #include "ui/base/l10n/l10n_util.h" | |
19 #include "ui/gfx/screen.h" | |
20 #include "ui/views/controls/webview/web_dialog_view.h" | |
21 #include "ui/views/widget/widget.h" | |
22 | |
23 using content::WebContents; | |
24 using content::WebUIMessageHandler; | |
25 | |
26 namespace { | |
27 | |
28 const int kBaseWidth = 1252; | |
29 const int kBaseHeight = 516; | |
30 const int kHorizontalMargin = 28; | |
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 | |
62 } // namespace | |
63 | |
64 KeyboardOverlayDelegate::KeyboardOverlayDelegate(const string16& title) | |
65 : title_(title), | |
66 view_(NULL) { | |
67 } | |
68 | |
69 KeyboardOverlayDelegate::~KeyboardOverlayDelegate() { | |
70 } | |
71 | |
72 void KeyboardOverlayDelegate::Show(views::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::GetDisplayNearestWindow( | |
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 | |
95 ui::ModalType KeyboardOverlayDelegate::GetDialogModalType() const { | |
96 return ui::MODAL_TYPE_SYSTEM; | |
97 } | |
98 | |
99 string16 KeyboardOverlayDelegate::GetDialogTitle() const { | |
100 return title_; | |
101 } | |
102 | |
103 GURL KeyboardOverlayDelegate::GetDialogContentURL() const { | |
104 std::string url_string(chrome::kChromeUIKeyboardOverlayURL); | |
105 return GURL(url_string); | |
106 } | |
107 | |
108 void KeyboardOverlayDelegate::GetWebUIMessageHandlers( | |
109 std::vector<WebUIMessageHandler*>* handlers) const { | |
110 handlers->push_back(new PaintMessageHandler(view_->GetWidget())); | |
111 } | |
112 | |
113 void KeyboardOverlayDelegate::GetDialogSize( | |
114 gfx::Size* size) const { | |
115 using std::min; | |
116 DCHECK(view_); | |
117 gfx::Rect rect = gfx::Screen::GetDisplayNearestWindow( | |
118 view_->GetWidget()->GetNativeView()).bounds(); | |
119 const int width = min(kBaseWidth, rect.width() - kHorizontalMargin); | |
120 const int height = width * kBaseHeight / kBaseWidth; | |
121 size->SetSize(width, height); | |
122 } | |
123 | |
124 std::string KeyboardOverlayDelegate::GetDialogArgs() const { | |
125 return "[]"; | |
126 } | |
127 | |
128 void KeyboardOverlayDelegate::OnDialogClosed( | |
129 const std::string& json_retval) { | |
130 delete this; | |
131 return; | |
132 } | |
133 | |
134 void KeyboardOverlayDelegate::OnCloseContents(WebContents* source, | |
135 bool* out_close_dialog) { | |
136 } | |
137 | |
138 bool KeyboardOverlayDelegate::ShouldShowDialogTitle() const { | |
139 return false; | |
140 } | |
141 | |
142 bool KeyboardOverlayDelegate::HandleContextMenu( | |
143 const content::ContextMenuParams& params) { | |
144 return true; | |
145 } | |
OLD | NEW |