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

Enable wt to inherit the working directory #3547

Closed
wants to merge 9 commits into from
16 changes: 16 additions & 0 deletions src/cascadia/TerminalApp/AppLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,22 @@ namespace winrt::TerminalApp::implementation
}
}

// Method Description:
// - Used to tell us that the first launched Terminal should ignore the
// "startingDirectory" setting, instead opting to inherit the current
// working directory of the application.
// Arguments:
// - <none>
// Return Value:
// - <none>
void AppLogic::SuppressStartupDirectory()
{
if (_root)
{
_root->SuppressStartupDirectory();
}
}

// -------------------------------- WinRT Events ---------------------------------
// Winrt events need a method for adding a callback to the event and removing the callback.
// These macros will define them both for you.
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/AppLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ namespace winrt::TerminalApp::implementation

void WindowCloseButtonClicked();

void SuppressStartupDirectory();

// -------------------------------- WinRT Events ---------------------------------
DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(RequestedThemeChanged, _requestedThemeChangedHandlers, winrt::Windows::Foundation::IInspectable, winrt::Windows::UI::Xaml::ElementTheme);

Expand Down
5 changes: 3 additions & 2 deletions src/cascadia/TerminalApp/AppLogic.idl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ namespace TerminalApp
MaximizedMode,
};

[default_interface] runtimeclass AppLogic
{
[default_interface] runtimeclass AppLogic {
AppLogic();

// For your own sanity, it's better to do setup outside the ctor.
Expand All @@ -35,6 +34,8 @@ namespace TerminalApp
void TitlebarClicked();
void WindowCloseButtonClicked();

void SuppressStartupDirectory();

event Windows.Foundation.TypedEventHandler<Object, Windows.UI.Xaml.UIElement> SetTitleBarContent;
event Windows.Foundation.TypedEventHandler<Object, String> TitleChanged;
event Windows.Foundation.TypedEventHandler<Object, LastTabClosedEventArgs> LastTabClosed;
Expand Down
25 changes: 25 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,18 @@ namespace winrt::TerminalApp::implementation
}

TerminalSettings settings = _settings->MakeSettings(profileGuid);

// microsoft/github#878: When called from a commandline context, we
// should ignore the "startingDirectory" property, and instead inherit
// the CWD of the application. For launches from the start menu, this
// will _not_ be set, and we'll use `startingDirectory`.
// Subsequent tabs, panes will all use `startingDirectory`.
if (_suppressStartupDirectory)
{
settings.StartingDirectory().clear();
_suppressStartupDirectory = false;
}

_CreateNewTabFromSettings(profileGuid, settings);

const int tabCount = static_cast<int>(_tabs.size());
Expand Down Expand Up @@ -1380,6 +1392,19 @@ namespace winrt::TerminalApp::implementation
_UpdateTabView();
}

// Method Description:
// - Used to tell us that the first launched Terminal should ignore the
// "startingDirectory" setting, instead opting to inherit the current
// working directory of the application.
// Arguments:
// - <none>
// Return Value:
// - <none>
void TerminalPage::SuppressStartupDirectory()
{
_suppressStartupDirectory = true;
}

// -------------------------------- WinRT Events ---------------------------------
// Winrt events need a method for adding a callback to the event and removing the callback.
// These macros will define them both for you.
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace winrt::TerminalApp::implementation

void CloseWindow();

void SuppressStartupDirectory();

// -------------------------------- WinRT Events ---------------------------------
DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(TitleChanged, _titleChangeHandlers, winrt::Windows::Foundation::IInspectable, winrt::hstring);
DECLARE_EVENT_WITH_TYPED_EVENT_HANDLER(LastTabClosed, _lastTabClosedHandlers, winrt::Windows::Foundation::IInspectable, winrt::TerminalApp::LastTabClosedEventArgs);
Expand Down Expand Up @@ -65,6 +67,8 @@ namespace winrt::TerminalApp::implementation
std::optional<int> _rearrangeFrom;
std::optional<int> _rearrangeTo;

bool _suppressStartupDirectory{ false };

void _ShowAboutDialog();
void _ShowCloseWarningDialog();

Expand Down
19 changes: 19 additions & 0 deletions src/cascadia/WindowsTerminal/AppHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using namespace winrt::Windows::UI::Composition;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Xaml::Hosting;
using namespace winrt::Windows::Foundation::Numerics;
using namespace winrt::Windows::ApplicationModel;
using namespace ::Microsoft::Console;
using namespace ::Microsoft::Console::Types;

Expand Down Expand Up @@ -66,6 +67,8 @@ void AppHost::Initialize()
{
_window->Initialize();

_ProcessCommandlineArgs();

if (_useNonClientArea)
{
// Register our callbar for when the app's non-client content changes.
Expand Down Expand Up @@ -98,6 +101,22 @@ void AppHost::Initialize()
_window->OnAppInitialized();
}

void AppHost::_ProcessCommandlineArgs()
{
// Check GetActivatedEventArgs. If it was an
// `Activation::ActivationKind::Launch`, it definitely wasn't a commandline
// launch. Shockingly, we _never_ get a
// Activation::ActivationKind::CommandLineLaunch. For our commandline
// launches, AppInstance::GetActivatedEventArgs is actually just null.
const auto args = AppInstance::GetActivatedEventArgs();
const bool wasCommandlineLaunch = args == nullptr;

if (wasCommandlineLaunch)
{
_logic.SuppressStartupDirectory();
}
}

// Method Description:
// - Called when the app's title changes. Fires off a window message so we can
// update the window's title on the main thread.
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/WindowsTerminal/AppHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ class AppHost
const winrt::Windows::UI::Xaml::ElementTheme& arg);
void _ToggleFullscreen(const winrt::Windows::Foundation::IInspectable& sender,
const winrt::TerminalApp::ToggleFullscreenEventArgs& arg);

void _ProcessCommandlineArgs();
};
4 changes: 3 additions & 1 deletion src/cascadia/WindowsTerminal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#include "resource.h"

using namespace winrt;

using namespace winrt::Windows::UI;
using namespace winrt::Windows::UI::Composition;
using namespace winrt::Windows::UI::Xaml::Hosting;
using namespace winrt::Windows::Foundation::Numerics;
using namespace winrt::Windows::ApplicationModel;

// Note: Generate GUID using TlgGuid.exe tool - seriously, it won't work if you
// just generate an arbitrary GUID
Expand Down Expand Up @@ -96,7 +98,7 @@ static void EnsureNativeArchitecture()
}
}

int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR /*cmdline*/, int /*nShowCmd*/)
{
TraceLoggingRegister(g_hWindowsTerminalProvider);
TraceLoggingWrite(
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/WindowsTerminal/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@ Module Name:
// Additional headers for various xaml features. We need:
// * Controls for grid
// * Media for ScaleTransform
// * ApplicationModel for activation things
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.ui.xaml.media.h>

#include <winrt/windows.applicationmodel.activation.h>
#include <winrt/windows.applicationmodel.h>

#include <wil/resource.h>
#include <wil/win32_helpers.h>

Expand Down