Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an action for immediately restarting a connection #14549

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,4 +1182,14 @@ namespace winrt::TerminalApp::implementation
args.Handled(handled);
}
}

void TerminalPage::_HandleRestartConnection(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (const auto& control{ _GetActiveControl() })
{
control.RestartConnection();
args.Handled(true);
}
}
}
84 changes: 45 additions & 39 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,46 +409,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation

// If we have a connection info with which to try and re-create the
// connection, then let's try doing that on Enter
if (_ConnectionInfo != nullptr)
if (ch == Enter)
{
if (ch == Enter)
{
// Manully update the startingDirectory. If we had a CWD set
// by the client, then let's try and use that as the CWD for
// a restart, so it's more "seamless"
_ConnectionInfo.Settings().Insert(L"startingDirectory", Windows::Foundation::PropertyValue::CreateString(WorkingDirectory()));
// pass in the magic "inheritCursor" setting to the
// connection's settings. This'll cause conpty to restart
// the connection at the current place in the buffer.
_ConnectionInfo.Settings().Insert(L"inheritCursor", Windows::Foundation::PropertyValue::CreateBoolean(true));
auto c = TerminalConnection::ConnectionInformation::CreateConnection(_ConnectionInfo);

// Get our current size in rows/cols, and hook them up to
// this connection too.
{
auto cx = gsl::narrow_cast<til::CoordType>(_panelWidth * _compositionScale);
auto cy = gsl::narrow_cast<til::CoordType>(_panelHeight * _compositionScale);
cx = std::max(cx, _actualFont.GetSize().width);
cy = std::max(cy, _actualFont.GetSize().height);
const auto viewInPixels = Viewport::FromDimensions({ 0, 0 }, { cx, cy });
const auto vp = _renderEngine->GetViewportInCharacters(viewInPixels);
const auto width = vp.Width();
const auto height = vp.Height();

c.Resize(height, width);
}

// Window owner too.
if (auto conpty{ c.try_as<TerminalConnection::ConptyConnection>() })
{
conpty.ReparentWindow(_owningHwnd);
}

_connection.Close();
_setConnection(c);
_connection.Start();
return true;
}
RestartConnection();
return true;
}
}

Expand Down Expand Up @@ -2180,4 +2144,46 @@ namespace winrt::Microsoft::Terminal::Control::implementation
}
}
}

void ControlCore::RestartConnection()
{
if (_ConnectionInfo != nullptr)
{
// Manually update the startingDirectory. If we had a CWD set
// by the client, then let's try and use that as the CWD for
// a restart, so it's more "seamless"
_ConnectionInfo.Settings().Insert(L"startingDirectory", Windows::Foundation::PropertyValue::CreateString(WorkingDirectory()));
// pass in the magic "inheritCursor" setting to the
// connection's settings. This'll cause conpty to restart
// the connection at the current place in the buffer.
_ConnectionInfo.Settings().Insert(L"inheritCursor", Windows::Foundation::PropertyValue::CreateBoolean(true));
auto c = TerminalConnection::ConnectionInformation::CreateConnection(_ConnectionInfo);

// Get our current size in rows/cols, and hook them up to
// this connection too.
{
auto cx = gsl::narrow_cast<til::CoordType>(_panelWidth * _compositionScale);
auto cy = gsl::narrow_cast<til::CoordType>(_panelHeight * _compositionScale);
cx = std::max(cx, _actualFont.GetSize().width);
cy = std::max(cy, _actualFont.GetSize().height);
const auto viewInPixels = Viewport::FromDimensions({ 0, 0 }, { cx, cy });
const auto vp = _renderEngine->GetViewportInCharacters(viewInPixels);
const auto width = vp.Width();
const auto height = vp.Height();

c.Resize(height, width);
}

// Window owner too.
if (auto conpty{ c.try_as<TerminalConnection::ConptyConnection>() })
{
conpty.ReparentWindow(_owningHwnd);
}

_connection.Close();
_setConnection(c);
_connection.Start();
}
}

}
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation

void Close();

void RestartConnection();

#pragma region ICoreState
const size_t TaskbarState() const noexcept;
const size_t TaskbarProgress() const noexcept;
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.idl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ namespace Microsoft.Terminal.Control

void ColorSelection(SelectionColor fg, SelectionColor bg, Microsoft.Terminal.Core.MatchMode matchMode);

void RestartConnection();

event FontSizeChangedEventArgs FontSizeChanged;

event Windows.Foundation.TypedEventHandler<Object, CopyToClipboardEventArgs> CopyToClipboard;
Expand Down
5 changes: 5 additions & 0 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3136,4 +3136,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
_core.ConnectionInfo(info);
}

void TermControl::RestartConnection()
{
_core.RestartConnection();
}
}
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
void ColorScheme(const winrt::Microsoft::Terminal::Core::Scheme& scheme) const noexcept;

void AdjustOpacity(const double opacity, const bool relative);
void RestartConnection();

WINRT_CALLBACK(PropertyChanged, Windows::UI::Xaml::Data::PropertyChangedEventHandler);

Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,7 @@ namespace Microsoft.Terminal.Control
Windows.UI.Xaml.Media.Brush BackgroundBrush { get; };

void ColorSelection(SelectionColor fg, SelectionColor bg, Microsoft.Terminal.Core.MatchMode matchMode);

void RestartConnection();
}
}
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static constexpr std::string_view ToggleBlockSelectionKey{ "toggleBlockSelection
static constexpr std::string_view SwitchSelectionEndpointKey{ "switchSelectionEndpoint" };
static constexpr std::string_view ColorSelectionKey{ "experimental.colorSelection" };
static constexpr std::string_view ExpandSelectionToWordKey{ "expandSelectionToWord" };
static constexpr std::string_view RestartConnectionKey{ "restartConnection" };

static constexpr std::string_view ActionKey{ "action" };

Expand Down Expand Up @@ -408,6 +409,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::SwitchSelectionEndpoint, RS_(L"SwitchSelectionEndpointCommandKey") },
{ ShortcutAction::ColorSelection, L"" }, // Intentionally omitted, must be generated by GenerateName
{ ShortcutAction::ExpandSelectionToWord, RS_(L"ExpandSelectionToWordCommandKey") },
{ ShortcutAction::RestartConnection, RS_(L"RestartConnectionKey") },
};
}();

Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalSettingsModel/AllShortcutActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
ON_ALL_ACTIONS(SwitchSelectionEndpoint) \
ON_ALL_ACTIONS(ColorSelection) \
ON_ALL_ACTIONS(ExpandSelectionToWord) \
ON_ALL_ACTIONS(CloseOtherPanes)
ON_ALL_ACTIONS(CloseOtherPanes) \
ON_ALL_ACTIONS(RestartConnection)

#define ALL_SHORTCUT_ACTIONS_WITH_ARGS \
ON_ALL_ACTIONS_WITH_ARGS(AdjustFontSize) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,4 +661,7 @@
<data name="CloseOtherPanesCommandKey" xml:space="preserve">
<value>Close all other panes</value>
</data>
<data name="RestartConnectionKey" xml:space="preserve">
<value>Restart connection</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@
{ "command": { "action": "movePane", "index": 6 } },
{ "command": { "action": "movePane", "index": 7 } },
{ "command": { "action": "movePane", "index": 8 } },
{ "command": "restartConnection" },

// Clipboard Integration
{ "command": { "action": "copy", "singleLine": false }, "keys": "ctrl+shift+c" },
Expand Down