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/aura/screen_mojo.h"
12 #include "mojo/aura/window_tree_host_mojo.h"
13 #include "mojo/public/cpp/bindings/allocation_scope.h"
14 #include "mojo/public/cpp/gles2/gles2.h"
15 #include "mojo/public/cpp/shell/application.h"
16 #include "mojo/public/cpp/system/core.h"
17 #include "mojo/public/interfaces/shell/shell.mojom.h"
18 #include "mojo/services/native_viewport/native_viewport.mojom.h"
19 #include "ui/aura/client/default_capture_client.h"
20 #include "ui/aura/client/window_tree_client.h"
21 #include "ui/aura/env.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_delegate.h"
24 #include "ui/base/hit_test.h"
25 #include "ui/gfx/canvas.h"
31 #define AURA_DEMO_EXPORT __declspec(dllexport)
34 #define AURA_DEMO_EXPORT __attribute__((visibility("default")))
40 // Trivial WindowDelegate implementation that draws a colored background.
41 class DemoWindowDelegate : public aura::WindowDelegate {
43 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
45 // Overridden from WindowDelegate:
46 virtual gfx::Size GetMinimumSize() const OVERRIDE {
50 virtual gfx::Size GetMaximumSize() const OVERRIDE {
54 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
55 const gfx::Rect& new_bounds) OVERRIDE {}
56 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
57 return gfx::kNullCursor;
59 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
62 virtual bool ShouldDescendIntoChildForEventHandling(
64 const gfx::Point& location) OVERRIDE {
67 virtual bool CanFocus() OVERRIDE { return true; }
68 virtual void OnCaptureLost() OVERRIDE {}
69 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
70 canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
72 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
73 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
74 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {}
75 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
76 virtual bool HasHitTestMask() const OVERRIDE { return false; }
77 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
82 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
85 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
87 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
88 aura::client::SetWindowTreeClient(window_, this);
91 virtual ~DemoWindowTreeClient() {
92 aura::client::SetWindowTreeClient(window_, NULL);
95 // Overridden from aura::client::WindowTreeClient:
96 virtual aura::Window* GetDefaultParent(aura::Window* context,
98 const gfx::Rect& bounds) OVERRIDE {
99 if (!capture_client_) {
100 capture_client_.reset(
101 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
107 aura::Window* window_;
109 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
111 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
114 class AuraDemo : public Application {
116 explicit AuraDemo(MojoHandle shell_handle) : Application(shell_handle) {
117 screen_.reset(ScreenMojo::Create());
118 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
120 NativeViewportPtr native_viewport;
121 ConnectTo("mojo:mojo_native_viewport_service", &native_viewport);
123 window_tree_host_.reset(new WindowTreeHostMojo(
124 native_viewport.Pass(),
126 base::Bind(&AuraDemo::HostContextCreated, base::Unretained(this))));
130 void HostContextCreated() {
131 window_tree_host_->InitHost();
133 window_tree_client_.reset(
134 new DemoWindowTreeClient(window_tree_host_->window()));
136 delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE));
137 window1_ = new aura::Window(delegate1_.get());
138 window1_->Init(aura::WINDOW_LAYER_TEXTURED);
139 window1_->SetBounds(gfx::Rect(100, 100, 400, 400));
141 window_tree_host_->window()->AddChild(window1_);
143 delegate2_.reset(new DemoWindowDelegate(SK_ColorRED));
144 window2_ = new aura::Window(delegate2_.get());
145 window2_->Init(aura::WINDOW_LAYER_TEXTURED);
146 window2_->SetBounds(gfx::Rect(200, 200, 350, 350));
148 window_tree_host_->window()->AddChild(window2_);
150 delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN));
151 window21_ = new aura::Window(delegate21_.get());
152 window21_->Init(aura::WINDOW_LAYER_TEXTURED);
153 window21_->SetBounds(gfx::Rect(10, 10, 50, 50));
155 window2_->AddChild(window21_);
157 window_tree_host_->Show();
160 scoped_ptr<ScreenMojo> screen_;
162 scoped_ptr<DemoWindowTreeClient> window_tree_client_;
164 scoped_ptr<DemoWindowDelegate> delegate1_;
165 scoped_ptr<DemoWindowDelegate> delegate2_;
166 scoped_ptr<DemoWindowDelegate> delegate21_;
168 aura::Window* window1_;
169 aura::Window* window2_;
170 aura::Window* window21_;
172 scoped_ptr<aura::WindowTreeHost> window_tree_host_;
175 } // namespace examples
178 extern "C" AURA_DEMO_EXPORT MojoResult CDECL MojoMain(
179 MojoHandle shell_handle) {
180 CommandLine::Init(0, NULL);
181 base::AtExitManager at_exit;
182 base::MessageLoop loop;
183 mojo::GLES2Initializer gles2;
185 // TODO(beng): This crashes in a DCHECK on X11 because this thread's
186 // MessageLoop is not of TYPE_UI. I think we need a way to build
187 // Aura that doesn't define platform-specific stuff.
188 aura::Env::CreateInstance(true);
189 mojo::examples::AuraDemo app(shell_handle);
192 return MOJO_RESULT_OK;