Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: components/view_manager/native_viewport/platform_viewport_common.cc

Issue 1184913004: Revert of android: Introduce a ui::PlatformWindow implementation for android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
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 explicit PlatformViewportCommon(Delegate* delegate) : delegate_(delegate) {
46 }
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 // TODO(sky): make density real.
60 metrics_->device_pixel_ratio = 1.f;
61 metrics_->size_in_pixels = mojo::Size::From(bounds.size());
62
63 #if defined(OS_WIN)
64 platform_window_reset(new ui::WinWindow(this));
65 #elif defined(USE_X11)
66 platform_window_.reset(new ui::X11Window(this));
67 #elif defined(OS_ANDROID)
68 platform_window_.reset(new ui::PlatformWindowAndroid(this));
69 #endif
70 platform_window_->SetBounds(bounds);
71 }
72
73 void Show() override { platform_window_->Show(); }
74
75 void Hide() override { platform_window_->Hide(); }
76
77 void Close() override { platform_window_->Close(); }
78
79 gfx::Size GetSize() override {
80 return metrics_->size_in_pixels.To<gfx::Size>();
81 }
82
83 void SetBounds(const gfx::Rect& bounds) override {
84 platform_window_->SetBounds(bounds);
85 }
86
87 // ui::PlatformWindowDelegate:
88 void OnBoundsChanged(const gfx::Rect& new_bounds) override {
89 // TODO(fsamuel): Use the real device_scale_factor.
90 delegate_->OnMetricsChanged(new_bounds.size(),
91 1.f /* device_scale_factor */);
92 }
93
94 void OnDamageRect(const gfx::Rect& damaged_region) override {}
95
96 void DispatchEvent(ui::Event* event) override {
97 mojo::EventPtr mojo_event(mojo::Event::From(*event));
98 if (event->IsMouseWheelEvent()) {
99 // Mojo's event type has a different meaning for wheel events. Convert
100 // between the two.
101 ui::MouseWheelEvent* wheel_event =
102 static_cast<ui::MouseWheelEvent*>(event);
103 DCHECK(mojo_event->pointer_data);
104 mojo_event->pointer_data->horizontal_wheel =
105 ConvertUIWheelValueToMojoValue(wheel_event->x_offset());
106 mojo_event->pointer_data->horizontal_wheel =
107 ConvertUIWheelValueToMojoValue(wheel_event->y_offset());
108 }
109 delegate_->OnEvent(mojo_event.Pass());
110
111 switch (event->type()) {
112 case ui::ET_MOUSE_PRESSED:
113 case ui::ET_TOUCH_PRESSED:
114 platform_window_->SetCapture();
115 break;
116 case ui::ET_MOUSE_RELEASED:
117 case ui::ET_TOUCH_RELEASED:
118 platform_window_->ReleaseCapture();
119 break;
120 default:
121 break;
122 }
123
124 #if defined(USE_X11)
125 // We want to emulate the WM_CHAR generation behaviour of Windows.
126 //
127 // On Linux, we've previously inserted characters by having
128 // InputMethodAuraLinux take all key down events and send a character event
129 // to the TextInputClient. This causes a mismatch in code that has to be
130 // shared between Windows and Linux, including blink code. Now that we're
131 // trying to have one way of doing things, we need to standardize on and
132 // emulate Windows character events.
133 //
134 // This is equivalent to what we're doing in the current Linux port, but
135 // done once instead of done multiple times in different places.
136 if (event->type() == ui::ET_KEY_PRESSED) {
137 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event);
138 ui::KeyEvent char_event(key_press_event->GetCharacter(),
139 key_press_event->key_code(),
140 key_press_event->flags());
141
142 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter());
143 DCHECK_EQ(key_press_event->key_code(), char_event.key_code());
144 DCHECK_EQ(key_press_event->flags(), char_event.flags());
145
146 char_event.SetExtendedKeyEventData(
147 make_scoped_ptr(new mojo::MojoExtendedKeyEventData(
148 key_press_event->GetLocatedWindowsKeyboardCode(),
149 key_press_event->GetText(),
150 key_press_event->GetUnmodifiedText())));
151 char_event.set_platform_keycode(key_press_event->platform_keycode());
152
153 delegate_->OnEvent(mojo::Event::From(char_event));
154 }
155 #endif
156 }
157
158 void OnCloseRequest() override { platform_window_->Close(); }
159
160 void OnClosed() override { delegate_->OnDestroyed(); }
161
162 void OnWindowStateChanged(ui::PlatformWindowState state) override {}
163
164 void OnLostCapture() override {}
165
166 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget) override {
167 delegate_->OnAcceleratedWidgetAvailable(widget,
168 metrics_->device_pixel_ratio);
169 }
170
171 void OnActivationChanged(bool active) override {}
172
173 scoped_ptr<ui::PlatformWindow> platform_window_;
174 Delegate* delegate_;
175 mojo::ViewportMetricsPtr metrics_;
176
177 DISALLOW_COPY_AND_ASSIGN(PlatformViewportCommon);
178 };
179
180 // static
181 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate,
182 bool headless) {
183 if (headless)
184 return PlatformViewportHeadless::Create(delegate);
185 return make_scoped_ptr(new PlatformViewportCommon(delegate));
186 }
187
188 } // namespace native_viewport
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698