Skip to content

Latest commit

 

History

History
154 lines (102 loc) · 21.2 KB

README.md

File metadata and controls

154 lines (102 loc) · 21.2 KB

ActionUI on Windows (WinUI 3)

ActionUI renders its JSON UI descriptions on Windows as native WinUI 3 interfaces (the Windows App SDK's UI framework), written in Swift and built from the same Swift package as the Apple frameworks and the Linux renderer. ActionUI JSON written for macOS opens on Windows unchanged, with the same element and property names, the same values and actions. Where Windows has a control of its own (a ListView, a NavigationView's pane toggle, the share sheet, WebView2), the renderer uses it, so the UI looks like a Windows app rather than a copy of macOS.

The renderer has two parts:

  • The shim (Shim), a small DLL in C++ with Microsoft's C++/WinRT projection, puts the parts of WinUI 3 that ActionUI uses behind plain C functions (Shim/include/ActionUIWinUIShim.h).
  • The renderer (Sources), in Swift, calls the shim as the Linux renderer calls GTK: one retained WinUI object per element, SwiftUI's layout reproduced in ActionUI's own panels, and the portable core (ActionUI/Core) shared with every platform.

Requirements

  • Windows 11, x64 or Arm64.

  • Microsoft's Visual C++ Redistributable (2015-2022) on the machine that runs the program, unless the program carries its DLLs beside it as the packages do (below, The ActionUI package for Windows); Visual Studio installs it on a build machine.

  • Windows App SDK runtime 2.7 on the machine that runs the program (Microsoft's Windows App SDK runtime installer installs it; Scripts/ci-windows.py --install-runtime installs it for the current user from the NuGet package's MSIX files).

  • To build:

    • a Swift 6.4 toolchain for Windows from swift.org;
    • Visual Studio or its Build Tools with the C++ workload (MSVC and a Windows SDK), for the shim; the build script takes the newest installation vswhere finds;
    • Python 3, which runs the build script (it installs fonttools with pip for the symbol outlines).

    The build script downloads the NuGet packages it needs itself (the Windows App SDK 2.7.0 packages, C++/WinRT and WebView2), at the versions pinned in WINUI_PACKAGES in Scripts/ci-windows.py.

Building and trying it

From the repository root, on Windows:

python Scripts\ci-windows.py winui --install-runtime --results ci-results

It builds the shim (.build\winui-shim\<arch>\ActionUIWinUIShim.dll), compiles the SF Symbols' Material Symbols outlines, and Fluent System Icons outlines for the names Material lacks, into it (Scripts/prepare-windows-symbols.py), runs the shim's C smoke test, builds the Swift package for Windows (windowsPackage() in Package.swift) in .build\ci-windows-winui, builds ActionUI.dll from its own package (ActionUIWinUI\Library, which links the add-ons) in .build\ci-windows-library, runs the smoke test, then renders every test file in Apps/Preview/TestFiles with ActionUIWinUIPreview into ci-results\preview-<name>.png, printing where each element was placed. --install-runtime is needed once per user (and on CI machines, which do not have the runtime). python Scripts\ci-windows.py shim builds and tests the shim alone. --part previews, --part apps or --part hosts runs one of three parts of the checks after the same builds (the previews; the viewer, the demo and the MCP server; ActionUI.dll and the programs and languages that load it), as the GitHub workflow does on three machines at once; without it, the script runs all three.

ActionUIWinUIPreview renders one ActionUI JSON file into a PNG of the window's content: ActionUIWinUIPreview UI.json OUT.png [WIDTH HEIGHT] [options]; without a size, the window opens at the size its content asks for, as an application's window does. Its options operate elements as a user or an accessibility tool would (invoke, toggle, select, type, scroll, drag with the real mouse), change the model as a host does (rows, values, properties), wait for network loads, and check what must follow (actions fired, widths, states); Apps/PreviewSupport/Preview.swift lists them. ActionUIWinUIAddOnPreview (Library) is the same tool with the add-ons ActionUI.dll links; the script renders Apps/Preview/TestFiles/AddOns with it. The GitHub workflow .github/workflows/windows.yml runs the same script on GitHub's x64 and Arm64 Windows runners.

