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.
8 #include "base/at_exit.h"
9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h"
11 #include "mojo/examples/aura_demo/demo_screen.h"
12 #include "mojo/examples/aura_demo/root_window_host_mojo.h"
13 #include "mojo/examples/compositor_app/compositor_host.h"
14 #include "mojo/examples/compositor_app/gles2_client_impl.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"
34 #define AURA_DEMO_EXPORT __declspec(dllexport)
37 #define AURA_DEMO_EXPORT __attribute__((visibility("default")))
43 // Trivial WindowDelegate implementation that draws a colored background.
44 class DemoWindowDelegate
: public aura::WindowDelegate
{
46 explicit DemoWindowDelegate(SkColor color
) : color_(color
) {}
48 // Overridden from WindowDelegate:
49 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{
53 virtual gfx::Size
GetMaximumSize() const OVERRIDE
{
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
;
62 virtual int GetNonClientComponent(const gfx::Point
& point
) const OVERRIDE
{
65 virtual bool ShouldDescendIntoChildForEventHandling(
67 const gfx::Point
& location
) OVERRIDE
{
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
);
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
{}
87 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate
);
90 class DemoWindowTreeClient
: public aura::client::WindowTreeClient
{
92 explicit DemoWindowTreeClient(aura::Window
* window
) : window_(window
) {
93 aura::client::SetWindowTreeClient(window_
, this);
96 virtual ~DemoWindowTreeClient() {
97 aura::client::SetWindowTreeClient(window_
, NULL
);
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()));
112 aura::Window
* window_
;
114 scoped_ptr
<aura::client::DefaultCaptureClient
> capture_client_
;
116 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient
);
119 class AuraDemo
: public ShellClient
{
121 explicit AuraDemo(ScopedMessagePipeHandle shell_handle
)
122 : shell_(shell_handle
.Pass(), this) {
123 screen_
.reset(DemoScreen::Create());
124 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, screen_
.get());
126 mojo::ScopedMessagePipeHandle client_handle
, native_viewport_handle
;
127 CreateMessagePipe(&client_handle
, &native_viewport_handle
);
128 root_window_host_
.reset(new WindowTreeHostMojo(
129 native_viewport_handle
.Pass(),
130 base::Bind(&AuraDemo::HostContextCreated
, base::Unretained(this))));
131 mojo::AllocationScope scope
;
132 shell_
->Connect("mojo:mojo_native_viewport_service", client_handle
.Pass());
135 virtual void AcceptConnection(ScopedMessagePipeHandle handle
) MOJO_OVERRIDE
{
136 NOTREACHED() << "AuraDemo can't be connected to.";
140 void HostContextCreated() {
141 aura::RootWindow::CreateParams
params(gfx::Rect(0, 0, 500, 500));
142 params
.host
= root_window_host_
.get();
143 root_window_
.reset(new aura::RootWindow(params
));
144 root_window_host_
->set_delegate(root_window_
.get());
145 root_window_
->Init();
147 window_tree_client_
.reset(new DemoWindowTreeClient(root_window_
->window()));
149 delegate1_
.reset(new DemoWindowDelegate(SK_ColorBLUE
));
150 window1_
= new aura::Window(delegate1_
.get());
151 window1_
->Init(aura::WINDOW_LAYER_TEXTURED
);
152 window1_
->SetBounds(gfx::Rect(100, 100, 400, 400));
154 root_window_
->window()->AddChild(window1_
);
156 delegate2_
.reset(new DemoWindowDelegate(SK_ColorRED
));
157 window2_
= new aura::Window(delegate2_
.get());
158 window2_
->Init(aura::WINDOW_LAYER_TEXTURED
);
159 window2_
->SetBounds(gfx::Rect(200, 200, 350, 350));
161 root_window_
->window()->AddChild(window2_
);
163 delegate21_
.reset(new DemoWindowDelegate(SK_ColorGREEN
));
164 window21_
= new aura::Window(delegate21_
.get());
165 window21_
->Init(aura::WINDOW_LAYER_TEXTURED
);
166 window21_
->SetBounds(gfx::Rect(10, 10, 50, 50));
168 window2_
->AddChild(window21_
);
170 root_window_
->host()->Show();
173 scoped_ptr
<DemoScreen
> screen_
;
175 scoped_ptr
<DemoWindowTreeClient
> window_tree_client_
;
177 scoped_ptr
<DemoWindowDelegate
> delegate1_
;
178 scoped_ptr
<DemoWindowDelegate
> delegate2_
;
179 scoped_ptr
<DemoWindowDelegate
> delegate21_
;
181 aura::Window
* window1_
;
182 aura::Window
* window2_
;
183 aura::Window
* window21_
;
185 mojo::RemotePtr
<Shell
> shell_
;
186 scoped_ptr
<WindowTreeHostMojo
> root_window_host_
;
187 scoped_ptr
<aura::RootWindow
> root_window_
;
190 } // namespace examples
193 extern "C" AURA_DEMO_EXPORT MojoResult CDECL
MojoMain(
194 MojoHandle shell_handle
) {
195 CommandLine::Init(0, NULL
);
196 base::AtExitManager at_exit
;
197 base::MessageLoop loop
;
198 MojoGLES2Initialize();
200 // TODO(beng): This crashes in a DCHECK on X11 because this thread's
201 // MessageLoop is not of TYPE_UI. I think we need a way to build
202 // Aura that doesn't define platform-specific stuff.
203 aura::Env::CreateInstance();
204 mojo::examples::AuraDemo
app(
205 mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle
)).Pass());
208 MojoGLES2Terminate();
209 return MOJO_RESULT_OK
;