1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6 #include <string>
7
8 #include "base/bind.h"
9 #include "mojo/application/application_runner_chromium.h"
10 #include "mojo/aura/context_factory_mojo.h"
11 #include "mojo/aura/screen_mojo.h"
12 #include "mojo/aura/window_tree_host_mojo.h"
13 #include "mojo/aura/window_tree_host_mojo_delegate.h"
14 #include "mojo/public/c/system/main.h"
15 #include "mojo/public/cpp/application/application_connection.h"
16 #include "mojo/public/cpp/application/application_delegate.h"
17 #include "mojo/public/cpp/application/application_impl.h"
18 #include "mojo/public/cpp/system/core.h"
19 #include "mojo/services/public/cpp/view_manager/view.h"
20 #include "mojo/services/public/cpp/view_manager/view_manager.h"
21 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
22 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
23 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.h"
24 #include "ui/aura/client/default_capture_client.h"
25 #include "ui/aura/client/window_tree_client.h"
26 #include "ui/aura/env.h"
27 #include "ui/aura/window.h"
28 #include "ui/aura/window_delegate.h"
29 #include "ui/base/hit_test.h"
30 #include "ui/gfx/canvas.h"
31 #include "ui/gfx/codec/png_codec.h"
32
33 namespace examples {
34
35 // Trivial WindowDelegate implementation that draws a colored background.
36 class DemoWindowDelegate : public aura::WindowDelegate {
37 public:
DemoWindowDelegate(SkColor color)38 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
39
40 // Overridden from WindowDelegate:
GetMinimumSize() const41 virtual gfx::Size GetMinimumSize() const OVERRIDE {
42 return gfx::Size();
43 }
44
GetMaximumSize() const45 virtual gfx::Size GetMaximumSize() const OVERRIDE {
46 return gfx::Size();
47 }
48
OnBoundsChanged(const gfx::Rect & old_bounds,const gfx::Rect & new_bounds)49 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
50 const gfx::Rect& new_bounds) OVERRIDE {}
GetCursor(const gfx::Point & point)51 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
52 return gfx::kNullCursor;
53 }
GetNonClientComponent(const gfx::Point & point) const54 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
55 return HTCAPTION;
56 }
ShouldDescendIntoChildForEventHandling(aura::Window * child,const gfx::Point & location)57 virtual bool ShouldDescendIntoChildForEventHandling(
58 aura::Window* child,
59 const gfx::Point& location) OVERRIDE {
60 return true;
61 }
CanFocus()62 virtual bool CanFocus() OVERRIDE { return true; }
OnCaptureLost()63 virtual void OnCaptureLost() OVERRIDE {}
OnPaint(gfx::Canvas * canvas)64 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
65 canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
66 }
OnDeviceScaleFactorChanged(float device_scale_factor)67 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
OnWindowDestroying(aura::Window * window)68 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
OnWindowDestroyed(aura::Window * window)69 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {}
OnWindowTargetVisibilityChanged(bool visible)70 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
HasHitTestMask() const71 virtual bool HasHitTestMask() const OVERRIDE { return false; }
GetHitTestMask(gfx::Path * mask) const72 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
73
74 private:
75 const SkColor color_;
76
77 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
78 };
79
80 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
81 public:
DemoWindowTreeClient(aura::Window * window)82 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
83 aura::client::SetWindowTreeClient(window_, this);
84 }
85
~DemoWindowTreeClient()86 virtual ~DemoWindowTreeClient() {
87 aura::client::SetWindowTreeClient(window_, NULL);
88 }
89
90 // Overridden from aura::client::WindowTreeClient:
GetDefaultParent(aura::Window * context,aura::Window * window,const gfx::Rect & bounds)91 virtual aura::Window* GetDefaultParent(aura::Window* context,
92 aura::Window* window,
93 const gfx::Rect& bounds) OVERRIDE {
94 if (!capture_client_) {
95 capture_client_.reset(
96 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
97 }
98 return window_;
99 }
100
101 private:
102 aura::Window* window_;
103 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
104
105 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
106 };
107
108 class AuraDemo : public mojo::ApplicationDelegate,
109 public mojo::WindowTreeHostMojoDelegate,
110 public mojo::ViewManagerDelegate {
111 public:
AuraDemo()112 AuraDemo() : window1_(NULL), window2_(NULL), window21_(NULL) {}
~AuraDemo()113 virtual ~AuraDemo() {}
114
115 private:
116 // Overridden from ViewManagerDelegate:
OnEmbed(mojo::ViewManager * view_manager,mojo::View * root,mojo::ServiceProviderImpl * exported_services,scoped_ptr<mojo::ServiceProvider> imported_services)117 virtual void OnEmbed(
118 mojo::ViewManager* view_manager,
119 mojo::View* root,
120 mojo::ServiceProviderImpl* exported_services,
121 scoped_ptr<mojo::ServiceProvider> imported_services) OVERRIDE {
122 // TODO(beng): this function could be called multiple times!
123 root_ = root;
124
125 window_tree_host_.reset(new mojo::WindowTreeHostMojo(root, this));
126 window_tree_host_->InitHost();
127
128 window_tree_client_.reset(
129 new DemoWindowTreeClient(window_tree_host_->window()));
130
131 delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE));
132 window1_ = new aura::Window(delegate1_.get());
133 window1_->Init(aura::WINDOW_LAYER_TEXTURED);
134 window1_->SetBounds(gfx::Rect(100, 100, 400, 400));
135 window1_->Show();
136 window_tree_host_->window()->AddChild(window1_);
137
138 delegate2_.reset(new DemoWindowDelegate(SK_ColorRED));
139 window2_ = new aura::Window(delegate2_.get());
140 window2_->Init(aura::WINDOW_LAYER_TEXTURED);
141 window2_->SetBounds(gfx::Rect(200, 200, 350, 350));
142 window2_->Show();
143 window_tree_host_->window()->AddChild(window2_);
144
145 delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN));
146 window21_ = new aura::Window(delegate21_.get());
147 window21_->Init(aura::WINDOW_LAYER_TEXTURED);
148 window21_->SetBounds(gfx::Rect(10, 10, 50, 50));
149 window21_->Show();
150 window2_->AddChild(window21_);
151
152 window_tree_host_->Show();
153 }
OnViewManagerDisconnected(mojo::ViewManager * view_manager)154 virtual void OnViewManagerDisconnected(
155 mojo::ViewManager* view_manager) OVERRIDE {
156 base::MessageLoop::current()->Quit();
157 }
158
159 // WindowTreeHostMojoDelegate:
CompositorContentsChanged(const SkBitmap & bitmap)160 virtual void CompositorContentsChanged(const SkBitmap& bitmap) OVERRIDE {
161 root_->SetContents(bitmap);
162 }
163
Initialize(mojo::ApplicationImpl * app)164 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE {
165 view_manager_client_factory_.reset(
166 new mojo::ViewManagerClientFactory(app->shell(), this));
167 aura::Env::CreateInstance(true);
168 context_factory_.reset(new mojo::ContextFactoryMojo);
169 aura::Env::GetInstance()->set_context_factory(context_factory_.get());
170 screen_.reset(mojo::ScreenMojo::Create());
171 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
172 }
173
ConfigureIncomingConnection(mojo::ApplicationConnection * connection)174 virtual bool ConfigureIncomingConnection(
175 mojo::ApplicationConnection* connection) MOJO_OVERRIDE {
176 connection->AddService(view_manager_client_factory_.get());
177 return true;
178 }
179
180 scoped_ptr<DemoWindowTreeClient> window_tree_client_;
181
182 scoped_ptr<ui::ContextFactory> context_factory_;
183
184 scoped_ptr<mojo::ScreenMojo> screen_;
185
186 scoped_ptr<DemoWindowDelegate> delegate1_;
187 scoped_ptr<DemoWindowDelegate> delegate2_;
188 scoped_ptr<DemoWindowDelegate> delegate21_;
189
190 aura::Window* window1_;
191 aura::Window* window2_;
192 aura::Window* window21_;
193
194 mojo::View* root_;
195
196 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_;
197
198 scoped_ptr<aura::WindowTreeHost> window_tree_host_;
199
200 DISALLOW_COPY_AND_ASSIGN(AuraDemo);
201 };
202
203 } // namespace examples
204
MojoMain(MojoHandle shell_handle)205 MojoResult MojoMain(MojoHandle shell_handle) {
206 mojo::ApplicationRunnerChromium runner(new examples::AuraDemo);
207 return runner.Run(shell_handle);
208 }
209