ActionUIWinUIDemo (Apps/Demo, a package of its own that links the add-ons) is the Windows counterpart of ActionUISwiftTestApp, as ActionUIAdwaitaDemo is on Linux: a launcher of the test app's JSON files and those of the add-ons that work on Windows, with the test app's own action handlers. It finds its JSON files and images as any app does (below, "An app's own files"); --resources <folder> names another folder for them, --open <Name> also opens one at launch, --action <actionID> fires an action on it, and --screenshot <file.png> saves it and quits. The script builds it in .build\ci-windows-demo, makes its folder, .build\ci-windows-package\actionui-demo-windows-<arch>\ (and a .zip of it), and runs it from there with only that folder and Windows' own on PATH, saving ci-results\demo-<name>.png. The folder holds the program and every DLL it needs, the Swift runtime's included, the test app's JSON files and those of the add-ons that work on Windows in Resources\, and the test app's images in Assets\: copied to a Windows 11 machine with the Windows App SDK runtime and the Visual C++ Redistributable, it runs without the Swift toolchain.

The ActionUI package for Windows

python Scripts\ci-windows.py winui --install-runtime --quick --actionui-package --results ci-results also makes the ActionUI package, .build\ci-windows-package\actionui-<version>-windows-<arch>.zip with its .zip.sha256: release builds of the demo in demo\ (with its JSON files and images), and of the viewer (ActionUIViewer.exe) and the verifier (actionui-verify.exe) in bin\, each folder with the DLLs its programs load and the Visual C++ runtime's (so the machine needs no Visual C++ Redistributable), the verifier's element schemas and the add-ons' in share\actionui\schemas\, and the licenses. The MCP server has a package of its own (Apps/ActionUIMCP/README.md, Install on Windows). Before it makes the zip, the script runs the three programs from the package, then installs the package, runs the installed verifier and uninstalls it.

Each package carries an installer (ActionUIWinUI/Install). Double-clicking Install.cmd installs the package for the current user, with no administrator rights: it checks every file against the package's SHA256SUMS.txt, copies the package to %LOCALAPPDATA%\Programs\<name>, adds bin to the user's PATH, adds Start menu shortcuts (the ActionUI package: "ActionUI Demo") and an entry in Settings > Apps > Installed apps, from which it uninstalls. When the Windows App SDK runtime is missing, it offers Microsoft's installer, which it downloads and runs only with a valid Microsoft signature. Running it again updates the installation in place; Get-Help .\Install.ps1 -Detailed lists its options. Uninstalling removes what the installation added, and keeps the programs' WebView data (cookies and cache, in %LOCALAPPDATA%\<program name>\WebView2) unless the user says to remove it.

Unsigned programs: what to expect

The packages' programs, DLLs and scripts carry no code signature (Authenticode), so Windows treats them as coming from an unknown publisher:

  • A file downloaded from the internet carries a mark, and File Explorer puts it on every file it unpacks from a downloaded zip. To avoid Windows asking about the package at all, unblock the zip before unpacking it: right-click it, Properties, check Unblock, OK (or in PowerShell, Unblock-File <the zip>).
  • Otherwise, Windows may stop Install.cmd with "Windows protected your PC" (click More info, then Run anyway) or with a security warning (click Run). The installer removes the mark from the copies it installs, so the installed programs then start without asking. Programs run from the unpacked folder without installing keep the mark, and Windows may ask before each one starts.
  • Smart App Control (Windows Security > App & browser control) blocks unsigned programs and DLLs, from File Explorer and from a terminal alike, and has no exception for a single program: while it is on, the programs do not run. It is off on most development machines (Windows turns it off after its evaluation on machines that run unsigned tools). Turning it off lets them run; current Windows 11 lets you turn it on again afterwards.
  • A company-managed machine may enforce an application control policy of its own (App Control for Business, AppLocker), which can block unsigned programs; only its administrators can allow them.
  • SHA256SUMS.txt, which the installer checks, finds a damaged or partly unpacked package, not a deliberately changed one: whoever can change the files can change the list. Compare the zip itself with the .zip.sha256 published beside it: (Get-FileHash -Algorithm SHA256 <the zip>).Hash.ToLower().

