| 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 "mojo/examples/window_manager/window_manager.mojom.h" | 8 #include "mojo/examples/window_manager/window_manager.mojom.h" |
| 8 #include "mojo/public/cpp/application/application.h" | 9 #include "mojo/public/cpp/application/application.h" |
| 10 #include "mojo/services/navigation/navigation.mojom.h" |
| 9 #include "mojo/services/public/cpp/view_manager/view.h" | 11 #include "mojo/services/public/cpp/view_manager/view.h" |
| 10 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 12 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 11 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 13 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 12 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 14 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
| 13 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" | 15 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" |
| 14 #include "ui/events/event_constants.h" | 16 #include "ui/events/event_constants.h" |
| 15 | 17 |
| 16 #if defined CreateWindow | 18 #if defined CreateWindow |
| 17 #undef CreateWindow | 19 #undef CreateWindow |
| 18 #endif | 20 #endif |
| 19 | 21 |
| 20 using mojo::view_manager::Id; | 22 using mojo::view_manager::Id; |
| 21 using mojo::view_manager::View; | 23 using mojo::view_manager::View; |
| 22 using mojo::view_manager::ViewManager; | 24 using mojo::view_manager::ViewManager; |
| 23 using mojo::view_manager::ViewManagerDelegate; | 25 using mojo::view_manager::ViewManagerDelegate; |
| 24 using mojo::view_manager::ViewObserver; | 26 using mojo::view_manager::ViewObserver; |
| 25 using mojo::view_manager::ViewTreeNode; | 27 using mojo::view_manager::ViewTreeNode; |
| 26 using mojo::view_manager::ViewTreeNodeObserver; | 28 using mojo::view_manager::ViewTreeNodeObserver; |
| 27 | 29 |
| 28 namespace mojo { | 30 namespace mojo { |
| 29 namespace examples { | 31 namespace examples { |
| 30 | 32 |
| 31 class WindowManager; | 33 class WindowManager; |
| 32 | 34 |
| 35 namespace { |
| 36 |
| 37 const SkColor kColors[] = { SK_ColorYELLOW, |
| 38 SK_ColorRED, |
| 39 SK_ColorGREEN, |
| 40 SK_ColorMAGENTA }; |
| 41 |
| 42 const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app"; |
| 43 const char kNestingAppURL[] = "mojo:mojo_nesting_app"; |
| 44 const char kMojoBrowserURL[] = "mojo:mojo_browser"; |
| 45 |
| 46 } // namespace |
| 47 |
| 33 class WindowManagerConnection : public InterfaceImpl<IWindowManager> { | 48 class WindowManagerConnection : public InterfaceImpl<IWindowManager> { |
| 34 public: | 49 public: |
| 35 explicit WindowManagerConnection(WindowManager* window_manager) | 50 explicit WindowManagerConnection(WindowManager* window_manager) |
| 36 : window_manager_(window_manager) {} | 51 : window_manager_(window_manager) {} |
| 37 virtual ~WindowManagerConnection() {} | 52 virtual ~WindowManagerConnection() {} |
| 38 | 53 |
| 39 private: | 54 private: |
| 40 // Overridden from IWindowManager: | 55 // Overridden from IWindowManager: |
| 41 virtual void CloseWindow(Id node_id) OVERRIDE; | 56 virtual void CloseWindow(Id node_id) OVERRIDE; |
| 42 | 57 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 62 // Overridden from Application: | 77 // Overridden from Application: |
| 63 virtual void Initialize() MOJO_OVERRIDE { | 78 virtual void Initialize() MOJO_OVERRIDE { |
| 64 AddService<WindowManagerConnection>(this); | 79 AddService<WindowManagerConnection>(this); |
| 65 ViewManager::Create(this, this); | 80 ViewManager::Create(this, this); |
| 66 } | 81 } |
| 67 | 82 |
| 68 // Overridden from ViewObserver: | 83 // Overridden from ViewObserver: |
| 69 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { | 84 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { |
| 70 if (event->action == ui::ET_MOUSE_RELEASED) { | 85 if (event->action == ui::ET_MOUSE_RELEASED) { |
| 71 if (event->flags & ui::EF_LEFT_MOUSE_BUTTON) | 86 if (event->flags & ui::EF_LEFT_MOUSE_BUTTON) |
| 72 CreateWindow("mojo:mojo_embedded_app"); | 87 CreateWindow(kEmbeddedAppURL); |
| 73 else if (event->flags & ui::EF_RIGHT_MOUSE_BUTTON) | 88 else if (event->flags & ui::EF_RIGHT_MOUSE_BUTTON) |
| 74 CreateWindow("mojo:mojo_nesting_app"); | 89 CreateWindow(kNestingAppURL); |
| 75 else if (event->flags & ui::EF_MIDDLE_MOUSE_BUTTON) | 90 else if (event->flags & ui::EF_MIDDLE_MOUSE_BUTTON) |
| 76 CreateWindow("mojo:mojo_browser"); | 91 CreateWindow(kMojoBrowserURL); |
| 77 } | 92 } |
| 78 } | 93 } |
| 79 | 94 |
| 80 // Overridden from ViewManagerDelegate: | 95 // Overridden from ViewManagerDelegate: |
| 81 virtual void OnRootAdded(ViewManager* view_manager, | 96 virtual void OnRootAdded(ViewManager* view_manager, |
| 82 ViewTreeNode* root) OVERRIDE { | 97 ViewTreeNode* root) OVERRIDE { |
| 83 DCHECK(!view_manager_); | 98 DCHECK(!view_manager_); |
| 84 view_manager_ = view_manager; | 99 view_manager_ = view_manager; |
| 85 | 100 |
| 86 ViewTreeNode* node = ViewTreeNode::Create(view_manager); | 101 ViewTreeNode* node = ViewTreeNode::Create(view_manager); |
| 87 view_manager->GetRoots().front()->AddChild(node); | 102 view_manager->GetRoots().front()->AddChild(node); |
| 88 node->SetBounds(gfx::Rect(800, 600)); | 103 node->SetBounds(gfx::Rect(800, 600)); |
| 89 parent_node_id_ = node->id(); | 104 parent_node_id_ = node->id(); |
| 90 | 105 |
| 91 View* view = View::Create(view_manager); | 106 View* view = View::Create(view_manager); |
| 92 node->SetActiveView(view); | 107 node->SetActiveView(view); |
| 93 view->SetColor(SK_ColorBLUE); | 108 view->SetColor(SK_ColorBLUE); |
| 94 view->AddObserver(this); | 109 view->AddObserver(this); |
| 95 } | 110 } |
| 96 | 111 |
| 97 void CreateWindow(const String& url) { | 112 void CreateWindow(const std::string& url) { |
| 98 ViewTreeNode* node = view_manager_->GetNodeById(parent_node_id_); | 113 ViewTreeNode* node = view_manager_->GetNodeById(parent_node_id_); |
| 99 | 114 |
| 100 gfx::Rect bounds(50, 50, 200, 200); | 115 gfx::Rect bounds(50, 50, 200, 200); |
| 101 if (!node->children().empty()) { | 116 if (!node->children().empty()) { |
| 102 gfx::Point position = node->children().back()->bounds().origin(); | 117 gfx::Point position = node->children().back()->bounds().origin(); |
| 103 position.Offset(50, 50); | 118 position.Offset(50, 50); |
| 104 bounds.set_origin(position); | 119 bounds.set_origin(position); |
| 105 } | 120 } |
| 106 | 121 |
| 107 ViewTreeNode* embedded = ViewTreeNode::Create(view_manager_); | 122 ViewTreeNode* embedded = ViewTreeNode::Create(view_manager_); |
| 108 node->AddChild(embedded); | 123 node->AddChild(embedded); |
| 109 embedded->SetBounds(bounds); | 124 embedded->SetBounds(bounds); |
| 110 embedded->Embed(url); | 125 embedded->Embed(url); |
| 126 |
| 127 // 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 |
| 129 // to support navigation. |
| 130 if (url == kEmbeddedAppURL || url == kNestingAppURL) { |
| 131 // 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 |
| 133 // service provider that allows it to talk to embeddee. |
| 134 navigation::NavigatorPtr navigator; |
| 135 ConnectTo(url, &navigator); |
| 136 navigation::NavigationDetailsPtr details( |
| 137 navigation::NavigationDetails::New()); |
| 138 size_t index = node->children().size() - 1; |
| 139 details->url = base::StringPrintf( |
| 140 "%s/%x", kEmbeddedAppURL, kColors[index % arraysize(kColors)]); |
| 141 navigator->Navigate(embedded->id(), details.Pass()); |
| 142 } |
| 111 } | 143 } |
| 112 | 144 |
| 113 ViewManager* view_manager_; | 145 ViewManager* view_manager_; |
| 114 Id parent_node_id_; | 146 Id parent_node_id_; |
| 115 | 147 |
| 116 DISALLOW_COPY_AND_ASSIGN(WindowManager); | 148 DISALLOW_COPY_AND_ASSIGN(WindowManager); |
| 117 }; | 149 }; |
| 118 | 150 |
| 119 void WindowManagerConnection::CloseWindow(Id node_id) { | 151 void WindowManagerConnection::CloseWindow(Id node_id) { |
| 120 window_manager_->CloseWindow(node_id); | 152 window_manager_->CloseWindow(node_id); |
| 121 } | 153 } |
| 122 | 154 |
| 123 } // namespace examples | 155 } // namespace examples |
| 124 | 156 |
| 125 // static | 157 // static |
| 126 Application* Application::Create() { | 158 Application* Application::Create() { |
| 127 return new examples::WindowManager; | 159 return new examples::WindowManager; |
| 128 } | 160 } |
| 129 | 161 |
| 130 } // namespace mojo | 162 } // namespace mojo |
| OLD | NEW |