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 "ash/keyboard_overlay/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 "content/public/browser/web_ui.h" | |
14 #include "content/public/browser/web_ui_message_handler.h" | |
15 #include "ui/base/l10n/l10n_util.h" | |
16 #include "ui/gfx/screen.h" | |
17 #include "ui/views/controls/webview/web_dialog_view.h" | |
18 #include "ui/views/widget/widget.h" | |
19 | |
20 using content::WebContents; | |
21 using content::WebUIMessageHandler; | |
22 | |
23 namespace { | |
24 | |
25 const int kBaseWidth = 1252; | |
26 const int kBaseHeight = 516; | |
27 const int kHorizontalMargin = 28; | |
28 | |
29 // A message handler for detecting the timing when the web contents is painted. | |
30 class PaintMessageHandler | |
31 : public WebUIMessageHandler, | |
32 public base::SupportsWeakPtr<PaintMessageHandler> { | |
33 public: | |
34 explicit PaintMessageHandler(views::Widget* widget) : widget_(widget) {} | |
35 virtual ~PaintMessageHandler() {} | |
36 | |
37 // WebUIMessageHandler implementation. | |
38 virtual void RegisterMessages() OVERRIDE; | |
39 | |
40 private: | |
41 void DidPaint(const ListValue* args); | |
42 | |
43 views::Widget* widget_; | |
44 | |
45 DISALLOW_COPY_AND_ASSIGN(PaintMessageHandler); | |
46 }; | |
47 | |
48 void PaintMessageHandler::RegisterMessages() { | |
49 web_ui()->RegisterMessageCallback( | |
50 "didPaint", | |
51 base::Bind(&PaintMessageHandler::DidPaint, base::Unretained(this))); | |
52 } | |
53 | |
54 void PaintMessageHandler::DidPaint(const ListValue* args) { | |
55 // Show the widget after the web content has been painted. | |
56 widget_->Show(); | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 KeyboardOverlayDelegate::KeyboardOverlayDelegate(const string16& title, | |
62 const GURL& url) | |
63 : title_(title), | |
64 url_(url), | |
65 view_(NULL) { | |
66 } | |
67 | |
68 KeyboardOverlayDelegate::~KeyboardOverlayDelegate() { | |
69 } | |
70 | |
71 void KeyboardOverlayDelegate::Show(views::WebDialogView* view) { | |
72 view_ = view; | |
73 | |
74 views::Widget* widget = new views::Widget; | |
75 views::Widget::InitParams params( | |
76 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
77 params.delegate = view; | |
78 widget->Init(params); | |
79 | |
80 // Show the widget at the bottom of the work area. | |
81 gfx::Size size; | |
82 GetDialogSize(&size); | |
83 const gfx::Rect& rect = gfx::Screen::GetDisplayNearestWindow( | |
84 widget->GetNativeView()).work_area(); | |
85 gfx::Rect bounds((rect.width() - size.width()) / 2, | |
86 rect.height() - size.height(), | |
87 size.width(), | |
88 size.height()); | |
89 widget->SetBounds(bounds); | |
90 | |
91 // The widget will be shown when the web contents gets ready to display. | |
92 } | |
93 | |
94 ui::ModalType KeyboardOverlayDelegate::GetDialogModalType() const { | |
95 return ui::MODAL_TYPE_SYSTEM; | |
96 } | |
97 | |
98 string16 KeyboardOverlayDelegate::GetDialogTitle() const { | |
99 return title_; | |
100 } | |
101 | |
102 GURL KeyboardOverlayDelegate::GetDialogContentURL() const { | |
103 return url_; | |
104 } | |
105 | |
106 void KeyboardOverlayDelegate::GetWebUIMessageHandlers( | |
107 std::vector<WebUIMessageHandler*>* handlers) const { | |
108 handlers->push_back(new PaintMessageHandler(view_->GetWidget())); | |
109 } | |
110 | |
111 void KeyboardOverlayDelegate::GetDialogSize( | |
112 gfx::Size* size) const { | |
113 using std::min; | |
114 DCHECK(view_); | |
115 gfx::Rect rect = gfx::Screen::GetDisplayNearestWindow( | |
116 view_->GetWidget()->GetNativeView()).bounds(); | |
117 const int width = min(kBaseWidth, rect.width() - kHorizontalMargin); | |
118 const int height = width * kBaseHeight / kBaseWidth; | |
119 size->SetSize(width, height); | |
120 } | |
121 | |
122 std::string KeyboardOverlayDelegate::GetDialogArgs() const { | |
123 return "[]"; | |
124 } | |
125 | |
126 void KeyboardOverlayDelegate::OnDialogClosed( | |
127 const std::string& json_retval) { | |
128 delete this; | |
129 return; | |
130 } | |
131 | |
132 void KeyboardOverlayDelegate::OnCloseContents(WebContents* source, | |
133 bool* out_close_dialog) { | |
134 } | |
135 | |
136 bool KeyboardOverlayDelegate::ShouldShowDialogTitle() const { | |
137 return false; | |
138 } | |
139 | |
140 bool KeyboardOverlayDelegate::HandleContextMenu( | |
141 const content::ContextMenuParams& params) { | |
142 return true; | |
143 } | |
OLD | NEW |