From 4275c50d426572d73d6db27848b55921e2611aa2 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Thu, 20 Jan 2022 09:30:08 -0600 Subject: [PATCH] fix a crash on startup where we might not have yet loaded the settings --- src/cascadia/TerminalApp/AppLogic.cpp | 6 ++++++ src/cascadia/TerminalApp/TerminalPage.cpp | 20 ++++++++++++++++++-- src/cascadia/WindowsTerminal/AppHost.cpp | 9 +++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/cascadia/TerminalApp/AppLogic.cpp b/src/cascadia/TerminalApp/AppLogic.cpp index 27a802de177..4f774dc205a 100644 --- a/src/cascadia/TerminalApp/AppLogic.cpp +++ b/src/cascadia/TerminalApp/AppLogic.cpp @@ -1474,6 +1474,12 @@ namespace winrt::TerminalApp::implementation bool AppLogic::ShouldImmediatelyHandoffToElevated() { + if (!_loadedInitialSettings) + { + // Load settings if we haven't already + LoadSettings(); + } + return _root != nullptr ? _root->ShouldImmediatelyHandoffToElevated(_settings) : false; } void AppLogic::HandoffToElevated() diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index a7c3240a99e..d3f6339b9be 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -372,6 +372,22 @@ namespace winrt::TerminalApp::implementation return true; } + // Method Description: + // - Escape hatch for immediately dispatching requests to elevated windows + // when first launched. At this point in startup, the window doesn't exist + // yet, XAML hasn't been started, but we need to dispatch these actions. + // We can't just go through ProcessStartupActions, because that processes + // the actions async using the XAL dispatcher (which doesn't exist yet) + // - DON'T CALL THIS if you haven't already checked + // ShouldImmediatelyHandoffToElevated. If you're thinking about calling + // this outside of the one place it's used, that's probably the wrong + // solution. + // Arguments: + // - settings: the settings we should use for dispatching these actions. At + // this point in startup, we hadn't otherwise been initialized with these, + // so use them now. + // Return Value: + // - void TerminalPage::HandoffToElevated(CascadiaSettings& settings) { if (!_startupActions) @@ -379,14 +395,14 @@ namespace winrt::TerminalApp::implementation return; } + // Hookup our event handlers to the ShortcutActionDispatch _settings = settings; _HookupKeyBindings(_settings.ActionMap()); - - // Hookup our event handlers to the ShortcutActionDispatch _RegisterActionCallbacks(); for (const auto& action : _startupActions) { + // only process new tabs and split panes. They're all going to the elevated window anyways. if (action.Action() == ShortcutAction::NewTab || action.Action() == ShortcutAction::SplitPane) { _actionDispatch->DoAction(action); diff --git a/src/cascadia/WindowsTerminal/AppHost.cpp b/src/cascadia/WindowsTerminal/AppHost.cpp index 9934e2a7d52..8c118406a8f 100644 --- a/src/cascadia/WindowsTerminal/AppHost.cpp +++ b/src/cascadia/WindowsTerminal/AppHost.cpp @@ -214,10 +214,15 @@ void AppHost::_HandleCommandlineArgs() } } - // TODO! + // This is a fix for GH#12190 andhopefully GH#12169. + // + // If the commandline we were provided is going to result in us only + // opening elevated terminal instances, then we need to not even create + // the window at all here. In that case, we're going through this + // special escape hatch to dispatch all the calls to elevate-shim, and + // then we're going to exit immediately. if (_logic.ShouldImmediatelyHandoffToElevated()) { - // DebugBreak(); _shouldCreateWindow = false; _logic.HandoffToElevated(); return;