| 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/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "mojo/examples/window_manager/window_manager.mojom.h" | 8 #include "mojo/examples/window_manager/window_manager.mojom.h" |
| 9 #include "mojo/public/cpp/application/application.h" | 9 #include "mojo/public/cpp/application/application.h" |
| 10 #include "mojo/services/navigation/navigation.mojom.h" | 10 #include "mojo/services/navigation/navigation.mojom.h" |
| 11 #include "mojo/services/public/cpp/view_manager/node.h" |
| 11 #include "mojo/services/public/cpp/view_manager/view.h" | 12 #include "mojo/services/public/cpp/view_manager/view.h" |
| 12 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 13 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 13 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 14 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 14 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 15 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
| 15 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" | |
| 16 #include "ui/events/event_constants.h" | 16 #include "ui/events/event_constants.h" |
| 17 | 17 |
| 18 #if defined CreateWindow | 18 #if defined CreateWindow |
| 19 #undef CreateWindow | 19 #undef CreateWindow |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 using mojo::view_manager::Id; | 22 using mojo::view_manager::Id; |
| 23 using mojo::view_manager::Node; |
| 24 using mojo::view_manager::NodeObserver; |
| 23 using mojo::view_manager::View; | 25 using mojo::view_manager::View; |
| 24 using mojo::view_manager::ViewManager; | 26 using mojo::view_manager::ViewManager; |
| 25 using mojo::view_manager::ViewManagerDelegate; | 27 using mojo::view_manager::ViewManagerDelegate; |
| 26 using mojo::view_manager::ViewObserver; | 28 using mojo::view_manager::ViewObserver; |
| 27 using mojo::view_manager::ViewTreeNode; | |
| 28 using mojo::view_manager::ViewTreeNodeObserver; | |
| 29 | 29 |
| 30 namespace mojo { | 30 namespace mojo { |
| 31 namespace examples { | 31 namespace examples { |
| 32 | 32 |
| 33 class WindowManager; | 33 class WindowManager; |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 const SkColor kColors[] = { SK_ColorYELLOW, | 37 const SkColor kColors[] = { SK_ColorYELLOW, |
| 38 SK_ColorRED, | 38 SK_ColorRED, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 class WindowManager : public Application, | 63 class WindowManager : public Application, |
| 64 public ViewObserver, | 64 public ViewObserver, |
| 65 public ViewManagerDelegate { | 65 public ViewManagerDelegate { |
| 66 public: | 66 public: |
| 67 WindowManager() : view_manager_(NULL) {} | 67 WindowManager() : view_manager_(NULL) {} |
| 68 virtual ~WindowManager() {} | 68 virtual ~WindowManager() {} |
| 69 | 69 |
| 70 void CloseWindow(Id node_id) { | 70 void CloseWindow(Id node_id) { |
| 71 ViewTreeNode* node = view_manager_->GetNodeById(node_id); | 71 Node* node = view_manager_->GetNodeById(node_id); |
| 72 DCHECK(node); | 72 DCHECK(node); |
| 73 node->Destroy(); | 73 node->Destroy(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 private: | 76 private: |
| 77 // Overridden from Application: | 77 // Overridden from Application: |
| 78 virtual void Initialize() MOJO_OVERRIDE { | 78 virtual void Initialize() MOJO_OVERRIDE { |
| 79 AddService<WindowManagerConnection>(this); | 79 AddService<WindowManagerConnection>(this); |
| 80 ViewManager::Create(this, this); | 80 ViewManager::Create(this, this); |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Overridden from ViewObserver: | 83 // Overridden from ViewObserver: |
| 84 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { | 84 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { |
| 85 if (event->action == ui::ET_MOUSE_RELEASED) { | 85 if (event->action == ui::ET_MOUSE_RELEASED) { |
| 86 if (event->flags & ui::EF_LEFT_MOUSE_BUTTON) | 86 if (event->flags & ui::EF_LEFT_MOUSE_BUTTON) |
| 87 CreateWindow(kEmbeddedAppURL); | 87 CreateWindow(kEmbeddedAppURL); |
| 88 else if (event->flags & ui::EF_RIGHT_MOUSE_BUTTON) | 88 else if (event->flags & ui::EF_RIGHT_MOUSE_BUTTON) |
| 89 CreateWindow(kNestingAppURL); | 89 CreateWindow(kNestingAppURL); |
| 90 else if (event->flags & ui::EF_MIDDLE_MOUSE_BUTTON) | 90 else if (event->flags & ui::EF_MIDDLE_MOUSE_BUTTON) |
| 91 CreateWindow(kMojoBrowserURL); | 91 CreateWindow(kMojoBrowserURL); |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 | 94 |
| 95 // Overridden from ViewManagerDelegate: | 95 // Overridden from ViewManagerDelegate: |
| 96 virtual void OnRootAdded(ViewManager* view_manager, | 96 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { |
| 97 ViewTreeNode* root) OVERRIDE { | |
| 98 DCHECK(!view_manager_); | 97 DCHECK(!view_manager_); |
| 99 view_manager_ = view_manager; | 98 view_manager_ = view_manager; |
| 100 | 99 |
| 101 ViewTreeNode* node = ViewTreeNode::Create(view_manager); | 100 Node* node = Node::Create(view_manager); |
| 102 view_manager->GetRoots().front()->AddChild(node); | 101 view_manager->GetRoots().front()->AddChild(node); |
| 103 node->SetBounds(gfx::Rect(800, 600)); | 102 node->SetBounds(gfx::Rect(800, 600)); |
| 104 parent_node_id_ = node->id(); | 103 parent_node_id_ = node->id(); |
| 105 | 104 |
| 106 View* view = View::Create(view_manager); | 105 View* view = View::Create(view_manager); |
| 107 node->SetActiveView(view); | 106 node->SetActiveView(view); |
| 108 view->SetColor(SK_ColorBLUE); | 107 view->SetColor(SK_ColorBLUE); |
| 109 view->AddObserver(this); | 108 view->AddObserver(this); |
| 110 } | 109 } |
| 111 | 110 |
| 112 void CreateWindow(const std::string& url) { | 111 void CreateWindow(const std::string& url) { |
| 113 ViewTreeNode* node = view_manager_->GetNodeById(parent_node_id_); | 112 Node* node = view_manager_->GetNodeById(parent_node_id_); |
| 114 | 113 |
| 115 gfx::Rect bounds(50, 50, 400, 400); | 114 gfx::Rect bounds(50, 50, 400, 400); |
| 116 if (!node->children().empty()) { | 115 if (!node->children().empty()) { |
| 117 gfx::Point position = node->children().back()->bounds().origin(); | 116 gfx::Point position = node->children().back()->bounds().origin(); |
| 118 position.Offset(50, 50); | 117 position.Offset(50, 50); |
| 119 bounds.set_origin(position); | 118 bounds.set_origin(position); |
| 120 } | 119 } |
| 121 | 120 |
| 122 ViewTreeNode* embedded = ViewTreeNode::Create(view_manager_); | 121 Node* embedded = Node::Create(view_manager_); |
| 123 node->AddChild(embedded); | 122 node->AddChild(embedded); |
| 124 embedded->SetBounds(bounds); | 123 embedded->SetBounds(bounds); |
| 125 embedded->Embed(url); | 124 embedded->Embed(url); |
| 126 | 125 |
| 127 // TODO(aa): Is there a way to ask for an interface and test whether it | 126 // TODO(aa): Is there a way to ask for an interface and test whether it |
| 128 // succeeded? That would be nicer than hard-coding the URLs that are known | 127 // succeeded? That would be nicer than hard-coding the URLs that are known |
| 129 // to support navigation. | 128 // to support navigation. |
| 130 if (url == kEmbeddedAppURL || url == kNestingAppURL) { | 129 if (url == kEmbeddedAppURL || url == kNestingAppURL) { |
| 131 // TODO(aa): This means that there can only ever be one instance of every | 130 // TODO(aa): This means that there can only ever be one instance of every |
| 132 // app, which seems wrong. Instead, perhaps embedder should get back a | 131 // app, which seems wrong. Instead, perhaps embedder should get back a |
| (...skipping 20 matching lines...) Expand all Loading... |
| 153 } | 152 } |
| 154 | 153 |
| 155 } // namespace examples | 154 } // namespace examples |
| 156 | 155 |
| 157 // static | 156 // static |
| 158 Application* Application::Create() { | 157 Application* Application::Create() { |
| 159 return new examples::WindowManager; | 158 return new examples::WindowManager; |
| 160 } | 159 } |
| 161 | 160 |
| 162 } // namespace mojo | 161 } // namespace mojo |
| OLD | NEW |