diff --git a/src/cascadia/TerminalApp/AppActionHandlers.cpp b/src/cascadia/TerminalApp/AppActionHandlers.cpp index 87aa865335e..394518bcbce 100644 --- a/src/cascadia/TerminalApp/AppActionHandlers.cpp +++ b/src/cascadia/TerminalApp/AppActionHandlers.cpp @@ -117,9 +117,11 @@ namespace winrt::TerminalApp::implementation } else if (const auto& realArgs = args.ActionArgs().try_as()) { - const auto termControl = _GetActiveControl(); - termControl.SendInput(realArgs.Input()); - args.Handled(true); + if (const auto termControl{ _GetActiveControl() }) + { + termControl.SendInput(realArgs.Input()); + args.Handled(true); + } } } @@ -308,9 +310,11 @@ namespace winrt::TerminalApp::implementation { if (const auto& realArgs = args.ActionArgs().try_as()) { - const auto termControl = _GetActiveControl(); - termControl.AdjustFontSize(realArgs.Delta()); - args.Handled(true); + if (const auto& termControl{ _GetActiveControl() }) + { + termControl.AdjustFontSize(realArgs.Delta()); + args.Handled(true); + } } } @@ -324,17 +328,21 @@ namespace winrt::TerminalApp::implementation void TerminalPage::_HandleResetFontSize(const IInspectable& /*sender*/, const ActionEventArgs& args) { - const auto termControl = _GetActiveControl(); - termControl.ResetFontSize(); - args.Handled(true); + if (const auto& termControl{ _GetActiveControl() }) + { + termControl.ResetFontSize(); + args.Handled(true); + } } void TerminalPage::_HandleToggleShaderEffects(const IInspectable& /*sender*/, const ActionEventArgs& args) { - const auto termControl = _GetActiveControl(); - termControl.ToggleShaderEffects(); - args.Handled(true); + if (const auto& termControl{ _GetActiveControl() }) + { + termControl.ToggleShaderEffects(); + args.Handled(true); + } } void TerminalPage::_HandleToggleFocusMode(const IInspectable& /*sender*/, diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index a0d34fbd1e0..0fd0bbe2085 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -902,30 +902,6 @@ namespace winrt::TerminalApp::implementation } } - // Method Description: - // Handles preview key on the SUI tab, by handling close tab / next tab / previous tab - // This is a temporary solution - we need to fix all key-bindings work from SUI as long as they don't harm - // the SUI behavior - // Arguments: - // - e: the KeyRoutedEventArgs containing info about the keystroke. - // Return Value: - // - - void TerminalPage::_SUIPreviewKeyDownHandler(Windows::Foundation::IInspectable const& /*sender*/, Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e) - { - auto key = e.OriginalKey(); - auto const ctrlDown = WI_IsFlagSet(CoreWindow::GetForCurrentThread().GetKeyState(winrt::Windows::System::VirtualKey::Control), CoreVirtualKeyStates::Down); - auto const altDown = WI_IsFlagSet(CoreWindow::GetForCurrentThread().GetKeyState(winrt::Windows::System::VirtualKey::Menu), CoreVirtualKeyStates::Down); - auto const shiftDown = WI_IsFlagSet(CoreWindow::GetForCurrentThread().GetKeyState(winrt::Windows::System::VirtualKey::Shift), CoreVirtualKeyStates::Down); - - winrt::Microsoft::Terminal::Control::KeyChord kc{ ctrlDown, altDown, shiftDown, static_cast(key) }; - const auto actionAndArgs = _settings.KeyMap().TryLookup(kc); - if (actionAndArgs && (actionAndArgs.Action() == ShortcutAction::CloseTab || actionAndArgs.Action() == ShortcutAction::NextTab || actionAndArgs.Action() == ShortcutAction::PrevTab || actionAndArgs.Action() == ShortcutAction::ClosePane)) - { - _actionDispatch->DoAction(actionAndArgs); - e.Handled(true); - } - } - // Method Description: // - Configure the AppKeyBindings to use our ShortcutActionDispatch and the updated KeyMapping // as the object to handle dispatching ShortcutAction events. @@ -1290,10 +1266,12 @@ namespace winrt::TerminalApp::implementation // Do nothing if for some reason, there's no terminal tab in focus. We don't want to crash. if (const auto terminalTab{ _GetFocusedTabImpl() }) { - const auto control = _GetActiveControl(); - const auto termHeight = control.GetViewHeight(); - auto scrollDelta = _ComputeScrollDelta(scrollDirection, termHeight); - terminalTab->Scroll(scrollDelta); + if (const auto& control{ _GetActiveControl() }) + { + const auto termHeight = control.ViewHeight(); + auto scrollDelta = _ComputeScrollDelta(scrollDirection, termHeight); + terminalTab->Scroll(scrollDelta); + } } } @@ -1689,8 +1667,11 @@ namespace winrt::TerminalApp::implementation // - true iff we we able to copy text (if a selection was active) bool TerminalPage::_CopyText(const bool singleLine, const Windows::Foundation::IReference& formats) { - const auto control = _GetActiveControl(); - return control.CopySelectionToClipboard(singleLine, formats); + if (const auto& control{ _GetActiveControl() }) + { + return control.CopySelectionToClipboard(singleLine, formats); + } + return false; } // Method Description: @@ -1707,8 +1688,10 @@ namespace winrt::TerminalApp::implementation // - Paste text from the Windows Clipboard to the focused terminal void TerminalPage::_PasteText() { - const auto control = _GetActiveControl(); - control.PasteTextFromClipboard(); + if (const auto& control{ _GetActiveControl() }) + { + control.PasteTextFromClipboard(); + } } // Function Description: @@ -2042,8 +2025,10 @@ namespace winrt::TerminalApp::implementation // - void TerminalPage::_Find() { - const auto termControl = _GetActiveControl(); - termControl.CreateSearchBoxControl(); + if (const auto& control{ _GetActiveControl() }) + { + control.CreateSearchBoxControl(); + } } // Method Description: @@ -2312,7 +2297,8 @@ namespace winrt::TerminalApp::implementation sui.SetHostingWindow(reinterpret_cast(*_hostingHwnd)); } - sui.PreviewKeyDown({ this, &TerminalPage::_SUIPreviewKeyDownHandler }); + // GH#8767 - let unhandled keys in the SUI try to run commands too. + sui.KeyDown({ this, &TerminalPage::_KeyDownHandler }); sui.OpenJson([weakThis{ get_weak() }](auto&& /*s*/, winrt::Microsoft::Terminal::Settings::Model::SettingsTarget e) { if (auto page{ weakThis.get() }) diff --git a/src/cascadia/TerminalApp/TerminalPage.h b/src/cascadia/TerminalApp/TerminalPage.h index 9925c838067..8d2b21b790d 100644 --- a/src/cascadia/TerminalApp/TerminalPage.h +++ b/src/cascadia/TerminalApp/TerminalPage.h @@ -197,7 +197,6 @@ namespace winrt::TerminalApp::implementation void _ThirdPartyNoticesOnClick(const IInspectable& sender, const Windows::UI::Xaml::RoutedEventArgs& eventArgs); void _KeyDownHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e); - void _SUIPreviewKeyDownHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e); void _HookupKeyBindings(const Microsoft::Terminal::Settings::Model::KeyMapping& keymap) noexcept; void _RegisterActionCallbacks();