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/memory/scoped_ptr.h" | |
8 #include "components/view_manager/native_viewport/platform_viewport_headless.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 "ui/events/event.h" | |
13 #include "ui/gfx/geometry/rect.h" | |
14 #include "ui/platform_window/platform_window_delegate.h" | |
15 #include "ui/platform_window/win/win_window.h" | |
16 | |
17 namespace native_viewport { | |
18 namespace { | |
19 float ConvertUIWheelValueToMojoValue(int offset) { | |
20 // Mojo's event type takes a value between -1 and 1. Normalize by allowing | |
21 // up to 20 of ui's offset. This is a bit arbitrary. | |
22 return std::max( | |
23 -1.0f, std::min(1.0f, static_cast<float>(offset) / | |
24 (20 * static_cast<float>( | |
25 ui::MouseWheelEvent::kWheelDelta)))); | |
26 } | |
27 } // namespace | |
28 | |
29 class PlatformViewportWin : public PlatformViewport, | |
30 public ui::PlatformWindowDelegate { | |
31 public: | |
32 explicit PlatformViewportWin(Delegate* delegate) | |
33 : delegate_(delegate) { | |
34 } | |
35 | |
36 ~PlatformViewportWin() override { | |
37 // Destroy the platform-window while |this| is still alive. | |
38 platform_window_.reset(); | |
39 } | |
40 | |
41 private: | |
42 // Overridden from PlatformViewport: | |
43 void Init(const gfx::Rect& bounds) override { | |
44 metrics_ = mojo::ViewportMetrics::New(); | |
45 // TODO(sky): make density real. | |
46 metrics_->device_pixel_ratio = 1.f; | |
47 metrics_->size_in_pixels = mojo::Size::From(bounds.size()); | |
48 platform_window_.reset(new ui::WinWindow(this, bounds)); | |
49 } | |
50 | |
51 void Show() override { | |
52 platform_window_->Show(); | |
53 } | |
54 | |
55 void Hide() override { | |
56 platform_window_->Hide(); | |
57 } | |
58 | |
59 void Close() override { | |
60 platform_window_->Close(); | |
61 } | |
62 | |
63 gfx::Size GetSize() override { | |
64 return metrics_->size_in_pixels.To<gfx::Size>(); | |
65 } | |
66 | |
67 void SetBounds(const gfx::Rect& bounds) override { | |
68 platform_window_->SetBounds(bounds); | |
69 } | |
70 | |
71 // ui::PlatformWindowDelegate: | |
72 void OnBoundsChanged(const gfx::Rect& new_bounds) override { | |
73 // TODO(fsamuel): Use the real device_scale_factor. | |
74 delegate_->OnMetricsChanged(new_bounds.size(), | |
75 1.f /* device_scale_factor */); | |
76 } | |
77 | |
78 void OnDamageRect(const gfx::Rect& damaged_region) override { | |
79 } | |
80 | |
81 void DispatchEvent(ui::Event* event) override { | |
82 // TODO(jam): this code is copied from the X11 version. | |
83 mojo::EventPtr mojo_event(mojo::Event::From(*event)); | |
84 if (event->IsMouseWheelEvent()) { | |
85 // Mojo's event type has a different meaning for wheel events. Convert | |
86 // between the two. | |
87 ui::MouseWheelEvent* wheel_event = | |
88 static_cast<ui::MouseWheelEvent*>(event); | |
89 DCHECK(mojo_event->pointer_data); | |
90 mojo_event->pointer_data->horizontal_wheel = | |
91 ConvertUIWheelValueToMojoValue(wheel_event->x_offset()); | |
92 mojo_event->pointer_data->horizontal_wheel = | |
93 ConvertUIWheelValueToMojoValue(wheel_event->y_offset()); | |
94 } | |
95 delegate_->OnEvent(mojo_event.Pass()); | |
96 | |
97 switch (event->type()) { | |
98 case ui::ET_MOUSE_PRESSED: | |
99 case ui::ET_TOUCH_PRESSED: | |
100 platform_window_->SetCapture(); | |
101 break; | |
102 case ui::ET_MOUSE_RELEASED: | |
103 case ui::ET_TOUCH_RELEASED: | |
104 platform_window_->ReleaseCapture(); | |
105 break; | |
106 default: | |
107 break; | |
108 } | |
109 } | |
110 | |
111 void OnCloseRequest() override { | |
112 platform_window_->Close(); | |
113 } | |
114 | |
115 void OnClosed() override { | |
116 delegate_->OnDestroyed(); | |
117 } | |
118 | |
119 void OnWindowStateChanged(ui::PlatformWindowState state) override { | |
120 } | |
121 | |
122 void OnLostCapture() override { | |
123 } | |
124 | |
125 void OnAcceleratedWidgetAvailable( | |
126 gfx::AcceleratedWidget widget) override { | |
127 delegate_->OnAcceleratedWidgetAvailable(widget, | |
128 metrics_->device_pixel_ratio); | |
129 } | |
130 | |
131 void OnActivationChanged(bool active) override {} | |
132 | |
133 scoped_ptr<ui::PlatformWindow> platform_window_; | |
134 Delegate* delegate_; | |
135 mojo::ViewportMetricsPtr metrics_; | |
136 | |
137 DISALLOW_COPY_AND_ASSIGN(PlatformViewportWin); | |
138 }; | |
139 | |
140 // static | |
141 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate, | |
142 bool headless) { | |
143 if (headless) | |
144 return PlatformViewportHeadless::Create(delegate); | |
145 return make_scoped_ptr(new PlatformViewportWin(delegate)); | |
146 } | |
147 | |
148 } // namespace native_viewport | |
OLD | NEW |