A Swift projection for WinRT APIs, written in pure Swift, for consuming modern Windows Runtime APIs, the Windows App SDK, WinUI and arbitrary WinRT components.
Swift/WinRT includes:
.winmd
files, based on the swift-dotnetmetadata project.RoInitialize
.For examples of using projections, refer to interop tests.
This project was inspired by its C++ namesake at thebrowsercompany/swift-winrt.
Note: Swift/WinRT requires Swift 5.10 or above due to uses of non-copyable types which crash the Swift 5.9 compiler.
Setup your project's build to:
nuget.org
.SwiftWinRT.exe
located in the NuGet package, specifying:
Projection.json
file to describe the modules to generate, which assemblies should contribute to each of them, and which types to include. Refer to this example or the schema.swift
subdirectory of the NuGet package.Projection.json
, there should be an assembly module (with projected types), an ABI module (with C code) and any number of namespace modules (with type aliases for convenience).SPM cannot integrate arbitrary build steps needed for fully integrating Swift/WinRT code generation in your build. Alternatives:
When invoking SwiftWinRT.exe
, specify to generate a Package.swift
file that references the swift
subdirectory of the NuGet package as the support module location, or reference it from this repository.
Swift/WinRT should support the majority of WinRT interop scenarios thanks to the following features:
IInspectable
IReference<T>
boxing, IAsyncInfo
/IAsyncOperation
, weak references and collection interfacesUIElement
to a Button
using as
Correctness & completeness first: The information loss from WinRT APIs should be minimal, including when this results in less Swifty code. For example, most methods are throwing as to capture any failure HRESULTs, and namespaces are simulated to avoid name clashes. If information loss is desirable for ergonomics, opt-in switches may be provided.
Readability of projections: The generated types and projection glue code should be as readable as possible to ensure correctness and so that one can easily debug into the generated code as necessary.
Interoperability with COM: It should be trivial to query WinRT objects for COM interfaces and use them as such. In fact, this project is designed to be extensible to eventually generate COM API descriptions from the Win32 Metadata project or COM type libraries.
Composability: It should be possible to generate projections for individual assemblies independently and have them interoperate.
Independence from Windows headers: The generated code should not rely on header files from the Windows SDK, but rather produce any C definitions it requires.