What a program ships

An unpackaged Windows program that shows ActionUI UIs carries, beside its .exe:

File Where it comes from Needed
ActionUIWinUIShim.dll built by Scripts/ci-windows.py always
Microsoft.WindowsAppRuntime.Bootstrap.dll NuGet microsoft.windowsappsdk.foundation, runtimes/win-<arch>/native/ always: it attaches the Windows App SDK runtime (actionUIWinUIInitialize)
<program>.exe.manifest as Apps/Smoke/ActionUIWinUISmoke.exe.manifest always: the Windows versions tested and per-monitor DPI awareness
Microsoft.Web.WebView2.Core.dll NuGet microsoft.web.webview2, runtimes/win-<arch>/native_uap/ only when the ActionUI JSON shows a WebView (below)
the Swift runtime's DLLs (swiftCore.dll, Foundation's and the others the program loads) the Swift toolchain's runtime folder (the one on PATH that holds swiftCore.dll), or the library package's bin always: ActionUI is written in Swift

And on the machine: the Windows App SDK runtime 2.7; Microsoft's Visual C++ Redistributable (2015-2022, the machine's architecture), which the Swift runtime and the shim load (vcruntime140.dll, msvcp140.dll) and which a new Windows installation may not have; and for WebView the WebView2 runtime. The library package's bin folder holds all of these for a program that loads ActionUI.dll; the demo's folder (above) is a complete example for a program written in Swift.

An app's own files: resources and assets

Windows has no app bundle and no asset catalog. An app installed from an installer or as an MSIX package is rooted at the folder of its program (the .exe), with its DLLs and data files there or in folders under it, so ActionUI looks for an app's own files from there, never in the current folder:

MyApp\
  MyApp.exe, MyApp.exe.manifest, the DLLs (above)
  Resources\     the app's resources (optional; without it they are beside MyApp.exe)
    Main.json, Settings.json, sheet.html, userscript.js, photo.png ...
  Assets\        the app's asset images, each file named after its asset
    Orb.png, Logo.svg ...   (named in the JSON as "Orb", "Logo")
  • Resources are named in the JSON by their whole file name, extension included, as on Apple and Linux: a LoadableView's "name": "Settings.json", an Image's "resourceName": "photo.png". A WebView user script's resourceName is the one exception: as on Apple, .js may be left out ("resourceName": "userscript" finds userscript.js). Resources are looked up in the program's Resources folder when it has one, then in the program's folder.
  • Assets are named in the JSON without an extension, as an asset in an Xcode asset catalog is: an Image's "assetName": "Orb", a Button's or a Tab's "assetImage": "Orb". On Windows the asset is the image file of that name with an image extension in the Assets folder: Assets\Orb.png, or Orb.svg, Orb.jpg... (.png is tried first, and the case of the name does not matter). The Assets folder is at the top level of the app's folder, whether or not there is a Resources folder; it is where Visual Studio's and MSIX's templates keep an app's images. (A name that already ends in an image extension, "assetName": "Orb.png", is taken as the file's name; JSON shared with Apple leaves the extension out.)
  • A name with a folder in it (a\b.json, ..\b.json), or a Windows device name (CON, NUL, COM1...), finds nothing.
  • These folders are also the app's own files for the trust policy: ActionUI JSON loaded from them is trusted, as the app's own.

A host that names its resource folder. A program whose files are elsewhere, or a host whose program is an interpreter (python.exe, node.exe, whose folder must never be searched or trusted), names the folder itself, before the first window loads. That folder is then used alone, and its Assets folder holds the assets:

