summaryrefslogtreecommitdiffstats
path: root/mojo/examples/embedded_app/embedded_app.cc
blob: a692f5a27051b03e17132e607af375d3b4d42b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/basictypes.h"
#include "base/bind.h"
#include "mojo/examples/window_manager/window_manager.mojom.h"
#include "mojo/public/cpp/application/application.h"
#include "mojo/services/public/cpp/view_manager/view.h"
#include "mojo/services/public/cpp/view_manager/view_manager.h"
#include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
#include "mojo/services/public/cpp/view_manager/view_observer.h"
#include "mojo/services/public/cpp/view_manager/view_tree_node.h"
#include "mojo/services/public/cpp/view_manager/view_tree_node_observer.h"
#include "ui/events/event_constants.h"

using mojo::view_manager::View;
using mojo::view_manager::ViewManager;
using mojo::view_manager::ViewManagerDelegate;
using mojo::view_manager::ViewObserver;
using mojo::view_manager::ViewTreeNode;
using mojo::view_manager::ViewTreeNodeObserver;

namespace mojo {
namespace examples {
namespace {

const SkColor kColors[] = { SK_ColorYELLOW,
                            SK_ColorRED,
                            SK_ColorGREEN,
                            SK_ColorMAGENTA };

}  // namespace

class EmbeddedApp : public Application,
                    public ViewManagerDelegate,
                    public ViewObserver,
                    public ViewTreeNodeObserver {
 public:
  EmbeddedApp() {}
  virtual ~EmbeddedApp() {}

 private:
  // Overridden from Application:
  virtual void Initialize() MOJO_OVERRIDE {
    ViewManager::Create(this, this);
    ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_);
  }

  // Overridden from ViewManagerDelegate:
  virtual void OnRootAdded(ViewManager* view_manager,
                           ViewTreeNode* root) OVERRIDE {
    View* view = View::Create(view_manager);
    view->AddObserver(this);
    root->SetActiveView(view);
    root->AddObserver(this);
    size_t index = view_manager->GetRoots().size() - 1;
    view->SetColor(kColors[index % arraysize(kColors)]);
  }
  virtual void OnRootRemoved(ViewManager* view_manager,
                             ViewTreeNode* root) OVERRIDE {
    std::map<ViewTreeNode*, View*>::const_iterator it =
        views_to_reap_.find(root);
    if (it != views_to_reap_.end())
      it->second->Destroy();
  }

  // Overridden from ViewObserver:
  virtual void OnViewInputEvent(View* view, EventPtr event) OVERRIDE {
    if (event->action == ui::ET_MOUSE_RELEASED)
      window_manager_->CloseWindow(view->node()->id());
  }

  // Overridden from ViewTreeNodeObserver:
  virtual void OnNodeActiveViewChange(
      ViewTreeNode* node,
      View* old_view,
      View* new_view,
      ViewTreeNodeObserver::DispositionChangePhase phase) OVERRIDE {
    if (new_view == 0)
      views_to_reap_[node] = old_view;
  }

  IWindowManagerPtr window_manager_;
  std::map<ViewTreeNode*, View*> views_to_reap_;

  DISALLOW_COPY_AND_ASSIGN(EmbeddedApp);
};

}  // namespace examples

// static
Application* Application::Create() {
  return new examples::EmbeddedApp;
}

}  // namespace mojo