Skip to Content

The PasteRich Chronicles - Part 2: Breaking into Microsoft PowerToys

Porting PasteRich to C# inside Advanced Paste and navigating the 4-million-line PowerToys architecture.

Table of contents

No titles in the blog post

I was analyzing the execution flow of my PasteRich daemon when I realized I needed hardcore enterprise open-source experience. What better place to integrate this functionality than Microsoft PowerToysโ€”the flagship open-source productivity suite for Windows? Building a standalone tool is one thing; architecting that logic directly into a 4-million-line monolithic beast is a completely different engineering discipline.


โ€‹

Part 2 of the PasteRich Chronicles: Architectural Feature PR on Microsoft PowerToys (PR #49460 / Issue #40202).


The Native C# Implementation in Advanced Paste

Rather than running an external wrapper, I engineered a native "Paste as Rich Text" action directly inside PowerToys Advanced Paste (RichTextHelper.cs). The engine ingests raw Markdown source text, converts it via Markdig.Signed, and packages the rendered markup into native Windows CF_HTML clipboard fragments. This guarantees rich formatted pasting directly into Microsoft Word, WordPad, and Outlook without losing formatting or relying on third-party web converters.

The real complexity lay across the Cross-Process IPC and WinRT boundary. PowerToys separates low-level Windows keyboard hooks in its C++ Runner from the C# WinUI 3 desktop host. To route the shortcut without breaking backwards compatibility:

  • Extended the C++/WinRT interop layer (Constants.idl, Constants.h, Constants.cpp, and shared_constants.h) to dispatch the new AdvancedPasteRichTextMessage IPC signal.
  • Registered the global hotkey in dllmain.cpp and routed the message directly to ExecutePasteFormatAsync inside the WinUI 3 runner (App.xaml.cs).
  • Preserved strict ABI stability by assigning an explicit stored integer ID (RichText = 11) and declaring an explicit display sequence in OptionsViewModel.cs to prevent enum shift regressions across existing user settings.


Code Review, Telemetry & Upstream Drift

When core maintainer @MuyuanMS reviewed the branch, we identified that WinAppDriver UI automation was inherently flaky in CI. I took immediate ownership: deleted the brittle UI automation entirely and replaced it with fast, deterministic MSTest unit tests in RichTextHelperTests.cs validating HTML fragment generation and empty clipboard edge cases. After securing sign-off from Microsoft telemetry maintainer @chatasweetie, I maintained the branch through a 448-commit upstream drift, cleanly resolving rebase conflicts against upstream's Phi Silica AI additions.


Architecting an enterprise feature PR isn't about writing isolated code in a vacuum. It's about respecting cross-process IPC boundaries, preserving binary ABI stability, and ruthlessly refactoring tests to satisfy core enterprise maintainers.

You can inspect the full implementation and review history directly on GitHub: microsoft/PowerToys PR #49460.

Archive

Comments

Sign in to leave a comment
The PasteRich Chronicles - Part 1: The Problem with Markdown on Windows
Why pasting Markdown into rich text fields is a nightmare, and how I solved it.