Host How it names the folder
Swift WinUIApplication.shared.resourceFolder = path; WinUIApplication.shared.resourceFolders tells the folders in use
C, C++ actionUIAppSetResourceFolder(path) (ActionUIApp.h), or actionUISetConfigurationJSON("{\"resourceFolder\": ...}") (ActionUIC.h)
Python actionui.Application(resource_folder="ui"); without it, the main script's folder
Node.js new actionui.Application({ resourceFolder: path.join(__dirname, 'ui') }), or actionui.configure({ resourceFolder }); without it, the main script's folder
Rust app.set_resource_folder(Some(path)) (App)

WebView and its DLL

A WebView is WinUI's WebView2 control. It needs two things, which are different:

  • The WebView2 runtime: the browser engine itself, shared by every app on the machine and kept up to date by Microsoft (the "Evergreen" runtime). Windows 11 ships it. Windows Server, and some older or locked-down machines, may not have it: Microsoft's WebView2 runtime installer (the "Evergreen Bootstrapper") installs it. A program does not ship it.
  • Microsoft.Web.WebView2.Core.dll: the WebView2 SDK's Windows Runtime component, through which WinUI starts and talks to that runtime (the SDK's loader is linked into it). It belongs to the app, not to Windows: neither Windows nor the Windows App SDK runtime provides it, and each program carries its own copy beside its .exe, or beside ActionUIWinUIShim.dll (x64 about 0.8 MB, Arm64 about 1.1 MB): the shim loads it from its own folder before the first WebView, so a host whose .exe is elsewhere (python.exe, node.exe) finds it in the library package's folder. A WinUI app built in Visual Studio gets it copied into its output by the NuGet package's build rules without noticing; a program built another way copies it itself.

The shim loads the DLL only by its full path, from its own folder or the program's. Without it there, no WebView is made, and the log says "Microsoft.Web.WebView2.Core.dll is not beside ActionUIWinUIShim.dll or the program"; a file of that name in the current folder or on PATH is never loaded. Without the runtime, a WebView logs why the browser failed, or that it has not started after 5 seconds.

A host program can also keep other DLLs from loading out of the current folder: keep the library package's DLLs beside the program or the shim, and call SetDllDirectoryW(L"") (or SetDefaultDllDirectories) first thing in its start. ActionUI does not change the process's DLL search on its own; its own programs (the Viewer and the test tools) do it.

Where to get it

The DLL comes from the microsoft.web.webview2 NuGet package, at runtimes/win-x64/native_uap/Microsoft.Web.WebView2.Core.dll or runtimes/win-arm64/native_uap/Microsoft.Web.WebView2.Core.dll (a .nupkg is a ZIP archive):

  • With the Windows App SDK in Visual Studio: referencing the Windows App SDK brings the matching WebView2 package with it, and the build copies the DLL. Nothing more to do.
  • Built with Scripts/ci-windows.py: the script downloads the package into .build\winui-shim\packages\microsoft.web.webview2\ and copies the DLL beside the smoke test and the preview. Copy it from there into your program's folder.
  • Downloaded by hand: https://api.nuget.org/v3-flatcontainer/microsoft.web.webview2/<version>/microsoft.web.webview2.<version>.nupkg, at the version below.

Which version

Use the WebView2 SDK version that the shim was built with. It is the one the Windows App SDK's WinUI package depends on: Microsoft.WindowsAppSDK.WinUI 2.7.0 depends on Microsoft.Web.WebView2 1.0.3719.77, the version pinned in WINUI_PACKAGES in Scripts/ci-windows.py. For another Windows App SDK version, its WinUI package's .nuspec names the matching one:

https://api.nuget.org/v3-flatcontainer/microsoft.windowsappsdk.winui/<version>/microsoft.windowsappsdk.winui.nuspec

Look for <dependency id="Microsoft.Web.WebView2" version="..." /> and use that version (a NuGet dependency names the lowest version that works, and the Windows App SDK is built and tested against it). Any newer WebView2 runtime works with it: the runtime keeps working with older SDKs.

