OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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/chromeos/login/webui_login_view.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/logging.h" | |
10 #include "chrome/browser/chromeos/login/login_utils.h" | |
11 #include "chrome/browser/chromeos/login/proxy_settings_dialog.h" | |
12 #include "chrome/browser/chromeos/status/clock_menu_button.h" | |
13 #include "chrome/browser/chromeos/status/input_method_menu_button.h" | |
14 #include "chrome/browser/chromeos/status/network_menu_button.h" | |
15 #include "chrome/browser/chromeos/status/status_area_view.h" | |
16 #include "chrome/browser/chromeos/wm_ipc.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/browser/profiles/profile_manager.h" | |
19 #include "chrome/browser/renderer_host/render_widget_host_view_views.h" | |
20 #include "chrome/browser/ui/touch/frame/keyboard_container_view.h" | |
21 #include "chrome/browser/ui/views/dom_view.h" | |
22 #include "chrome/browser/ui/views/tab_contents/tab_contents_view_touch.h" | |
23 #include "content/browser/renderer_host/render_view_host.h" | |
24 #include "content/browser/tab_contents/tab_contents.h" | |
25 #include "content/common/notification_service.h" | |
26 #include "googleurl/src/gurl.h" | |
27 #include "ui/base/x/x11_util.h" | |
28 #include "ui/gfx/transform.h" | |
29 #include "views/controls/textfield/textfield.h" | |
30 #include "views/widget/widget.h" | |
31 | |
32 // TODO(rharrison): Modify this class to support both touch and non-touch | |
33 | |
34 namespace { | |
35 | |
36 const int kKeyboardHeight = 300; | |
37 const int kKeyboardSlideDuration = 500; // In milliseconds | |
38 | |
39 PropertyAccessor<bool>* GetFocusedStateAccessor() { | |
40 static PropertyAccessor<bool> state; | |
41 return &state; | |
42 } | |
43 | |
44 bool TabContentsHasFocus(const TabContents* contents) { | |
45 views::View* view = static_cast<TabContentsViewTouch*>(contents->view()); | |
46 return view->Contains(view->GetFocusManager()->GetFocusedView()); | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 namespace chromeos { | |
52 | |
53 // static | |
54 const char WebUILoginView::kViewClassName[] = | |
55 "browser/chromeos/login/WebUILoginView"; | |
56 | |
57 // WebUILoginView public: ------------------------------------------------------ | |
58 | |
59 WebUILoginView::WebUILoginView() | |
60 : profile_(NULL), | |
61 status_area_(NULL), | |
62 webui_login_(NULL), | |
63 keyboard_showing_(false), | |
64 focus_listener_added_(false), | |
65 keyboard_(NULL) { | |
66 } | |
67 | |
68 void WebUILoginView::Init(const GURL& login_url) { | |
69 CHECK(!login_url.is_empty()); | |
70 profile_ = ProfileManager::GetDefaultProfile(); | |
71 webui_login_ = new DOMView(); | |
72 AddChildView(webui_login_); | |
73 webui_login_->Init(profile_, NULL); | |
74 webui_login_->LoadURL(login_url); | |
75 webui_login_->SetVisible(true); | |
76 | |
77 InitStatusArea(); | |
78 | |
79 registrar_.Add(this, | |
80 NotificationType::FOCUS_CHANGED_IN_PAGE, | |
81 NotificationService::AllSources()); | |
82 registrar_.Add(this, | |
83 NotificationType::TAB_CONTENTS_DESTROYED, | |
84 NotificationService::AllSources()); | |
85 } | |
86 | |
87 // static | |
88 views::Widget* WebUILoginView::CreateWindowContainingView( | |
89 const gfx::Rect& bounds, | |
90 const GURL& login_url, | |
91 WebUILoginView** view) { | |
92 views::Widget* window = new views::Widget; | |
93 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); | |
94 params.bounds = bounds; | |
95 window->Init(params); | |
96 *view = new WebUILoginView(); | |
97 (*view)->Init(login_url); | |
98 | |
99 window->SetContentsView(*view); | |
100 | |
101 (*view)->UpdateWindowType(); | |
102 | |
103 // This keeps the window from flashing at startup. | |
104 GdkWindow* gdk_window = window->GetNativeView()->window; | |
sky
2011/05/23 15:16:39
Are you sure we still need this? I ask as we don't
rharrison
2011/05/25 02:58:55
This doesn't appear to be needed anymore.
| |
105 gdk_window_set_back_pixmap(gdk_window, NULL, false); | |
106 | |
107 return window; | |
108 } | |
109 | |
110 std::string WebUILoginView::GetClassName() const { | |
111 return kViewClassName; | |
112 } | |
113 | |
114 gfx::NativeWindow WebUILoginView::GetNativeWindow() const { | |
115 return GetWidget()->GetNativeWindow(); | |
116 } | |
117 | |
118 void WebUILoginView::FocusWillChange(views::View* focused_before, | |
119 views::View* focused_now) { | |
120 VirtualKeyboardType before = DecideKeyboardStateForView(focused_before); | |
121 VirtualKeyboardType now = DecideKeyboardStateForView(focused_now); | |
122 if (before != now) { | |
123 // TODO(varunjain): support other types of keyboard. | |
124 UpdateKeyboardAndLayout(now == GENERIC); | |
125 } | |
126 } | |
127 | |
128 // WebUILoginView protected: --------------------------------------------------- | |
129 | |
130 void WebUILoginView::Layout() { | |
131 const int kCornerPadding = 5; | |
132 gfx::Size status_area_size = status_area_->GetPreferredSize(); | |
133 status_area_->SetBounds( | |
134 width() - status_area_size.width() - kCornerPadding, | |
135 kCornerPadding, | |
136 status_area_size.width(), | |
137 status_area_size.height()); | |
138 | |
139 if (webui_login_) | |
140 webui_login_->SetBoundsRect(bounds()); | |
141 | |
142 // TODO(rharrison): Hide touch specific code behind TOUCH_UI defines | |
143 if (!keyboard_) | |
144 return; | |
145 | |
146 keyboard_->SetVisible(keyboard_showing_); | |
147 gfx::Rect keyboard_bounds = bounds(); | |
148 keyboard_bounds.set_y(keyboard_bounds.height() - kKeyboardHeight); | |
149 keyboard_bounds.set_height(kKeyboardHeight); | |
150 keyboard_->SetBoundsRect(keyboard_bounds); | |
151 } | |
152 | |
153 void WebUILoginView::ChildPreferredSizeChanged(View* child) { | |
154 Layout(); | |
155 SchedulePaint(); | |
156 } | |
157 | |
158 Profile* WebUILoginView::GetProfile() const { | |
159 return NULL; | |
160 } | |
161 | |
162 void WebUILoginView::ExecuteBrowserCommand(int id) const { | |
163 } | |
164 | |
165 bool WebUILoginView::ShouldOpenButtonOptions( | |
166 const views::View* button_view) const { | |
167 if (button_view == status_area_->network_view()) | |
168 return true; | |
169 | |
170 if (button_view == status_area_->clock_view() || | |
171 button_view == status_area_->input_method_view()) | |
172 return false; | |
173 | |
174 return true; | |
175 } | |
176 | |
177 void WebUILoginView::OpenButtonOptions(const views::View* button_view) { | |
178 if (button_view == status_area_->network_view()) { | |
179 if (proxy_settings_dialog_.get() == NULL) { | |
180 proxy_settings_dialog_.reset(new ProxySettingsDialog( | |
181 this, GetNativeWindow())); | |
182 } | |
183 proxy_settings_dialog_->Show(); | |
184 } | |
185 } | |
186 | |
187 StatusAreaHost::ScreenMode WebUILoginView::GetScreenMode() const { | |
188 return kLoginMode; | |
189 } | |
190 | |
191 StatusAreaHost::TextStyle WebUILoginView::GetTextStyle() const { | |
192 return kWhitePlain; | |
193 } | |
194 | |
195 void WebUILoginView::OnDialogClosed() { | |
196 } | |
197 | |
198 void WebUILoginView::OnLocaleChanged() { | |
199 // Proxy settings dialog contains localized strings. | |
200 proxy_settings_dialog_.reset(); | |
201 SchedulePaint(); | |
202 } | |
203 | |
204 // WebUILoginView private: ----------------------------------------------------- | |
205 | |
206 void WebUILoginView::InitStatusArea() { | |
207 DCHECK(status_area_ == NULL); | |
208 status_area_ = new StatusAreaView(this); | |
209 status_area_->Init(); | |
210 AddChildView(status_area_); | |
211 } | |
212 | |
213 void WebUILoginView::UpdateWindowType() { | |
214 std::vector<int> params; | |
215 WmIpc::instance()->SetWindowType( | |
216 GTK_WIDGET(GetNativeWindow()), | |
217 WM_IPC_WINDOW_LOGIN_WEBUI, | |
218 ¶ms); | |
219 } | |
220 | |
221 void WebUILoginView::InitVirtualKeyboard() { | |
222 if (keyboard_) | |
223 return; | |
224 | |
225 keyboard_ = new KeyboardContainerView(profile_, NULL); | |
226 keyboard_->SetVisible(false); | |
227 AddChildView(keyboard_); | |
228 } | |
229 | |
230 void WebUILoginView::UpdateKeyboardAndLayout(bool should_show_keyboard) { | |
231 if (should_show_keyboard) | |
232 InitVirtualKeyboard(); | |
233 | |
234 if (should_show_keyboard == keyboard_showing_) | |
235 return; | |
236 | |
237 DCHECK(keyboard_); | |
238 | |
239 keyboard_showing_ = should_show_keyboard; | |
240 Layout(); | |
241 } | |
242 | |
243 WebUILoginView::VirtualKeyboardType | |
244 WebUILoginView::DecideKeyboardStateForView(views::View* view) { | |
245 if (!view) | |
246 return NONE; | |
247 | |
248 std::string cname = view->GetClassName(); | |
249 if (cname == views::Textfield::kViewClassName) { | |
250 return GENERIC; | |
251 } else if (cname == RenderWidgetHostViewViews::kViewClassName) { | |
252 TabContents* contents = webui_login_->tab_contents(); | |
253 bool* editable = contents ? GetFocusedStateAccessor()->GetProperty( | |
254 contents->property_bag()) : NULL; | |
255 if (editable && *editable) | |
256 return GENERIC; | |
257 } | |
258 return NONE; | |
259 } | |
260 | |
261 void WebUILoginView::Observe(NotificationType type, | |
262 const NotificationSource& source, | |
263 const NotificationDetails& details) { | |
264 if (type == NotificationType::FOCUS_CHANGED_IN_PAGE) { | |
265 // Only modify the keyboard state if the currently active tab sent the | |
266 // notification. | |
267 const TabContents* current_tab = webui_login_->tab_contents(); | |
268 TabContents* source_tab = Source<TabContents>(source).ptr(); | |
269 const bool editable = *Details<const bool>(details).ptr(); | |
270 | |
271 if (current_tab == source_tab && TabContentsHasFocus(source_tab)) | |
272 UpdateKeyboardAndLayout(editable); | |
273 | |
274 // Save the state of the focused field so that the keyboard visibility | |
275 // can be determined after tab switching. | |
276 GetFocusedStateAccessor()->SetProperty( | |
277 source_tab->property_bag(), editable); | |
278 } else if (type == NotificationType::TAB_CONTENTS_DESTROYED) { | |
279 GetFocusedStateAccessor()->DeleteProperty( | |
280 Source<TabContents>(source).ptr()->property_bag()); | |
281 } | |
282 } | |
283 | |
284 } // namespace chromeos | |
OLD | NEW |