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/view_manager/native_viewport/platform_viewport.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "components/view_manager/public/interfaces/view_manager.mojom.h" | |
10 #include "mojo/converters/geometry/geometry_type_converters.h" | |
11 #include "mojo/converters/input_events/input_events_type_converters.h" | |
12 #include "mojo/converters/input_events/mojo_extended_key_event_data.h" | |
13 #include "ui/events/event.h" | |
14 #include "ui/events/event_utils.h" | |
15 #include "ui/events/platform/platform_event_dispatcher.h" | |
16 #include "ui/gfx/geometry/rect.h" | |
17 #include "ui/platform_window/platform_window.h" | |
18 #include "ui/platform_window/platform_window_delegate.h" | |
19 #include "ui/platform_window/stub/stub_window.h" | |
20 | |
21 #if defined(OS_WIN) | |
22 #include "ui/platform_window/win/win_window.h" | |
23 #elif defined(USE_X11) | |
24 #include "ui/platform_window/x11/x11_window.h" | |
25 #elif defined(OS_ANDROID) | |
26 #include "ui/platform_window/android/platform_window_android.h" | |
27 #endif | |
28 | |
29 namespace native_viewport { | |
30 namespace { | |
31 | |
32 float ConvertUIWheelValueToMojoValue(int offset) { | |
33 // Mojo's event type takes a value between -1 and 1. Normalize by allowing | |
34 // up to 20 of ui's offset. This is a bit arbitrary. | |
35 return std::max( | |
36 -1.0f, std::min(1.0f, static_cast<float>(offset) / | |
37 (20 * static_cast<float>( | |
38 ui::MouseWheelEvent::kWheelDelta)))); | |
39 } | |
40 } // namespace | |
41 | |
42 class PlatformViewportCommon : public PlatformViewport, | |
43 public ui::PlatformWindowDelegate { | |
44 public: | |
45 PlatformViewportCommon(Delegate* delegate, bool headless) | |
46 : delegate_(delegate), headless_(headless) {} | |
47 | |
48 ~PlatformViewportCommon() override { | |
49 // Destroy the platform-window while |this| is still alive. | |
50 platform_window_.reset(); | |
51 } | |
52 | |
53 private: | |
54 // Overridden from PlatformViewport: | |
55 void Init(const gfx::Rect& bounds) override { | |
56 CHECK(!platform_window_); | |
57 | |
58 metrics_ = mojo::ViewportMetrics::New(); | |
59 metrics_->size_in_pixels = mojo::Size::From(bounds.size()); | |
60 | |
61 if (headless_) { | |
62 platform_window_.reset(new ui::StubWindow(this)); | |
63 } else { | |
64 #if defined(OS_WIN) | |
65 platform_window_.reset(new ui::WinWindow(this, bounds)); | |
66 #elif defined(USE_X11) | |
67 platform_window_.reset(new ui::X11Window(this)); | |
68 #elif defined(OS_ANDROID) | |
69 platform_window_.reset(new ui::PlatformWindowAndroid(this)); | |
70 #endif | |
71 } | |
72 platform_window_->SetBounds(bounds); | |
73 } | |
74 | |
75 void Show() override { platform_window_->Show(); } | |
76 | |
77 void Hide() override { platform_window_->Hide(); } | |
78 | |
79 void Close() override { platform_window_->Close(); } | |
80 | |
81 gfx::Size GetSize() override { | |
82 return metrics_->size_in_pixels.To<gfx::Size>(); | |
83 } | |
84 | |
85 void SetBounds(const gfx::Rect& bounds) override { | |
86 platform_window_->SetBounds(bounds); | |
87 } | |
88 | |
89 // ui::PlatformWindowDelegate: | |
90 void OnBoundsChanged(const gfx::Rect& new_bounds) override { | |
91 delegate_->OnMetricsChanged(new_bounds.size(), | |
92 metrics_->device_pixel_ratio); | |
93 } | |
94 | |
95 void OnDamageRect(const gfx::Rect& damaged_region) override {} | |
96 | |
97 void DispatchEvent(ui::Event* event) override { | |
98 mojo::EventPtr mojo_event(mojo::Event::From(*event)); | |
99 if (event->IsMouseWheelEvent()) { | |
100 // Mojo's event type has a different meaning for wheel events. Convert | |
101 // between the two. | |
102 ui::MouseWheelEvent* wheel_event = | |
103 static_cast<ui::MouseWheelEvent*>(event); | |
104 DCHECK(mojo_event->pointer_data); | |
105 mojo_event->pointer_data->horizontal_wheel = | |
106 ConvertUIWheelValueToMojoValue(wheel_event->x_offset()); | |
107 mojo_event->pointer_data->horizontal_wheel = | |
108 ConvertUIWheelValueToMojoValue(wheel_event->y_offset()); | |
109 } | |
110 delegate_->OnEvent(mojo_event.Pass()); | |
111 | |
112 switch (event->type()) { | |
113 case ui::ET_MOUSE_PRESSED: | |
114 case ui::ET_TOUCH_PRESSED: | |
115 platform_window_->SetCapture(); | |
116 break; | |
117 case ui::ET_MOUSE_RELEASED: | |
118 case ui::ET_TOUCH_RELEASED: | |
119 platform_window_->ReleaseCapture(); | |
120 break; | |
121 default: | |
122 break; | |
123 } | |
124 | |
125 #if defined(USE_X11) | |
126 // We want to emulate the WM_CHAR generation behaviour of Windows. | |
127 // | |
128 // On Linux, we've previously inserted characters by having | |
129 // InputMethodAuraLinux take all key down events and send a character event | |
130 // to the TextInputClient. This causes a mismatch in code that has to be | |
131 // shared between Windows and Linux, including blink code. Now that we're | |
132 // trying to have one way of doing things, we need to standardize on and | |
133 // emulate Windows character events. | |
134 // | |
135 // This is equivalent to what we're doing in the current Linux port, but | |
136 // done once instead of done multiple times in different places. | |
137 if (event->type() == ui::ET_KEY_PRESSED) { | |
138 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event); | |
139 ui::KeyEvent char_event(key_press_event->GetCharacter(), | |
140 key_press_event->key_code(), | |
141 key_press_event->flags()); | |
142 | |
143 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter()); | |
144 DCHECK_EQ(key_press_event->key_code(), char_event.key_code()); | |
145 DCHECK_EQ(key_press_event->flags(), char_event.flags()); | |
146 | |
147 char_event.SetExtendedKeyEventData( | |
148 make_scoped_ptr(new mojo::MojoExtendedKeyEventData( | |
149 key_press_event->GetLocatedWindowsKeyboardCode(), | |
150 key_press_event->GetText(), | |
151 key_press_event->GetUnmodifiedText()))); | |
152 char_event.set_platform_keycode(key_press_event->platform_keycode()); | |
153 | |
154 delegate_->OnEvent(mojo::Event::From(char_event)); | |
155 } | |
156 #endif | |
157 } | |
158 | |
159 void OnCloseRequest() override { platform_window_->Close(); } | |
160 | |
161 void OnClosed() override { delegate_->OnDestroyed(); } | |
162 | |
163 void OnWindowStateChanged(ui::PlatformWindowState state) override {} | |
164 | |
165 void OnLostCapture() override {} | |
166 | |
167 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget, | |
168 float device_pixel_ratio) override { | |
169 metrics_->device_pixel_ratio = device_pixel_ratio; | |
170 delegate_->OnAcceleratedWidgetAvailable(widget, | |
171 metrics_->device_pixel_ratio); | |
172 } | |
173 | |
174 void OnActivationChanged(bool active) override {} | |
175 | |
176 scoped_ptr<ui::PlatformWindow> platform_window_; | |
177 Delegate* delegate_; | |
178 bool headless_; | |
179 mojo::ViewportMetricsPtr metrics_; | |
180 | |
181 DISALLOW_COPY_AND_ASSIGN(PlatformViewportCommon); | |
182 }; | |
183 | |
184 // static | |
185 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate, | |
186 bool headless) { | |
187 return make_scoped_ptr(new PlatformViewportCommon(delegate, headless)); | |
188 } | |
189 | |
190 } // namespace native_viewport | |
OLD | NEW |