| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "services/native_viewport/platform_viewport.h" | |
| 6 | |
| 7 #include "mojo/converters/geometry/geometry_type_converters.h" | |
| 8 #include "mojo/converters/input_events/input_events_type_converters.h" | |
| 9 #include "mojo/converters/input_events/mojo_extended_key_event_data.h" | |
| 10 #include "ui/events/event.h" | |
| 11 #include "ui/events/platform/platform_event_dispatcher.h" | |
| 12 #include "ui/events/platform/platform_event_source.h" | |
| 13 #include "ui/ozone/public/cursor_factory_ozone.h" | |
| 14 #include "ui/ozone/public/ozone_platform.h" | |
| 15 #include "ui/ozone/public/surface_factory_ozone.h" | |
| 16 #include "ui/platform_window/platform_window.h" | |
| 17 #include "ui/platform_window/platform_window_delegate.h" | |
| 18 | |
| 19 namespace native_viewport { | |
| 20 namespace { | |
| 21 | |
| 22 float ConvertUIWheelValueToMojoValue(int offset) { | |
| 23 // Mojo's event type takes a value between -1 and 1. Normalize by allowing | |
| 24 // up to 20 of ui's offset. This is a bit arbitrary. | |
| 25 return std::max( | |
| 26 -1.0f, std::min(1.0f, static_cast<float>(offset) / | |
| 27 (20 * static_cast<float>( | |
| 28 ui::MouseWheelEvent::kWheelDelta)))); | |
| 29 } | |
| 30 } // namespace | |
| 31 | |
| 32 // TODO(spang): Deduplicate with PlatformViewportX11.. but there's a hack | |
| 33 // in there that prevents this. | |
| 34 class PlatformViewportOzone : public PlatformViewport, | |
| 35 public ui::PlatformWindowDelegate { | |
| 36 public: | |
| 37 explicit PlatformViewportOzone(Delegate* delegate) : delegate_(delegate) { | |
| 38 ui::OzonePlatform::InitializeForUI(); | |
| 39 } | |
| 40 | |
| 41 ~PlatformViewportOzone() override { | |
| 42 // Destroy the platform-window while |this| is still alive. | |
| 43 platform_window_.reset(); | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 // Overridden from PlatformViewport: | |
| 48 void Init(const gfx::Rect& bounds) override { | |
| 49 platform_window_ = | |
| 50 ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds); | |
| 51 | |
| 52 metrics_ = mojo::ViewportMetrics::New(); | |
| 53 metrics_->size = mojo::Size::From(bounds.size()); | |
| 54 } | |
| 55 | |
| 56 void Show() override { platform_window_->Show(); } | |
| 57 | |
| 58 void Hide() override { platform_window_->Hide(); } | |
| 59 | |
| 60 void Close() override { platform_window_->Close(); } | |
| 61 | |
| 62 gfx::Size GetSize() override { | |
| 63 return platform_window_->GetBounds().size(); | |
| 64 } | |
| 65 | |
| 66 void SetBounds(const gfx::Rect& bounds) override { | |
| 67 platform_window_->SetBounds(bounds); | |
| 68 } | |
| 69 | |
| 70 // ui::PlatformWindowDelegate: | |
| 71 void OnBoundsChanged(const gfx::Rect& new_bounds) override { | |
| 72 metrics_->size = mojo::Size::From(new_bounds.size()); | |
| 73 delegate_->OnMetricsChanged(metrics_.Clone()); | |
| 74 } | |
| 75 | |
| 76 void OnDamageRect(const gfx::Rect& damaged_region) override {} | |
| 77 | |
| 78 void DispatchEvent(ui::Event* event) override { | |
| 79 mojo::EventPtr mojo_event(mojo::Event::From(*event)); | |
| 80 if (event->IsMouseWheelEvent()) { | |
| 81 // Mojo's event type has a different meaning for wheel events. Convert | |
| 82 // between the two. | |
| 83 ui::MouseWheelEvent* wheel_event = | |
| 84 static_cast<ui::MouseWheelEvent*>(event); | |
| 85 DCHECK(mojo_event->pointer_data); | |
| 86 mojo_event->pointer_data->horizontal_wheel = | |
| 87 ConvertUIWheelValueToMojoValue(wheel_event->x_offset()); | |
| 88 mojo_event->pointer_data->horizontal_wheel = | |
| 89 ConvertUIWheelValueToMojoValue(wheel_event->y_offset()); | |
| 90 } | |
| 91 delegate_->OnEvent(mojo_event.Pass()); | |
| 92 | |
| 93 switch (event->type()) { | |
| 94 case ui::ET_MOUSE_PRESSED: | |
| 95 case ui::ET_TOUCH_PRESSED: | |
| 96 platform_window_->SetCapture(); | |
| 97 break; | |
| 98 case ui::ET_MOUSE_RELEASED: | |
| 99 case ui::ET_TOUCH_RELEASED: | |
| 100 platform_window_->ReleaseCapture(); | |
| 101 break; | |
| 102 default: | |
| 103 break; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 void OnCloseRequest() override { platform_window_->Close(); } | |
| 108 | |
| 109 void OnClosed() override { delegate_->OnDestroyed(); } | |
| 110 | |
| 111 void OnWindowStateChanged(ui::PlatformWindowState state) override {} | |
| 112 | |
| 113 void OnLostCapture() override {} | |
| 114 | |
| 115 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override { | |
| 116 delegate_->OnAcceleratedWidgetAvailable(widget); | |
| 117 } | |
| 118 | |
| 119 void OnActivationChanged(bool active) override {} | |
| 120 | |
| 121 scoped_ptr<ui::PlatformWindow> platform_window_; | |
| 122 Delegate* delegate_; | |
| 123 mojo::ViewportMetricsPtr metrics_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(PlatformViewportOzone); | |
| 126 }; | |
| 127 | |
| 128 // static | |
| 129 scoped_ptr<PlatformViewport> PlatformViewport::Create( | |
| 130 mojo::ApplicationImpl* application_, | |
| 131 Delegate* delegate) { | |
| 132 return scoped_ptr<PlatformViewport>( | |
| 133 new PlatformViewportOzone(delegate)).Pass(); | |
| 134 } | |
| 135 | |
| 136 } // namespace native_viewport | |
| OLD | NEW |