aura_demo.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5#include <stdio.h>
6#include <string>
7
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/window_tree_host_mojo.h"
13#include "mojo/public/bindings/allocation_scope.h"
14#include "mojo/public/gles2/gles2_cpp.h"
15#include "mojo/public/shell/application.h"
16#include "mojo/public/shell/shell.mojom.h"
17#include "mojo/public/system/core.h"
18#include "mojo/public/system/macros.h"
19#include "mojo/services/native_viewport/native_viewport.mojom.h"
20#include "ui/aura/client/default_capture_client.h"
21#include "ui/aura/client/window_tree_client.h"
22#include "ui/aura/env.h"
23#include "ui/aura/window.h"
24#include "ui/aura/window_delegate.h"
25#include "ui/base/hit_test.h"
26#include "ui/gfx/canvas.h"
27
28#if defined(WIN32)
29#if !defined(CDECL)
30#define CDECL __cdecl
31#endif
32#define AURA_DEMO_EXPORT __declspec(dllexport)
33#else
34#define CDECL
35#define AURA_DEMO_EXPORT __attribute__((visibility("default")))
36#endif
37
38namespace mojo {
39namespace examples {
40
41// Trivial WindowDelegate implementation that draws a colored background.
42class DemoWindowDelegate : public aura::WindowDelegate {
43 public:
44  explicit DemoWindowDelegate(SkColor color) : color_(color) {}
45
46  // Overridden from WindowDelegate:
47  virtual gfx::Size GetMinimumSize() const OVERRIDE {
48    return gfx::Size();
49  }
50
51  virtual gfx::Size GetMaximumSize() const OVERRIDE {
52    return gfx::Size();
53  }
54
55  virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
56                               const gfx::Rect& new_bounds) OVERRIDE {}
57  virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
58    return gfx::kNullCursor;
59  }
60  virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
61    return HTCAPTION;
62  }
63  virtual bool ShouldDescendIntoChildForEventHandling(
64      aura::Window* child,
65      const gfx::Point& location) OVERRIDE {
66    return true;
67  }
68  virtual bool CanFocus() OVERRIDE { return true; }
69  virtual void OnCaptureLost() OVERRIDE {}
70  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
71    canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
72  }
73  virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
74  virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
75  virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {}
76  virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
77  virtual bool HasHitTestMask() const OVERRIDE { return false; }
78  virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
79  virtual void DidRecreateLayer(ui::Layer* old_layer,
80                                ui::Layer* new_layer) OVERRIDE {}
81
82 private:
83  SkColor color_;
84
85  DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
86};
87
88class DemoWindowTreeClient : public aura::client::WindowTreeClient {
89 public:
90  explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
91    aura::client::SetWindowTreeClient(window_, this);
92  }
93
94  virtual ~DemoWindowTreeClient() {
95    aura::client::SetWindowTreeClient(window_, NULL);
96  }
97
98  // Overridden from aura::client::WindowTreeClient:
99  virtual aura::Window* GetDefaultParent(aura::Window* context,
100                                         aura::Window* window,
101                                         const gfx::Rect& bounds) OVERRIDE {
102    if (!capture_client_) {
103      capture_client_.reset(
104          new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
105    }
106    return window_;
107  }
108
109 private:
110  aura::Window* window_;
111
112  scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
113
114  DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
115};
116
117class AuraDemo : public Application {
118 public:
119  explicit AuraDemo(MojoHandle shell_handle) : Application(shell_handle) {
120    screen_.reset(DemoScreen::Create());
121    gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
122
123    InterfacePipe<NativeViewport, AnyInterface> pipe;
124
125    mojo::AllocationScope scope;
126    shell()->Connect("mojo:mojo_native_viewport_service",
127                     pipe.handle_to_peer.Pass());
128    window_tree_host_.reset(new WindowTreeHostMojo(
129        pipe.handle_to_self.Pass(),
130        gfx::Rect(800, 600),
131        base::Bind(&AuraDemo::HostContextCreated, base::Unretained(this))));
132  }
133
134 private:
135  void HostContextCreated() {
136    window_tree_host_->InitHost();
137
138    window_tree_client_.reset(
139        new DemoWindowTreeClient(window_tree_host_->window()));
140
141    delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE));
142    window1_ = new aura::Window(delegate1_.get());
143    window1_->Init(aura::WINDOW_LAYER_TEXTURED);
144    window1_->SetBounds(gfx::Rect(100, 100, 400, 400));
145    window1_->Show();
146    window_tree_host_->window()->AddChild(window1_);
147
148    delegate2_.reset(new DemoWindowDelegate(SK_ColorRED));
149    window2_ = new aura::Window(delegate2_.get());
150    window2_->Init(aura::WINDOW_LAYER_TEXTURED);
151    window2_->SetBounds(gfx::Rect(200, 200, 350, 350));
152    window2_->Show();
153    window_tree_host_->window()->AddChild(window2_);
154
155    delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN));
156    window21_ = new aura::Window(delegate21_.get());
157    window21_->Init(aura::WINDOW_LAYER_TEXTURED);
158    window21_->SetBounds(gfx::Rect(10, 10, 50, 50));
159    window21_->Show();
160    window2_->AddChild(window21_);
161
162    window_tree_host_->Show();
163  }
164
165  scoped_ptr<DemoScreen> screen_;
166
167  scoped_ptr<DemoWindowTreeClient> window_tree_client_;
168
169  scoped_ptr<DemoWindowDelegate> delegate1_;
170  scoped_ptr<DemoWindowDelegate> delegate2_;
171  scoped_ptr<DemoWindowDelegate> delegate21_;
172
173  aura::Window* window1_;
174  aura::Window* window2_;
175  aura::Window* window21_;
176
177  scoped_ptr<aura::WindowTreeHost> window_tree_host_;
178};
179
180}  // namespace examples
181}  // namespace mojo
182
183extern "C" AURA_DEMO_EXPORT MojoResult CDECL MojoMain(
184    MojoHandle shell_handle) {
185  CommandLine::Init(0, NULL);
186  base::AtExitManager at_exit;
187  base::MessageLoop loop;
188  mojo::GLES2Initializer gles2;
189
190  // TODO(beng): This crashes in a DCHECK on X11 because this thread's
191  //             MessageLoop is not of TYPE_UI. I think we need a way to build
192  //             Aura that doesn't define platform-specific stuff.
193  aura::Env::CreateInstance();
194  mojo::examples::AuraDemo app(shell_handle);
195  loop.Run();
196
197  return MOJO_RESULT_OK;
198}
199