All TermControl/TermApp/TermConnection event handlers should be TypedEventHandlers #1471

Open
opened 2026-01-30 22:28:12 +00:00 by claunia · 0 comments
Owner

Originally created by @DHowett-MSFT on GitHub (Jun 1, 2019).

WinRT design guidelines dictate that event handlers should have two arguments, T and EventArgs. For any class T that produces an event, this ensures that the event handler can identify which instance of T fired the currently-handled event.

The signature for an event handler delegate should consist of two parameters: sender (IInspectable), and args (some event argument type, for example RoutedEventArgs).

Given that we are exposing an internal API, we can specify a more specific T than IInspectable.

C++/WinRT supports this by way of winrt::TypedEventHandler<T, Args...>.

We should move all the TermConnection, TermApp and TermControl event handlers to use TypedEventHandlers.

bonus

We can get rid of DECLARE_EVENT and DEFINE_EVENT, and augment DECLARE_EVENT_WITH_TYPED_HANDLER (and DEFINE...TYPED) to use typename class_type as the sender type, so that we don't need to repeat ourselves:

880272c748/src/cascadia/inc/cppwinrt_utils.h (L42-L47)

-#define DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(name, eventHandler, sender, args) \
+#define DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(name, eventHandler, args) \
     public: \
-    winrt::event_token name(Windows::Foundation::TypedEventHandler<sender, args> const& handler); \
+    winrt::event_token name(Windows::Foundation::TypedEventHandler<typename class_type, args> const& handler); \     void name(winrt::event_token const& token) noexcept; \
     private: \
-    winrt::event<Windows::Foundation::TypedEventHandler<sender, args>> eventHandler;
+    winrt::event<Windows::Foundation::TypedEventHandler<typename class_type, args>> eventHandler;

which, when used, becomes:

-DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(PasteFromClipboard, _clipboardPasteHandlers, TerminalControl::TermControl, TerminalControl::PasteFromClipboardEventArgs);
+DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(PasteFromClipboard, _clipboardPasteHandlers, TerminalControl::PasteFromClipboardEventArgs);
Originally created by @DHowett-MSFT on GitHub (Jun 1, 2019). WinRT [design guidelines](https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/author-events#design-guidelines) dictate that event handlers should have two arguments, `T` and `EventArgs`. For any class `T` that produces an event, this ensures that the event _handler_ can identify which instance of `T` fired the currently-handled event. > The signature for an event handler delegate should consist of two parameters: sender (`IInspectable`), and args (some event argument type, for example `RoutedEventArgs`). Given that we are exposing an internal API, we can specify a more specific `T` than `IInspectable`. C++/WinRT supports this by way of `winrt::TypedEventHandler<T, Args...>`. We should move all the TermConnection, TermApp and TermControl event handlers to use `TypedEventHandler`s. ### bonus We can get rid of `DECLARE_EVENT` and `DEFINE_EVENT`, and augment `DECLARE_EVENT_WITH_TYPED_HANDLER` (and `DEFINE...TYPED`) to use `typename class_type` as the sender type, so that we don't need to repeat ourselves: https://github.com/microsoft/terminal/blob/880272c7483a3bb6893dbec3725d0eb7375ab78a/src/cascadia/inc/cppwinrt_utils.h#L42-L47 ```diff -#define DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(name, eventHandler, sender, args) \ +#define DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(name, eventHandler, args) \ public: \ - winrt::event_token name(Windows::Foundation::TypedEventHandler<sender, args> const& handler); \ + winrt::event_token name(Windows::Foundation::TypedEventHandler<typename class_type, args> const& handler); \ void name(winrt::event_token const& token) noexcept; \ private: \ - winrt::event<Windows::Foundation::TypedEventHandler<sender, args>> eventHandler; + winrt::event<Windows::Foundation::TypedEventHandler<typename class_type, args>> eventHandler; ``` which, when used, becomes: ```diff -DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(PasteFromClipboard, _clipboardPasteHandlers, TerminalControl::TermControl, TerminalControl::PasteFromClipboardEventArgs); +DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(PasteFromClipboard, _clipboardPasteHandlers, TerminalControl::PasteFromClipboardEventArgs); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#1471