{"id":6104,"date":"2020-09-08T11:24:51","date_gmt":"2020-09-08T18:24:51","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/commandline\/?p=6104"},"modified":"2020-09-08T11:24:51","modified_gmt":"2020-09-08T18:24:51","slug":"building-windows-terminal-with-winui","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/commandline\/building-windows-terminal-with-winui\/","title":{"rendered":"Building Windows Terminal with WinUI"},"content":{"rendered":"<p>WinUI and Windows Terminal have a strong relationship that goes back to the origins of Windows Terminal. This blog post goes into the history and architecture of how these two technologies came together.<\/p>\n<h2>History<\/h2>\n<p>The history of <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/apps\/winui\/\" rel=\"noopener noreferrer\" target=\"_blank\">WinUI<\/a> and Windows Terminal goes back to December 2017. This was around the time the first prototyping of the application that would become Windows Terminal began. We, as the Windows Terminal team, had just started getting <a href=\"https:\/\/devblogs.microsoft.com\/commandline\/windows-command-line-introducing-the-windows-pseudo-console-conpty\/\" rel=\"noopener noreferrer\" target=\"_blank\">conpty<\/a> into a place that was good enough to be useful as a translation layer between the console and a terminal application. We knew that we wanted to have conpty be able to power a new terminal application, allowing us to build a new user experience for the command line on Windows.<\/p>\n<p>When we were in the earliest planning stages for Terminal, we knew right off the bat that we wanted to be able to build a modern application that used the best features the Windows platform had to offer. We wanted to make sure that it was visually consistent with other inbox applications and followed Fluent Design principles.<\/p>\n<p>We made the choice to build Windows Terminal as a UWP application. That would immediately grant us access to UWP XAML and the whole <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/apps\/winui\/winui2\/\" rel=\"noopener noreferrer\" target=\"_blank\">WinUI 2 library<\/a>, which has native controls, styles, and capabilities built specifically for Windows apps. At the time, &#8220;UWP applications&#8221; had special restrictions placed on them by the OS. These security features ensured that UWP apps delivered by the Microsoft Store were unable to access restricted locations within the OS, or leave artifacts laying around on disk, to simplify the uninstall process. These UWP apps are all launched inside a &#8220;low integrity app container&#8221;, which basically means they have effectively <em>no<\/em> permissions on the OS. They&#8217;re not even allowed to change their working directory. They&#8217;re allowed to launch external processes, but these external processes are additionally bound by the same restrictions the app is bound to.<\/p>\n<p>Therefore, if we wanted to build Terminal as a UWP app in 2018, then any of the shells that we would spawn (like cmd.exe, powershell.exe or bash) would be unable to do anything to the system. Could you imagine using your shell without being able to change directories, read file contents, or launch any other processes that you could interact with? Obviously, this was a non-starter for us.<\/p>\n<p>We spent a good portion of that year discussing our options with a number of teams across the Windows organization. We considered options like a special restricted capability for launching full-trust processes from a UWP. We tried pushing on allowing UWP apps to run outside the app container. However, most of the routes we pursued ended up either as a gaping security hole, or as something that would require years of effort across the organization. (In fact, many of these discussions helped lead to the effort now known as &#8220;<a href=\"https:\/\/github.com\/microsoft\/ProjectReunion\" rel=\"noopener noreferrer\" target=\"_blank\">Project Reunion<\/a>&#8220;, as well as the <a href=\"https:\/\/github.com\/microsoft\/microsoft-ui-xaml\/blob\/master\/docs\/roadmap.md\" rel=\"noopener noreferrer\" target=\"_blank\">WinUI 3 effort<\/a> that extends the modern UI framework to Desktop apps).<\/p>\n<h2>XAML Islands<\/h2>\n<p>Fortunately for us, the solution was actually closer than we thought. Just down the hall from us was the WinUI team, and little did we know, they had been building <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/apps\/desktop\/modernize\/xaml-islands\" rel=\"noopener noreferrer\" target=\"_blank\">XAML Islands<\/a> for the better part of the previous year. XAML Islands would enable a traditional Win32-style application to host UWP XAML content within its window without the traditional restrictions of a UWP application. As it turns out, this was <em>exactly<\/em> what we needed.<\/p>\n<p>What we ended up building was an application that was composed of two parts:<\/p>\n<ul>\n<li>\n<p>A thin Win32 window that is mostly just responsible for drawing a window to the screen and starting up the XAML Hosting infrastructure<\/p>\n<\/li>\n<li>\n<p>The bulk of the application which is built as a UWP XAML Application that uses WinUI 2. This layer doesn&#8217;t even know that it&#8217;s being hosted by a Win32 window. For all it knows, it&#8217;s just a normal C++\/WinRT UWP application.<\/p>\n<\/li>\n<\/ul>\n<p>This model allows Terminal to get the best of both worlds. As a Win32 application, we&#8217;re able to use all of the legacy Win32 APIs that a terminal app might need to launch processes and query the filesystem. We&#8217;ve also been able to use the power of UWP XAML and WinUI 2 to help build the UI of Terminal quickly and beautifully.<\/p>\n<h2>Architecture<\/h2>\n<p>For a more detailed breakdown of Windows Terminal&#8217;s architecture, let&#8217;s refer to the following diagram:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/commandline\/wp-content\/uploads\/sites\/33\/2020\/09\/terminal-architecture.png\">