From 68cce101bc7b8056b7b1f040956b000ccbef1621 Mon Sep 17 00:00:00 2001 From: "Dustin L. Howett" Date: Mon, 12 Dec 2022 14:59:23 -0600 Subject: [PATCH] Remove our dependency on Microsoft.Toolkit.Win32.UI.XamlApplication (#14520) We originally needed this library (or a separate DLL in our own project) to handle hooking up the XAML resource loader to the providers that our application needed. It was introduced in its nascent form in 2019, in a PR titled "Make XAML files work." It appears we no longer need it, and the provider hookup is being handled by our `AppT2` base class override. I've tested this in Windows 10 Vb running unpackaged, and it seems to work totally fine. Crazy. Removing this dependency saves us a couple hundred kilobytes on disk and removes one consumer of the App CRT from our package. --- dep/nuget/packages.config | 1 - scratch/ScratchIslandApp/SampleApp/App.cpp | 49 +++++++++++++++---- scratch/ScratchIslandApp/SampleApp/App.h | 10 ++++ scratch/ScratchIslandApp/SampleApp/App.idl | 4 +- scratch/ScratchIslandApp/SampleApp/App.xaml | 23 +++++---- .../SampleApp/SampleAppLib.vcxproj | 11 ++++- .../SampleApp/dll/SampleApp.vcxproj | 1 - scratch/ScratchIslandApp/SampleApp/pch.h | 1 - .../TerminalApp.LocalTests.vcxproj | 1 - src/cascadia/TerminalApp/App.cpp | 49 +++++++++++++++---- src/cascadia/TerminalApp/App.h | 12 +++++ src/cascadia/TerminalApp/App.idl | 4 +- src/cascadia/TerminalApp/App.xaml | 23 +++++---- .../TerminalApp/TerminalAppLib.vcxproj | 9 +++- .../TerminalApp/dll/TerminalApp.vcxproj | 1 - src/cascadia/TerminalApp/pch.h | 1 - .../WindowsTerminal/WindowsTerminal.vcxproj | 1 - src/common.nugetversions.props | 3 -- src/common.nugetversions.targets | 7 --- 19 files changed, 148 insertions(+), 63 deletions(-) diff --git a/dep/nuget/packages.config b/dep/nuget/packages.config index ffa247ab5d..0a62482b48 100644 --- a/dep/nuget/packages.config +++ b/dep/nuget/packages.config @@ -2,7 +2,6 @@ - diff --git a/scratch/ScratchIslandApp/SampleApp/App.cpp b/scratch/ScratchIslandApp/SampleApp/App.cpp index 5a2ae523f6..2b1df560e6 100644 --- a/scratch/ScratchIslandApp/SampleApp/App.cpp +++ b/scratch/ScratchIslandApp/SampleApp/App.cpp @@ -12,19 +12,12 @@ using namespace winrt::Windows::UI::Xaml; using namespace winrt::Windows::UI::Xaml::Controls; using namespace winrt::Windows::UI::Xaml::Navigation; +namespace xaml = ::winrt::Windows::UI::Xaml; + namespace winrt::SampleApp::implementation { App::App() { - // This is the same trick that Initialize() is about to use to figure out whether we're coming - // from a UWP context or from a Win32 context - // See https://github.com/windows-toolkit/Microsoft.Toolkit.Win32/blob/52611c57d89554f357f281d0c79036426a7d9257/Microsoft.Toolkit.Win32.UI.XamlApplication/XamlApplication.cpp#L42 - const auto dispatcherQueue = ::winrt::Windows::System::DispatcherQueue::GetForCurrentThread(); - if (dispatcherQueue) - { - _isUwp = true; - } - Initialize(); // Disable XAML's automatic backplating of text when in High Contrast @@ -33,6 +26,44 @@ namespace winrt::SampleApp::implementation HighContrastAdjustment(::winrt::Windows::UI::Xaml::ApplicationHighContrastAdjustment::None); } + void App::Initialize() + { + const auto dispatcherQueue = winrt::Windows::System::DispatcherQueue::GetForCurrentThread(); + if (!dispatcherQueue) + { + _windowsXamlManager = xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread(); + } + else + { + _isUwp = true; + } + } + + void App::Close() + { + if (_bIsClosed) + { + return; + } + + _bIsClosed = true; + + if (_windowsXamlManager) + { + _windowsXamlManager.Close(); + } + _windowsXamlManager = nullptr; + + Exit(); + { + MSG msg = {}; + while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) + { + ::DispatchMessageW(&msg); + } + } + } + SampleAppLogic App::Logic() { static SampleAppLogic logic; diff --git a/scratch/ScratchIslandApp/SampleApp/App.h b/scratch/ScratchIslandApp/SampleApp/App.h index fb6235d753..9debfd86a2 100644 --- a/scratch/ScratchIslandApp/SampleApp/App.h +++ b/scratch/ScratchIslandApp/SampleApp/App.h @@ -12,12 +12,22 @@ namespace winrt::SampleApp::implementation { public: App(); + void Initialize(); + void Close(); void OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs const&); + bool IsDisposed() const + { + return _bIsClosed; + } + SampleApp::SampleAppLogic Logic(); private: bool _isUwp = false; + winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager _windowsXamlManager = nullptr; + winrt::Windows::Foundation::Collections::IVector _providers = winrt::single_threaded_vector(); + bool _bIsClosed = false; }; } diff --git a/scratch/ScratchIslandApp/SampleApp/App.idl b/scratch/ScratchIslandApp/SampleApp/App.idl index e51a73332f..9a8e1affed 100644 --- a/scratch/ScratchIslandApp/SampleApp/App.idl +++ b/scratch/ScratchIslandApp/SampleApp/App.idl @@ -7,10 +7,12 @@ namespace SampleApp { // ADD ARBITRARY APP LOGIC TO SampleAppLogic.idl, NOT HERE. // This is for XAML platform setup only. - [default_interface] runtimeclass App : Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication + [default_interface] runtimeclass App : Windows.UI.Xaml.Application, Windows.Foundation.IClosable { App(); SampleAppLogic Logic { get; }; + + Boolean IsDisposed { get; }; } } diff --git a/scratch/ScratchIslandApp/SampleApp/App.xaml b/scratch/ScratchIslandApp/SampleApp/App.xaml index c6a2328f41..42a5ee1888 100644 --- a/scratch/ScratchIslandApp/SampleApp/App.xaml +++ b/scratch/ScratchIslandApp/SampleApp/App.xaml @@ -2,20 +2,19 @@ Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See LICENSE in the project root for license information. --> - + - + @@ -67,5 +66,5 @@ - - + + diff --git a/scratch/ScratchIslandApp/SampleApp/SampleAppLib.vcxproj b/scratch/ScratchIslandApp/SampleApp/SampleAppLib.vcxproj index 8e06424ce8..478fbeac20 100644 --- a/scratch/ScratchIslandApp/SampleApp/SampleAppLib.vcxproj +++ b/scratch/ScratchIslandApp/SampleApp/SampleAppLib.vcxproj @@ -20,7 +20,6 @@ true - true @@ -128,6 +127,16 @@ false false + + + true + false + $(TargetPlatformMoniker) + $(TargetPlatformDisplayName) + CppWinRTImplicitlyExpandTargetPlatform + True + + diff --git a/scratch/ScratchIslandApp/SampleApp/dll/SampleApp.vcxproj b/scratch/ScratchIslandApp/SampleApp/dll/SampleApp.vcxproj index f1a5c8968b..105ac1cf4b 100644 --- a/scratch/ScratchIslandApp/SampleApp/dll/SampleApp.vcxproj +++ b/scratch/ScratchIslandApp/SampleApp/dll/SampleApp.vcxproj @@ -13,7 +13,6 @@ true - true diff --git a/scratch/ScratchIslandApp/SampleApp/pch.h b/scratch/ScratchIslandApp/SampleApp/pch.h index ba01e45e45..dbb486ab75 100644 --- a/scratch/ScratchIslandApp/SampleApp/pch.h +++ b/scratch/ScratchIslandApp/SampleApp/pch.h @@ -46,7 +46,6 @@ #include "winrt/Windows.UI.Xaml.Markup.h" #include "winrt/Windows.UI.ViewManagement.h" -#include #include #include #include diff --git a/src/cascadia/LocalTests_TerminalApp/TerminalApp.LocalTests.vcxproj b/src/cascadia/LocalTests_TerminalApp/TerminalApp.LocalTests.vcxproj index 4d9ec0ab83..cff561b960 100644 --- a/src/cascadia/LocalTests_TerminalApp/TerminalApp.LocalTests.vcxproj +++ b/src/cascadia/LocalTests_TerminalApp/TerminalApp.LocalTests.vcxproj @@ -23,7 +23,6 @@ - true diff --git a/src/cascadia/TerminalApp/App.cpp b/src/cascadia/TerminalApp/App.cpp index 4d7e5f9376..138d4a7d24 100644 --- a/src/cascadia/TerminalApp/App.cpp +++ b/src/cascadia/TerminalApp/App.cpp @@ -12,19 +12,12 @@ using namespace winrt::Windows::UI::Xaml; using namespace winrt::Windows::UI::Xaml::Controls; using namespace winrt::Windows::UI::Xaml::Navigation; +namespace xaml = ::winrt::Windows::UI::Xaml; + namespace winrt::TerminalApp::implementation { App::App() { - // This is the same trick that Initialize() is about to use to figure out whether we're coming - // from a UWP context or from a Win32 context - // See https://github.com/windows-toolkit/Microsoft.Toolkit.Win32/blob/52611c57d89554f357f281d0c79036426a7d9257/Microsoft.Toolkit.Win32.UI.XamlApplication/XamlApplication.cpp#L42 - const auto dispatcherQueue = ::winrt::Windows::System::DispatcherQueue::GetForCurrentThread(); - if (dispatcherQueue) - { - _isUwp = true; - } - Initialize(); // Disable XAML's automatic backplating of text when in High Contrast @@ -33,12 +26,50 @@ namespace winrt::TerminalApp::implementation HighContrastAdjustment(::winrt::Windows::UI::Xaml::ApplicationHighContrastAdjustment::None); } + void App::Initialize() + { + const auto dispatcherQueue = winrt::Windows::System::DispatcherQueue::GetForCurrentThread(); + if (!dispatcherQueue) + { + _windowsXamlManager = xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread(); + } + else + { + _isUwp = true; + } + } + AppLogic App::Logic() { static AppLogic logic; return logic; } + void App::Close() + { + if (_bIsClosed) + { + return; + } + + _bIsClosed = true; + + if (_windowsXamlManager) + { + _windowsXamlManager.Close(); + } + _windowsXamlManager = nullptr; + + Exit(); + { + MSG msg = {}; + while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) + { + ::DispatchMessageW(&msg); + } + } + } + /// /// Invoked when the application is launched normally by the end user. Other entry points /// will be used such as when the application is launched to open a specific file. diff --git a/src/cascadia/TerminalApp/App.h b/src/cascadia/TerminalApp/App.h index 259d317553..830c762f17 100644 --- a/src/cascadia/TerminalApp/App.h +++ b/src/cascadia/TerminalApp/App.h @@ -5,6 +5,7 @@ #include "App.g.h" #include "App.base.h" +#include namespace winrt::TerminalApp::implementation { @@ -13,11 +14,22 @@ namespace winrt::TerminalApp::implementation public: App(); void OnLaunched(const Windows::ApplicationModel::Activation::LaunchActivatedEventArgs&); + void Initialize(); TerminalApp::AppLogic Logic(); + void Close(); + + bool IsDisposed() const + { + return _bIsClosed; + } + private: bool _isUwp = false; + winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager _windowsXamlManager = nullptr; + winrt::Windows::Foundation::Collections::IVector _providers = winrt::single_threaded_vector(); + bool _bIsClosed = false; }; } diff --git a/src/cascadia/TerminalApp/App.idl b/src/cascadia/TerminalApp/App.idl index 5e000c5a18..516442148c 100644 --- a/src/cascadia/TerminalApp/App.idl +++ b/src/cascadia/TerminalApp/App.idl @@ -7,10 +7,12 @@ namespace TerminalApp { // ADD ARBITRARY APP LOGIC TO AppLogic.idl, NOT HERE. // This is for XAML platform setup only. - [default_interface] runtimeclass App : Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication + [default_interface] runtimeclass App : Windows.UI.Xaml.Application, Windows.Foundation.IClosable { App(); AppLogic Logic { get; }; + + Boolean IsDisposed { get; }; } } diff --git a/src/cascadia/TerminalApp/App.xaml b/src/cascadia/TerminalApp/App.xaml index 3da02dff31..36a3a1be07 100644 --- a/src/cascadia/TerminalApp/App.xaml +++ b/src/cascadia/TerminalApp/App.xaml @@ -2,20 +2,19 @@ Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See LICENSE in the project root for license information. --> - + - + @@ -202,5 +201,5 @@ - - + + diff --git a/src/cascadia/TerminalApp/TerminalAppLib.vcxproj b/src/cascadia/TerminalApp/TerminalAppLib.vcxproj index 511152775f..6123f0de1c 100644 --- a/src/cascadia/TerminalApp/TerminalAppLib.vcxproj +++ b/src/cascadia/TerminalApp/TerminalAppLib.vcxproj @@ -19,7 +19,6 @@ true - true true @@ -382,6 +381,14 @@ false false + + true + false + $(TargetPlatformMoniker) + $(TargetPlatformDisplayName) + CppWinRTImplicitlyExpandTargetPlatform + True + diff --git a/src/cascadia/TerminalApp/dll/TerminalApp.vcxproj b/src/cascadia/TerminalApp/dll/TerminalApp.vcxproj index ea5aa2c2bc..3bee808b96 100644 --- a/src/cascadia/TerminalApp/dll/TerminalApp.vcxproj +++ b/src/cascadia/TerminalApp/dll/TerminalApp.vcxproj @@ -14,7 +14,6 @@ true - true true diff --git a/src/cascadia/TerminalApp/pch.h b/src/cascadia/TerminalApp/pch.h index 9946783ab8..a7805b89a4 100644 --- a/src/cascadia/TerminalApp/pch.h +++ b/src/cascadia/TerminalApp/pch.h @@ -50,7 +50,6 @@ #include #include -#include #include #include #include diff --git a/src/cascadia/WindowsTerminal/WindowsTerminal.vcxproj b/src/cascadia/WindowsTerminal/WindowsTerminal.vcxproj index 1172c8bab4..cd555c7049 100644 --- a/src/cascadia/WindowsTerminal/WindowsTerminal.vcxproj +++ b/src/cascadia/WindowsTerminal/WindowsTerminal.vcxproj @@ -18,7 +18,6 @@ true - true true true true diff --git a/src/common.nugetversions.props b/src/common.nugetversions.props index 76c1de8a65..c6945b5d5e 100644 --- a/src/common.nugetversions.props +++ b/src/common.nugetversions.props @@ -6,9 +6,6 @@ - - - $(MSBuildThisFileDirectory)..\packages\Microsoft.Taef.10.60.210621002 diff --git a/src/common.nugetversions.targets b/src/common.nugetversions.targets index 08a695e815..78c37d8633 100644 --- a/src/common.nugetversions.targets +++ b/src/common.nugetversions.targets @@ -40,9 +40,6 @@ - - - @@ -79,10 +76,6 @@ - - - -