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

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

Issue 120503003: run aura_demo as a mojo app (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: builds on X11 now too Created 6 years, 11 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
OLDNEW
(Empty)
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/message_loop/message_loop.h"
9 #include "mojo/common/bindings_support_impl.h"
10 #include "mojo/examples/aura_demo/demo_screen.h"
11 #include "mojo/examples/aura_demo/root_window_host_mojo.h"
12 #include "mojo/examples/compositor_app/compositor_host.h"
13 #include "mojo/examples/compositor_app/gles2_client_impl.h"
14 #include "mojo/public/bindings/lib/bindings_support.h"
15 #include "mojo/public/bindings/lib/remote_ptr.h"
16 #include "mojo/public/gles2/gles2.h"
17 #include "mojo/public/system/core.h"
18 #include "mojo/public/system/macros.h"
19 #include "mojom/native_viewport.h"
20 #include "mojom/shell.h"
21 #include "ui/aura/client/default_capture_client.h"
22 #include "ui/aura/client/window_tree_client.h"
23 #include "ui/aura/env.h"
24 #include "ui/aura/root_window.h"
25 #include "ui/aura/window.h"
26 #include "ui/aura/window_delegate.h"
27 #include "ui/base/hit_test.h"
28 #include "ui/gfx/canvas.h"
29
30 #if defined(WIN32)
31 #if !defined(CDECL)
32 #define CDECL __cdecl
33 #endif
34 #define AURA_DEMO_EXPORT __declspec(dllexport)
35 #else
36 #define CDECL
37 #define AURA_DEMO_EXPORT __attribute__((visibility("default")))
38 #endif
39
40 namespace mojo {
41 namespace examples {
42
43 // Trivial WindowDelegate implementation that draws a colored background.
44 class DemoWindowDelegate : public aura::WindowDelegate {
45 public:
46 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
47
48 // Overridden from WindowDelegate:
49 virtual gfx::Size GetMinimumSize() const OVERRIDE {
50 return gfx::Size();
51 }
52
53 virtual gfx::Size GetMaximumSize() const OVERRIDE {
54 return gfx::Size();
55 }
56
57 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
58 const gfx::Rect& new_bounds) OVERRIDE {}
59 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
60 return gfx::kNullCursor;
61 }
62 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
63 return HTCAPTION;
64 }
65 virtual bool ShouldDescendIntoChildForEventHandling(
66 aura::Window* child,
67 const gfx::Point& location) OVERRIDE {
68 return true;
69 }
70 virtual bool CanFocus() OVERRIDE { return true; }
71 virtual void OnCaptureLost() OVERRIDE {}
72 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
73 canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
74 }
75 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
76 virtual void OnWindowDestroying() OVERRIDE {}
77 virtual void OnWindowDestroyed() OVERRIDE {}
78 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
79 virtual bool HasHitTestMask() const OVERRIDE { return false; }
80 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
81 virtual void DidRecreateLayer(ui::Layer* old_layer,
82 ui::Layer* new_layer) OVERRIDE {}
83
84 private:
85 SkColor color_;
86
87 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
88 };
89
90 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
91 public:
92 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
93 aura::client::SetWindowTreeClient(window_, this);
94 }
95
96 virtual ~DemoWindowTreeClient() {
97 aura::client::SetWindowTreeClient(window_, NULL);
98 }
99
100 // Overridden from aura::client::WindowTreeClient:
101 virtual aura::Window* GetDefaultParent(aura::Window* context,
102 aura::Window* window,
103 const gfx::Rect& bounds) OVERRIDE {
104 if (!capture_client_) {
105 capture_client_.reset(
106 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
107 }
108 return window_;
109 }
110
111 private:
112 aura::Window* window_;
113
114 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
115
116 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
117 };
118
119 class AuraDemo : public ShellClientStub {
120 public:
121 explicit AuraDemo(ScopedMessagePipeHandle shell_handle)
122 : shell_(shell_handle.Pass()) {
123 shell_.SetPeer(this);
124
125 screen_.reset(DemoScreen::Create());
126 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
127
128 mojo::ScopedMessagePipeHandle client_handle, native_viewport_handle;
129 CreateMessagePipe(&client_handle, &native_viewport_handle);
130 root_window_host_.reset(new RootWindowHostMojo(
131 native_viewport_handle.Pass(),
132 base::Bind(&AuraDemo::HostContextCreated, base::Unretained(this))));
133 mojo::AllocationScope scope;
134 shell_->Connect("mojo:mojo_native_viewport_service", client_handle.Pass());
135 }
136
137 virtual void AcceptConnection(ScopedMessagePipeHandle handle) MOJO_OVERRIDE {
138 NOTREACHED() << "AuraDemo can't be connected to.";
139 }
140
141 private:
142
sky 2014/01/06 23:19:43 nit: remove newline.
143 void HostContextCreated() {
144 aura::RootWindow::CreateParams params(gfx::Rect(0, 0, 500, 500));
145 params.host = root_window_host_.get();
146 root_window_.reset(new aura::RootWindow(params));
147 root_window_host_->set_delegate(root_window_.get());
148 root_window_->Init();
149
150 window_tree_client_.reset(new DemoWindowTreeClient(root_window_->window()));
151
152 delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE));
153 window1_.reset(new aura::Window(delegate1_.get()));
154 window1_->Init(ui::LAYER_TEXTURED);
155 window1_->SetBounds(gfx::Rect(100, 100, 400, 400));
156 window1_->Show();
157 root_window_->window()->AddChild(window1_.get());
158
159 delegate2_.reset(new DemoWindowDelegate(SK_ColorRED));
160 window2_.reset(new aura::Window(delegate2_.get()));
161 window2_->Init(ui::LAYER_TEXTURED);
162 window2_->SetBounds(gfx::Rect(200, 200, 350, 350));
163 window2_->Show();
164 root_window_->window()->AddChild(window2_.get());
165
166 delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN));
167 window21_.reset(new aura::Window(delegate21_.get()));
168 window21_->Init(ui::LAYER_TEXTURED);
169 window21_->SetBounds(gfx::Rect(10, 10, 50, 50));
170 window21_->Show();
171 window2_->AddChild(window21_.get());
172
173 root_window_->host()->Show();
174 }
175
176 scoped_ptr<DemoScreen> screen_;
177
178 scoped_ptr<DemoWindowTreeClient> window_tree_client_;
179
180 scoped_ptr<aura::Window> window1_;
181 scoped_ptr<aura::Window> window2_;
182 scoped_ptr<aura::Window> window21_;
183
184 scoped_ptr<DemoWindowDelegate> delegate1_;
185 scoped_ptr<DemoWindowDelegate> delegate2_;
186 scoped_ptr<DemoWindowDelegate> delegate21_;
187
188 mojo::RemotePtr<Shell> shell_;
189 scoped_ptr<RootWindowHostMojo> root_window_host_;
190 scoped_ptr<aura::RootWindow> root_window_;
191 };
192
193 } // namespace examples
194 } // namespace mojo
195
196 extern "C" AURA_DEMO_EXPORT MojoResult CDECL MojoMain(
197 MojoHandle shell_handle) {
198 base::MessageLoop loop;
199 mojo::common::BindingsSupportImpl bindings_support_impl;
200 mojo::BindingsSupport::Set(&bindings_support_impl);
201 MojoGLES2Initialize();
202
203 // TODO(beng): This crashes in a DCHECK on X11 because this thread's
204 // MessageLoop is not of TYPE_UI. I think we need a way to build
205 // Aura that doesn't define platform-specific stuff.
206 aura::Env::CreateInstance();
207 mojo::examples::AuraDemo app(
208 mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle)).Pass());
209 loop.Run();
210
211 MojoGLES2Terminate();
212 mojo::BindingsSupport::Set(NULL);
213 return MOJO_RESULT_OK;
214 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698