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/bind.h"
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"
29
30 namespace mojo {
31 namespace examples {
32
33 // Trivial WindowDelegate implementation that draws a colored background.
34 class DemoWindowDelegate : public aura::WindowDelegate {
35 public:
DemoWindowDelegate(SkColor color)36 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
37
38 // Overridden from WindowDelegate:
GetMinimumSize() const39 virtual gfx::Size GetMinimumSize() const OVERRIDE {
40 return gfx::Size();
41 }
42
GetMaximumSize() const43 virtual gfx::Size GetMaximumSize() const OVERRIDE {
44 return gfx::Size();
45 }
46
OnBoundsChanged(const gfx::Rect & old_bounds,const gfx::Rect & new_bounds)47 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
48 const gfx::Rect& new_bounds) OVERRIDE {}
GetCursor(const gfx::Point & point)49 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
50 return gfx::kNullCursor;
51 }
GetNonClientComponent(const gfx::Point & point) const52 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
53 return HTCAPTION;
54 }
ShouldDescendIntoChildForEventHandling(aura::Window * child,const gfx::Point & location)55 virtual bool ShouldDescendIntoChildForEventHandling(
56 aura::Window* child,
57 const gfx::Point& location) OVERRIDE {
58 return true;
59 }
CanFocus()60 virtual bool CanFocus() OVERRIDE { return true; }
OnCaptureLost()61 virtual void OnCaptureLost() OVERRIDE {}
OnPaint(gfx::Canvas * canvas)62 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
63 canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
64 }
OnDeviceScaleFactorChanged(float device_scale_factor)65 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
OnWindowDestroying(aura::Window * window)66 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
OnWindowDestroyed(aura::Window * window)67 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {}
OnWindowTargetVisibilityChanged(bool visible)68 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
HasHitTestMask() const69 virtual bool HasHitTestMask() const OVERRIDE { return false; }
GetHitTestMask(gfx::Path * mask) const70 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
71
72 private:
73 const SkColor color_;
74
75 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
76 };
77
78 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
79 public:
DemoWindowTreeClient(aura::Window * window)80 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
81 aura::client::SetWindowTreeClient(window_, this);
82 }
83
~DemoWindowTreeClient()84 virtual ~DemoWindowTreeClient() {
85 aura::client::SetWindowTreeClient(window_, NULL);
86 }
87
88 // Overridden from aura::client::WindowTreeClient:
GetDefaultParent(aura::Window * context,aura::Window * window,const gfx::Rect & bounds)89 virtual aura::Window* GetDefaultParent(aura::Window* context,
90 aura::Window* window,
91 const gfx::Rect& bounds) OVERRIDE {
92 if (!capture_client_) {
93 capture_client_.reset(
94 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
95 }
96 return window_;
97 }
98
99 private:
100 aura::Window* window_;
101 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
102
103 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
104 };
105
106 class AuraDemo : public Application,
107 public WindowTreeHostMojoDelegate,
108 public view_manager::ViewManagerDelegate {
109 public:
AuraDemo()110 AuraDemo()
111 : window1_(NULL),
112 window2_(NULL),
113 window21_(NULL),
114 view_(NULL) {
115 view_manager::ViewManager::Create(this, this);
116 }
~AuraDemo()117 virtual ~AuraDemo() {}
118
119 private:
120 // Overridden from view_manager::ViewManagerDelegate:
OnRootAdded(view_manager::ViewManager * view_manager,view_manager::Node * root)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_);
126
127 window_tree_host_.reset(new WindowTreeHostMojo(root, this));
128 window_tree_host_->InitHost();
129
130 window_tree_client_.reset(
131 new DemoWindowTreeClient(window_tree_host_->window()));
132
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));
137 window1_->Show();
138 window_tree_host_->window()->AddChild(window1_);
139
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));
144 window2_->Show();
145 window_tree_host_->window()->AddChild(window2_);
146
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));
151 window21_->Show();
152 window2_->AddChild(window21_);
153
154 window_tree_host_->Show();
155 }
156
157 // WindowTreeHostMojoDelegate:
CompositorContentsChanged(const SkBitmap & bitmap)158 virtual void CompositorContentsChanged(const SkBitmap& bitmap) OVERRIDE {
159 view_->SetContents(bitmap);
160 }
161
Initialize()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());
168 }
169
170 scoped_ptr<DemoWindowTreeClient> window_tree_client_;
171
172 scoped_ptr<ui::ContextFactory> context_factory_;
173
174 scoped_ptr<ScreenMojo> screen_;
175
176 scoped_ptr<DemoWindowDelegate> delegate1_;
177 scoped_ptr<DemoWindowDelegate> delegate2_;
178 scoped_ptr<DemoWindowDelegate> delegate21_;
179
180 aura::Window* window1_;
181 aura::Window* window2_;
182 aura::Window* window21_;
183
184 view_manager::View* view_;
185
186 scoped_ptr<aura::WindowTreeHost> window_tree_host_;
187
188 DISALLOW_COPY_AND_ASSIGN(AuraDemo);
189 };
190
191 } // namespace examples
192
193 // static
Create()194 Application* Application::Create() {
195 return new examples::AuraDemo();
196 }
197
198 } // namespace mojo
199