Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: mojo/examples/embedded_app/embedded_app.cc

Issue 311423003: Provide an API to the example window manager app that supports closing a window. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/examples/embedded_app/DEPS ('k') | mojo/examples/window_manager/DEPS » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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() : view_manager_(NULL) {}
18 virtual ~EmbeddedApp() {} 38 virtual ~EmbeddedApp() {}
19 39
20 private: 40 private:
21 // Overridden from Application: 41 // Overridden from Application:
22 virtual void Initialize() MOJO_OVERRIDE { 42 virtual void Initialize() MOJO_OVERRIDE {
23 view_manager::ViewManager::Create(this, 43 ViewManager::Create(this,
24 base::Bind(&EmbeddedApp::OnRootAdded, base::Unretained(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_);
25 } 47 }
26 48
27 void OnRootAdded(view_manager::ViewManager* view_manager) { 49 // Overridden from ViewObserver:
50 virtual void OnViewInputEvent(View* view, EventPtr event) OVERRIDE {
51 if (event->action == ui::ET_MOUSE_RELEASED)
52 window_manager_->CloseWindow(view->node()->id());
53 }
54
55 // Overridden from ViewTreeNodeObserver:
56 virtual void OnNodeActiveViewChange(
57 ViewTreeNode* node,
58 View* old_view,
59 View* new_view,
60 ViewTreeNodeObserver::DispositionChangePhase phase) OVERRIDE {
61 if (new_view == 0)
62 views_to_reap_[node] = old_view;
63 }
64
65 void OnRootAdded(ViewManager* view_manager, ViewTreeNode* root) {
28 if (!view_manager_) 66 if (!view_manager_)
29 view_manager_ = view_manager; 67 view_manager_ = view_manager;
30 DCHECK_EQ(view_manager_, view_manager); 68 DCHECK_EQ(view_manager_, view_manager);
31 69
32 if (view_manager_->roots().size() == 1) { 70 View* view = View::Create(view_manager_);
33 view_manager::View* view = view_manager::View::Create(view_manager_); 71 view->AddObserver(this);
34 view_manager_->roots().front()->SetActiveView(view); 72 root->SetActiveView(view);
35 view->SetColor(SK_ColorYELLOW); 73 root->AddObserver(this);
36 } else { 74 size_t index = view_manager_->roots().size() - 1;
37 view_manager::View* view = view_manager::View::Create(view_manager_); 75 view->SetColor(kColors[index % arraysize(kColors)]);
38 view_manager_->roots().back()->SetActiveView(view);
39 view->SetColor(SK_ColorRED);
40 }
41 } 76 }
42 77
43 view_manager::ViewManager* view_manager_; 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_;
87 ViewManager* view_manager_;
88 std::map<ViewTreeNode*, View*> views_to_reap_;
44 89
45 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); 90 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp);
46 }; 91 };
47 92
48 } // namespace examples 93 } // namespace examples
49 94
50 // static 95 // static
51 Application* Application::Create() { 96 Application* Application::Create() {
52 return new examples::EmbeddedApp; 97 return new examples::EmbeddedApp;
53 } 98 }
54 99
55 } // namespace mojo 100 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/examples/embedded_app/DEPS ('k') | mojo/examples/window_manager/DEPS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698