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