blob: 7734c0e1065025f19b4cfd1a80630aa772d5b84e [file] [log] [blame]
[email protected]bddb2842014-01-27 22:38:191// 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/files/file_path.h"
11#include "base/i18n/icu_util.h"
[email protected]e9da2b72014-03-18 17:07:2512#include "base/macros.h"
[email protected]bddb2842014-01-27 22:38:1913#include "base/message_loop/message_loop.h"
14#include "base/path_service.h"
[email protected]78ecb7c2014-05-06 22:34:2415#include "mojo/aura/screen_mojo.h"
16#include "mojo/aura/window_tree_host_mojo.h"
[email protected]a4b0f4082014-02-27 22:07:4317#include "mojo/examples/launcher/launcher.mojom.h"
[email protected]66bf7e22014-05-27 16:50:2918#include "mojo/public/cpp/application/application.h"
[email protected]e7a94442014-03-31 03:53:3619#include "mojo/public/cpp/gles2/gles2.h"
[email protected]2fb8c532014-03-28 13:23:2320#include "mojo/public/cpp/system/core.h"
[email protected]66bf7e22014-05-27 16:50:2921#include "mojo/public/interfaces/service_provider/service_provider.mojom.h"
[email protected]a4b0f4082014-02-27 22:07:4322#include "mojo/services/native_viewport/native_viewport.mojom.h"
[email protected]bddb2842014-01-27 22:38:1923#include "ui/aura/client/aura_constants.h"
[email protected]bddb2842014-01-27 22:38:1924#include "ui/aura/client/default_capture_client.h"
25#include "ui/aura/client/window_tree_client.h"
26#include "ui/aura/env.h"
[email protected]bddb2842014-01-27 22:38:1927#include "ui/aura/test/test_focus_client.h"
28#include "ui/aura/window.h"
29#include "ui/aura/window_delegate.h"
[email protected]fcc51c952014-02-21 21:31:2630#include "ui/aura/window_event_dispatcher.h"
[email protected]bddb2842014-01-27 22:38:1931#include "ui/aura/window_tree_host.h"
32#include "ui/base/hit_test.h"
33#include "ui/base/ime/input_method.h"
34#include "ui/base/ime/input_method_delegate.h"
35#include "ui/base/ime/input_method_factory.h"
36#include "ui/base/resource/resource_bundle.h"
37#include "ui/gfx/canvas.h"
38#include "ui/views/background.h"
39#include "ui/views/border.h"
40#include "ui/views/controls/textfield/textfield.h"
41#include "ui/views/controls/textfield/textfield_controller.h"
42#include "ui/views/layout/fill_layout.h"
43#include "ui/views/view.h"
44#include "ui/views/widget/widget.h"
[email protected]f3b1efd2014-04-30 04:48:2045#include "ui/wm/core/default_activation_client.h"
[email protected]bddb2842014-01-27 22:38:1946#include "url/gurl.h"
47
48#if defined(WIN32)
49#if !defined(CDECL)
50#define CDECL __cdecl
51#endif
52#define LAUNCHER_EXPORT __declspec(dllexport)
53#else
54#define CDECL
55#define LAUNCHER_EXPORT __attribute__((visibility("default")))
56#endif
57
58namespace mojo {
59namespace examples {
60
61class MinimalInputEventFilter : public ui::internal::InputMethodDelegate,
62 public ui::EventHandler {
63 public:
64 explicit MinimalInputEventFilter(aura::Window* root)
65 : root_(root),
66 input_method_(ui::CreateInputMethod(this,
67 gfx::kNullAcceleratedWidget)) {
[email protected]7d3470b2014-05-05 20:28:3568 ui::InitializeInputMethod();
[email protected]bddb2842014-01-27 22:38:1969 input_method_->Init(true);
70 root_->AddPreTargetHandler(this);
71 root_->SetProperty(aura::client::kRootWindowInputMethodKey,
72 input_method_.get());
73 }
74
75 virtual ~MinimalInputEventFilter() {
76 root_->RemovePreTargetHandler(this);
77 root_->SetProperty(aura::client::kRootWindowInputMethodKey,
78 static_cast<ui::InputMethod*>(NULL));
79 }
80
81 private:
82 // ui::EventHandler:
83 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
[email protected]19d46db2014-05-22 17:52:4584 // See the comment in InputMethodEventFilter::OnKeyEvent() for details.
85 if (event->IsTranslated()) {
86 event->SetTranslated(false);
[email protected]bddb2842014-01-27 22:38:1987 } else {
88 if (input_method_->DispatchKeyEvent(*event))
89 event->StopPropagation();
90 }
91 }
92
[email protected]086e6d42014-01-30 13:38:5493 // ui::internal::InputMethodDelegate:
94 virtual bool DispatchKeyEventPostIME(const ui::KeyEvent& event) OVERRIDE {
[email protected]19d46db2014-05-22 17:52:4595 // See the comment in InputMethodEventFilter::DispatchKeyEventPostIME() for
96 // details.
97 ui::KeyEvent aura_event(event);
98 aura_event.SetTranslated(true);
[email protected]5da34bd2014-01-31 08:24:4999 ui::EventDispatchDetails details =
[email protected]2374d1812014-03-04 03:42:27100 root_->GetHost()->dispatcher()->OnEventFromSource(&aura_event);
[email protected]5da34bd2014-01-31 08:24:49101 return aura_event.handled() || details.dispatcher_destroyed;
[email protected]bddb2842014-01-27 22:38:19102 }
103
104 aura::Window* root_;
105 scoped_ptr<ui::InputMethod> input_method_;
106
107 DISALLOW_COPY_AND_ASSIGN(MinimalInputEventFilter);
108};
109
110class LauncherWindowTreeClient : public aura::client::WindowTreeClient {
111 public:
112 explicit LauncherWindowTreeClient(aura::Window* window) : window_(window) {
113 aura::client::SetWindowTreeClient(window_, this);
114 }
115
116 virtual ~LauncherWindowTreeClient() {
117 aura::client::SetWindowTreeClient(window_, NULL);
118 }
119
120 // Overridden from aura::client::WindowTreeClient:
121 virtual aura::Window* GetDefaultParent(aura::Window* context,
122 aura::Window* window,
123 const gfx::Rect& bounds) OVERRIDE {
124 return window_;
125 }
126
127 private:
128 aura::Window* window_;
129
130 DISALLOW_COPY_AND_ASSIGN(LauncherWindowTreeClient);
131};
132
[email protected]1fd496142014-01-29 05:16:02133// Called when the user has submitted a URL by pressing Enter.
134class URLReceiver {
135 public:
136 virtual void OnURLEntered(const std::string& url_text) = 0;
137
138 protected:
139 virtual ~URLReceiver() {}
140};
141
[email protected]bddb2842014-01-27 22:38:19142class LauncherController : public views::TextfieldController {
143 public:
[email protected]1fd496142014-01-29 05:16:02144 explicit LauncherController(URLReceiver* url_receiver)
145 : url_receiver_(url_receiver) {}
[email protected]bddb2842014-01-27 22:38:19146
147 void InitInWindow(aura::Window* parent) {
148 views::Widget* widget = new views::Widget;
149 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
150 params.parent = parent;
151 params.bounds = parent->bounds();
[email protected]4f254232014-05-19 22:59:07152 params.activatable = views::Widget::InitParams::ACTIVATABLE_YES;
[email protected]bddb2842014-01-27 22:38:19153 widget->Init(params);
154
155 views::View* container = new views::View;
156 container->set_background(
157 views::Background::CreateSolidBackground(SK_ColorYELLOW));
158 container->SetBorder(
159 views::Border::CreateEmptyBorder(10, 10, 10, 10));
160 container->SetLayoutManager(new views::FillLayout);
161 widget->SetContentsView(container);
162
163 views::Textfield* textfield = new views::Textfield;
164 textfield->set_controller(this);
165 container->AddChildView(textfield);
[email protected]1fd496142014-01-29 05:16:02166 textfield->RequestFocus();
[email protected]bddb2842014-01-27 22:38:19167
168 container->Layout();
169
170 widget->Show();
171 }
172
173 private:
174 // Overridden from views::TextfieldController:
175 virtual bool HandleKeyEvent(views::Textfield* sender,
176 const ui::KeyEvent& key_event) OVERRIDE {
177 if (key_event.key_code() == ui::VKEY_RETURN) {
178 GURL url(sender->text());
[email protected]1fd496142014-01-29 05:16:02179 printf("Enter pressed with URL: %s\n", url.spec().c_str());
180 url_receiver_->OnURLEntered(url.spec());
[email protected]bddb2842014-01-27 22:38:19181 }
182 return false;
183 }
184
[email protected]1fd496142014-01-29 05:16:02185 URLReceiver* url_receiver_;
186
[email protected]bddb2842014-01-27 22:38:19187 DISALLOW_COPY_AND_ASSIGN(LauncherController);
188};
189
[email protected]eaae49d2014-05-16 06:04:21190class LauncherImpl : public InterfaceImpl<Launcher>,
[email protected]1fd496142014-01-29 05:16:02191 public URLReceiver {
[email protected]bddb2842014-01-27 22:38:19192 public:
[email protected]eaae49d2014-05-16 06:04:21193 explicit LauncherImpl(Application* app)
194 : app_(app),
195 launcher_controller_(this),
[email protected]1fd496142014-01-29 05:16:02196 pending_show_(false) {
[email protected]78ecb7c2014-05-06 22:34:24197 screen_.reset(ScreenMojo::Create());
[email protected]bddb2842014-01-27 22:38:19198 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
199
[email protected]5e1a8322014-05-09 22:35:51200 NativeViewportPtr viewport;
[email protected]eaae49d2014-05-16 06:04:21201 app_->ConnectTo("mojo:mojo_native_viewport_service", &viewport);
[email protected]76b8e582014-02-11 14:44:25202
[email protected]e8cf7332014-02-14 18:54:58203 window_tree_host_.reset(new WindowTreeHostMojo(
[email protected]5e1a8322014-05-09 22:35:51204 viewport.Pass(), gfx::Rect(50, 50, 450, 60),
[email protected]1fd496142014-01-29 05:16:02205 base::Bind(&LauncherImpl::HostContextCreated, base::Unretained(this))));
[email protected]bddb2842014-01-27 22:38:19206 }
207
[email protected]1fd496142014-01-29 05:16:02208 private:
[email protected]1fd496142014-01-29 05:16:02209 // Overridden from Launcher:
210 virtual void Show() OVERRIDE {
[email protected]2f2620332014-02-28 10:07:38211 if (!window_tree_host_.get()) {
[email protected]1fd496142014-01-29 05:16:02212 pending_show_ = true;
213 return;
214 }
[email protected]2f2620332014-02-28 10:07:38215 window_tree_host_->Show();
[email protected]1fd496142014-01-29 05:16:02216 }
217 virtual void Hide() OVERRIDE {
[email protected]2f2620332014-02-28 10:07:38218 window_tree_host_->Hide();
[email protected]1fd496142014-01-29 05:16:02219 }
220
221 // Overridden from URLReceiver:
[email protected]96a21522014-01-29 05:56:20222 virtual void OnURLEntered(const std::string& url_text) OVERRIDE {
[email protected]2b4e10f2014-05-29 19:00:36223 client()->OnURLEntered(String::From(url_text));
[email protected]1fd496142014-01-29 05:16:02224 }
225
[email protected]bddb2842014-01-27 22:38:19226 void HostContextCreated() {
[email protected]2f2620332014-02-28 10:07:38227 window_tree_host_->InitHost();
228 window_tree_host_->window()->SetBounds(gfx::Rect(450, 60));
[email protected]bddb2842014-01-27 22:38:19229
230 focus_client_.reset(new aura::test::TestFocusClient());
[email protected]2f2620332014-02-28 10:07:38231 aura::client::SetFocusClient(window_tree_host_->window(),
232 focus_client_.get());
[email protected]f3b1efd2014-04-30 04:48:20233 new wm::DefaultActivationClient(window_tree_host_->window());
[email protected]bddb2842014-01-27 22:38:19234 capture_client_.reset(
[email protected]2f2620332014-02-28 10:07:38235 new aura::client::DefaultCaptureClient(window_tree_host_->window()));
236 ime_filter_.reset(new MinimalInputEventFilter(window_tree_host_->window()));
[email protected]bddb2842014-01-27 22:38:19237
238 window_tree_client_.reset(
[email protected]2f2620332014-02-28 10:07:38239 new LauncherWindowTreeClient(window_tree_host_->window()));
[email protected]bddb2842014-01-27 22:38:19240
[email protected]2f2620332014-02-28 10:07:38241 launcher_controller_.InitInWindow(window_tree_host_->window());
[email protected]bddb2842014-01-27 22:38:19242
[email protected]1fd496142014-01-29 05:16:02243 if (pending_show_) {
244 pending_show_ = false;
245 Show();
246 }
[email protected]bddb2842014-01-27 22:38:19247 }
248
[email protected]eaae49d2014-05-16 06:04:21249 Application* app_;
[email protected]78ecb7c2014-05-06 22:34:24250 scoped_ptr<ScreenMojo> screen_;
[email protected]bddb2842014-01-27 22:38:19251 scoped_ptr<LauncherWindowTreeClient> window_tree_client_;
[email protected]bddb2842014-01-27 22:38:19252 scoped_ptr<aura::client::FocusClient> focus_client_;
253 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
254 scoped_ptr<ui::EventHandler> ime_filter_;
255
256 LauncherController launcher_controller_;
257
[email protected]2f2620332014-02-28 10:07:38258 scoped_ptr<aura::WindowTreeHost> window_tree_host_;
[email protected]1fd496142014-01-29 05:16:02259
260 bool pending_show_;
[email protected]bddb2842014-01-27 22:38:19261};
262
263} // namespace examples
264} // namespace mojo
265
266extern "C" LAUNCHER_EXPORT MojoResult CDECL MojoMain(
[email protected]66bf7e22014-05-27 16:50:29267 MojoHandle service_provider_handle) {
[email protected]7cb2c672014-05-24 12:19:58268 base::CommandLine::Init(0, NULL);
[email protected]bddb2842014-01-27 22:38:19269 base::AtExitManager at_exit;
270 base::i18n::InitializeICU();
271
272 base::FilePath pak_dir;
273 PathService::Get(base::DIR_MODULE, &pak_dir);
274 base::FilePath pak_file;
275 pak_file = pak_dir.Append(FILE_PATH_LITERAL("ui_test.pak"));
276 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file);
277
278 base::MessageLoop loop;
279 mojo::GLES2Initializer gles2;
280
281 // TODO(beng): This crashes in a DCHECK on X11 because this thread's
282 // MessageLoop is not of TYPE_UI. I think we need a way to build
283 // Aura that doesn't define platform-specific stuff.
[email protected]5b883abb2014-05-05 06:44:10284 aura::Env::CreateInstance(true);
[email protected]bddb2842014-01-27 22:38:19285
[email protected]66bf7e22014-05-27 16:50:29286 mojo::Application app(service_provider_handle);
[email protected]eaae49d2014-05-16 06:04:21287 app.AddService<mojo::examples::LauncherImpl>(&app);
[email protected]5e1a8322014-05-09 22:35:51288
289 loop.Run();
[email protected]bddb2842014-01-27 22:38:19290 return MOJO_RESULT_OK;
291}