Repository: microsoft/terminal (language: cpp)

Issue:
Update to WinUI 2.8 (was: History size overlaps with widget controls)
### Windows Terminal version

1.15.1863.0

### Windows build number

10.0.22000.778

### Other Software

_No response_

### Steps to reproduce

Enter a large history size, INT_MAX used for testing

### Expected Behavior

No overlapping

### Actual Behavior

![max_history_size](https://user-images.githubusercontent.com/3933920/178622277-6827ea18-30c7-44de-b0c4-c243b8149d9f.png)


## _maintainer note_

Also:
* [ ] #9822



Discussion / hints:
isn't this it https://github.com/microsoft/microsoft-ui-xaml/issues/5983 ?
Sure looks like it to me, thanks! We haven't had an opportunity to move to WinUI 2.8 yet, but I'm gonna tentatively use this to track upgrading. We also need to do this to fix the crashes associated with #12413 in downlevel OS versions. 
Taking this out of 1.16, cause it seems like there's a lot of issues with 2.8 and we don't have the runway to sort them all out. The important bit, the #12413 crash, we're gonna fix with a minor hotfix to 2.7.

Fix patch:
diff --git a/build/rules/Microsoft.UI.Xaml.Additional.targets b/build/rules/Microsoft.UI.Xaml.Additional.targets
new file mode 100644
index 00000000000..dbd6eb3f35c
--- /dev/null
+++ b/build/rules/Microsoft.UI.Xaml.Additional.targets
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <!--
+    This file contains targets that override behavior in Microsoft.UI.Xaml and
+    related packages.
+
+    For example: All XAML needs is a reference to WebView2; it does not need the
+    DLL and it does not need for us to copy the WinMD into the output folder. It
+    also doesn't require the WebView2 loader since we're not actually using
+    WebView2. Therefore, we can get away with *not including the WebView2
+    package* and only adding a reference to its winmd.
+  -->
+  <ItemGroup>
+    <Reference Include="$(WebView2PackageRoot)\lib\Microsoft.Web.WebView2.Core.winmd" />
+  </ItemGroup>
+</Project>
diff --git a/dep/nuget/packages.config b/dep/nuget/packages.config
index 5015d1ee970..09a9c4935d4 100644
--- a/dep/nuget/packages.config
+++ b/dep/nuget/packages.config
@@ -8,7 +8,8 @@
   <package id="Microsoft.VCRTForwarders.140" version="1.0.4" targetFramework="native" />
   <package id="Microsoft.Internal.Windows.Terminal.ThemeHelpers" version="0.6.220404001" targetFramework="native" />
   <package id="Microsoft.VisualStudio.Setup.Configuration.Native" version="2.3.2262" targetFramework="native" developmentDependency="true" />
-  <package id="Microsoft.UI.Xaml" version="2.7.3" targetFramework="native" />
+  <package id="Microsoft.UI.Xaml" version="2.8.2" targetFramework="native" />
+  <package id="Microsoft.Web.WebView2" version="1.0.1661.34" targetFramework="native" />
   <package id="Microsoft.Windows.ImplementationLibrary" version="1.0.220201.1" targetFramework="native" developmentDependency="true" />
 
   <!-- Managed packages -->
diff --git a/src/cascadia/TerminalApp/TabBase.cpp b/src/cascadia/TerminalApp/TabBase.cpp
index 54b25c48b08..6761ab3dac1 100644
--- a/src/cascadia/TerminalApp/TabBase.cpp
+++ b/src/cascadia/TerminalApp/TabBase.cpp
@@ -475,33 +475,36 @@ namespace winrt::TerminalApp::implementation
         // In GH#11294 we thought we'd still need to set
         // TabViewItemHeaderBackground manually, but GH#11382 discovered that
         // Background() was actually okay after all.
+
+        const auto& tabItemResources{ TabViewItem().Resources() };
+
         TabViewItem().Background(deselectedTabBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderBackgroundSelected"), selectedTabBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderBackgroundPointerOver"), hoverTabBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderBackgroundPressed"), selectedTabBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderBackgroundSelected"), selectedTabBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderBackgroundPointerOver"), hoverTabBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderBackgroundPressed"), selectedTabBrush);
 
         // Similarly, TabViewItem().Foreground()  sets the color for the text
         // when the TabViewItem isn't selected, but not when it is hovered,
         // pressed, dragged, or selected, so we'll need to just set them all
         // anyways.
         TabViewItem().Foreground(deselectedFontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderForeground"), deselectedFontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderForegroundSelected"), fontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderForegroundPointerOver"), fontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderForegroundPressed"), fontBrush);
