| OLD | NEW |
| (Empty) |
| 1 // 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 "components/native_viewport/platform_viewport.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "mojo/converters/geometry/geometry_type_converters.h" | |
| 10 #include "mojo/converters/input_events/input_events_type_converters.h" | |
| 11 #include "mojo/converters/input_events/mojo_extended_key_event_data.h" | |
| 12 #include "ui/events/event.h" | |
| 13 #include "ui/events/event_utils.h" | |
| 14 #include "ui/events/platform/platform_event_dispatcher.h" | |
| 15 #include "ui/gfx/geometry/rect.h" | |
| 16 #include "ui/platform_window/platform_window.h" | |
| 17 #include "ui/platform_window/platform_window_delegate.h" | |
| 18 #include "ui/platform_window/x11/x11_window.h" | |
| 19 | |
| 20 namespace native_viewport { | |
| 21 namespace { | |
| 22 | |
| 23 float ConvertUIWheelValueToMojoValue(int offset) { | |
| 24 // Mojo's event type takes a value between -1 and 1. Normalize by allowing | |
| 25 // up to 20 of ui's offset. This is a bit arbitrary. | |
| 26 return std::max( | |
| 27 -1.0f, std::min(1.0f, static_cast<float>(offset) / | |
| 28 (20 * static_cast<float>( | |
| 29 ui::MouseWheelEvent::kWheelDelta)))); | |
| 30 } | |
| 31 } // namespace | |
| 32 | |
| 33 class PlatformViewportX11 : public PlatformViewport, | |
| 34 public ui::PlatformWindowDelegate { | |
| 35 public: | |
| 36 explicit PlatformViewportX11(Delegate* delegate) : delegate_(delegate) { | |
| 37 } | |
| 38 | |
| 39 ~PlatformViewportX11() override { | |
| 40 // Destroy the platform-window while |this| is still alive. | |
| 41 platform_window_.reset(); | |
| 42 } | |
| 43 | |
| 44 private: | |
| 45 // Overridden from PlatformViewport: | |
| 46 void Init(const gfx::Rect& bounds) override { | |
| 47 CHECK(!platform_window_); | |
| 48 | |
| 49 metrics_ = mojo::ViewportMetrics::New(); | |
| 50 // TODO(sky): make density real. | |
| 51 metrics_->device_pixel_ratio = 1.f; | |
| 52 metrics_->size = mojo::Size::From(bounds.size()); | |
| 53 | |
| 54 platform_window_.reset(new ui::X11Window(this)); | |
| 55 platform_window_->SetBounds(bounds); | |
| 56 } | |
| 57 | |
| 58 void Show() override { platform_window_->Show(); } | |
| 59 | |
| 60 void Hide() override { platform_window_->Hide(); } | |
| 61 | |
| 62 void Close() override { platform_window_->Close(); } | |
| 63 | |
| 64 gfx::Size GetSize() override { return metrics_->size.To<gfx::Size>(); } | |
| 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 // We want to emulate the WM_CHAR generation behaviour of Windows. | |
| 107 // | |
| 108 // On Linux, we've previously inserted characters by having | |
| 109 // InputMethodAuraLinux take all key down events and send a character event | |
| 110 // to the TextInputClient. This causes a mismatch in code that has to be | |
| 111 // shared between Windows and Linux, including blink code. Now that we're | |
| 112 // trying to have one way of doing things, we need to standardize on and | |
| 113 // emulate Windows character events. | |
| 114 // | |
| 115 // This is equivalent to what we're doing in the current Linux port, but | |
| 116 // done once instead of done multiple times in different places. | |
| 117 if (event->type() == ui::ET_KEY_PRESSED) { | |
| 118 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event); | |
| 119 ui::KeyEvent char_event(key_press_event->GetCharacter(), | |
| 120 key_press_event->key_code(), | |
| 121 key_press_event->flags()); | |
| 122 | |
| 123 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter()); | |
| 124 DCHECK_EQ(key_press_event->key_code(), char_event.key_code()); | |
| 125 DCHECK_EQ(key_press_event->flags(), char_event.flags()); | |
| 126 | |
| 127 char_event.SetExtendedKeyEventData( | |
| 128 make_scoped_ptr(new mojo::MojoExtendedKeyEventData( | |
| 129 key_press_event->GetLocatedWindowsKeyboardCode(), | |
| 130 key_press_event->GetText(), | |
| 131 key_press_event->GetUnmodifiedText()))); | |
| 132 char_event.set_platform_keycode(key_press_event->platform_keycode()); | |
| 133 | |
| 134 delegate_->OnEvent(mojo::Event::From(char_event)); | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 void OnCloseRequest() override { platform_window_->Close(); } | |
| 139 | |
| 140 void OnClosed() override { delegate_->OnDestroyed(); } | |
| 141 | |
| 142 void OnWindowStateChanged(ui::PlatformWindowState state) override {} | |
| 143 | |
| 144 void OnLostCapture() override {} | |
| 145 | |
| 146 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override { | |
| 147 delegate_->OnAcceleratedWidgetAvailable(widget, | |
| 148 metrics_->device_pixel_ratio); | |
| 149 } | |
| 150 | |
| 151 void OnActivationChanged(bool active) override {} | |
| 152 | |
| 153 scoped_ptr<ui::PlatformWindow> platform_window_; | |
| 154 Delegate* delegate_; | |
| 155 mojo::ViewportMetricsPtr metrics_; | |
| 156 | |
| 157 DISALLOW_COPY_AND_ASSIGN(PlatformViewportX11); | |
| 158 }; | |
| 159 | |
| 160 // static | |
| 161 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) { | |
| 162 return make_scoped_ptr(new PlatformViewportX11(delegate)); | |
| 163 } | |
| 164 | |
| 165 } // namespace native_viewport | |
| OLD | NEW |