1 // Copyright (c) 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.
5 #include "mojo/examples/aura_demo/window_tree_host_mojo.h"
7 #include "mojo/examples/aura_demo/demo_context_factory.h"
8 #include "mojo/public/bindings/allocation_scope.h"
9 #include "mojo/public/gles2/gles2.h"
10 #include "mojo/services/native_viewport/geometry_conversions.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_tree_host_delegate.h"
14 #include "ui/compositor/compositor.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_constants.h"
17 #include "ui/gfx/geometry/insets.h"
18 #include "ui/gfx/geometry/rect.h"
24 ui::ContextFactory* WindowTreeHostMojo::context_factory_ = NULL;
26 ////////////////////////////////////////////////////////////////////////////////
27 // WindowTreeHostMojo, public:
29 WindowTreeHostMojo::WindowTreeHostMojo(
30 ScopedNativeViewportHandle viewport_handle,
31 const gfx::Rect& bounds,
32 const base::Callback<void()>& compositor_created_callback)
33 : native_viewport_(viewport_handle.Pass(), this),
34 compositor_created_callback_(compositor_created_callback),
36 AllocationScope scope;
37 native_viewport_->Create(bounds);
39 ScopedMessagePipeHandle gles2_client_handle;
40 CreateMessagePipe(&gles2_handle_, &gles2_client_handle);
42 // The ContextFactory must exist before any Compositors are created.
43 if (!context_factory_) {
44 scoped_ptr<DemoContextFactory> cf(new DemoContextFactory(this));
46 context_factory_ = cf.release();
47 ui::ContextFactory::SetInstance(context_factory_);
49 CHECK(context_factory_) << "No GL bindings.";
51 native_viewport_->CreateGLES2Context(gles2_client_handle.Pass());
54 WindowTreeHostMojo::~WindowTreeHostMojo() {}
56 ////////////////////////////////////////////////////////////////////////////////
57 // WindowTreeHostMojo, aura::WindowTreeHost implementation:
59 aura::RootWindow* WindowTreeHostMojo::GetRootWindow() {
60 return delegate_->AsRootWindow();
63 gfx::AcceleratedWidget WindowTreeHostMojo::GetAcceleratedWidget() {
64 NOTIMPLEMENTED() << "GetAcceleratedWidget";
65 return gfx::kNullAcceleratedWidget;
68 void WindowTreeHostMojo::Show() {
70 native_viewport_->Show();
73 void WindowTreeHostMojo::Hide() {
74 native_viewport_->Hide();
78 void WindowTreeHostMojo::ToggleFullScreen() {
82 gfx::Rect WindowTreeHostMojo::GetBounds() const {
86 void WindowTreeHostMojo::SetBounds(const gfx::Rect& bounds) {
87 AllocationScope scope;
88 native_viewport_->SetBounds(bounds);
91 gfx::Insets WindowTreeHostMojo::GetInsets() const {
96 void WindowTreeHostMojo::SetInsets(const gfx::Insets& insets) {
100 gfx::Point WindowTreeHostMojo::GetLocationOnNativeScreen() const {
101 return gfx::Point(0, 0);
104 void WindowTreeHostMojo::SetCapture() {
108 void WindowTreeHostMojo::ReleaseCapture() {
112 bool WindowTreeHostMojo::QueryMouseLocation(gfx::Point* location_return) {
113 NOTIMPLEMENTED() << "QueryMouseLocation";
117 bool WindowTreeHostMojo::ConfineCursorToRootWindow() {
122 void WindowTreeHostMojo::UnConfineCursor() {
126 void WindowTreeHostMojo::PostNativeEvent(
127 const base::NativeEvent& native_event) {
131 void WindowTreeHostMojo::OnDeviceScaleFactorChanged(float device_scale_factor) {
135 void WindowTreeHostMojo::PrepareForShutdown() {
139 void WindowTreeHostMojo::SetCursorNative(gfx::NativeCursor cursor) {
143 void WindowTreeHostMojo::MoveCursorToNative(const gfx::Point& location) {
147 void WindowTreeHostMojo::OnCursorVisibilityChangedNative(bool show) {
151 ////////////////////////////////////////////////////////////////////////////////
152 // WindowTreeHostMojo, ui::EventSource implementation:
154 ui::EventProcessor* WindowTreeHostMojo::GetEventProcessor() {
155 return delegate_->GetEventProcessor();
158 ////////////////////////////////////////////////////////////////////////////////
159 // WindowTreeHostMojo, NativeViewportClient implementation:
161 void WindowTreeHostMojo::OnCreated() {
162 CreateCompositor(GetAcceleratedWidget());
163 compositor_created_callback_.Run();
166 void WindowTreeHostMojo::OnBoundsChanged(const Rect& bounds) {
167 bounds_ = gfx::Rect(bounds.position().x(), bounds.position().y(),
168 bounds.size().width(), bounds.size().height());
170 window()->SetBounds(gfx::Rect(bounds_.size()));
171 NotifyHostResized(bounds_.size());
174 void WindowTreeHostMojo::OnDestroyed() {
175 base::MessageLoop::current()->Quit();
178 void WindowTreeHostMojo::OnEvent(const Event& event) {
179 if (!event.location().is_null())
180 native_viewport_->AckEvent(event);
182 switch (event.action()) {
183 case ui::ET_MOUSE_PRESSED:
184 case ui::ET_MOUSE_DRAGGED:
185 case ui::ET_MOUSE_RELEASED:
186 case ui::ET_MOUSE_MOVED:
187 case ui::ET_MOUSE_ENTERED:
188 case ui::ET_MOUSE_EXITED: {
189 gfx::Point location(event.location().x(), event.location().y());
190 ui::MouseEvent ev(static_cast<ui::EventType>(event.action()), location,
191 location, event.flags(), 0);
192 SendEventToProcessor(&ev);
195 case ui::ET_KEY_PRESSED:
196 case ui::ET_KEY_RELEASED: {
198 static_cast<ui::EventType>(event.action()),
199 static_cast<ui::KeyboardCode>(event.key_data().key_code()),
200 event.flags(), event.key_data().is_char());
201 SendEventToProcessor(&ev);
204 // TODO(beng): touch, etc.
208 } // namespace examples