OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/macros.h" | 5 #include "base/macros.h" |
6 #include "base/strings/string_util.h" | 6 #include "base/strings/string_util.h" |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "examples/window_manager/window_manager.mojom.h" | 8 #include "examples/window_manager/window_manager.mojom.h" |
9 #include "mojo/application/application_runner_chromium.h" | 9 #include "mojo/application/application_runner_chromium.h" |
10 #include "mojo/common/common_type_converters.h" | 10 #include "mojo/common/common_type_converters.h" |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 DISALLOW_COPY_AND_ASSIGN(KeyboardManager); | 147 DISALLOW_COPY_AND_ASSIGN(KeyboardManager); |
148 }; | 148 }; |
149 | 149 |
150 // This is the basics of creating a views widget with a textfield. | 150 // This is the basics of creating a views widget with a textfield. |
151 // TODO: cleanup! | 151 // TODO: cleanup! |
152 class Browser : public ApplicationDelegate, | 152 class Browser : public ApplicationDelegate, |
153 public ViewManagerDelegate, | 153 public ViewManagerDelegate, |
154 public views::TextfieldController, | 154 public views::TextfieldController, |
155 public ViewObserver { | 155 public ViewObserver { |
156 public: | 156 public: |
157 Browser() | 157 Browser() : shell_(nullptr), root_(NULL), widget_(NULL) {} |
158 : shell_(nullptr), view_manager_(NULL), root_(NULL), widget_(NULL) {} | |
159 | 158 |
160 virtual ~Browser() { | 159 virtual ~Browser() { |
161 if (root_) | 160 if (root_) |
162 root_->RemoveObserver(this); | 161 root_->RemoveObserver(this); |
163 } | 162 } |
164 | 163 |
165 private: | 164 private: |
166 // Overridden from ApplicationDelegate: | 165 // Overridden from ApplicationDelegate: |
167 virtual void Initialize(ApplicationImpl* app) override { | 166 virtual void Initialize(ApplicationImpl* app) override { |
168 shell_ = app->shell(); | 167 shell_ = app->shell(); |
(...skipping 27 matching lines...) Expand all Loading... |
196 params.delegate = widget_delegate; | 195 params.delegate = widget_delegate; |
197 params.bounds = gfx::Rect(view->bounds().width, view->bounds().height); | 196 params.bounds = gfx::Rect(view->bounds().width, view->bounds().height); |
198 widget_->Init(params); | 197 widget_->Init(params); |
199 // KeyboardManager handles deleting itself when the widget is destroyed. | 198 // KeyboardManager handles deleting itself when the widget is destroyed. |
200 new KeyboardManager(widget_, window_manager_.get(), view); | 199 new KeyboardManager(widget_, window_manager_.get(), view); |
201 widget_->Show(); | 200 widget_->Show(); |
202 textfield->RequestFocus(); | 201 textfield->RequestFocus(); |
203 } | 202 } |
204 | 203 |
205 // ViewManagerDelegate: | 204 // ViewManagerDelegate: |
206 virtual void OnEmbed(ViewManager* view_manager, | 205 virtual void OnEmbed(View* root, |
207 View* root, | |
208 ServiceProviderImpl* exported_services, | 206 ServiceProviderImpl* exported_services, |
209 scoped_ptr<ServiceProvider> imported_services) override { | 207 scoped_ptr<ServiceProvider> imported_services) override { |
210 // TODO: deal with OnEmbed() being invoked multiple times. | 208 // TODO: deal with OnEmbed() being invoked multiple times. |
211 ConnectToService(imported_services.get(), &navigator_host_); | 209 ConnectToService(imported_services.get(), &navigator_host_); |
212 view_manager_ = view_manager; | |
213 root_ = root; | 210 root_ = root; |
214 root_->AddObserver(this); | 211 root_->AddObserver(this); |
215 root_->SetFocus(); | 212 root_->SetFocus(); |
216 CreateWidget(root_); | 213 CreateWidget(root_); |
217 } | 214 } |
218 virtual void OnViewManagerDisconnected( | 215 virtual void OnViewManagerDisconnected( |
219 ViewManager* view_manager) override { | 216 ViewManager* view_manager) override { |
220 DCHECK_EQ(view_manager_, view_manager); | |
221 view_manager_ = NULL; | |
222 base::MessageLoop::current()->Quit(); | 217 base::MessageLoop::current()->Quit(); |
223 } | 218 } |
224 | 219 |
225 // views::TextfieldController: | 220 // views::TextfieldController: |
226 virtual bool HandleKeyEvent(views::Textfield* sender, | 221 virtual bool HandleKeyEvent(views::Textfield* sender, |
227 const ui::KeyEvent& key_event) override { | 222 const ui::KeyEvent& key_event) override { |
228 if (key_event.key_code() == ui::VKEY_RETURN) { | 223 if (key_event.key_code() == ui::VKEY_RETURN) { |
229 GURL url(sender->text()); | 224 GURL url(sender->text()); |
230 if (!url.is_valid()) { | 225 if (!url.is_valid()) { |
231 LOG(ERROR) << "Not a valid URL: " << sender->text(); | 226 LOG(ERROR) << "Not a valid URL: " << sender->text(); |
(...skipping 20 matching lines...) Expand all Loading... |
252 virtual void OnViewDestroyed(View* view) override { | 247 virtual void OnViewDestroyed(View* view) override { |
253 DCHECK_EQ(root_, view); | 248 DCHECK_EQ(root_, view); |
254 view->RemoveObserver(this); | 249 view->RemoveObserver(this); |
255 root_ = NULL; | 250 root_ = NULL; |
256 } | 251 } |
257 | 252 |
258 Shell* shell_; | 253 Shell* shell_; |
259 | 254 |
260 scoped_ptr<ViewsInit> views_init_; | 255 scoped_ptr<ViewsInit> views_init_; |
261 | 256 |
262 ViewManager* view_manager_; | |
263 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; | 257 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; |
264 View* root_; | 258 View* root_; |
265 views::Widget* widget_; | 259 views::Widget* widget_; |
266 NavigatorHostPtr navigator_host_; | 260 NavigatorHostPtr navigator_host_; |
267 IWindowManagerPtr window_manager_; | 261 IWindowManagerPtr window_manager_; |
268 | 262 |
269 DISALLOW_COPY_AND_ASSIGN(Browser); | 263 DISALLOW_COPY_AND_ASSIGN(Browser); |
270 }; | 264 }; |
271 | 265 |
272 } // namespace examples | 266 } // namespace examples |
273 } // namespace mojo | 267 } // namespace mojo |
274 | 268 |
275 MojoResult MojoMain(MojoHandle shell_handle) { | 269 MojoResult MojoMain(MojoHandle shell_handle) { |
276 mojo::ApplicationRunnerChromium runner(new mojo::examples::Browser); | 270 mojo::ApplicationRunnerChromium runner(new mojo::examples::Browser); |
277 return runner.Run(shell_handle); | 271 return runner.Run(shell_handle); |
278 } | 272 } |
OLD | NEW |