-
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderCloseButtonForeground"), deselectedFontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderCloseButtonForegroundPressed"), secondaryFontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderCloseButtonForegroundPointerOver"), fontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderPressedCloseButtonForeground"), fontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderPointerOverCloseButtonForeground"), fontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderSelectedCloseButtonForeground"), fontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderCloseButtonBackgroundPressed"), subtleFillColorTertiaryBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewItemHeaderCloseButtonBackgroundPointerOver"), subtleFillColorSecondaryBrush);
-
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewButtonForegroundActiveTab"), fontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewButtonForegroundPressed"), fontBrush);
-        TabViewItem().Resources().Insert(winrt::box_value(L"TabViewButtonForegroundPointerOver"), fontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderForeground"), deselectedFontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderForegroundSelected"), fontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderForegroundPointerOver"), fontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderForegroundPressed"), fontBrush);
+
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderCloseButtonForeground"), deselectedFontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderCloseButtonForegroundPressed"), secondaryFontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderCloseButtonForegroundPointerOver"), fontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderPressedCloseButtonForeground"), fontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderPointerOverCloseButtonForeground"), fontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderSelectedCloseButtonForeground"), fontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderCloseButtonBackgroundPressed"), subtleFillColorTertiaryBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewItemHeaderCloseButtonBackgroundPointerOver"), subtleFillColorSecondaryBrush);
+
+        tabItemResources.Insert(winrt::box_value(L"TabViewButtonForegroundActiveTab"), fontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewButtonForegroundPressed"), fontBrush);
+        tabItemResources.Insert(winrt::box_value(L"TabViewButtonForegroundPointerOver"), fontBrush);
 
         _RefreshVisualState();
     }
@@ -537,13 +540,15 @@ namespace winrt::TerminalApp::implementation
             L"TabViewButtonForegroundPointerOver"
         };
 
+        const auto& tabItemResources{ TabViewItem().Resources() };
+
         // simply clear any of the colors in the tab's dict
         for (const auto& keyString : keys)
         {
             auto key = winrt::box_value(keyString);
-            if (TabViewItem().Resources().HasKey(key))
+            if (tabItemResources.HasKey(key))
             {
-                TabViewItem().Resources().Remove(key);
+                tabItemResources.Remove(key);
             }
         }
 
@@ -556,24 +561,20 @@ namespace winrt::TerminalApp::implementation
     }
 
     // Method Description:
-    // Toggles the visual state of the tab view item,
-    // so that changes to the tab color are reflected immediately
+    // BODGY
+    // - Toggles the requested theme of the tab view item,
+    //   so that changes to the tab color are reflected immediately
+    // - Prior to MUX 2.8, we toggled the visual state here, but that seemingly
+    //   doesn't work in 2.8.
     // Arguments:
     // - <none>
     // Return Value:
     // - <none>
     void TabBase::_RefreshVisualState()
     {
-        if (TabViewItem().IsSelected())
-        {
-            VisualStateManager::GoToState(TabViewItem(), L"Normal", true);
-            VisualStateManager::GoToState(TabViewItem(), L"Selected", true);
-        }
-        else
-        {
-            VisualStateManager::GoToState(TabViewItem(), L"Selected", true);
-            VisualStateManager::GoToState(TabViewItem(), L"Normal", true);
-        }
+        const auto& reqTheme = TabViewItem().RequestedTheme();
+        TabViewItem().RequestedTheme(ElementTheme::Light);
+        TabViewItem().RequestedTheme(ElementTheme::Dark);
+        TabViewItem().RequestedTheme(reqTheme);
     }
-
 }
diff --git a/src/common.nugetversions.props b/src/common.nugetversions.props
index 19147f5e977..a3ac0f8ec9b 100644
--- a/src/common.nugetversions.props
+++ b/src/common.nugetversions.props
@@ -36,4 +36,7 @@
     <SeleniumSupportPathRoot>$(MSBuildThisFileDirectory)..\packages\Selenium.Support.3.5.0</SeleniumSupportPathRoot>
   </PropertyGroup>
 
+  <!-- WinUI (which depends on WebView2 as of 2.8.0) -->
+  <Import Project="$(MSBuildThisFileDirectory)..\packages\Microsoft.UI.Xaml.2.8.2\build\native\Microsoft.UI.Xaml.props" Condition="'$(TerminalMUX)' == 'true' and Exists('$(MSBuildThisFileDirectory)..\packages\Microsoft.UI.Xaml.2.8.2\build\native\Microsoft.UI.Xaml.props')" />
+
 </Project>
diff --git a/src/common.nugetversions.targets b/src/common.nugetversions.targets
index 91a26fdf59a..e047b9931d2 100644
--- a/src/common.nugetversions.targets
+++ b/src/common.nugetversions.targets
@@ -16,6 +16,8 @@
   <PropertyGroup>
     <Terminal_NuGetEXE>&quot;$(SolutionDir)dep\nuget\nuget.exe&quot;</Terminal_NuGetEXE>
     <Terminal_GlobalPackagesConfigPath>$(SolutionDir)dep\nuget\packages.config</Terminal_GlobalPackagesConfigPath>
