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