diff --git a/src/cascadia/TerminalApp/Pane.cpp b/src/cascadia/TerminalApp/Pane.cpp index fe3607b4678..73054081cc7 100644 --- a/src/cascadia/TerminalApp/Pane.cpp +++ b/src/cascadia/TerminalApp/Pane.cpp @@ -1340,6 +1340,29 @@ TermControl Pane::GetLastFocusedTerminalControl() } } +IPaneContent Pane::GetLastFocusedContent() +{ + if (!_IsLeaf()) + { + if (_lastActive) + { + auto pane = shared_from_this(); + while (const auto p = pane->_parentChildPath.lock()) + { + if (p->_IsLeaf()) + { + return p->_content; + } + pane = p; + } + // We didn't find our child somehow, they might have closed under us. + } + return _firstChild->GetLastFocusedContent(); + } + + return _content; +} + // Method Description: // - Gets the TermControl of this pane. If this Pane is not a leaf this will // return the nullptr; @@ -1480,7 +1503,11 @@ void Pane::UpdateVisuals() void Pane::_Focus() { _GotFocusHandlers(shared_from_this(), FocusState::Programmatic); - _content.Focus(FocusState::Programmatic); + if (const auto& lastContent{ GetLastFocusedContent() }) + { + lastContent.Focus(FocusState::Programmatic); + } + } // Method Description: @@ -2176,7 +2203,7 @@ void Pane::_SetupEntranceAnimation() auto child = isFirstChild ? _firstChild : _secondChild; auto childGrid = child->_root; // If we are splitting a parent pane this may be null - auto control = child->_content.GetRoot(); + auto control = child->_content ? child->_content.GetRoot() : nullptr; // Build up our animation: // * it'll take as long as our duration (200ms) // * it'll change the value of our property from 0 to secondSize diff --git a/src/cascadia/TerminalApp/Pane.h b/src/cascadia/TerminalApp/Pane.h index e8631047f36..f096ae5c9ab 100644 --- a/src/cascadia/TerminalApp/Pane.h +++ b/src/cascadia/TerminalApp/Pane.h @@ -73,6 +73,7 @@ class Pane : public std::enable_shared_from_this std::shared_ptr GetActivePane(); winrt::Microsoft::Terminal::Control::TermControl GetLastFocusedTerminalControl(); + winrt::TerminalApp::IPaneContent GetLastFocusedContent(); winrt::Microsoft::Terminal::Control::TermControl GetTerminalControl() const; winrt::Microsoft::Terminal::Settings::Model::Profile GetFocusedProfile(); bool IsConnectionClosed() const; diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index 8e271850844..1ef15cafe68 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -4598,7 +4598,10 @@ namespace winrt::TerminalApp::implementation { if (const auto& pane{ tab->GetActivePane() }) { - terminalBrush = pane->GetContent().BackgroundBrush(); + if (const auto& lastContent{ pane->GetLastFocusedContent() }) + { + terminalBrush = lastContent.BackgroundBrush(); + } } }