| OLD | NEW |
| (Empty) |
| 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 "base/macros.h" | |
| 10 #include "mojo/application/application_runner_chromium.h" | |
| 11 #include "mojo/aura/screen_mojo.h" | |
| 12 #include "mojo/aura/window_tree_host_mojo.h" | |
| 13 #include "mojo/public/c/system/main.h" | |
| 14 #include "mojo/public/cpp/application/application_connection.h" | |
| 15 #include "mojo/public/cpp/application/application_delegate.h" | |
| 16 #include "mojo/public/cpp/application/application_impl.h" | |
| 17 #include "mojo/public/cpp/system/core.h" | |
| 18 #include "mojo/services/native_viewport/public/interfaces/native_viewport.mojom.
h" | |
| 19 #include "mojo/services/view_manager/public/cpp/view.h" | |
| 20 #include "mojo/services/view_manager/public/cpp/view_manager.h" | |
| 21 #include "mojo/services/view_manager/public/cpp/view_manager_client_factory.h" | |
| 22 #include "mojo/services/view_manager/public/cpp/view_manager_delegate.h" | |
| 23 #include "ui/aura/client/default_capture_client.h" | |
| 24 #include "ui/aura/client/window_tree_client.h" | |
| 25 #include "ui/aura/env.h" | |
| 26 #include "ui/aura/window.h" | |
| 27 #include "ui/aura/window_delegate.h" | |
| 28 #include "ui/base/hit_test.h" | |
| 29 #include "ui/gfx/canvas.h" | |
| 30 #include "ui/gfx/codec/png_codec.h" | |
| 31 | |
| 32 namespace examples { | |
| 33 | |
| 34 // Trivial WindowDelegate implementation that draws a colored background. | |
| 35 class DemoWindowDelegate : public aura::WindowDelegate { | |
| 36 public: | |
| 37 explicit DemoWindowDelegate(SkColor color) : color_(color) {} | |
| 38 | |
| 39 // Overridden from WindowDelegate: | |
| 40 virtual gfx::Size GetMinimumSize() const override { | |
| 41 return gfx::Size(); | |
| 42 } | |
| 43 | |
| 44 virtual gfx::Size GetMaximumSize() const override { | |
| 45 return gfx::Size(); | |
| 46 } | |
| 47 | |
| 48 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, | |
| 49 const gfx::Rect& new_bounds) override {} | |
| 50 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) override { | |
| 51 return gfx::kNullCursor; | |
| 52 } | |
| 53 virtual int GetNonClientComponent(const gfx::Point& point) const override { | |
| 54 return HTCAPTION; | |
| 55 } | |
| 56 virtual bool ShouldDescendIntoChildForEventHandling( | |
| 57 aura::Window* child, | |
| 58 const gfx::Point& location) override { | |
| 59 return true; | |
| 60 } | |
| 61 virtual bool CanFocus() override { return true; } | |
| 62 virtual void OnCaptureLost() override {} | |
| 63 virtual void OnPaint(gfx::Canvas* canvas) override { | |
| 64 canvas->DrawColor(color_, SkXfermode::kSrc_Mode); | |
| 65 } | |
| 66 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) override {} | |
| 67 virtual void OnWindowDestroying(aura::Window* window) override {} | |
| 68 virtual void OnWindowDestroyed(aura::Window* window) override {} | |
| 69 virtual void OnWindowTargetVisibilityChanged(bool visible) override {} | |
| 70 virtual bool HasHitTestMask() const override { return false; } | |
| 71 virtual void GetHitTestMask(gfx::Path* mask) const override {} | |
| 72 | |
| 73 private: | |
| 74 const SkColor color_; | |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate); | |
| 77 }; | |
| 78 | |
| 79 class DemoWindowTreeClient : public aura::client::WindowTreeClient { | |
| 80 public: | |
| 81 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) { | |
| 82 aura::client::SetWindowTreeClient(window_, this); | |
| 83 } | |
| 84 | |
| 85 virtual ~DemoWindowTreeClient() { | |
| 86 aura::client::SetWindowTreeClient(window_, NULL); | |
| 87 } | |
| 88 | |
| 89 // Overridden from aura::client::WindowTreeClient: | |
| 90 virtual aura::Window* GetDefaultParent(aura::Window* context, | |
| 91 aura::Window* window, | |
| 92 const gfx::Rect& bounds) override { | |
| 93 if (!capture_client_) { | |
| 94 capture_client_.reset( | |
| 95 new aura::client::DefaultCaptureClient(window_->GetRootWindow())); | |
| 96 } | |
| 97 return window_; | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 aura::Window* window_; | |
| 102 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_; | |
| 103 | |
| 104 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient); | |
| 105 }; | |
| 106 | |
| 107 class AuraDemo : public mojo::ApplicationDelegate, | |
| 108 public mojo::ViewManagerDelegate { | |
| 109 public: | |
| 110 AuraDemo() | |
| 111 : shell_(nullptr), window1_(NULL), window2_(NULL), window21_(NULL) {} | |
| 112 virtual ~AuraDemo() {} | |
| 113 | |
| 114 private: | |
| 115 // Overridden from ViewManagerDelegate: | |
| 116 virtual void OnEmbed( | |
| 117 mojo::View* root, | |
| 118 mojo::ServiceProviderImpl* exported_services, | |
| 119 scoped_ptr<mojo::ServiceProvider> imported_services) override { | |
| 120 // TODO(beng): this function could be called multiple times! | |
| 121 root_ = root; | |
| 122 | |
| 123 window_tree_host_.reset(new mojo::WindowTreeHostMojo(shell_, root)); | |
| 124 window_tree_host_->InitHost(); | |
| 125 | |
| 126 window_tree_client_.reset( | |
| 127 new DemoWindowTreeClient(window_tree_host_->window())); | |
| 128 | |
| 129 delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE)); | |
| 130 window1_ = new aura::Window(delegate1_.get()); | |
| 131 window1_->Init(aura::WINDOW_LAYER_TEXTURED); | |
| 132 window1_->SetBounds(gfx::Rect(100, 100, 400, 400)); | |
| 133 window1_->Show(); | |
| 134 window_tree_host_->window()->AddChild(window1_); | |
| 135 | |
| 136 delegate2_.reset(new DemoWindowDelegate(SK_ColorRED)); | |
| 137 window2_ = new aura::Window(delegate2_.get()); | |
| 138 window2_->Init(aura::WINDOW_LAYER_TEXTURED); | |
| 139 window2_->SetBounds(gfx::Rect(200, 200, 350, 350)); | |
| 140 window2_->Show(); | |
| 141 window_tree_host_->window()->AddChild(window2_); | |
| 142 | |
| 143 delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN)); | |
| 144 window21_ = new aura::Window(delegate21_.get()); | |
| 145 window21_->Init(aura::WINDOW_LAYER_TEXTURED); | |
| 146 window21_->SetBounds(gfx::Rect(10, 10, 50, 50)); | |
| 147 window21_->Show(); | |
| 148 window2_->AddChild(window21_); | |
| 149 | |
| 150 window_tree_host_->Show(); | |
| 151 } | |
| 152 virtual void OnViewManagerDisconnected( | |
| 153 mojo::ViewManager* view_manager) override { | |
| 154 base::MessageLoop::current()->Quit(); | |
| 155 } | |
| 156 | |
| 157 virtual void Initialize(mojo::ApplicationImpl* app) override { | |
| 158 shell_ = app->shell(); | |
| 159 view_manager_client_factory_.reset( | |
| 160 new mojo::ViewManagerClientFactory(shell_, this)); | |
| 161 aura::Env::CreateInstance(true); | |
| 162 screen_.reset(mojo::ScreenMojo::Create()); | |
| 163 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); | |
| 164 } | |
| 165 | |
| 166 virtual bool ConfigureIncomingConnection( | |
| 167 mojo::ApplicationConnection* connection) override { | |
| 168 connection->AddService(view_manager_client_factory_.get()); | |
| 169 return true; | |
| 170 } | |
| 171 | |
| 172 mojo::Shell* shell_; | |
| 173 | |
| 174 scoped_ptr<DemoWindowTreeClient> window_tree_client_; | |
| 175 | |
| 176 scoped_ptr<mojo::ScreenMojo> screen_; | |
| 177 | |
| 178 scoped_ptr<DemoWindowDelegate> delegate1_; | |
| 179 scoped_ptr<DemoWindowDelegate> delegate2_; | |
| 180 scoped_ptr<DemoWindowDelegate> delegate21_; | |
| 181 | |
| 182 aura::Window* window1_; | |
| 183 aura::Window* window2_; | |
| 184 aura::Window* window21_; | |
| 185 | |
| 186 mojo::View* root_; | |
| 187 | |
| 188 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_; | |
| 189 | |
| 190 scoped_ptr<aura::WindowTreeHost> window_tree_host_; | |
| 191 | |
| 192 DISALLOW_COPY_AND_ASSIGN(AuraDemo); | |
| 193 }; | |
| 194 | |
| 195 } // namespace examples | |
| 196 | |
| 197 MojoResult MojoMain(MojoHandle shell_handle) { | |
| 198 mojo::ApplicationRunnerChromium runner(new examples::AuraDemo); | |
| 199 return runner.Run(shell_handle); | |
| 200 } | |
| OLD | NEW |