+
+    <WebView2PackageRoot>$(MSBuildThisFileDirectory)..\packages\Microsoft.Web.WebView2.1.0.1661.34</WebView2PackageRoot>
   </PropertyGroup>
 
   <Target Name="_Terminal_NuGetPackageManager"
@@ -53,7 +55,13 @@
     <Import Project="$(MSBuildThisFileDirectory)..\packages\Microsoft.VisualStudio.Setup.Configuration.Native.2.3.2262\build\native\Microsoft.VisualStudio.Setup.Configuration.Native.targets" Condition="'$(TerminalVisualStudioSetup)' == 'true' and Exists('$(MSBuildThisFileDirectory)..\packages\Microsoft.VisualStudio.Setup.Configuration.Native.2.3.2262\build\native\Microsoft.VisualStudio.Setup.Configuration.Native.targets')" />
 
     <!-- WinUI (which depends on WebView2 as of 2.8.0) -->
-    <Import Project="$(MSBuildThisFileDirectory)..\packages\Microsoft.UI.Xaml.2.7.3\build\native\Microsoft.UI.Xaml.targets" Condition="'$(TerminalMUX)' == 'true' and Exists('$(MSBuildThisFileDirectory)..\packages\Microsoft.UI.Xaml.2.7.3\build\native\Microsoft.UI.Xaml.targets')" />
+    <Import Project="$(MSBuildThisFileDirectory)..\packages\Microsoft.UI.Xaml.2.8.2\build\native\Microsoft.UI.Xaml.targets" Condition="'$(TerminalMUX)' == 'true' and Exists('$(MSBuildThisFileDirectory)..\packages\Microsoft.UI.Xaml.2.8.2\build\native\Microsoft.UI.Xaml.targets')" />
+    <!--
+      Instead of adding our dependency on WebView2, fake it with another set of rules for XAML.
+      See Microsoft.UI.Xaml.Additional.targets for more info.
+      Make sure to update WebView2PackageRoot above to match the WebView2 dependency version.
+    -->
+    <Import Project="$(MSBuildThisFileDirectory)..\build\rules\Microsoft.UI.Xaml.Additional.targets" Condition="'$(TerminalMUX)' == 'true'" />
 
     <!-- WIL (so widely used that this one does not have a TerminalWIL opt-in property; it is automatic) -->
     <Import Project="$(MSBuildThisFileDirectory)..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('$(MSBuildThisFileDirectory)..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
@@ -86,7 +94,8 @@
     <Error Condition="'$(TerminalVisualStudioSetup)' == 'true' AND !Exists('$(MSBuildThisFileDirectory)..\packages\Microsoft.VisualStudio.Setup.Configuration.Native.2.3.2262\build\native\Microsoft.VisualStudio.Setup.Configuration.Native.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(MSBuildThisFileDirectory)..\packages\Microsoft.VisualStudio.Setup.Configuration.Native.2.3.2262\build\native\Microsoft.VisualStudio.Setup.Configuration.Native.targets'))" />
 
     <!-- WinUI (which depends on WebView2 as of 2.8.0) -->
-    <Error Condition="'$(TerminalMUX)' == 'true' AND !Exists('$(MSBuildThisFileDirectory)..\packages\Microsoft.UI.Xaml.2.7.3\build\native\Microsoft.UI.Xaml.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(MSBuildThisFileDirectory)..\packages\Microsoft.UI.Xaml.2.7.3\build\native\Microsoft.UI.Xaml.targets'))" />
+    <Error Condition="'$(TerminalMUX)' == 'true' AND !Exists('$(MSBuildThisFileDirectory)..\packages\Microsoft.UI.Xaml.2.8.2\build\native\Microsoft.UI.Xaml.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(MSBuildThisFileDirectory)..\packages\Microsoft.UI.Xaml.2.8.2\build\native\Microsoft.UI.Xaml.targets'))" />
+    <Error Condition="'$(TerminalMUX)' == 'true' AND !Exists('$(MSBuildThisFileDirectory)..\packages\Microsoft.Web.WebView2.1.0.1661.34\build\native\Microsoft.Web.WebView2.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(MSBuildThisFileDirectory)..\packages\Microsoft.Web.WebView2.1.0.1661.34\build\native\Microsoft.Web.WebView2.targets'))" />
 
     <!-- WIL (so widely used that this one does not have a TerminalWIL opt-in property; it is automatic) -->
     <Error Condition="!Exists('$(MSBuildThisFileDirectory)..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(MSBuildThisFileDirectory)..\packages\Microsoft.Windows.ImplementationLibrary.1.0.220201.1\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />


Test patch: