| 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "examples/keyboard/keyboard.mojom.h" | |
| 8 #include "examples/window_manager/debug_panel_host.mojom.h" | 7 #include "examples/window_manager/debug_panel_host.mojom.h" |
| 9 #include "examples/window_manager/window_manager.mojom.h" | 8 #include "examples/window_manager/window_manager.mojom.h" |
| 10 #include "mojo/application/application_runner_chromium.h" | 9 #include "mojo/application/application_runner_chromium.h" |
| 11 #include "mojo/converters/geometry/geometry_type_converters.h" | 10 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 12 #include "mojo/converters/input_events/input_events_type_converters.h" | 11 #include "mojo/converters/input_events/input_events_type_converters.h" |
| 13 #include "mojo/public/c/system/main.h" | 12 #include "mojo/public/c/system/main.h" |
| 14 #include "mojo/public/cpp/application/application_connection.h" | 13 #include "mojo/public/cpp/application/application_connection.h" |
| 15 #include "mojo/public/cpp/application/application_delegate.h" | 14 #include "mojo/public/cpp/application/application_delegate.h" |
| 16 #include "mojo/public/cpp/application/application_impl.h" | 15 #include "mojo/public/cpp/application/application_impl.h" |
| 17 #include "mojo/public/cpp/application/interface_factory_impl.h" | 16 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 | 49 |
| 51 class WindowManagerConnection : public InterfaceImpl<IWindowManager> { | 50 class WindowManagerConnection : public InterfaceImpl<IWindowManager> { |
| 52 public: | 51 public: |
| 53 explicit WindowManagerConnection(WindowManager* window_manager) | 52 explicit WindowManagerConnection(WindowManager* window_manager) |
| 54 : window_manager_(window_manager) {} | 53 : window_manager_(window_manager) {} |
| 55 virtual ~WindowManagerConnection() {} | 54 virtual ~WindowManagerConnection() {} |
| 56 | 55 |
| 57 private: | 56 private: |
| 58 // Overridden from IWindowManager: | 57 // Overridden from IWindowManager: |
| 59 virtual void CloseWindow(Id view_id) override; | 58 virtual void CloseWindow(Id view_id) override; |
| 60 virtual void ShowKeyboard(Id view_id, RectPtr bounds) override; | |
| 61 virtual void HideKeyboard(Id view_id) override; | |
| 62 | 59 |
| 63 WindowManager* window_manager_; | 60 WindowManager* window_manager_; |
| 64 | 61 |
| 65 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnection); | 62 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnection); |
| 66 }; | 63 }; |
| 67 | 64 |
| 68 class NavigatorHostImpl : public InterfaceImpl<NavigatorHost> { | 65 class NavigatorHostImpl : public InterfaceImpl<NavigatorHost> { |
| 69 public: | 66 public: |
| 70 explicit NavigatorHostImpl(WindowManager* window_manager, Id view_id) | 67 explicit NavigatorHostImpl(WindowManager* window_manager, Id view_id) |
| 71 : window_manager_(window_manager), view_id_(view_id) {} | 68 : window_manager_(window_manager), view_id_(view_id) {} |
| 72 virtual ~NavigatorHostImpl() { | 69 virtual ~NavigatorHostImpl() { |
| 73 } | 70 } |
| 74 | 71 |
| 75 private: | 72 private: |
| 76 virtual void DidNavigateLocally(const mojo::String& url) override; | 73 virtual void DidNavigateLocally(const mojo::String& url) override; |
| 77 virtual void RequestNavigate(Target target, URLRequestPtr request) override; | 74 virtual void RequestNavigate(Target target, URLRequestPtr request) override; |
| 78 | 75 |
| 79 WindowManager* window_manager_; | 76 WindowManager* window_manager_; |
| 80 Id view_id_; | 77 Id view_id_; |
| 81 | 78 |
| 82 DISALLOW_COPY_AND_ASSIGN(NavigatorHostImpl); | 79 DISALLOW_COPY_AND_ASSIGN(NavigatorHostImpl); |
| 83 }; | 80 }; |
| 84 | 81 |
| 85 class KeyboardManager : public KeyboardClient, | |
| 86 public ViewObserver { | |
| 87 public: | |
| 88 KeyboardManager() : view_manager_(NULL), view_(NULL) { | |
| 89 } | |
| 90 virtual ~KeyboardManager() { | |
| 91 if (view_) | |
| 92 view_->parent()->RemoveObserver(this); | |
| 93 } | |
| 94 | |
| 95 View* view() { return view_; } | |
| 96 | |
| 97 void Init(ApplicationImpl* application, | |
| 98 ViewManager* view_manager, | |
| 99 View* parent, | |
| 100 const Rect& bounds) { | |
| 101 view_manager_ = view_manager; | |
| 102 view_ = view_manager->CreateView(); | |
| 103 view_->SetBounds(bounds); | |
| 104 parent->AddChild(view_); | |
| 105 view_->Embed("mojo:keyboard"); | |
| 106 application->ConnectToService("mojo:keyboard", &keyboard_service_); | |
| 107 keyboard_service_.set_client(this); | |
| 108 parent->AddObserver(this); | |
| 109 } | |
| 110 | |
| 111 void Show(Id view_id, const Rect& bounds) { | |
| 112 keyboard_service_->SetTarget(view_id); | |
| 113 view_->SetVisible(true); | |
| 114 } | |
| 115 | |
| 116 void Hide(Id view_id) { | |
| 117 keyboard_service_->SetTarget(0); | |
| 118 view_->SetVisible(false); | |
| 119 } | |
| 120 | |
| 121 private: | |
| 122 // KeyboardClient: | |
| 123 virtual void OnKeyboardEvent(Id view_id, | |
| 124 int32_t code, | |
| 125 int32_t flags) override { | |
| 126 // TODO(sky): figure this out. Code use to dispatch events, but that's a | |
| 127 // hack. Instead strings should be passed through, or maybe a richer text | |
| 128 // input interface. | |
| 129 } | |
| 130 | |
| 131 // Overridden from ViewObserver: | |
| 132 virtual void OnViewBoundsChanged(View* parent, | |
| 133 const Rect& old_bounds, | |
| 134 const Rect& new_bounds) override { | |
| 135 Rect keyboard_bounds(view_->bounds()); | |
| 136 keyboard_bounds.y = | |
| 137 new_bounds.y + new_bounds.height - keyboard_bounds.height; | |
| 138 keyboard_bounds.width = | |
| 139 keyboard_bounds.width + new_bounds.width - old_bounds.width; | |
| 140 view_->SetBounds(keyboard_bounds); | |
| 141 } | |
| 142 virtual void OnViewDestroyed(View* parent) override { | |
| 143 DCHECK_EQ(parent, view_->parent()); | |
| 144 parent->RemoveObserver(this); | |
| 145 view_ = NULL; | |
| 146 } | |
| 147 | |
| 148 KeyboardServicePtr keyboard_service_; | |
| 149 ViewManager* view_manager_; | |
| 150 | |
| 151 // View the keyboard is attached to. | |
| 152 View* view_; | |
| 153 | |
| 154 DISALLOW_COPY_AND_ASSIGN(KeyboardManager); | |
| 155 }; | |
| 156 | |
| 157 class RootLayoutManager : public ViewObserver { | 82 class RootLayoutManager : public ViewObserver { |
| 158 public: | 83 public: |
| 159 RootLayoutManager(ViewManager* view_manager, | 84 RootLayoutManager(ViewManager* view_manager, |
| 160 View* root, | 85 View* root, |
| 161 Id content_view_id, | 86 Id content_view_id, |
| 162 Id launcher_ui_view_id, | 87 Id launcher_ui_view_id, |
| 163 Id control_panel_view_id) | 88 Id control_panel_view_id) |
| 164 : root_(root), | 89 : root_(root), |
| 165 view_manager_(view_manager), | 90 view_manager_(view_manager), |
| 166 content_view_id_(content_view_id), | 91 content_view_id_(content_view_id), |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 } | 204 } |
| 280 | 205 |
| 281 void CloseWindow(Id view_id) { | 206 void CloseWindow(Id view_id) { |
| 282 WindowVector::iterator iter = GetWindowByViewId(view_id); | 207 WindowVector::iterator iter = GetWindowByViewId(view_id); |
| 283 DCHECK(iter != windows_.end()); | 208 DCHECK(iter != windows_.end()); |
| 284 Window* window = *iter; | 209 Window* window = *iter; |
| 285 windows_.erase(iter); | 210 windows_.erase(iter); |
| 286 window->view()->Destroy(); | 211 window->view()->Destroy(); |
| 287 } | 212 } |
| 288 | 213 |
| 289 void ShowKeyboard(Id view_id, const Rect& bounds) { | |
| 290 // TODO: this needs to validate |view_id|. That is, it shouldn't assume | |
| 291 // |view_id| is valid and it also needs to make sure the client that sent | |
| 292 // this really owns |view_id|. | |
| 293 // TODO: honor |bounds|. | |
| 294 if (!keyboard_manager_) { | |
| 295 keyboard_manager_.reset(new KeyboardManager); | |
| 296 View* parent = view_manager_->GetRoot(); | |
| 297 int ideal_height = 200; | |
| 298 // TODO(sky): 10 is a bit of a hack here. There is a bug that causes | |
| 299 // white strips to appear when 0 is used. Figure this out! | |
| 300 Rect keyboard_bounds; | |
| 301 keyboard_bounds.x = 10; | |
| 302 keyboard_bounds.y = parent->bounds().height - ideal_height; | |
| 303 keyboard_bounds.width = parent->bounds().width - 20; | |
| 304 keyboard_bounds.height = ideal_height; | |
| 305 keyboard_manager_->Init(app_, view_manager_, parent, keyboard_bounds); | |
| 306 } | |
| 307 keyboard_manager_->Show(view_id, bounds); | |
| 308 } | |
| 309 | |
| 310 void HideKeyboard(Id view_id) { | |
| 311 // See comment in ShowKeyboard() about validating args. | |
| 312 if (keyboard_manager_) | |
| 313 keyboard_manager_->Hide(view_id); | |
| 314 } | |
| 315 | 214 |
| 316 void DidNavigateLocally(uint32 source_view_id, const mojo::String& url) { | 215 void DidNavigateLocally(uint32 source_view_id, const mojo::String& url) { |
| 317 LOG(ERROR) << "DidNavigateLocally: source_view_id: " << source_view_id | 216 LOG(ERROR) << "DidNavigateLocally: source_view_id: " << source_view_id |
| 318 << " url: " << url.To<std::string>(); | 217 << " url: " << url.To<std::string>(); |
| 319 } | 218 } |
| 320 | 219 |
| 321 void RequestNavigate(uint32 source_view_id, | 220 void RequestNavigate(uint32 source_view_id, |
| 322 Target target, | 221 Target target, |
| 323 URLRequestPtr request) { | 222 URLRequestPtr request) { |
| 324 OnLaunch(source_view_id, target, request->url); | 223 OnLaunch(source_view_id, target, request->url); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 const String& url, | 306 const String& url, |
| 408 InterfaceRequest<ServiceProvider> service_provider) override { | 307 InterfaceRequest<ServiceProvider> service_provider) override { |
| 409 const Id kInvalidSourceViewId = 0; | 308 const Id kInvalidSourceViewId = 0; |
| 410 OnLaunch(kInvalidSourceViewId, TARGET_DEFAULT, url); | 309 OnLaunch(kInvalidSourceViewId, TARGET_DEFAULT, url); |
| 411 } | 310 } |
| 412 | 311 |
| 413 // Overridden from ui::EventHandler: | 312 // Overridden from ui::EventHandler: |
| 414 virtual void OnEvent(ui::Event* event) override { | 313 virtual void OnEvent(ui::Event* event) override { |
| 415 View* view = | 314 View* view = |
| 416 static_cast<window_manager::ViewTarget*>(event->target())->view(); | 315 static_cast<window_manager::ViewTarget*>(event->target())->view(); |
| 417 if (event->type() == ui::ET_MOUSE_PRESSED && | 316 if (event->type() == ui::ET_MOUSE_PRESSED) |
| 418 !IsDescendantOfKeyboard(view)) { | |
| 419 view->SetFocus(); | 317 view->SetFocus(); |
| 420 } | |
| 421 } | 318 } |
| 422 | 319 |
| 423 void OnLaunch(uint32 source_view_id, | 320 void OnLaunch(uint32 source_view_id, |
| 424 Target requested_target, | 321 Target requested_target, |
| 425 const mojo::String& url) { | 322 const mojo::String& url) { |
| 426 Target target = navigation_target_; | 323 Target target = navigation_target_; |
| 427 if (target == TARGET_DEFAULT) { | 324 if (target == TARGET_DEFAULT) { |
| 428 if (requested_target != TARGET_DEFAULT) { | 325 if (requested_target != TARGET_DEFAULT) { |
| 429 target = requested_target; | 326 target = requested_target; |
| 430 } else { | 327 } else { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 Window* CreateWindow(const Rect& bounds) { | 380 Window* CreateWindow(const Rect& bounds) { |
| 484 View* content = view_manager_->GetViewById(content_view_id_); | 381 View* content = view_manager_->GetViewById(content_view_id_); |
| 485 View* view = view_manager_->CreateView(); | 382 View* view = view_manager_->CreateView(); |
| 486 content->AddChild(view); | 383 content->AddChild(view); |
| 487 view->SetBounds(bounds); | 384 view->SetBounds(bounds); |
| 488 view->SetVisible(true); | 385 view->SetVisible(true); |
| 489 view->SetFocus(); | 386 view->SetFocus(); |
| 490 return new Window(this, view); | 387 return new Window(this, view); |
| 491 } | 388 } |
| 492 | 389 |
| 493 bool IsDescendantOfKeyboard(View* target) { | |
| 494 return keyboard_manager_.get() && | |
| 495 keyboard_manager_->view()->Contains(target); | |
| 496 } | |
| 497 | |
| 498 Id CreateControlPanel(View* root) { | 390 Id CreateControlPanel(View* root) { |
| 499 View* view = view_manager_->CreateView(); | 391 View* view = view_manager_->CreateView(); |
| 500 root->AddChild(view); | 392 root->AddChild(view); |
| 501 | 393 |
| 502 Rect bounds; | 394 Rect bounds; |
| 503 bounds.x = root->bounds().width - kControlPanelWidth - kBorderInset; | 395 bounds.x = root->bounds().width - kControlPanelWidth - kBorderInset; |
| 504 bounds.y = kBorderInset * 2 + kTextfieldHeight; | 396 bounds.y = kBorderInset * 2 + kTextfieldHeight; |
| 505 bounds.width = kControlPanelWidth; | 397 bounds.width = kControlPanelWidth; |
| 506 bounds.height = | 398 bounds.height = |
| 507 root->bounds().height - kBorderInset * 3 - kTextfieldHeight; | 399 root->bounds().height - kBorderInset * 3 - kTextfieldHeight; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 535 window_manager_factory_; | 427 window_manager_factory_; |
| 536 | 428 |
| 537 scoped_ptr<mojo::ServiceProvider> debug_panel_; | 429 scoped_ptr<mojo::ServiceProvider> debug_panel_; |
| 538 Window* launcher_ui_; | 430 Window* launcher_ui_; |
| 539 WindowVector windows_; | 431 WindowVector windows_; |
| 540 ViewManager* view_manager_; | 432 ViewManager* view_manager_; |
| 541 scoped_ptr<RootLayoutManager> root_layout_manager_; | 433 scoped_ptr<RootLayoutManager> root_layout_manager_; |
| 542 | 434 |
| 543 scoped_ptr<window_manager::WindowManagerApp> window_manager_app_; | 435 scoped_ptr<window_manager::WindowManagerApp> window_manager_app_; |
| 544 | 436 |
| 545 // Id of the view most content is added to. The keyboard is NOT added here. | 437 // Id of the view most content is added to. |
| 546 Id content_view_id_; | 438 Id content_view_id_; |
| 547 | 439 |
| 548 // Id of the debug panel. | 440 // Id of the debug panel. |
| 549 Id control_panel_id_; | 441 Id control_panel_id_; |
| 550 | 442 |
| 551 GURL url_; | 443 GURL url_; |
| 552 Target navigation_target_; | 444 Target navigation_target_; |
| 553 | 445 |
| 554 scoped_ptr<KeyboardManager> keyboard_manager_; | |
| 555 ApplicationImpl* app_; | 446 ApplicationImpl* app_; |
| 556 | 447 |
| 557 mojo::Binding<examples::DebugPanelHost> binding_; | 448 mojo::Binding<examples::DebugPanelHost> binding_; |
| 558 | 449 |
| 559 DISALLOW_COPY_AND_ASSIGN(WindowManager); | 450 DISALLOW_COPY_AND_ASSIGN(WindowManager); |
| 560 }; | 451 }; |
| 561 | 452 |
| 562 void WindowManagerConnection::CloseWindow(Id view_id) { | 453 void WindowManagerConnection::CloseWindow(Id view_id) { |
| 563 window_manager_->CloseWindow(view_id); | 454 window_manager_->CloseWindow(view_id); |
| 564 } | 455 } |
| 565 | 456 |
| 566 void WindowManagerConnection::ShowKeyboard(Id view_id, RectPtr bounds) { | |
| 567 window_manager_->ShowKeyboard(view_id, *bounds); | |
| 568 } | |
| 569 | |
| 570 void WindowManagerConnection::HideKeyboard(Id view_id) { | |
| 571 window_manager_->HideKeyboard(view_id); | |
| 572 } | |
| 573 | |
| 574 void NavigatorHostImpl::DidNavigateLocally(const mojo::String& url) { | 457 void NavigatorHostImpl::DidNavigateLocally(const mojo::String& url) { |
| 575 window_manager_->DidNavigateLocally(view_id_, url); | 458 window_manager_->DidNavigateLocally(view_id_, url); |
| 576 } | 459 } |
| 577 | 460 |
| 578 void NavigatorHostImpl::RequestNavigate(Target target, URLRequestPtr request) { | 461 void NavigatorHostImpl::RequestNavigate(Target target, URLRequestPtr request) { |
| 579 window_manager_->RequestNavigate(view_id_, target, request.Pass()); | 462 window_manager_->RequestNavigate(view_id_, target, request.Pass()); |
| 580 } | 463 } |
| 581 | 464 |
| 582 } // namespace examples | 465 } // namespace examples |
| 583 } // namespace mojo | 466 } // namespace mojo |
| 584 | 467 |
| 585 MojoResult MojoMain(MojoHandle shell_handle) { | 468 MojoResult MojoMain(MojoHandle shell_handle) { |
| 586 mojo::ApplicationRunnerChromium runner(new mojo::examples::WindowManager); | 469 mojo::ApplicationRunnerChromium runner(new mojo::examples::WindowManager); |
| 587 return runner.Run(shell_handle); | 470 return runner.Run(shell_handle); |
| 588 } | 471 } |
| OLD | NEW |