Its data, and its license

  • The browser keeps its data (cookies, cache) in %LOCALAPPDATA%\<program name>\WebView2, unless the host sets WEBVIEW2_USER_DATA_FOLDER before the first WebView is made. By default WebView2 would put it beside the program, where an installed program cannot write.
  • Microsoft.Web.WebView2.Core.dll is under the WebView2 SDK's license (the package's LICENSE.txt), a BSD-style license from Microsoft: it may be redistributed in binary form provided the copyright notice ("Copyright (C) Microsoft Corporation. All rights reserved."), the license's conditions and its disclaimer are reproduced in the documentation or other materials that ship with it (a third-party notices file is the usual place), and Microsoft's name is not used to endorse or promote the product. A program that ships the DLL, and a distribution of ActionUI that includes it, carries that notice. The WebView2 runtime is not redistributed this way: the program uses the one on the machine.

Downloads for less-trusted content

ActionUI JSON from a server that is not one of the host's trusted origins, or from a file outside the app's resources, is less trusted. Windows' own network stacks send the app's cookies and sign in to servers with the user's Windows account on their own (Negotiate, NTLM), so ActionUI avoids them for this content:

  • LoadableView and AsyncImage (also Table image cells) download with WinHTTP, with no cookies and no Windows sign-in. A server that asks for a sign-in gets a failure.
  • A WebView gets a private browser profile (unless the trust policy allows webViewPersistentData), which keeps no cookies or storage on disk, shares nothing with other WebViews and does not sign in on its own.
  • VideoPlayer is the exception: Windows' media player downloads the video itself, sends the app's cookies and signs in on its own. A host that shows less-trusted content from servers it does not control can refuse remote video for it in its trust policy (playMedia).

Trusted content (the app's own, and JSON from its trusted origins) keeps Windows' network stacks and their settings.

Symbols and their licenses

SF Symbols (Apple's) may not be shipped, so ActionUIWinUIShim.dll draws other glyphs for their names, compiled into it as outlines by Scripts/prepare-windows-symbols.py (no font ships):

  • Google's Material Symbols (Rounded), through ActionUI's SF Symbols -> Material map (ActionUIAndroid/library/src/main/assets/symbols/sf_to_material.map, shared with Android and Linux), at each row's fill and weight, but at most weight 300: Windows' own icons are thinner than SF Symbols, which the map's weights match. Apache License 2.0.
  • Microsoft's Fluent System Icons (the Resizable family), for the SF names the Material map has no usable glyph for, through ActionUI's SF Symbols -> Fluent map (Tools/fluent_symbols/sf_to_fluent.map), drawn as Fluent draws them, Windows' own thinner look. MIT License (Copyright (c) 2020 Microsoft Corporation): the copyright notice and the license's permission notice go with copies of the outlines.

A program that ships the shim, and a distribution of ActionUI that includes it, carries both licenses' notices (a third-party notices file is the usual place). Each font is downloaded at a pinned commit, ActionUIAndroid/library/material-symbols-font.commit and Tools/fluent_symbols/fluent-icons-font.commit, the commit each map was made for.

Layout of this folder

  • Shim/: the C++/WinRT shim. include/ActionUIWinUIShim.h is its C interface, src/ its implementation, Tests/ its C smoke test.
  • CWinUI/: the module map through which Swift sees the shim's header.
  • Sources/: the Swift renderer: Elements/ (one file per element type), Layout/ (stacks, grids, the modifier box), Runtime/ (render nodes, syncing, toolbars, the registry), Interop/ (the shim's objects and events in Swift), Styling/.
  • Apps/Smoke/: the Swift smoke test (ActionUIWinUISmoke) and the program manifest it and the preview use.
  • Apps/Demo/: ActionUIWinUIDemo, the test app's counterpart, a package of its own that links the add-ons.
  • Apps/Preview/: ActionUIWinUIPreview, and in TestFiles/ the ActionUI JSON files CI renders and checks (in TestFiles/AddOns/ those of the add-on elements, which CI renders with ActionUIWinUIAddOnPreview from Library/). They are test files only; nothing ships them.