Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / ui / aura / demo / demo_main.cc
blobfe6ea80003c1276a136dcf6fc75fa51455d4d435
1 // Copyright (c) 2012 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 "base/at_exit.h"
6 #include "base/command_line.h"
7 #include "base/i18n/icu_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "third_party/skia/include/core/SkXfermode.h"
11 #include "ui/aura/client/default_capture_client.h"
12 #include "ui/aura/client/window_tree_client.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/test/test_focus_client.h"
15 #include "ui/aura/test/test_screen.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_delegate.h"
18 #include "ui/aura/window_tree_host.h"
19 #include "ui/base/hit_test.h"
20 #include "ui/compositor/test/in_process_context_factory.h"
21 #include "ui/events/event.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/rect.h"
24 #include "ui/gl/gl_surface.h"
26 #if defined(USE_X11)
27 #include "ui/gfx/x/x11_connection.h"
28 #endif
30 #if defined(OS_WIN)
31 #include "ui/gfx/win/dpi.h"
32 #endif
34 namespace {
36 // Trivial WindowDelegate implementation that draws a colored background.
37 class DemoWindowDelegate : public aura::WindowDelegate {
38 public:
39 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
41 // Overridden from WindowDelegate:
42 gfx::Size GetMinimumSize() const override { return gfx::Size(); }
44 gfx::Size GetMaximumSize() const override { return gfx::Size(); }
46 void OnBoundsChanged(const gfx::Rect& old_bounds,
47 const gfx::Rect& new_bounds) override {}
48 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
49 return gfx::kNullCursor;
51 int GetNonClientComponent(const gfx::Point& point) const override {
52 return HTCAPTION;
54 bool ShouldDescendIntoChildForEventHandling(
55 aura::Window* child,
56 const gfx::Point& location) override {
57 return true;
59 bool CanFocus() override { return true; }
60 void OnCaptureLost() override {}
61 void OnPaint(gfx::Canvas* canvas) override {
62 canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
64 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
65 void OnWindowDestroying(aura::Window* window) override {}
66 void OnWindowDestroyed(aura::Window* window) override {}
67 void OnWindowTargetVisibilityChanged(bool visible) override {}
68 bool HasHitTestMask() const override { return false; }
69 void GetHitTestMask(gfx::Path* mask) const override {}
71 private:
72 SkColor color_;
74 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
77 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
78 public:
79 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
80 aura::client::SetWindowTreeClient(window_, this);
83 ~DemoWindowTreeClient() override {
84 aura::client::SetWindowTreeClient(window_, NULL);
87 // Overridden from aura::client::WindowTreeClient:
88 aura::Window* GetDefaultParent(aura::Window* context,
89 aura::Window* window,
90 const gfx::Rect& bounds) override {
91 if (!capture_client_) {
92 capture_client_.reset(
93 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
95 return window_;
98 private:
99 aura::Window* window_;
101 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
103 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
106 int DemoMain() {
107 #if defined(USE_X11)
108 // This demo uses InProcessContextFactory which uses X on a separate Gpu
109 // thread.
110 gfx::InitializeThreadedX11();
111 #endif
113 gfx::GLSurface::InitializeOneOff();
115 #if defined(OS_WIN)
116 gfx::InitDeviceScaleFactor(1.0f);
117 #endif
119 // The ContextFactory must exist before any Compositors are created.
120 scoped_ptr<ui::InProcessContextFactory> context_factory(
121 new ui::InProcessContextFactory());
122 context_factory->set_use_test_surface(false);
124 // Create the message-loop here before creating the root window.
125 base::MessageLoopForUI message_loop;
127 aura::Env::CreateInstance(true);
128 aura::Env::GetInstance()->set_context_factory(context_factory.get());
129 scoped_ptr<aura::TestScreen> test_screen(
130 aura::TestScreen::Create(gfx::Size()));
131 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get());
132 scoped_ptr<aura::WindowTreeHost> host(
133 test_screen->CreateHostForPrimaryDisplay());
134 scoped_ptr<DemoWindowTreeClient> window_tree_client(
135 new DemoWindowTreeClient(host->window()));
136 aura::test::TestFocusClient focus_client;
137 aura::client::SetFocusClient(host->window(), &focus_client);
139 // Create a hierarchy of test windows.
140 DemoWindowDelegate window_delegate1(SK_ColorBLUE);
141 aura::Window window1(&window_delegate1);
142 window1.set_id(1);
143 window1.Init(aura::WINDOW_LAYER_TEXTURED);
144 window1.SetBounds(gfx::Rect(100, 100, 400, 400));
145 window1.Show();
146 aura::client::ParentWindowWithContext(&window1, host->window(), gfx::Rect());
148 DemoWindowDelegate window_delegate2(SK_ColorRED);
149 aura::Window window2(&window_delegate2);
150 window2.set_id(2);
151 window2.Init(aura::WINDOW_LAYER_TEXTURED);
152 window2.SetBounds(gfx::Rect(200, 200, 350, 350));
153 window2.Show();
154 aura::client::ParentWindowWithContext(&window2, host->window(), gfx::Rect());
156 DemoWindowDelegate window_delegate3(SK_ColorGREEN);
157 aura::Window window3(&window_delegate3);
158 window3.set_id(3);
159 window3.Init(aura::WINDOW_LAYER_TEXTURED);
160 window3.SetBounds(gfx::Rect(10, 10, 50, 50));
161 window3.Show();
162 window2.AddChild(&window3);
164 host->Show();
165 base::MessageLoopForUI::current()->Run();
167 return 0;
170 } // namespace
172 int main(int argc, char** argv) {
173 CommandLine::Init(argc, argv);
175 // The exit manager is in charge of calling the dtors of singleton objects.
176 base::AtExitManager exit_manager;
178 base::i18n::InitializeICU();
180 return DemoMain();