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