aura_demo.cc revision 4ad1aa43a48567659193a298fad74f55e00b3dd9
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/cpp/gles2/gles2.h" 15#include "mojo/public/cpp/system/core.h" 16#include "mojo/public/shell/application.h" 17#include "mojo/public/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" 26 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 36 37namespace mojo { 38namespace examples { 39 40// Trivial WindowDelegate implementation that draws a colored background. 41class DemoWindowDelegate : public aura::WindowDelegate { 42 public: 43 explicit DemoWindowDelegate(SkColor color) : color_(color) {} 44 45 // Overridden from WindowDelegate: 46 virtual gfx::Size GetMinimumSize() const OVERRIDE { 47 return gfx::Size(); 48 } 49 50 virtual gfx::Size GetMaximumSize() const OVERRIDE { 51 return gfx::Size(); 52 } 53 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; 58 } 59 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { 60 return HTCAPTION; 61 } 62 virtual bool ShouldDescendIntoChildForEventHandling( 63 aura::Window* child, 64 const gfx::Point& location) OVERRIDE { 65 return true; 66 } 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); 71 } 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 {} 78 79 private: 80 SkColor color_; 81 82 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate); 83}; 84 85class DemoWindowTreeClient : public aura::client::WindowTreeClient { 86 public: 87 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) { 88 aura::client::SetWindowTreeClient(window_, this); 89 } 90 91 virtual ~DemoWindowTreeClient() { 92 aura::client::SetWindowTreeClient(window_, NULL); 93 } 94 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())); 102 } 103 return window_; 104 } 105 106 private: 107 aura::Window* window_; 108 109 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_; 110 111 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient); 112}; 113 114class AuraDemo : public Application { 115 public: 116 explicit AuraDemo(MojoHandle shell_handle) : Application(shell_handle) { 117 screen_.reset(DemoScreen::Create()); 118 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); 119 120 InterfacePipe<NativeViewport, AnyInterface> pipe; 121 122 mojo::AllocationScope scope; 123 shell()->Connect("mojo:mojo_native_viewport_service", 124 pipe.handle_to_peer.Pass()); 125 window_tree_host_.reset(new WindowTreeHostMojo( 126 pipe.handle_to_self.Pass(), 127 gfx::Rect(800, 600), 128 base::Bind(&AuraDemo::HostContextCreated, base::Unretained(this)))); 129 } 130 131 private: 132 void HostContextCreated() { 133 window_tree_host_->InitHost(); 134 135 window_tree_client_.reset( 136 new DemoWindowTreeClient(window_tree_host_->window())); 137 138 delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE)); 139 window1_ = new aura::Window(delegate1_.get()); 140 window1_->Init(aura::WINDOW_LAYER_TEXTURED); 141 window1_->SetBounds(gfx::Rect(100, 100, 400, 400)); 142 window1_->Show(); 143 window_tree_host_->window()->AddChild(window1_); 144 145 delegate2_.reset(new DemoWindowDelegate(SK_ColorRED)); 146 window2_ = new aura::Window(delegate2_.get()); 147 window2_->Init(aura::WINDOW_LAYER_TEXTURED); 148 window2_->SetBounds(gfx::Rect(200, 200, 350, 350)); 149 window2_->Show(); 150 window_tree_host_->window()->AddChild(window2_); 151 152 delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN)); 153 window21_ = new aura::Window(delegate21_.get()); 154 window21_->Init(aura::WINDOW_LAYER_TEXTURED); 155 window21_->SetBounds(gfx::Rect(10, 10, 50, 50)); 156 window21_->Show(); 157 window2_->AddChild(window21_); 158 159 window_tree_host_->Show(); 160 } 161 162 scoped_ptr<DemoScreen> screen_; 163 164 scoped_ptr<DemoWindowTreeClient> window_tree_client_; 165 166 scoped_ptr<DemoWindowDelegate> delegate1_; 167 scoped_ptr<DemoWindowDelegate> delegate2_; 168 scoped_ptr<DemoWindowDelegate> delegate21_; 169 170 aura::Window* window1_; 171 aura::Window* window2_; 172 aura::Window* window21_; 173 174 scoped_ptr<aura::WindowTreeHost> window_tree_host_; 175}; 176 177} // namespace examples 178} // namespace mojo 179 180extern "C" AURA_DEMO_EXPORT MojoResult CDECL MojoMain( 181 MojoHandle shell_handle) { 182 CommandLine::Init(0, NULL); 183 base::AtExitManager at_exit; 184 base::MessageLoop loop; 185 mojo::GLES2Initializer gles2; 186 187 // TODO(beng): This crashes in a DCHECK on X11 because this thread's 188 // MessageLoop is not of TYPE_UI. I think we need a way to build 189 // Aura that doesn't define platform-specific stuff. 190 aura::Env::CreateInstance(); 191 mojo::examples::AuraDemo app(shell_handle); 192 loop.Run(); 193 194 return MOJO_RESULT_OK; 195} 196