summaryrefslogtreecommitdiffstats
path: root/mojo/examples/window_manager/window_manager.cc
blob: d998f0ca4a1860e08433d24606967ad1f6497cb7 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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 "ui/events/event_constants.h"

#if defined CreateWindow
#undef CreateWindow
#endif

using mojo::view_manager::Id;
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 {

class WindowManager;

class WindowManagerConnection : public InterfaceImpl<IWindowManager> {
 public:
  explicit WindowManagerConnection(WindowManager* window_manager)
      : window_manager_(window_manager) {}
  virtual ~WindowManagerConnection() {}

 private:
  // Overridden from IWindowManager:
  virtual void CloseWindow(Id node_id) OVERRIDE;

  WindowManager* window_manager_;

  DISALLOW_COPY_AND_ASSIGN(WindowManagerConnection);
};

class WindowManager : public Application,
                      public ViewObserver,
                      public ViewManagerDelegate {
 public:
  WindowManager() : view_manager_(NULL) {}
  virtual ~WindowManager() {}

  void CloseWindow(Id node_id) {
    ViewTreeNode* node = view_manager_->GetNodeById(node_id);
    DCHECK(node);
    node->Destroy();
  }

 private:
  // Overridden from Application:
  virtual void Initialize() MOJO_OVERRIDE {
    AddService<WindowManagerConnection>(this);
    ViewManager::Create(this, this);
  }

    // Overridden from ViewObserver:
  virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE {
    if (event->action == ui::ET_MOUSE_RELEASED) {
      if (event->flags & ui::EF_LEFT_MOUSE_BUTTON)
        CreateWindow("mojo:mojo_embedded_app");
      else if (event->flags & ui::EF_RIGHT_MOUSE_BUTTON)
        CreateWindow("mojo:mojo_nesting_app");
      else if (event->flags & ui::EF_MIDDLE_MOUSE_BUTTON)
        CreateWindow("mojo:mojo_browser");
    }
  }

  // Overridden from ViewManagerDelegate:
  virtual void OnRootAdded(ViewManager* view_manager,
                           ViewTreeNode* root) OVERRIDE {
    DCHECK(!view_manager_);
    view_manager_ = view_manager;

    ViewTreeNode* node = ViewTreeNode::Create(view_manager);
    view_manager->GetRoots().front()->AddChild(node);
    node->SetBounds(gfx::Rect(800, 600));
    parent_node_id_ = node->id();

    View* view = View::Create(view_manager);
    node->SetActiveView(view);
    view->SetColor(SK_ColorBLUE);
    view->AddObserver(this);
  }

  void CreateWindow(const String& url) {
    ViewTreeNode* node = view_manager_->GetNodeById(parent_node_id_);

    gfx::Rect bounds(50, 50, 200, 200);
    if (!node->children().empty()) {
      gfx::Point position = node->children().back()->bounds().origin();
      position.Offset(50, 50);
      bounds.set_origin(position);
    }

    ViewTreeNode* embedded = ViewTreeNode::Create(view_manager_);
    node->AddChild(embedded);
    embedded->SetBounds(bounds);
    embedded->Embed(url);
  }

  ViewManager* view_manager_;
  Id parent_node_id_;

  DISALLOW_COPY_AND_ASSIGN(WindowManager);
};

void WindowManagerConnection::CloseWindow(Id node_id) {
  window_manager_->CloseWindow(node_id);
}

}  // namespace examples

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

}  // namespace mojo