Compare commits

...

3 Commits

Author SHA1 Message Date
Mike Griese
3d1611f78a This works for #15173, but no me gusta 2023-05-01 15:00:56 -05:00
Mike Griese
072620625a a toast for showing the CWD of the Terminal. This is for debugging 2023-05-01 14:20:00 -05:00
Mike Griese
816f8b2026 dirty: stash a string as a "virtual" CWD, ala #5506
In the dirtiest way possible, this seems to work for most "start with this CWD" scenarios.
2023-05-01 14:19:22 -05:00
14 changed files with 123 additions and 19 deletions

View File

@@ -10,7 +10,7 @@ namespace Microsoft.Terminal.Remoting
CommandlineArgs(String[] args, String cwd, UInt32 showWindowCommand);
String[] Commandline { get; set; };
String CurrentDirectory();
String CurrentDirectory { get; };
UInt32 ShowWindowCommand { get; };
};

View File

@@ -1021,6 +1021,13 @@ namespace winrt::TerminalApp::implementation
args.Handled(true);
}
void TerminalPage::_HandleDisplayWorkingDirectory(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
ShowTerminalWorkingDirectory();
args.Handled(true);
}
void TerminalPage::_HandleGlobalSummon(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{

View File

@@ -700,6 +700,9 @@
<data name="RenameFailedToast.Subtitle" xml:space="preserve">
<value>Another window with that name already exists</value>
</data>
<data name="WindowCwdToast.Subtitle" xml:space="preserve">
<value>This is the CWD of the Terminal window itself</value>
</data>
<data name="WindowMaximizeButtonToolTip" xml:space="preserve">
<value>Maximize</value>
</data>
@@ -823,4 +826,4 @@
<data name="ClosePaneToolTip" xml:space="preserve">
<value>Close the active pane if multiple panes are present</value>
</data>
</root>
</root>

View File

@@ -554,13 +554,24 @@ namespace winrt::TerminalApp::implementation
// back once we're done. This looks weird though, because we have to set
// up the scope_exit _first_. We'll release the scope_exit if we don't
// actually need it.
auto originalCwd{ wil::GetCurrentDirectoryW<std::wstring>() };
auto restoreCwd = wil::scope_exit([&originalCwd]() {
// auto originalRealCwd{ wil::GetCurrentDirectoryW<std::wstring>() };
if (initial)
{
// _WindowProperties.VirtualWorkingDirectory(cwd.empty() ? originalRealCwd.c_str() : cwd.c_str());
LOG_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(L"%SystemRoot%\\System32"));
}
auto originalVirtualCwd{ _WindowProperties.VirtualWorkingDirectory() };
auto restoreCwd = wil::scope_exit([&originalVirtualCwd, this /*&originalCwd, &initial*/]() {
// ignore errors, we'll just power on through. We'd rather do
// something rather than fail silently if the directory doesn't
// actually exist.
LOG_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(originalCwd.c_str()));
// LOG_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(L"%SystemRoot%\\System32"));
_WindowProperties.VirtualWorkingDirectory(originalVirtualCwd);
});
if (cwd.empty())
{
restoreCwd.release();
@@ -570,9 +581,15 @@ namespace winrt::TerminalApp::implementation
// ignore errors, we'll just power on through. We'd rather do
// something rather than fail silently if the directory doesn't
// actually exist.
LOG_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(cwd.c_str()));
// LOG_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(cwd.c_str()));
_WindowProperties.VirtualWorkingDirectory(cwd);
}
// if (initial)
// {
// _WindowProperties.VirtualWorkingDirectory(cwd.empty() ? originalCwd : cwd);
// }
if (auto page{ weakThis.get() })
{
for (const auto& action : actions)
@@ -1117,6 +1134,7 @@ namespace winrt::TerminalApp::implementation
if (dispatchToElevatedWindow)
{
newTerminalArgs.StartingDirectory(_evaluatePathForCwd(newTerminalArgs.StartingDirectory()));
_OpenElevatedWT(newTerminalArgs);
}
else
@@ -1157,6 +1175,23 @@ namespace winrt::TerminalApp::implementation
}
}
winrt::hstring TerminalPage::_evaluatePathForCwd(const winrt::hstring& path)
{
auto resultPath{ path };
const bool looksLikeLinux = resultPath.size() == 1 &&
(resultPath[0] == L'~' || resultPath[0] == L'/');
// if (newWorkingDirectory.size() == 0 || looksLikeLinux)
if (!looksLikeLinux)
{ // We only want to resolve the new WD against the CWD if it doesn't look like a Linux path (see GH#592)
// auto cwdString{ wil::GetCurrentDirectoryW<std::wstring>() };
auto cwdString{ _WindowProperties.VirtualWorkingDirectory().c_str() };
std::filesystem::path cwd{ cwdString };
cwd /= path.c_str();
resultPath = winrt::hstring{ cwd.wstring() };
}
return resultPath;
}
// Method Description:
// - Creates a new connection based on the profile settings
// Arguments:
@@ -1225,15 +1260,18 @@ 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.
auto newWorkingDirectory{ settings.StartingDirectory() };
if (newWorkingDirectory.size() == 0 || newWorkingDirectory.size() == 1 &&
!(newWorkingDirectory[0] == L'~' || newWorkingDirectory[0] == L'/'))
{ // We only want to resolve the new WD against the CWD if it doesn't look like a Linux path (see GH#592)
auto cwdString{ wil::GetCurrentDirectoryW<std::wstring>() };
std::filesystem::path cwd{ cwdString };
cwd /= settings.StartingDirectory().c_str();
newWorkingDirectory = winrt::hstring{ cwd.wstring() };
}
auto newWorkingDirectory{ _evaluatePathForCwd(settings.StartingDirectory()) };
// const bool looksLikeLinux = newWorkingDirectory.size() == 1 &&
// (newWorkingDirectory[0] == L'~' || newWorkingDirectory[0] == L'/');
// // if (newWorkingDirectory.size() == 0 || looksLikeLinux)
// if (!looksLikeLinux)
// { // We only want to resolve the new WD against the CWD if it doesn't look like a Linux path (see GH#592)
// // auto cwdString{ wil::GetCurrentDirectoryW<std::wstring>() };
// auto cwdString{ _WindowProperties.VirtualWorkingDirectory().c_str() };
// std::filesystem::path cwd{ cwdString };
// cwd /= settings.StartingDirectory().c_str();
// newWorkingDirectory = winrt::hstring{ cwd.wstring() };
// }
auto conhostConn = TerminalConnection::ConptyConnection();
auto valueSet = TerminalConnection::ConptyConnection::CreateSettings(settings.Commandline(),
@@ -4047,6 +4085,33 @@ namespace winrt::TerminalApp::implementation
}
}
winrt::fire_and_forget TerminalPage::ShowTerminalWorkingDirectory()
{
auto weakThis{ get_weak() };
co_await wil::resume_foreground(Dispatcher());
if (auto page{ weakThis.get() })
{
// If we haven't ever loaded the TeachingTip, then do so now and
// create the toast for it.
if (page->_windowCwdToast == nullptr)
{
if (auto tip{ page->FindName(L"WindowCwdToast").try_as<MUX::Controls::TeachingTip>() })
{
page->_windowCwdToast = std::make_shared<Toast>(tip);
// Make sure to use the weak ref when setting up this
// callback.
tip.Closed({ page->get_weak(), &TerminalPage::_FocusActiveControl });
}
}
_UpdateTeachingTipTheme(WindowCwdToast().try_as<winrt::Windows::UI::Xaml::FrameworkElement>());
if (page->_windowCwdToast != nullptr)
{
page->_windowCwdToast->Open();
}
}
}
// Method Description:
// - Called when the user hits the "Ok" button on the WindowRenamer TeachingTip.
// - Will raise an event that will bubble up to the monarch, asking if this
@@ -4226,6 +4291,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;
}

