Skip to content
Merged
17 changes: 10 additions & 7 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,7 @@ namespace winrt::TerminalApp::implementation
if (profile)
{
newTerminalArgs.Profile(::Microsoft::Console::Utils::GuidToString(profile.Guid()));
newTerminalArgs.StartingDirectory(_evaluatePathForCwd(profile.EvaluatedStartingDirectory()));
}
}

Expand Down Expand Up @@ -1165,6 +1166,11 @@ namespace winrt::TerminalApp::implementation
}
}

std::wstring TerminalPage::_evaluatePathForCwd(const std::wstring_view path)
{
return Utils::EvaluateStartingDirectory(_WindowProperties.VirtualWorkingDirectory(), path);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

winrt::hstring is implicitly convertible to std::wstring_view, so there's no need for explicit casts like this most of the time. It just doesn't work for something like std::filesystem::path::operator/=, because C++ wouldn't know which of the 31 overloads to call, given a hstring.

Also, when cppwinrt generates String parameters for functions, it'll not use winrt::hstring but winrt::param::hstring which binds to almost any string type, include std::wstring, without making any extra copies on the heap. We can thus just return a std::wstring from this function and it'll work without any additional modifications.


// Method Description:
// - Creates a new connection based on the profile settings
// Arguments:
Expand Down Expand Up @@ -1233,13 +1239,7 @@ namespace winrt::TerminalApp::implementation
// construction, because the connection might not spawn the child
// process until later, on another thread, after we've already
// restored the CWD to its original value.
const auto currentVirtualDir{ _WindowProperties.VirtualWorkingDirectory() };
const auto cwdString{ std::wstring_view{ currentVirtualDir } };
const auto requestedStartingDir{ settings.StartingDirectory() };
auto newWorkingDirectory = winrt::hstring{
Utils::EvaluateStartingDirectory(cwdString, std::wstring_view{ requestedStartingDir })
};

auto newWorkingDirectory{ _evaluatePathForCwd(settings.StartingDirectory()) };
auto conhostConn = TerminalConnection::ConptyConnection();
auto valueSet = TerminalConnection::ConptyConnection::CreateSettings(settings.Commandline(),
newWorkingDirectory,
Expand Down Expand Up @@ -4240,6 +4240,9 @@ namespace winrt::TerminalApp::implementation
// whatever the default profile's GUID is.

newTerminalArgs.Profile(::Microsoft::Console::Utils::GuidToString(profile.Guid()));

newTerminalArgs.StartingDirectory(_evaluatePathForCwd(controlSettings.DefaultSettings().StartingDirectory()));

_OpenElevatedWT(newTerminalArgs);
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ namespace winrt::TerminalApp::implementation
void _OpenNewTabDropdown();
HRESULT _OpenNewTab(const Microsoft::Terminal::Settings::Model::NewTerminalArgs& newTerminalArgs, winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection existingConnection = nullptr);
void _CreateNewTabFromPane(std::shared_ptr<Pane> pane, uint32_t insertPosition = -1);

std::wstring _evaluatePathForCwd(std::wstring_view path);

winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection _CreateConnectionFromSettings(Microsoft::Terminal::Settings::Model::Profile profile, Microsoft::Terminal::Settings::Model::TerminalSettings settings, const bool inheritCursor);
winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection _duplicateConnectionForRestart(std::shared_ptr<Pane> pane);
void _restartPaneConnection(const std::shared_ptr<Pane>& pane);
Expand Down