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 "mojo/examples/window_manager/window_manager.mojom.h" | 7 #include "mojo/examples/window_manager/window_manager.mojom.h" |
8 #include "mojo/public/cpp/application/application.h" | 8 #include "mojo/public/cpp/application/application.h" |
9 #include "mojo/services/public/cpp/view_manager/view.h" | 9 #include "mojo/services/public/cpp/view_manager/view.h" |
10 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 10 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
11 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 11 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
12 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" | 12 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" |
13 #include "ui/events/event_constants.h" | 13 #include "ui/events/event_constants.h" |
14 | 14 |
15 #if defined CreateWindow | 15 #if defined CreateWindow |
16 #undef CreateWindow | 16 #undef CreateWindow |
17 #endif | 17 #endif |
18 | 18 |
| 19 using mojo::view_manager::TransportNodeId; |
| 20 using mojo::view_manager::View; |
| 21 using mojo::view_manager::ViewManager; |
| 22 using mojo::view_manager::ViewManagerDelegate; |
| 23 using mojo::view_manager::ViewObserver; |
| 24 using mojo::view_manager::ViewTreeNode; |
| 25 using mojo::view_manager::ViewTreeNodeObserver; |
| 26 |
19 namespace mojo { | 27 namespace mojo { |
20 namespace examples { | 28 namespace examples { |
21 | 29 |
22 class WindowManager; | 30 class WindowManager; |
23 | 31 |
24 class WindowManagerConnection : public InterfaceImpl<IWindowManager> { | 32 class WindowManagerConnection : public InterfaceImpl<IWindowManager> { |
25 public: | 33 public: |
26 explicit WindowManagerConnection(WindowManager* window_manager) | 34 explicit WindowManagerConnection(WindowManager* window_manager) |
27 : window_manager_(window_manager) {} | 35 : window_manager_(window_manager) {} |
28 virtual ~WindowManagerConnection() {} | 36 virtual ~WindowManagerConnection() {} |
29 | 37 |
30 private: | 38 private: |
31 // Overridden from IWindowManager: | 39 // Overridden from IWindowManager: |
32 virtual void CloseWindow(view_manager::TransportNodeId node_id) OVERRIDE; | 40 virtual void CloseWindow(TransportNodeId node_id) OVERRIDE; |
33 | 41 |
34 WindowManager* window_manager_; | 42 WindowManager* window_manager_; |
35 | 43 |
36 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnection); | 44 DISALLOW_COPY_AND_ASSIGN(WindowManagerConnection); |
37 }; | 45 }; |
38 | 46 |
39 class WindowManager : public Application, | 47 class WindowManager : public Application, |
40 public view_manager::ViewObserver { | 48 public ViewObserver, |
| 49 public ViewManagerDelegate { |
41 public: | 50 public: |
42 WindowManager() {} | 51 WindowManager() : view_manager_(NULL) {} |
43 virtual ~WindowManager() {} | 52 virtual ~WindowManager() {} |
44 | 53 |
45 void CloseWindow(view_manager::TransportNodeId node_id) { | 54 void CloseWindow(TransportNodeId node_id) { |
46 DCHECK(view_manager_); | 55 ViewTreeNode* node = view_manager_->GetNodeById(node_id); |
47 view_manager::ViewTreeNode* node = view_manager_->GetNodeById(node_id); | |
48 DCHECK(node); | 56 DCHECK(node); |
49 node->Destroy(); | 57 node->Destroy(); |
50 } | 58 } |
51 | 59 |
52 private: | 60 private: |
53 // Overridden from Application: | 61 // Overridden from Application: |
54 virtual void Initialize() MOJO_OVERRIDE { | 62 virtual void Initialize() MOJO_OVERRIDE { |
55 AddService<WindowManagerConnection>(this); | 63 AddService<WindowManagerConnection>(this); |
| 64 ViewManager::Create(this, this); |
| 65 } |
56 | 66 |
57 view_manager_ = view_manager::ViewManager::CreateBlocking(this); | 67 // Overridden from ViewObserver: |
58 view_manager::ViewTreeNode* node = | 68 virtual void OnViewInputEvent(View* view, EventPtr event) OVERRIDE { |
59 view_manager::ViewTreeNode::Create(view_manager_); | 69 if (event->action == ui::ET_MOUSE_RELEASED) { |
60 view_manager_->roots().front()->AddChild(node); | 70 if (event->flags & ui::EF_LEFT_MOUSE_BUTTON) |
| 71 CreateWindow("mojo:mojo_embedded_app"); |
| 72 else if (event->flags & ui::EF_RIGHT_MOUSE_BUTTON) |
| 73 CreateWindow("mojo:mojo_nesting_app"); |
| 74 } |
| 75 } |
| 76 |
| 77 // Overridden from ViewManagerDelegate: |
| 78 virtual void OnRootAdded(ViewManager* view_manager, |
| 79 ViewTreeNode* root) OVERRIDE { |
| 80 DCHECK(!view_manager_); |
| 81 view_manager_ = view_manager; |
| 82 |
| 83 ViewTreeNode* node = ViewTreeNode::Create(view_manager); |
| 84 view_manager->roots().front()->AddChild(node); |
61 node->SetBounds(gfx::Rect(800, 600)); | 85 node->SetBounds(gfx::Rect(800, 600)); |
62 parent_node_id_ = node->id(); | 86 parent_node_id_ = node->id(); |
63 | 87 |
64 view_manager::View* view = view_manager::View::Create(view_manager_); | 88 View* view = View::Create(view_manager); |
65 node->SetActiveView(view); | 89 node->SetActiveView(view); |
66 view->SetColor(SK_ColorBLUE); | 90 view->SetColor(SK_ColorBLUE); |
67 view->AddObserver(this); | 91 view->AddObserver(this); |
68 } | 92 } |
69 | 93 |
70 // Overridden from ViewObserver: | 94 void CreateWindow(const String& url) { |
71 virtual void OnViewInputEvent(view_manager::View* view, | 95 ViewTreeNode* node = view_manager_->GetNodeById(parent_node_id_); |
72 EventPtr event) OVERRIDE { | |
73 if (event->action == ui::ET_MOUSE_RELEASED) | |
74 CreateWindow(); | |
75 } | |
76 | |
77 void CreateWindow() { | |
78 view_manager::ViewTreeNode* node = | |
79 view_manager_->GetNodeById(parent_node_id_); | |
80 | 96 |
81 gfx::Rect bounds(50, 50, 200, 200); | 97 gfx::Rect bounds(50, 50, 200, 200); |
82 if (!node->children().empty()) { | 98 if (!node->children().empty()) { |
83 gfx::Point position = node->children().back()->bounds().origin(); | 99 gfx::Point position = node->children().back()->bounds().origin(); |
84 position.Offset(50, 50); | 100 position.Offset(50, 50); |
85 bounds.set_origin(position); | 101 bounds.set_origin(position); |
86 } | 102 } |
87 | 103 |
88 view_manager::ViewTreeNode* embedded = | 104 ViewTreeNode* embedded = ViewTreeNode::Create(view_manager_); |
89 view_manager::ViewTreeNode::Create(view_manager_); | |
90 node->AddChild(embedded); | 105 node->AddChild(embedded); |
91 embedded->SetBounds(bounds); | 106 embedded->SetBounds(bounds); |
92 embedded->Embed("mojo:mojo_embedded_app"); | 107 embedded->Embed(url); |
93 } | 108 } |
94 | 109 |
95 view_manager::ViewManager* view_manager_; | 110 ViewManager* view_manager_; |
96 view_manager::TransportNodeId parent_node_id_; | 111 TransportNodeId parent_node_id_; |
97 | 112 |
98 DISALLOW_COPY_AND_ASSIGN(WindowManager); | 113 DISALLOW_COPY_AND_ASSIGN(WindowManager); |
99 }; | 114 }; |
100 | 115 |
101 void WindowManagerConnection::CloseWindow( | 116 void WindowManagerConnection::CloseWindow(TransportNodeId node_id) { |
102 view_manager::TransportNodeId node_id) { | |
103 window_manager_->CloseWindow(node_id); | 117 window_manager_->CloseWindow(node_id); |
104 } | 118 } |
105 | 119 |
106 } // namespace examples | 120 } // namespace examples |
107 | 121 |
108 // static | 122 // static |
109 Application* Application::Create() { | 123 Application* Application::Create() { |
110 return new examples::WindowManager; | 124 return new examples::WindowManager; |
111 } | 125 } |
112 | 126 |
113 } // namespace mojo | 127 } // namespace mojo |
OLD | NEW |