View File

@@ -149,6 +149,7 @@ namespace winrt::TerminalApp::implementation
winrt::fire_and_forget IdentifyWindow();
winrt::fire_and_forget RenameFailed();
winrt::fire_and_forget ShowTerminalWorkingDirectory();
winrt::fire_and_forget ProcessStartupActions(Windows::Foundation::Collections::IVector<Microsoft::Terminal::Settings::Model::ActionAndArgs> actions,
const bool initial,
@@ -257,6 +258,7 @@ namespace winrt::TerminalApp::implementation
std::shared_ptr<Toast> _windowIdToast{ nullptr };
std::shared_ptr<Toast> _windowRenameFailedToast{ nullptr };
std::shared_ptr<Toast> _windowCwdToast{ nullptr };
winrt::Windows::UI::Xaml::Controls::TextBox::LayoutUpdated_revoker _renamerLayoutUpdatedRevoker;
int _renamerLayoutCount{ 0 };
@@ -293,6 +295,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);
winrt::hstring _evaluatePathForCwd(const winrt::hstring& 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);

View File

@@ -51,6 +51,8 @@ namespace TerminalApp
String WindowNameForDisplay { get; };
String WindowIdForDisplay { get; };
String VirtualWorkingDirectory { get; set; };
Boolean IsQuakeWindow();
};

View File

@@ -205,5 +205,12 @@
Text="{x:Bind WindowProperties.WindowName, Mode=OneWay}" />
</mux:TeachingTip.Content>
</mux:TeachingTip>
<mux:TeachingTip x:Name="WindowCwdToast"
x:Uid="WindowCwdToast"
Title="{x:Bind WindowProperties.VirtualWorkingDirectory, Mode=OneWay}"
x:Load="False"
IsLightDismissEnabled="True"
Subtitle="{x:Bind WindowProperties.VirtualWorkingDirectory, Mode=OneWay}" />
</Grid>
</Page>

View File

