Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "mojo/examples/aura_demo/window_tree_host_mojo.h" |
| 6 | |
| 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" |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame^] | 13 | #include "ui/aura/window_event_dispatcher.h" |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 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" |
| 19 | |
| 20 | namespace mojo { |
| 21 | namespace examples { |
| 22 | |
| 23 | // static |
| 24 | ui::ContextFactory* WindowTreeHostMojo::context_factory_ = NULL; |
| 25 | |
| 26 | //////////////////////////////////////////////////////////////////////////////// |
| 27 | // WindowTreeHostMojo, public: |
| 28 | |
| 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), |
| 35 | bounds_(bounds) { |
| 36 | AllocationScope scope; |
| 37 | native_viewport_->Create(bounds); |
| 38 | |
| 39 | ScopedMessagePipeHandle gles2_client_handle; |
| 40 | CreateMessagePipe(&gles2_handle_, &gles2_client_handle); |
| 41 | |
| 42 | // The ContextFactory must exist before any Compositors are created. |
| 43 | if (!context_factory_) { |
| 44 | scoped_ptr<DemoContextFactory> cf(new DemoContextFactory(this)); |
| 45 | if (cf->Initialize()) |
| 46 | context_factory_ = cf.release(); |
| 47 | ui::ContextFactory::SetInstance(context_factory_); |
| 48 | } |
| 49 | CHECK(context_factory_) << "No GL bindings."; |
| 50 | |
| 51 | native_viewport_->CreateGLES2Context(gles2_client_handle.Pass()); |
| 52 | } |
| 53 | |
| 54 | WindowTreeHostMojo::~WindowTreeHostMojo() {} |
| 55 | |
| 56 | //////////////////////////////////////////////////////////////////////////////// |
| 57 | // WindowTreeHostMojo, aura::WindowTreeHost implementation: |
| 58 | |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 59 | gfx::AcceleratedWidget WindowTreeHostMojo::GetAcceleratedWidget() { |
| 60 | NOTIMPLEMENTED() << "GetAcceleratedWidget"; |
| 61 | return gfx::kNullAcceleratedWidget; |
| 62 | } |
| 63 | |
| 64 | void WindowTreeHostMojo::Show() { |
| 65 | window()->Show(); |
| 66 | native_viewport_->Show(); |
| 67 | } |
| 68 | |
| 69 | void WindowTreeHostMojo::Hide() { |
| 70 | native_viewport_->Hide(); |
| 71 | window()->Hide(); |
| 72 | } |
| 73 | |
| 74 | void WindowTreeHostMojo::ToggleFullScreen() { |
| 75 | NOTIMPLEMENTED(); |
| 76 | } |
| 77 | |
| 78 | gfx::Rect WindowTreeHostMojo::GetBounds() const { |
| 79 | return bounds_; |
| 80 | } |
| 81 | |
| 82 | void WindowTreeHostMojo::SetBounds(const gfx::Rect& bounds) { |
| 83 | AllocationScope scope; |
| 84 | native_viewport_->SetBounds(bounds); |
| 85 | } |
| 86 | |
| 87 | gfx::Insets WindowTreeHostMojo::GetInsets() const { |
| 88 | NOTIMPLEMENTED(); |
| 89 | return gfx::Insets(); |
| 90 | } |
| 91 | |
| 92 | void WindowTreeHostMojo::SetInsets(const gfx::Insets& insets) { |
| 93 | NOTIMPLEMENTED(); |
| 94 | } |
| 95 | |
| 96 | gfx::Point WindowTreeHostMojo::GetLocationOnNativeScreen() const { |
| 97 | return gfx::Point(0, 0); |
| 98 | } |
| 99 | |
| 100 | void WindowTreeHostMojo::SetCapture() { |
| 101 | NOTIMPLEMENTED(); |
| 102 | } |
| 103 | |
| 104 | void WindowTreeHostMojo::ReleaseCapture() { |
| 105 | NOTIMPLEMENTED(); |
| 106 | } |
| 107 | |
| 108 | bool WindowTreeHostMojo::QueryMouseLocation(gfx::Point* location_return) { |
| 109 | NOTIMPLEMENTED() << "QueryMouseLocation"; |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | bool WindowTreeHostMojo::ConfineCursorToRootWindow() { |
| 114 | NOTIMPLEMENTED(); |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | void WindowTreeHostMojo::UnConfineCursor() { |
| 119 | NOTIMPLEMENTED(); |
| 120 | } |
| 121 | |
| 122 | void WindowTreeHostMojo::PostNativeEvent( |
| 123 | const base::NativeEvent& native_event) { |
| 124 | NOTIMPLEMENTED(); |
| 125 | } |
| 126 | |
| 127 | void WindowTreeHostMojo::OnDeviceScaleFactorChanged(float device_scale_factor) { |
| 128 | NOTIMPLEMENTED(); |
| 129 | } |
| 130 | |
| 131 | void WindowTreeHostMojo::PrepareForShutdown() { |
| 132 | NOTIMPLEMENTED(); |
| 133 | } |
| 134 | |
| 135 | void WindowTreeHostMojo::SetCursorNative(gfx::NativeCursor cursor) { |
| 136 | NOTIMPLEMENTED(); |
| 137 | } |
| 138 | |
| 139 | void WindowTreeHostMojo::MoveCursorToNative(const gfx::Point& location) { |
| 140 | NOTIMPLEMENTED(); |
| 141 | } |
| 142 | |
| 143 | void WindowTreeHostMojo::OnCursorVisibilityChangedNative(bool show) { |
| 144 | NOTIMPLEMENTED(); |
| 145 | } |
| 146 | |
| 147 | //////////////////////////////////////////////////////////////////////////////// |
| 148 | // WindowTreeHostMojo, ui::EventSource implementation: |
| 149 | |
| 150 | ui::EventProcessor* WindowTreeHostMojo::GetEventProcessor() { |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame^] | 151 | return dispatcher(); |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | //////////////////////////////////////////////////////////////////////////////// |
| 155 | // WindowTreeHostMojo, NativeViewportClient implementation: |
| 156 | |
| 157 | void WindowTreeHostMojo::OnCreated() { |
| 158 | CreateCompositor(GetAcceleratedWidget()); |
| 159 | compositor_created_callback_.Run(); |
| 160 | } |
| 161 | |
| 162 | void WindowTreeHostMojo::OnBoundsChanged(const Rect& bounds) { |
| 163 | bounds_ = gfx::Rect(bounds.position().x(), bounds.position().y(), |
| 164 | bounds.size().width(), bounds.size().height()); |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame^] | 165 | window()->SetBounds(gfx::Rect(bounds_.size())); |
| 166 | OnHostResized(bounds_.size()); |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void WindowTreeHostMojo::OnDestroyed() { |
| 170 | base::MessageLoop::current()->Quit(); |
| 171 | } |
| 172 | |
| 173 | void WindowTreeHostMojo::OnEvent(const Event& event) { |
| 174 | if (!event.location().is_null()) |
| 175 | native_viewport_->AckEvent(event); |
| 176 | |
| 177 | switch (event.action()) { |
| 178 | case ui::ET_MOUSE_PRESSED: |
| 179 | case ui::ET_MOUSE_DRAGGED: |
| 180 | case ui::ET_MOUSE_RELEASED: |
| 181 | case ui::ET_MOUSE_MOVED: |
| 182 | case ui::ET_MOUSE_ENTERED: |
| 183 | case ui::ET_MOUSE_EXITED: { |
| 184 | gfx::Point location(event.location().x(), event.location().y()); |
| 185 | ui::MouseEvent ev(static_cast<ui::EventType>(event.action()), location, |
| 186 | location, event.flags(), 0); |
| 187 | SendEventToProcessor(&ev); |
| 188 | break; |
| 189 | } |
| 190 | case ui::ET_KEY_PRESSED: |
| 191 | case ui::ET_KEY_RELEASED: { |
| 192 | ui::KeyEvent ev( |
| 193 | static_cast<ui::EventType>(event.action()), |
| 194 | static_cast<ui::KeyboardCode>(event.key_data().key_code()), |
| 195 | event.flags(), event.key_data().is_char()); |
| 196 | SendEventToProcessor(&ev); |
| 197 | break; |
| 198 | } |
| 199 | // TODO(beng): touch, etc. |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | } // namespace examples |
| 204 | } // namespace mojo |