@@ -140,152 +140,170 @@ struct CompositionInputKeyboardSource : winrt::implements<
140
140
CompositionEventHandler::CompositionEventHandler (
141
141
const winrt::Microsoft::ReactNative::ReactContext &context,
142
142
const winrt::Microsoft::ReactNative::ReactNativeIsland &reactNativeIsland)
143
- : m_context(context), m_wkRootView(reactNativeIsland) {
143
+ : m_context(context), m_wkRootView(reactNativeIsland) {}
144
+
145
+ void CompositionEventHandler::Initialize () noexcept {
144
146
#ifdef USE_WINUI3
145
- if (auto island = reactNativeIsland .Island ()) {
147
+ if (auto island = m_wkRootView. get () .Island ()) {
146
148
auto pointerSource = winrt::Microsoft::UI::Input::InputPointerSource::GetForIsland (island);
147
149
148
150
m_pointerPressedToken =
149
- pointerSource.PointerPressed ([this ](
151
+ pointerSource.PointerPressed ([wkThis = weak_from_this () ](
150
152
winrt::Microsoft::UI::Input::InputPointerSource const &,
151
153
winrt::Microsoft::UI::Input::PointerEventArgs const &args) {
152
- if (auto strongRootView = m_wkRootView.get ()) {
153
- if (SurfaceId () == -1 )
154
- return ;
155
-
156
- auto pp = winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::PointerPoint>(
157
- args.CurrentPoint (), strongRootView.ScaleFactor ());
158
- onPointerPressed (pp, args.KeyModifiers ());
154
+ if (auto strongThis = wkThis.lock ()) {
155
+ if (auto strongRootView = strongThis->m_wkRootView .get ()) {
156
+ if (strongThis->SurfaceId () == -1 )
157
+ return ;
158
+
159
+ auto pp = winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::PointerPoint>(
160
+ args.CurrentPoint (), strongRootView.ScaleFactor ());
161
+ strongThis->onPointerPressed (pp, args.KeyModifiers ());
162
+ }
159
163
}
160
164
});
161
165
162
166
m_pointerReleasedToken =
163
- pointerSource.PointerReleased ([this ](
167
+ pointerSource.PointerReleased ([wkThis = weak_from_this () ](
164
168
winrt::Microsoft::UI::Input::InputPointerSource const &,
165
169
winrt::Microsoft::UI::Input::PointerEventArgs const &args) {
166
- if (auto strongRootView = m_wkRootView.get ()) {
167
- if (SurfaceId () == -1 )
168
- return ;
169
-
170
- auto pp = winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::PointerPoint>(
171
- args.CurrentPoint (), strongRootView.ScaleFactor ());
172
- onPointerReleased (pp, args.KeyModifiers ());
170
+ if (auto strongThis = wkThis.lock ()) {
171
+ if (auto strongRootView = strongThis->m_wkRootView .get ()) {
172
+ if (strongThis->SurfaceId () == -1 )
173
+ return ;
174
+
175
+ auto pp = winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::PointerPoint>(
176
+ args.CurrentPoint (), strongRootView.ScaleFactor ());
177
+ strongThis->onPointerReleased (pp, args.KeyModifiers ());
178
+ }
173
179
}
174
180
});
175
181
176
- m_pointerMovedToken = pointerSource.PointerMoved ([this ](
182
+ m_pointerMovedToken = pointerSource.PointerMoved ([wkThis = weak_from_this () ](
177
183
winrt::Microsoft::UI::Input::InputPointerSource const &,
178
184
winrt::Microsoft::UI::Input::PointerEventArgs const &args) {
179
- if (auto strongRootView = m_wkRootView.get ()) {
180
- if (SurfaceId () == -1 )
181
- return ;
182
-
183
- auto pp = winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::PointerPoint>(
184
- args.CurrentPoint (), strongRootView.ScaleFactor ());
185
- onPointerMoved (pp, args.KeyModifiers ());
185
+ if (auto strongThis = wkThis.lock ()) {
186
+ if (auto strongRootView = strongThis->m_wkRootView .get ()) {
187
+ if (strongThis->SurfaceId () == -1 )
188
+ return ;
189
+
190
+ auto pp = winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::PointerPoint>(
191
+ args.CurrentPoint (), strongRootView.ScaleFactor ());
192
+ strongThis->onPointerMoved (pp, args.KeyModifiers ());
193
+ }
186
194
}
187
195
});
188
196
189
197
m_pointerCaptureLostToken =
190
- pointerSource.PointerCaptureLost ([this ](
198
+ pointerSource.PointerCaptureLost ([wkThis = weak_from_this () ](
191
199
winrt::Microsoft::UI::Input::InputPointerSource const &,
192
200
winrt::Microsoft::UI::Input::PointerEventArgs const &args) {
193
- if (auto strongRootView = m_wkRootView.get ()) {
194
- if (SurfaceId () == -1 )
195
- return ;
196
-
197
- auto pp = winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::PointerPoint>(
198
- args.CurrentPoint (), strongRootView.ScaleFactor ());
199
- onPointerCaptureLost (pp, args.KeyModifiers ());
201
+ if (auto strongThis = wkThis.lock ()) {
202
+ if (auto strongRootView = strongThis->m_wkRootView .get ()) {
203
+ if (strongThis->SurfaceId () == -1 )
204
+ return ;
205
+
206
+ auto pp = winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::PointerPoint>(
207
+ args.CurrentPoint (), strongRootView.ScaleFactor ());
208
+ strongThis->onPointerCaptureLost (pp, args.KeyModifiers ());
209
+ }
200
210
}
201
211
});
202
212
203
213
m_pointerWheelChangedToken =
204
- pointerSource.PointerWheelChanged ([this ](
214
+ pointerSource.PointerWheelChanged ([wkThis = weak_from_this () ](
205
215
winrt::Microsoft::UI::Input::InputPointerSource const &,
206
216
winrt::Microsoft::UI::Input::PointerEventArgs const &args) {
207
- if (auto strongRootView = m_wkRootView.get ()) {
208
- if (SurfaceId () == -1 )
209
- return ;
210
-
211
- auto pp = winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::PointerPoint>(
212
- args.CurrentPoint (), strongRootView.ScaleFactor ());
213
- onPointerWheelChanged (pp, args.KeyModifiers ());
217
+ if (auto strongThis = wkThis.lock ()) {
218
+ if (auto strongRootView = strongThis->m_wkRootView .get ()) {
219
+ if (strongThis->SurfaceId () == -1 )
220
+ return ;
221
+
222
+ auto pp = winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::PointerPoint>(
223
+ args.CurrentPoint (), strongRootView.ScaleFactor ());
224
+ strongThis->onPointerWheelChanged (pp, args.KeyModifiers ());
225
+ }
214
226
}
215
227
});
216
228
217
229
auto keyboardSource = winrt::Microsoft::UI::Input::InputKeyboardSource::GetForIsland (island);
218
230
219
- m_keyDownToken = keyboardSource.KeyDown ([this ](
231
+ m_keyDownToken = keyboardSource.KeyDown ([wkThis = weak_from_this () ](
220
232
winrt::Microsoft::UI::Input::InputKeyboardSource const &source,
221
233
winrt::Microsoft::UI::Input::KeyEventArgs const &args) {
222
- if (auto strongRootView = m_wkRootView.get ()) {
223
- if (SurfaceId () == -1 )
224
- return ;
225
-
226
- auto focusedComponent = RootComponentView ().GetFocusedComponent ();
227
- auto keyboardSource = winrt::make<CompositionInputKeyboardSource>(source);
228
- auto keyArgs =
229
- winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::KeyRoutedEventArgs>(
230
- focusedComponent
231
- ? focusedComponent.Tag ()
232
- : static_cast <facebook::react::Tag>(
233
- winrt::get_self<winrt::Microsoft::ReactNative::implementation::ReactNativeIsland>(
234
- strongRootView)
235
- ->RootTag ()),
236
- args,
237
- keyboardSource);
238
- onKeyDown (keyArgs);
239
- winrt::get_self<CompositionInputKeyboardSource>(keyboardSource)->Disconnect ();
234
+ if (auto strongThis = wkThis.lock ()) {
235
+ if (auto strongRootView = strongThis->m_wkRootView .get ()) {
236
+ if (strongThis->SurfaceId () == -1 )
237
+ return ;
238
+
239
+ auto focusedComponent = strongThis->RootComponentView ().GetFocusedComponent ();
240
+ auto keyboardSource = winrt::make<CompositionInputKeyboardSource>(source);
241
+ auto keyArgs =
242
+ winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::KeyRoutedEventArgs>(
243
+ focusedComponent
244
+ ? focusedComponent.Tag ()
245
+ : static_cast <facebook::react::Tag>(
246
+ winrt::get_self<winrt::Microsoft::ReactNative::implementation::ReactNativeIsland>(
247
+ strongRootView)
248
+ ->RootTag ()),
249
+ args,
250
+ keyboardSource);
251
+ strongThis->onKeyDown (keyArgs);
252
+ winrt::get_self<CompositionInputKeyboardSource>(keyboardSource)->Disconnect ();
253
+ }
240
254
}
241
255
});
242
256
243
- m_keyUpToken = keyboardSource.KeyUp ([this ](
257
+ m_keyUpToken = keyboardSource.KeyUp ([wkThis = weak_from_this () ](
244
258
winrt::Microsoft::UI::Input::InputKeyboardSource const &source,
245
259
winrt::Microsoft::UI::Input::KeyEventArgs const &args) {
246
- if (auto strongRootView = m_wkRootView.get ()) {
247
- if (SurfaceId () == -1 )
248
- return ;
249
-
250
- auto focusedComponent = RootComponentView ().GetFocusedComponent ();
251
- auto keyboardSource = winrt::make<CompositionInputKeyboardSource>(source);
252
- auto keyArgs =
253
- winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::KeyRoutedEventArgs>(
254
- focusedComponent
255
- ? focusedComponent.Tag ()
256
- : static_cast <facebook::react::Tag>(
257
- winrt::get_self<winrt::Microsoft::ReactNative::implementation::ReactNativeIsland>(
258
- strongRootView)
259
- ->RootTag ()),
260
- args,
261
- keyboardSource);
262
- onKeyUp (keyArgs);
263
- winrt::get_self<CompositionInputKeyboardSource>(keyboardSource)->Disconnect ();
260
+ if (auto strongThis = wkThis.lock ()) {
261
+ if (auto strongRootView = strongThis->m_wkRootView .get ()) {
262
+ if (strongThis->SurfaceId () == -1 )
263
+ return ;
264
+
265
+ auto focusedComponent = strongThis->RootComponentView ().GetFocusedComponent ();
266
+ auto keyboardSource = winrt::make<CompositionInputKeyboardSource>(source);
267
+ auto keyArgs =
268
+ winrt::make<winrt::Microsoft::ReactNative::Composition::Input::implementation::KeyRoutedEventArgs>(
269
+ focusedComponent
270
+ ? focusedComponent.Tag ()
271
+ : static_cast <facebook::react::Tag>(
272
+ winrt::get_self<winrt::Microsoft::ReactNative::implementation::ReactNativeIsland>(
273
+ strongRootView)
274
+ ->RootTag ()),
275
+ args,
276
+ keyboardSource);
277
+ strongThis->onKeyUp (keyArgs);
278
+ winrt::get_self<CompositionInputKeyboardSource>(keyboardSource)->Disconnect ();
279
+ }
264
280
}
265
281
});
266
282
267
283
m_characterReceivedToken =
268
- keyboardSource.CharacterReceived ([this ](
284
+ keyboardSource.CharacterReceived ([wkThis = weak_from_this () ](
269
285
winrt::Microsoft::UI::Input::InputKeyboardSource const &source,
270
286
winrt::Microsoft::UI::Input::CharacterReceivedEventArgs const &args) {
271
- if (auto strongRootView = m_wkRootView.get ()) {
272
- if (SurfaceId () == -1 )
273
- return ;
274
-
275
- auto focusedComponent = RootComponentView ().GetFocusedComponent ();
276
- auto keyboardSource = winrt::make<CompositionInputKeyboardSource>(source);
277
- auto charArgs = winrt::make<
278
- winrt::Microsoft::ReactNative::Composition::Input::implementation::CharacterReceivedRoutedEventArgs>(
279
- focusedComponent
280
- ? focusedComponent.Tag ()
281
- : static_cast <facebook::react::Tag>(
282
- winrt::get_self<winrt::Microsoft::ReactNative::implementation::ReactNativeIsland>(
283
- strongRootView)
284
- ->RootTag ()),
285
- args,
286
- keyboardSource);
287
- onCharacterReceived (charArgs);
288
- winrt::get_self<CompositionInputKeyboardSource>(keyboardSource)->Disconnect ();
287
+ if (auto strongThis = wkThis.lock ()) {
288
+ if (auto strongRootView = strongThis->m_wkRootView .get ()) {
289
+ if (strongThis->SurfaceId () == -1 )
290
+ return ;
291
+
292
+ auto focusedComponent = strongThis->RootComponentView ().GetFocusedComponent ();
293
+ auto keyboardSource = winrt::make<CompositionInputKeyboardSource>(source);
294
+ auto charArgs = winrt::make<
295
+ winrt::Microsoft::ReactNative::Composition::Input::implementation::CharacterReceivedRoutedEventArgs>(
296
+ focusedComponent
297
+ ? focusedComponent.Tag ()
298
+ : static_cast <facebook::react::Tag>(
299
+ winrt::get_self<winrt::Microsoft::ReactNative::implementation::ReactNativeIsland>(
300
+ strongRootView)
301
+ ->RootTag ()),
302
+ args,
303
+ keyboardSource);
304
+ strongThis->onCharacterReceived (charArgs);
305
+ winrt::get_self<CompositionInputKeyboardSource>(keyboardSource)->Disconnect ();
306
+ }
289
307
}
290
308
});
291
309
}
0 commit comments