Shell / ShellClient -> ServiceProvider
[chromium-blink-merge.git] / mojo / examples / aura_demo / aura_demo.cc
blob7e4ec8c73ebd83a3b9de9983c485c90b06b8c53d
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.
5 #include <stdio.h>
6 #include <string>
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/application/application.h"
14 #include "mojo/public/cpp/bindings/allocation_scope.h"
15 #include "mojo/public/cpp/gles2/gles2.h"
16 #include "mojo/public/cpp/system/core.h"
17 #include "mojo/public/interfaces/service_provider/service_provider.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"
27 #if defined(WIN32)
28 #if !defined(CDECL)
29 #define CDECL __cdecl
30 #endif
31 #define AURA_DEMO_EXPORT __declspec(dllexport)
32 #else
33 #define CDECL
34 #define AURA_DEMO_EXPORT __attribute__((visibility("default")))
35 #endif
37 namespace mojo {
38 namespace examples {
40 // Trivial WindowDelegate implementation that draws a colored background.
41 class DemoWindowDelegate : public aura::WindowDelegate {
42 public:
43 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
45 // Overridden from WindowDelegate:
46 virtual gfx::Size GetMinimumSize() const OVERRIDE {
47 return gfx::Size();
50 virtual gfx::Size GetMaximumSize() const OVERRIDE {
51 return gfx::Size();
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 {
60 return HTCAPTION;
62 virtual bool ShouldDescendIntoChildForEventHandling(
63 aura::Window* child,
64 const gfx::Point& location) OVERRIDE {
65 return true;
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 {}
79 private:
80 SkColor color_;
82 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
85 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
86 public:
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,
97 aura::Window* window,
98 const gfx::Rect& bounds) OVERRIDE {
99 if (!capture_client_) {
100 capture_client_.reset(
101 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
103 return window_;
106 private:
107 aura::Window* window_;
109 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
111 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
114 class AuraDemo : public Application {
115 public:
116 explicit AuraDemo(MojoHandle service_provider_handle)
117 : Application(service_provider_handle) {
118 screen_.reset(ScreenMojo::Create());
119 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
121 NativeViewportPtr native_viewport;
122 ConnectTo("mojo:mojo_native_viewport_service", &native_viewport);
124 window_tree_host_.reset(new WindowTreeHostMojo(
125 native_viewport.Pass(),
126 gfx::Rect(800, 600),
127 base::Bind(&AuraDemo::HostContextCreated, base::Unretained(this))));
130 private:
131 void HostContextCreated() {
132 window_tree_host_->InitHost();
134 window_tree_client_.reset(
135 new DemoWindowTreeClient(window_tree_host_->window()));
137 delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE));
138 window1_ = new aura::Window(delegate1_.get());
139 window1_->Init(aura::WINDOW_LAYER_TEXTURED);
140 window1_->SetBounds(gfx::Rect(100, 100, 400, 400));
141 window1_->Show();
142 window_tree_host_->window()->AddChild(window1_);
144 delegate2_.reset(new DemoWindowDelegate(SK_ColorRED));
145 window2_ = new aura::Window(delegate2_.get());
146 window2_->Init(aura::WINDOW_LAYER_TEXTURED);
147 window2_->SetBounds(gfx::Rect(200, 200, 350, 350));
148 window2_->Show();
149 window_tree_host_->window()->AddChild(window2_);
151 delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN));
152 window21_ = new aura::Window(delegate21_.get());
153 window21_->Init(aura::WINDOW_LAYER_TEXTURED);
154 window21_->SetBounds(gfx::Rect(10, 10, 50, 50));
155 window21_->Show();
156 window2_->AddChild(window21_);
158 window_tree_host_->Show();
161 scoped_ptr<ScreenMojo> screen_;
163 scoped_ptr<DemoWindowTreeClient> window_tree_client_;
165 scoped_ptr<DemoWindowDelegate> delegate1_;
166 scoped_ptr<DemoWindowDelegate> delegate2_;
167 scoped_ptr<DemoWindowDelegate> delegate21_;
169 aura::Window* window1_;
170 aura::Window* window2_;
171 aura::Window* window21_;
173 scoped_ptr<aura::WindowTreeHost> window_tree_host_;
176 } // namespace examples
177 } // namespace mojo
179 extern "C" AURA_DEMO_EXPORT MojoResult CDECL MojoMain(
180 MojoHandle service_provider_handle) {
181 base::CommandLine::Init(0, NULL);
182 base::AtExitManager at_exit;
183 base::MessageLoop loop;
184 mojo::GLES2Initializer gles2;
186 // TODO(beng): This crashes in a DCHECK on X11 because this thread's
187 // MessageLoop is not of TYPE_UI. I think we need a way to build
188 // Aura that doesn't define platform-specific stuff.
189 aura::Env::CreateInstance(true);
190 mojo::examples::AuraDemo app(service_provider_handle);
191 loop.Run();
193 return MOJO_RESULT_OK;