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 "mojo/services/public/cpp/view_manager/view_tree_node_observer.h" | 13 #include "mojo/services/public/cpp/view_manager/view_tree_node_observer.h" |
14 #include "ui/events/event_constants.h" | 14 #include "ui/events/event_constants.h" |
15 | 15 |
16 using mojo::view_manager::View; | 16 using mojo::view_manager::View; |
17 using mojo::view_manager::ViewManager; | 17 using mojo::view_manager::ViewManager; |
| 18 using mojo::view_manager::ViewManagerDelegate; |
18 using mojo::view_manager::ViewObserver; | 19 using mojo::view_manager::ViewObserver; |
19 using mojo::view_manager::ViewTreeNode; | 20 using mojo::view_manager::ViewTreeNode; |
20 using mojo::view_manager::ViewTreeNodeObserver; | 21 using mojo::view_manager::ViewTreeNodeObserver; |
21 | 22 |
22 namespace mojo { | 23 namespace mojo { |
23 namespace examples { | 24 namespace examples { |
24 namespace { | 25 namespace { |
25 | 26 |
26 const SkColor kColors[] = { SK_ColorYELLOW, | 27 const SkColor kColors[] = { SK_ColorYELLOW, |
27 SK_ColorRED, | 28 SK_ColorRED, |
28 SK_ColorGREEN, | 29 SK_ColorGREEN, |
29 SK_ColorMAGENTA }; | 30 SK_ColorMAGENTA }; |
30 | 31 |
31 } // namespace | 32 } // namespace |
32 | 33 |
33 class EmbeddedApp : public Application, | 34 class EmbeddedApp : public Application, |
| 35 public ViewManagerDelegate, |
34 public ViewObserver, | 36 public ViewObserver, |
35 public ViewTreeNodeObserver { | 37 public ViewTreeNodeObserver { |
36 public: | 38 public: |
37 EmbeddedApp() : view_manager_(NULL) {} | 39 EmbeddedApp() {} |
38 virtual ~EmbeddedApp() {} | 40 virtual ~EmbeddedApp() {} |
39 | 41 |
40 private: | 42 private: |
41 // Overridden from Application: | 43 // Overridden from Application: |
42 virtual void Initialize() MOJO_OVERRIDE { | 44 virtual void Initialize() MOJO_OVERRIDE { |
43 ViewManager::Create(this, | 45 ViewManager::Create(this, this); |
44 base::Bind(&EmbeddedApp::OnRootAdded, base::Unretained(this)), | |
45 base::Bind(&EmbeddedApp::OnRootRemoved, base::Unretained(this))); | |
46 ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_); | 46 ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_); |
47 } | 47 } |
48 | 48 |
| 49 // Overridden from ViewManagerDelegate: |
| 50 virtual void OnRootAdded(ViewManager* view_manager, |
| 51 ViewTreeNode* root) OVERRIDE { |
| 52 View* view = View::Create(view_manager); |
| 53 view->AddObserver(this); |
| 54 root->SetActiveView(view); |
| 55 root->AddObserver(this); |
| 56 size_t index = view_manager->roots().size() - 1; |
| 57 view->SetColor(kColors[index % arraysize(kColors)]); |
| 58 } |
| 59 virtual void OnRootRemoved(ViewManager* view_manager, |
| 60 ViewTreeNode* root) OVERRIDE { |
| 61 std::map<ViewTreeNode*, View*>::const_iterator it = |
| 62 views_to_reap_.find(root); |
| 63 if (it != views_to_reap_.end()) |
| 64 it->second->Destroy(); |
| 65 } |
| 66 |
49 // Overridden from ViewObserver: | 67 // Overridden from ViewObserver: |
50 virtual void OnViewInputEvent(View* view, EventPtr event) OVERRIDE { | 68 virtual void OnViewInputEvent(View* view, EventPtr event) OVERRIDE { |
51 if (event->action == ui::ET_MOUSE_RELEASED) | 69 if (event->action == ui::ET_MOUSE_RELEASED) |
52 window_manager_->CloseWindow(view->node()->id()); | 70 window_manager_->CloseWindow(view->node()->id()); |
53 } | 71 } |
54 | 72 |
55 // Overridden from ViewTreeNodeObserver: | 73 // Overridden from ViewTreeNodeObserver: |
56 virtual void OnNodeActiveViewChange( | 74 virtual void OnNodeActiveViewChange( |
57 ViewTreeNode* node, | 75 ViewTreeNode* node, |
58 View* old_view, | 76 View* old_view, |
59 View* new_view, | 77 View* new_view, |
60 ViewTreeNodeObserver::DispositionChangePhase phase) OVERRIDE { | 78 ViewTreeNodeObserver::DispositionChangePhase phase) OVERRIDE { |
61 if (new_view == 0) | 79 if (new_view == 0) |
62 views_to_reap_[node] = old_view; | 80 views_to_reap_[node] = old_view; |
63 } | 81 } |
64 | 82 |
65 void OnRootAdded(ViewManager* view_manager, ViewTreeNode* root) { | |
66 if (!view_manager_) | |
67 view_manager_ = view_manager; | |
68 DCHECK_EQ(view_manager_, view_manager); | |
69 | |
70 View* view = View::Create(view_manager_); | |
71 view->AddObserver(this); | |
72 root->SetActiveView(view); | |
73 root->AddObserver(this); | |
74 size_t index = view_manager_->roots().size() - 1; | |
75 view->SetColor(kColors[index % arraysize(kColors)]); | |
76 } | |
77 | |
78 void OnRootRemoved(ViewManager* view_manager, | |
79 ViewTreeNode* root) { | |
80 std::map<ViewTreeNode*, View*>::const_iterator it = | |
81 views_to_reap_.find(root); | |
82 if (it != views_to_reap_.end()) | |
83 it->second->Destroy(); | |
84 } | |
85 | |
86 IWindowManagerPtr window_manager_; | 83 IWindowManagerPtr window_manager_; |
87 ViewManager* view_manager_; | |
88 std::map<ViewTreeNode*, View*> views_to_reap_; | 84 std::map<ViewTreeNode*, View*> views_to_reap_; |
89 | 85 |
90 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); | 86 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); |
91 }; | 87 }; |
92 | 88 |
93 } // namespace examples | 89 } // namespace examples |
94 | 90 |
95 // static | 91 // static |
96 Application* Application::Create() { | 92 Application* Application::Create() { |
97 return new examples::EmbeddedApp; | 93 return new examples::EmbeddedApp; |
98 } | 94 } |
99 | 95 |
100 } // namespace mojo | 96 } // namespace mojo |
OLD | NEW |