Fix OOP <webview> resize and autosize.
[chromium-blink-merge.git] / ui / aura / demo / demo_main.cc
blob689f87dd2367d3011b05524f21fc683b4767dc07
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 "base/power_monitor/power_monitor.h"
11 #include "base/power_monitor/power_monitor_device_source.h"
12 #include "third_party/skia/include/core/SkXfermode.h"
13 #include "ui/aura/client/default_capture_client.h"
14 #include "ui/aura/client/window_tree_client.h"
15 #include "ui/aura/env.h"
16 #include "ui/aura/test/test_focus_client.h"
17 #include "ui/aura/test/test_screen.h"
18 #include "ui/aura/window.h"
19 #include "ui/aura/window_delegate.h"
20 #include "ui/aura/window_tree_host.h"
21 #include "ui/base/hit_test.h"
22 #include "ui/compositor/paint_recorder.h"
23 #include "ui/compositor/test/in_process_context_factory.h"
24 #include "ui/events/event.h"
25 #include "ui/gfx/canvas.h"
26 #include "ui/gfx/geometry/rect.h"
27 #include "ui/gfx/skia_util.h"
28 #include "ui/gl/gl_surface.h"
30 #if defined(USE_X11)
31 #include "ui/gfx/x/x11_connection.h"
32 #endif
34 #if defined(OS_WIN)
35 #include "ui/gfx/win/dpi.h"
36 #endif
38 namespace {
40 // Trivial WindowDelegate implementation that draws a colored background.
41 class DemoWindowDelegate : public aura::WindowDelegate {
42 public:
43 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
45 // Overridden from WindowDelegate:
46 gfx::Size GetMinimumSize() const override { return gfx::Size(); }
48 gfx::Size GetMaximumSize() const override { return gfx::Size(); }
50 void OnBoundsChanged(const gfx::Rect& old_bounds,
51 const gfx::Rect& new_bounds) override {
52 window_bounds_ = new_bounds;
54 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
55 return gfx::kNullCursor;
57 int GetNonClientComponent(const gfx::Point& point) const override {
58 return HTCAPTION;
60 bool ShouldDescendIntoChildForEventHandling(
61 aura::Window* child,
62 const gfx::Point& location) override {
63 return true;
65 bool CanFocus() override { return true; }
66 void OnCaptureLost() override {}
67 void OnPaint(const ui::PaintContext& context) override {
68 ui::PaintRecorder recorder(context, window_bounds_.size());
69 recorder.canvas()->DrawColor(color_, SkXfermode::kSrc_Mode);
70 gfx::Rect r;
71 recorder.canvas()->GetClipBounds(&r);
72 // Fill with a non-solid color so that the compositor will exercise its
73 // texture upload path.
74 while (!r.IsEmpty()) {
75 r.Inset(2, 2);
76 recorder.canvas()->FillRect(r, color_, SkXfermode::kXor_Mode);
79 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
80 void OnWindowDestroying(aura::Window* window) override {}
81 void OnWindowDestroyed(aura::Window* window) override {}
82 void OnWindowTargetVisibilityChanged(bool visible) override {}
83 bool HasHitTestMask() const override { return false; }
84 void GetHitTestMask(gfx::Path* mask) const override {}
86 private:
87 SkColor color_;
88 gfx::Rect window_bounds_;
90 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
93 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
94 public:
95 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
96 aura::client::SetWindowTreeClient(window_, this);
99 ~DemoWindowTreeClient() override {
100 aura::client::SetWindowTreeClient(window_, nullptr);
103 // Overridden from aura::client::WindowTreeClient:
104 aura::Window* GetDefaultParent(aura::Window* context,
105 aura::Window* window,
106 const gfx::Rect& bounds) override {
107 if (!capture_client_) {
108 capture_client_.reset(
109 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
111 return window_;
114 private:
115 aura::Window* window_;
117 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
119 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
122 int DemoMain() {
123 #if defined(USE_X11)
124 // This demo uses InProcessContextFactory which uses X on a separate Gpu
125 // thread.
126 gfx::InitializeThreadedX11();
127 #endif
129 gfx::GLSurface::InitializeOneOff();
131 #if defined(OS_WIN)
132 gfx::InitDeviceScaleFactor(1.0f);
133 #endif
135 // The ContextFactory must exist before any Compositors are created.
136 bool context_factory_for_test = false;
137 scoped_ptr<ui::InProcessContextFactory> context_factory(
138 new ui::InProcessContextFactory(context_factory_for_test, nullptr));
139 context_factory->set_use_test_surface(false);
141 // Create the message-loop here before creating the root window.
142 base::MessageLoopForUI message_loop;
144 base::PowerMonitor power_monitor(make_scoped_ptr(
145 new base::PowerMonitorDeviceSource));
147 aura::Env::CreateInstance(true);
148 aura::Env::GetInstance()->set_context_factory(context_factory.get());
149 scoped_ptr<aura::TestScreen> test_screen(
150 aura::TestScreen::Create(gfx::Size()));
151 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get());
152 scoped_ptr<aura::WindowTreeHost> host(
153 test_screen->CreateHostForPrimaryDisplay());
154 scoped_ptr<DemoWindowTreeClient> window_tree_client(
155 new DemoWindowTreeClient(host->window()));
156 aura::test::TestFocusClient focus_client;
157 aura::client::SetFocusClient(host->window(), &focus_client);
159 // Create a hierarchy of test windows.
160 gfx::Rect window1_bounds(100, 100, 400, 400);
161 DemoWindowDelegate window_delegate1(SK_ColorBLUE);
162 aura::Window window1(&window_delegate1);
163 window1.set_id(1);
164 window1.Init(ui::LAYER_TEXTURED);
165 window1.SetBounds(window1_bounds);
166 window1.Show();
167 aura::client::ParentWindowWithContext(&window1, host->window(), gfx::Rect());
169 gfx::Rect window2_bounds(200, 200, 350, 350);
170 DemoWindowDelegate window_delegate2(SK_ColorRED);
171 aura::Window window2(&window_delegate2);
172 window2.set_id(2);
173 window2.Init(ui::LAYER_TEXTURED);
174 window2.SetBounds(window2_bounds);
175 window2.Show();
176 aura::client::ParentWindowWithContext(&window2, host->window(), gfx::Rect());
178 gfx::Rect window3_bounds(10, 10, 50, 50);
179 DemoWindowDelegate window_delegate3(SK_ColorGREEN);
180 aura::Window window3(&window_delegate3);
181 window3.set_id(3);
182 window3.Init(ui::LAYER_TEXTURED);
183 window3.SetBounds(window3_bounds);
184 window3.Show();
185 window2.AddChild(&window3);
187 host->Show();
188 base::MessageLoopForUI::current()->Run();
190 return 0;
193 } // namespace
195 int main(int argc, char** argv) {
196 base::CommandLine::Init(argc, argv);
198 // The exit manager is in charge of calling the dtors of singleton objects.
199 base::AtExitManager exit_manager;
201 base::i18n::InitializeICU();
203 return DemoMain();