@@ -1013,8 +1013,10 @@ namespace winrt::TerminalApp::implementation
// Return Value:
// - the result of the first command who's parsing returned a non-zero code,
// or 0. (see TerminalWindow::_ParseArgs)
int32_t TerminalWindow::SetStartupCommandline(array_view<const winrt::hstring> args)
int32_t TerminalWindow::SetStartupCommandline(array_view<const winrt::hstring> args, winrt::hstring cwd)
{
_WindowProperties->SetInitialCwd(cwd);
// This is called in AppHost::ctor(), before we've created the window
// (or called TerminalWindow::Initialize)
const auto result = _appArgs.ParseArgs(args);
@@ -1336,6 +1338,7 @@ namespace winrt::TerminalApp::implementation
CATCH_LOG();
}
}
uint64_t WindowProperties::WindowId() const noexcept
{
return _WindowId;

View File

@@ -48,8 +48,13 @@ namespace winrt::TerminalApp::implementation
winrt::hstring WindowNameForDisplay() const noexcept;
bool IsQuakeWindow() const noexcept;
WINRT_OBSERVABLE_PROPERTY(winrt::hstring, VirtualWorkingDirectory, _PropertyChangedHandlers, L"");
WINRT_CALLBACK(PropertyChanged, Windows::UI::Xaml::Data::PropertyChangedEventHandler);
public:
void SetInitialCwd(const winrt::hstring& cwd) { _VirtualWorkingDirectory = cwd; };
private:
winrt::hstring _WindowName{};
uint64_t _WindowId{ 0 };
@@ -71,7 +76,7 @@ namespace winrt::TerminalApp::implementation
bool HasCommandlineArguments() const noexcept;
int32_t SetStartupCommandline(array_view<const winrt::hstring> actions);
int32_t SetStartupCommandline(array_view<const winrt::hstring> actions, winrt::hstring cwd);
void SetStartupContent(const winrt::hstring& content, const Windows::Foundation::IReference<Windows::Foundation::Rect>& contentBounds);
int32_t ExecuteCommandline(array_view<const winrt::hstring> actions, const winrt::hstring& cwd);
void SetSettingsStartupArgs(const std::vector<winrt::Microsoft::Terminal::Settings::Model::ActionAndArgs>& actions);

View File

@@ -53,7 +53,7 @@ namespace TerminalApp
Boolean HasCommandlineArguments();
Int32 SetStartupCommandline(String[] commands);
Int32 SetStartupCommandline(String[] commands, String cwd);
void SetStartupContent(String json, Windows.Foundation.IReference<Windows.Foundation.Rect> bounds);
Int32 ExecuteCommandline(String[] commands, String cwd);
String ParseCommandlineMessage { get; };

View File

@@ -72,6 +72,7 @@ static constexpr std::string_view IdentifyWindowKey{ "identifyWindow" };
static constexpr std::string_view IdentifyWindowsKey{ "identifyWindows" };
static constexpr std::string_view RenameWindowKey{ "renameWindow" };
static constexpr std::string_view OpenWindowRenamerKey{ "openWindowRenamer" };
static constexpr std::string_view DisplayWorkingDirectoryKey{ "displayTerminalCwd" };
static constexpr std::string_view GlobalSummonKey{ "globalSummon" };
static constexpr std::string_view QuakeModeKey{ "quakeMode" };
static constexpr std::string_view FocusPaneKey{ "focusPane" };
@@ -401,6 +402,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::IdentifyWindows, RS_(L"IdentifyWindowsCommandKey") },
{ ShortcutAction::RenameWindow, RS_(L"ResetWindowNameCommandKey") },
{ ShortcutAction::OpenWindowRenamer, RS_(L"OpenWindowRenamerCommandKey") },
{ ShortcutAction::DisplayWorkingDirectory, L"Display Terminal's CWD TODO! localize" },
{ ShortcutAction::GlobalSummon, MustGenerate },
{ ShortcutAction::QuakeMode, RS_(L"QuakeModeCommandKey") },
{ ShortcutAction::FocusPane, MustGenerate },

View File

@@ -85,6 +85,7 @@
ON_ALL_ACTIONS(IdentifyWindows) \
ON_ALL_ACTIONS(RenameWindow) \
ON_ALL_ACTIONS(OpenWindowRenamer) \
ON_ALL_ACTIONS(DisplayWorkingDirectory) \
ON_ALL_ACTIONS(GlobalSummon) \
ON_ALL_ACTIONS(QuakeMode) \
ON_ALL_ACTIONS(FocusPane) \

View File

@@ -374,6 +374,7 @@
{ "command": "openSystemMenu", "keys": "alt+space" },
{ "command": "quit" },
{ "command": "restoreLastClosed"},
{ "command": "displayTerminalCwd"},
// Tab Management
// "command": "closeTab" is unbound by default.

View File

@@ -175,7 +175,7 @@ void AppHost::_HandleCommandlineArgs(const Remoting::WindowRequestedArgs& window
}
else if (args)
{
const auto result = _windowLogic.SetStartupCommandline(args.Commandline());
const auto result = _windowLogic.SetStartupCommandline(args.Commandline(), args.CurrentDirectory());
const auto message = _windowLogic.ParseCommandlineMessage();
if (!message.empty())
{