Skip to content

Commit

Permalink
sui: Improve the "use parent process directory" checkbox (#8827)
Browse files Browse the repository at this point in the history
Make the "use parent process directory" checkbox rely on a computed
property in the ProfileViewModel. It will be enabled when the starting
directory is empty and disabled when it's not. When it's unchecked, the
last-used value will be restored. If there is no last-used value, it
will be set to %USERPROFILE%.

Closes #8805
  • Loading branch information
PankajBhojwani authored Jan 22, 2021
1 parent d45cc4c commit acf36d0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 22 deletions.
76 changes: 59 additions & 17 deletions src/cascadia/TerminalSettingsEditor/Profiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
// into the path TextBox, we properly update the checkbox and stored
// _lastBgImagePath. Without this, then we'll permanently hide the text
// box, prevent it from ever being changed again.
//
// We do the same for the starting directory path
PropertyChanged([this](auto&&, const Data::PropertyChangedEventArgs& args) {
if (args.PropertyName() == L"BackgroundImagePath")
{
Expand All @@ -46,6 +48,11 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
_NotifyChanges(L"BackgroundImageSettingsVisible");
}
else if (args.PropertyName() == L"StartingDirectory")
{
_NotifyChanges(L"UseParentProcessDirectory");
_NotifyChanges(L"UseCustomStartingDirectory");
}
});

// Cache the original BG image path. If the user clicks "Use desktop
Expand All @@ -55,6 +62,12 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
_lastBgImagePath = BackgroundImagePath();
}

// Do the same for the starting directory
if (!StartingDirectory().empty())
{
_lastStartingDirectoryPath = StartingDirectory();
}
}

bool ProfileViewModel::CanDeleteProfile() const
Expand Down Expand Up @@ -109,6 +122,52 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
}
}

bool ProfileViewModel::UseParentProcessDirectory()
{
return StartingDirectory().empty();
}

// This function simply returns the opposite of UseParentProcessDirectory.
// We bind the 'IsEnabled' parameters of the textbox and browse button
// to this because it needs to be the reverse of UseParentProcessDirectory
// but we don't want to create a whole new converter for inverting a boolean
bool ProfileViewModel::UseCustomStartingDirectory()
{
return !UseParentProcessDirectory();
}

void ProfileViewModel::UseParentProcessDirectory(const bool useParent)
{
if (useParent)
{
// Stash the current value of StartingDirectory. If the user
// checks and un-checks the "Use parent process directory" button, we want
// the path that we display in the text box to remain unchanged.
//
// Only stash this value if it's not empty
if (!StartingDirectory().empty())
{
_lastStartingDirectoryPath = StartingDirectory();
}
StartingDirectory(L"");
}
else
{
// Restore the path we had previously cached as long as it wasn't empty
// If it was empty, set the starting directory to %USERPROFILE%
// (we need to set it to something non-empty otherwise we will automatically
// disable the text box)
if (_lastStartingDirectoryPath.empty())
{
StartingDirectory(L"%USERPROFILE%");
}
else
{
StartingDirectory(_lastStartingDirectoryPath);
}
}
}

bool ProfileViewModel::BackgroundImageSettingsVisible()
{
return IsBaseLayer() || BackgroundImagePath() != L"";
Expand Down Expand Up @@ -213,23 +272,6 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
state->DeleteProfile();
}

void Profiles::UseParentProcessDirectory_Check(IInspectable const& /*sender*/, RoutedEventArgs const& /*e*/)
{
auto state{ winrt::get_self<ProfilePageNavigationState>(_State) };
state->Profile().StartingDirectory(L"");

// Disable the text box and browse button
StartingDirectory().IsEnabled(false);
StartingDirectoryBrowse().IsEnabled(false);
}

void Profiles::UseParentProcessDirectory_Uncheck(IInspectable const& /*sender*/, RoutedEventArgs const& /*e*/)
{
// Enable the text box and browse button
StartingDirectory().IsEnabled(true);
StartingDirectoryBrowse().IsEnabled(true);
}

fire_and_forget Profiles::BackgroundImage_Click(IInspectable const&, RoutedEventArgs const&)
{
auto lifetime = get_strong();
Expand Down
6 changes: 4 additions & 2 deletions src/cascadia/TerminalSettingsEditor/Profiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation

bool UseDesktopBGImage();
void UseDesktopBGImage(const bool useDesktop);
bool UseParentProcessDirectory();
void UseParentProcessDirectory(const bool useParent);
bool UseCustomStartingDirectory();
bool BackgroundImageSettingsVisible();

GETSET_PROPERTY(bool, IsBaseLayer, false);
Expand Down Expand Up @@ -67,6 +70,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
private:
Model::Profile _profile;
winrt::hstring _lastBgImagePath;
winrt::hstring _lastStartingDirectoryPath;
};

struct DeleteProfileEventArgs :
Expand Down Expand Up @@ -121,8 +125,6 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
fire_and_forget Icon_Click(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& e);
void BIAlignment_Click(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& e);
void DeleteConfirmation_Click(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& e);
void UseParentProcessDirectory_Check(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& e);
void UseParentProcessDirectory_Uncheck(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& e);

// CursorShape visibility logic
void CursorShape_Changed(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& e);
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsEditor/Profiles.idl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Microsoft.Terminal.Settings.Editor
Boolean CanDeleteProfile { get; };
Boolean IsBaseLayer;
Boolean UseDesktopBGImage;
Boolean UseParentProcessDirectory;
Boolean UseCustomStartingDirectory { get; };
Boolean BackgroundImageSettingsVisible { get; };

OBSERVABLE_PROJECTED_SETTING(String, Name);
Expand Down
7 changes: 4 additions & 3 deletions src/cascadia/TerminalSettingsEditor/Profiles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,17 @@ the MIT License. See LICENSE in the project root for license information. -->
<TextBox x:Uid="Profile_StartingDirectory"
x:Name="StartingDirectory"
Text="{x:Bind State.Profile.StartingDirectory, Mode=TwoWay}"
Style="{StaticResource TextBoxSettingStyle}"/>
Style="{StaticResource TextBoxSettingStyle}"
IsEnabled="{x:Bind State.Profile.UseCustomStartingDirectory, Mode=OneWay}"/>
<Button x:Uid="Profile_StartingDirectoryBrowse"
x:Name="StartingDirectoryBrowse"
Click="StartingDirectory_Click"
IsEnabled="{x:Bind State.Profile.UseCustomStartingDirectory, Mode=OneWay}"
Style="{StaticResource BrowseButtonStyle}"/>
</StackPanel>
<CheckBox x:Uid="Profile_StartingDirectoryUseParentCheckbox"
x:Name="StartingDirectoryUseParentCheckbox"
Checked="UseParentProcessDirectory_Check"
Unchecked="UseParentProcessDirectory_Uncheck"/>
IsChecked="{x:Bind State.Profile.UseParentProcessDirectory, Mode=TwoWay}"/>
</StackPanel>
</ContentPresenter>

Expand Down

0 comments on commit acf36d0

Please sign in to comment.