blob: 5200b012c126fb8181515d37d783e808de3fb302 [file] [log] [blame]
[email protected]538f9562012-01-17 17:46:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]81585f32011-07-29 19:32:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]81585f32011-07-29 19:32:065#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"
[email protected]59e69e742013-06-18 20:27:529#include "base/message_loop/message_loop.h"
[email protected]81585f32011-07-29 19:32:0610#include "third_party/skia/include/core/SkXfermode.h"
[email protected]48dea152012-11-02 20:26:4611#include "ui/aura/client/default_capture_client.h"
[email protected]e3225e02013-10-23 20:44:3712#include "ui/aura/client/window_tree_client.h"
[email protected]e050ef142012-03-21 01:04:2413#include "ui/aura/env.h"
[email protected]980969542013-10-18 22:07:3414#include "ui/aura/test/test_focus_client.h"
[email protected]6bdf7952012-11-14 10:10:5815#include "ui/aura/test/test_screen.h"
[email protected]dbf3d402011-09-14 21:32:1616#include "ui/aura/window.h"
17#include "ui/aura/window_delegate.h"
[email protected]2f2620332014-02-28 10:07:3818#include "ui/aura/window_tree_host.h"
[email protected]912be0e2011-11-09 19:05:2419#include "ui/base/hit_test.h"
[email protected]27a80e752014-03-13 20:36:4120#include "ui/compositor/test/in_process_context_factory.h"
[email protected]86ccbd42013-09-18 18:11:5421#include "ui/events/event.h"
[email protected]94a0d2582012-03-09 00:30:5922#include "ui/gfx/canvas.h"
tfarina3b0452d2014-12-31 15:20:0923#include "ui/gfx/geometry/rect.h"
[email protected]af7c5d92014-02-03 19:53:1524#include "ui/gl/gl_surface.h"
[email protected]81585f32011-07-29 19:32:0625
[email protected]4a6bef32011-09-07 02:06:0126#if defined(USE_X11)
[email protected]82f884d6ffb2014-04-15 03:34:5427#include "ui/gfx/x/x11_connection.h"
[email protected]4a6bef32011-09-07 02:06:0128#endif
29
[email protected]804f4792014-04-22 22:54:3730#if defined(OS_WIN)
31#include "ui/gfx/win/dpi.h"
32#endif
33
[email protected]81585f32011-07-29 19:32:0634namespace {
35
[email protected]8d8c7732011-08-25 22:35:1336// Trivial WindowDelegate implementation that draws a colored background.
[email protected]81585f32011-07-29 19:32:0637class DemoWindowDelegate : public aura::WindowDelegate {
38 public:
[email protected]8d8c7732011-08-25 22:35:1339 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
[email protected]81585f32011-07-29 19:32:0640
[email protected]8dd791d2011-09-16 16:37:3041 // Overridden from WindowDelegate:
dcheng5bdeb6b2014-10-27 20:58:0542 gfx::Size GetMinimumSize() const override { return gfx::Size(); }
[email protected]fa574b62012-11-26 04:11:3843
dcheng5bdeb6b2014-10-27 20:58:0544 gfx::Size GetMaximumSize() const override { return gfx::Size(); }
[email protected]fa574b62012-11-26 04:11:3845
dcheng5bdeb6b2014-10-27 20:58:0546 void OnBoundsChanged(const gfx::Rect& old_bounds,
47 const gfx::Rect& new_bounds) override {}
yukishiino907eabf2015-01-28 04:31:1248 ui::TextInputClient* GetFocusedTextInputClient() override { return nullptr; }
dcheng5bdeb6b2014-10-27 20:58:0549 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
[email protected]62c9f102014-03-27 06:07:0450 return gfx::kNullCursor;
51 }
dcheng5bdeb6b2014-10-27 20:58:0552 int GetNonClientComponent(const gfx::Point& point) const override {
[email protected]eaf6dd52011-09-02 00:39:2653 return HTCAPTION;
54 }
dcheng5bdeb6b2014-10-27 20:58:0555 bool ShouldDescendIntoChildForEventHandling(
[email protected]334b81892012-06-16 16:38:1456 aura::Window* child,
mostynb3b3d52b2014-10-09 10:54:2757 const gfx::Point& location) override {
[email protected]334b81892012-06-16 16:38:1458 return true;
59 }
dcheng5bdeb6b2014-10-27 20:58:0560 bool CanFocus() override { return true; }
61 void OnCaptureLost() override {}
62 void OnPaint(gfx::Canvas* canvas) override {
[email protected]6fae7972012-04-10 20:12:0863 canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
[email protected]8d8c7732011-08-25 22:35:1364 }
dcheng5bdeb6b2014-10-27 20:58:0565 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
66 void OnWindowDestroying(aura::Window* window) override {}
67 void OnWindowDestroyed(aura::Window* window) override {}
68 void OnWindowTargetVisibilityChanged(bool visible) override {}
69 bool HasHitTestMask() const override { return false; }
70 void GetHitTestMask(gfx::Path* mask) const override {}
[email protected]970aa362011-08-30 20:03:3471
[email protected]81585f32011-07-29 19:32:0672 private:
[email protected]b1b155512011-08-18 22:47:5073 SkColor color_;
74
[email protected]81585f32011-07-29 19:32:0675 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
76};
77
[email protected]e3225e02013-10-23 20:44:3778class DemoWindowTreeClient : public aura::client::WindowTreeClient {
[email protected]b370f4f2012-03-12 20:16:2379 public:
[email protected]e3225e02013-10-23 20:44:3780 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
81 aura::client::SetWindowTreeClient(window_, this);
[email protected]b370f4f2012-03-12 20:16:2382 }
83
dcheng5bdeb6b2014-10-27 20:58:0584 ~DemoWindowTreeClient() override {
yukishiino907eabf2015-01-28 04:31:1285 aura::client::SetWindowTreeClient(window_, nullptr);
[email protected]b370f4f2012-03-12 20:16:2386 }
87
[email protected]e3225e02013-10-23 20:44:3788 // Overridden from aura::client::WindowTreeClient:
dcheng5bdeb6b2014-10-27 20:58:0589 aura::Window* GetDefaultParent(aura::Window* context,
90 aura::Window* window,
91 const gfx::Rect& bounds) override {
[email protected]363e7542013-04-23 22:33:1492 if (!capture_client_) {
[email protected]be2a0ac2012-07-24 11:23:2293 capture_client_.reset(
[email protected]e3225e02013-10-23 20:44:3794 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
[email protected]be2a0ac2012-07-24 11:23:2295 }
[email protected]e3225e02013-10-23 20:44:3796 return window_;
[email protected]b370f4f2012-03-12 20:16:2397 }
98
99 private:
[email protected]e3225e02013-10-23 20:44:37100 aura::Window* window_;
[email protected]b370f4f2012-03-12 20:16:23101
[email protected]48dea152012-11-02 20:26:46102 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
[email protected]be2a0ac2012-07-24 11:23:22103
[email protected]e3225e02013-10-23 20:44:37104 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
[email protected]b370f4f2012-03-12 20:16:23105};
[email protected]81585f32011-07-29 19:32:06106
[email protected]f7a0b4d22012-09-18 16:31:01107int DemoMain() {
[email protected]27a80e752014-03-13 20:36:41108#if defined(USE_X11)
109 // This demo uses InProcessContextFactory which uses X on a separate Gpu
110 // thread.
[email protected]82f884d6ffb2014-04-15 03:34:54111 gfx::InitializeThreadedX11();
[email protected]27a80e752014-03-13 20:36:41112#endif
[email protected]f321f80b2013-09-11 22:59:14113
[email protected]af7c5d92014-02-03 19:53:15114 gfx::GLSurface::InitializeOneOff();
115
[email protected]804f4792014-04-22 22:54:37116#if defined(OS_WIN)
117 gfx::InitDeviceScaleFactor(1.0f);
118#endif
119
[email protected]f321f80b2013-09-11 22:59:14120 // The ContextFactory must exist before any Compositors are created.
weiliangc5efa0a12015-01-29 19:56:46121 bool context_factory_for_test = false;
[email protected]27a80e752014-03-13 20:36:41122 scoped_ptr<ui::InProcessContextFactory> context_factory(
jbaumanf400ce532015-02-03 04:54:59123 new ui::InProcessContextFactory(context_factory_for_test, nullptr));
skybd253e82014-11-11 01:49:07124 context_factory->set_use_test_surface(false);
[email protected]27a80e752014-03-13 20:36:41125
126 // Create the message-loop here before creating the root window.
127 base::MessageLoopForUI message_loop;
[email protected]f321f80b2013-09-11 22:59:14128
[email protected]5b883abb2014-05-05 06:44:10129 aura::Env::CreateInstance(true);
[email protected]fa69f2b62014-05-22 21:47:58130 aura::Env::GetInstance()->set_context_factory(context_factory.get());
[email protected]ad3a62c2014-06-20 13:18:34131 scoped_ptr<aura::TestScreen> test_screen(
132 aura::TestScreen::Create(gfx::Size()));
[email protected]2ad669a2013-02-15 04:36:56133 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get());
[email protected]2f2620332014-02-28 10:07:38134 scoped_ptr<aura::WindowTreeHost> host(
135 test_screen->CreateHostForPrimaryDisplay());
136 scoped_ptr<DemoWindowTreeClient> window_tree_client(
137 new DemoWindowTreeClient(host->window()));
[email protected]980969542013-10-18 22:07:34138 aura::test::TestFocusClient focus_client;
[email protected]2f2620332014-02-28 10:07:38139 aura::client::SetFocusClient(host->window(), &focus_client);
[email protected]81585f32011-07-29 19:32:06140
[email protected]b1b155512011-08-18 22:47:50141 // Create a hierarchy of test windows.
[email protected]8d8c7732011-08-25 22:35:13142 DemoWindowDelegate window_delegate1(SK_ColorBLUE);
143 aura::Window window1(&window_delegate1);
[email protected]b1b155512011-08-18 22:47:50144 window1.set_id(1);
[email protected]52b02a72014-01-08 21:41:50145 window1.Init(aura::WINDOW_LAYER_TEXTURED);
[email protected]7fca53d42011-09-29 15:38:12146 window1.SetBounds(gfx::Rect(100, 100, 400, 400));
[email protected]a3929aba2011-09-30 18:30:17147 window1.Show();
[email protected]2f2620332014-02-28 10:07:38148 aura::client::ParentWindowWithContext(&window1, host->window(), gfx::Rect());
[email protected]b1b155512011-08-18 22:47:50149
[email protected]8d8c7732011-08-25 22:35:13150 DemoWindowDelegate window_delegate2(SK_ColorRED);
151 aura::Window window2(&window_delegate2);
[email protected]b1b155512011-08-18 22:47:50152 window2.set_id(2);
[email protected]52b02a72014-01-08 21:41:50153 window2.Init(aura::WINDOW_LAYER_TEXTURED);
[email protected]7fca53d42011-09-29 15:38:12154 window2.SetBounds(gfx::Rect(200, 200, 350, 350));
[email protected]a3929aba2011-09-30 18:30:17155 window2.Show();
[email protected]2f2620332014-02-28 10:07:38156 aura::client::ParentWindowWithContext(&window2, host->window(), gfx::Rect());
[email protected]b1b155512011-08-18 22:47:50157
[email protected]8d8c7732011-08-25 22:35:13158 DemoWindowDelegate window_delegate3(SK_ColorGREEN);
159 aura::Window window3(&window_delegate3);
[email protected]b1b155512011-08-18 22:47:50160 window3.set_id(3);
[email protected]52b02a72014-01-08 21:41:50161 window3.Init(aura::WINDOW_LAYER_TEXTURED);
[email protected]7fca53d42011-09-29 15:38:12162 window3.SetBounds(gfx::Rect(10, 10, 50, 50));
[email protected]a3929aba2011-09-30 18:30:17163 window3.Show();
[email protected]5ebe6102012-11-28 21:00:03164 window2.AddChild(&window3);
[email protected]b1b155512011-08-18 22:47:50165
[email protected]2f2620332014-02-28 10:07:38166 host->Show();
[email protected]7060d6592013-04-29 19:01:48167 base::MessageLoopForUI::current()->Run();
[email protected]9783633c2011-10-27 15:41:52168
[email protected]81585f32011-07-29 19:32:06169 return 0;
170}
[email protected]f7a0b4d22012-09-18 16:31:01171
[email protected]f7a0b4d22012-09-18 16:31:01172} // namespace
173
174int main(int argc, char** argv) {
avi6b10fd02014-12-23 05:51:23175 base::CommandLine::Init(argc, argv);
[email protected]f7a0b4d22012-09-18 16:31:01176
177 // The exit manager is in charge of calling the dtors of singleton objects.
178 base::AtExitManager exit_manager;
179
[email protected]d7477f02013-08-23 00:39:09180 base::i18n::InitializeICU();
[email protected]f7a0b4d22012-09-18 16:31:01181
[email protected]56deede2012-10-01 21:25:12182 return DemoMain();
[email protected]f7a0b4d22012-09-18 16:31:01183}