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/cpp/application/application.h"
14 #include "mojo/public/cpp/system/core.h"
15 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h"
16 #include "mojo/services/public/cpp/view_manager/node.h"
17 #include "mojo/services/public/cpp/view_manager/view.h"
18 #include "mojo/services/public/cpp/view_manager/view_manager.h"
19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
20 #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.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/window.h"
25 #include "ui/aura/window_delegate.h"
26 #include "ui/base/hit_test.h"
27 #include "ui/gfx/canvas.h"
28 #include "ui/gfx/codec/png_codec.h"
33 // Trivial WindowDelegate implementation that draws a colored background.
34 class DemoWindowDelegate
: public aura::WindowDelegate
{
36 explicit DemoWindowDelegate(SkColor color
) : color_(color
) {}
38 // Overridden from WindowDelegate:
39 virtual gfx::Size
GetMinimumSize() const OVERRIDE
{
43 virtual gfx::Size
GetMaximumSize() const OVERRIDE
{
47 virtual void OnBoundsChanged(const gfx::Rect
& old_bounds
,
48 const gfx::Rect
& new_bounds
) OVERRIDE
{}
49 virtual gfx::NativeCursor
GetCursor(const gfx::Point
& point
) OVERRIDE
{
50 return gfx::kNullCursor
;
52 virtual int GetNonClientComponent(const gfx::Point
& point
) const OVERRIDE
{
55 virtual bool ShouldDescendIntoChildForEventHandling(
57 const gfx::Point
& location
) OVERRIDE
{
60 virtual bool CanFocus() OVERRIDE
{ return true; }
61 virtual void OnCaptureLost() OVERRIDE
{}
62 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
{
63 canvas
->DrawColor(color_
, SkXfermode::kSrc_Mode
);
65 virtual void OnDeviceScaleFactorChanged(float device_scale_factor
) OVERRIDE
{}
66 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
{}
67 virtual void OnWindowDestroyed(aura::Window
* window
) OVERRIDE
{}
68 virtual void OnWindowTargetVisibilityChanged(bool visible
) OVERRIDE
{}
69 virtual bool HasHitTestMask() const OVERRIDE
{ return false; }
70 virtual void GetHitTestMask(gfx::Path
* mask
) const OVERRIDE
{}
75 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate
);
78 class DemoWindowTreeClient
: public aura::client::WindowTreeClient
{
80 explicit DemoWindowTreeClient(aura::Window
* window
) : window_(window
) {
81 aura::client::SetWindowTreeClient(window_
, this);
84 virtual ~DemoWindowTreeClient() {
85 aura::client::SetWindowTreeClient(window_
, NULL
);
88 // Overridden from aura::client::WindowTreeClient:
89 virtual aura::Window
* GetDefaultParent(aura::Window
* context
,
91 const gfx::Rect
& bounds
) OVERRIDE
{
92 if (!capture_client_
) {
93 capture_client_
.reset(
94 new aura::client::DefaultCaptureClient(window_
->GetRootWindow()));
100 aura::Window
* window_
;
101 scoped_ptr
<aura::client::DefaultCaptureClient
> capture_client_
;
103 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient
);
106 class AuraDemo
: public Application
,
107 public WindowTreeHostMojoDelegate
,
108 public view_manager::ViewManagerDelegate
{
115 view_manager::ViewManager::Create(this, this);
117 virtual ~AuraDemo() {}
120 // Overridden from view_manager::ViewManagerDelegate:
121 virtual void OnRootAdded(view_manager::ViewManager
* view_manager
,
122 view_manager::Node
* root
) OVERRIDE
{
123 // TODO(beng): this function could be called multiple times!
124 view_
= view_manager::View::Create(view_manager
);
125 root
->SetActiveView(view_
);
127 window_tree_host_
.reset(new WindowTreeHostMojo(root
, this));
128 window_tree_host_
->InitHost();
130 window_tree_client_
.reset(
131 new DemoWindowTreeClient(window_tree_host_
->window()));
133 delegate1_
.reset(new DemoWindowDelegate(SK_ColorBLUE
));
134 window1_
= new aura::Window(delegate1_
.get());
135 window1_
->Init(aura::WINDOW_LAYER_TEXTURED
);
136 window1_
->SetBounds(gfx::Rect(100, 100, 400, 400));
138 window_tree_host_
->window()->AddChild(window1_
);
140 delegate2_
.reset(new DemoWindowDelegate(SK_ColorRED
));
141 window2_
= new aura::Window(delegate2_
.get());
142 window2_
->Init(aura::WINDOW_LAYER_TEXTURED
);
143 window2_
->SetBounds(gfx::Rect(200, 200, 350, 350));
145 window_tree_host_
->window()->AddChild(window2_
);
147 delegate21_
.reset(new DemoWindowDelegate(SK_ColorGREEN
));
148 window21_
= new aura::Window(delegate21_
.get());
149 window21_
->Init(aura::WINDOW_LAYER_TEXTURED
);
150 window21_
->SetBounds(gfx::Rect(10, 10, 50, 50));
152 window2_
->AddChild(window21_
);
154 window_tree_host_
->Show();
157 // WindowTreeHostMojoDelegate:
158 virtual void CompositorContentsChanged(const SkBitmap
& bitmap
) OVERRIDE
{
159 view_
->SetContents(bitmap
);
162 virtual void Initialize() OVERRIDE
{
163 aura::Env::CreateInstance(true);
164 context_factory_
.reset(new ContextFactoryMojo
);
165 aura::Env::GetInstance()->set_context_factory(context_factory_
.get());
166 screen_
.reset(ScreenMojo::Create());
167 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, screen_
.get());
170 scoped_ptr
<DemoWindowTreeClient
> window_tree_client_
;
172 scoped_ptr
<ui::ContextFactory
> context_factory_
;
174 scoped_ptr
<ScreenMojo
> screen_
;
176 scoped_ptr
<DemoWindowDelegate
> delegate1_
;
177 scoped_ptr
<DemoWindowDelegate
> delegate2_
;
178 scoped_ptr
<DemoWindowDelegate
> delegate21_
;
180 aura::Window
* window1_
;
181 aura::Window
* window2_
;
182 aura::Window
* window21_
;
184 view_manager::View
* view_
;
186 scoped_ptr
<aura::WindowTreeHost
> window_tree_host_
;
188 DISALLOW_COPY_AND_ASSIGN(AuraDemo
);
191 } // namespace examples
194 Application
* Application::Create() {
195 return new examples::AuraDemo();