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.
9 #include "mojo/aura/context_factory_mojo.h"
10 #include "mojo/aura/screen_mojo.h"
11 #include "mojo/aura/window_tree_host_mojo.h"
12 #include "mojo/aura/window_tree_host_mojo_delegate.h"
13 #include "mojo/public/c/system/main.h"
14 #include "mojo/public/cpp/application/application_connection.h"
15 #include "mojo/public/cpp/application/application_delegate.h"
16 #include "mojo/public/cpp/application/application_impl.h"
17 #include "mojo/public/cpp/application/application_runner_chromium.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"
36 // Trivial WindowDelegate implementation that draws a colored background.
37 class DemoWindowDelegate
: public aura::WindowDelegate
{
39 explicit DemoWindowDelegate(SkColor color
) : color_(color
) {}
41 // Overridden from WindowDelegate:
42 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{
46 virtual gfx::Size
GetMaximumSize() const OVERRIDE
{
50 virtual void OnBoundsChanged(const gfx::Rect
& old_bounds
,
51 const gfx::Rect
& new_bounds
) OVERRIDE
{}
52 virtual gfx::NativeCursor
GetCursor(const gfx::Point
& point
) OVERRIDE
{
53 return gfx::kNullCursor
;
55 virtual int GetNonClientComponent(const gfx::Point
& point
) const OVERRIDE
{
58 virtual bool ShouldDescendIntoChildForEventHandling(
60 const gfx::Point
& location
) OVERRIDE
{
63 virtual bool CanFocus() OVERRIDE
{ return true; }
64 virtual void OnCaptureLost() OVERRIDE
{}
65 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
66 canvas
->DrawColor(color_
, SkXfermode::kSrc_Mode
);
68 virtual void OnDeviceScaleFactorChanged(float device_scale_factor
) OVERRIDE
{}
69 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
{}
70 virtual void OnWindowDestroyed(aura::Window
* window
) OVERRIDE
{}
71 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{}
72 virtual bool HasHitTestMask() const OVERRIDE
{ return false; }
73 virtual void GetHitTestMask(gfx::Path
* mask
) const OVERRIDE
{}
78 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate
);
81 class DemoWindowTreeClient
: public aura::client::WindowTreeClient
{
83 explicit DemoWindowTreeClient(aura::Window
* window
) : window_(window
) {
84 aura::client::SetWindowTreeClient(window_
, this);
87 virtual ~DemoWindowTreeClient() {
88 aura::client::SetWindowTreeClient(window_
, NULL
);
91 // Overridden from aura::client::WindowTreeClient:
92 virtual aura::Window
* GetDefaultParent(aura::Window
* context
,
94 const gfx::Rect
& bounds
) OVERRIDE
{
95 if (!capture_client_
) {
96 capture_client_
.reset(
97 new aura::client::DefaultCaptureClient(window_
->GetRootWindow()));
103 aura::Window
* window_
;
104 scoped_ptr
<aura::client::DefaultCaptureClient
> capture_client_
;
106 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient
);
109 class AuraDemo
: public ApplicationDelegate
,
110 public WindowTreeHostMojoDelegate
,
111 public ViewManagerDelegate
{
113 AuraDemo() : window1_(NULL
), window2_(NULL
), window21_(NULL
) {}
114 virtual ~AuraDemo() {}
117 // Overridden from ViewManagerDelegate:
118 virtual void OnEmbed(ViewManager
* view_manager
,
120 ServiceProviderImpl
* exported_services
,
121 scoped_ptr
<ServiceProvider
> imported_services
) OVERRIDE
{
122 // TODO(beng): this function could be called multiple times!
125 window_tree_host_
.reset(new WindowTreeHostMojo(root
, this));
126 window_tree_host_
->InitHost();
128 window_tree_client_
.reset(
129 new DemoWindowTreeClient(window_tree_host_
->window()));
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));
136 window_tree_host_
->window()->AddChild(window1_
);
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));
143 window_tree_host_
->window()->AddChild(window2_
);
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));
150 window2_
->AddChild(window21_
);
152 window_tree_host_
->Show();
154 virtual void OnViewManagerDisconnected(
155 ViewManager
* view_manager
) OVERRIDE
{
156 base::MessageLoop::current()->Quit();
159 // WindowTreeHostMojoDelegate:
160 virtual void CompositorContentsChanged(const SkBitmap
& bitmap
) OVERRIDE
{
161 root_
->SetContents(bitmap
);
164 virtual void Initialize(ApplicationImpl
* app
) MOJO_OVERRIDE
{
165 view_manager_client_factory_
.reset(
166 new ViewManagerClientFactory(app
->shell(), this));
167 aura::Env::CreateInstance(true);
168 context_factory_
.reset(new ContextFactoryMojo
);
169 aura::Env::GetInstance()->set_context_factory(context_factory_
.get());
170 screen_
.reset(ScreenMojo::Create());
171 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, screen_
.get());
174 virtual bool ConfigureIncomingConnection(ApplicationConnection
* connection
)
176 connection
->AddService(view_manager_client_factory_
.get());
180 scoped_ptr
<DemoWindowTreeClient
> window_tree_client_
;
182 scoped_ptr
<ui::ContextFactory
> context_factory_
;
184 scoped_ptr
<ScreenMojo
> screen_
;
186 scoped_ptr
<DemoWindowDelegate
> delegate1_
;
187 scoped_ptr
<DemoWindowDelegate
> delegate2_
;
188 scoped_ptr
<DemoWindowDelegate
> delegate21_
;
190 aura::Window
* window1_
;
191 aura::Window
* window2_
;
192 aura::Window
* window21_
;
196 scoped_ptr
<ViewManagerClientFactory
> view_manager_client_factory_
;
198 scoped_ptr
<aura::WindowTreeHost
> window_tree_host_
;
200 DISALLOW_COPY_AND_ASSIGN(AuraDemo
);
203 } // namespace examples
206 MojoResult
MojoMain(MojoHandle shell_handle
) {
207 mojo::ApplicationRunnerChromium
runner(new mojo::examples::AuraDemo
);
208 return runner
.Run(shell_handle
);