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 Terminal closing with ALT + F4 and warning of multiple open tabs #2526

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
97 changes: 88 additions & 9 deletions src/cascadia/TerminalApp/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,26 @@ namespace winrt::TerminalApp::implementation
}

// Method Description:
// - Show a ContentDialog with a single button to dismiss. Uses the
// - Show a ContentDialog with buttons to take further action. Uses the
// FrameworkElements provided as the title and content of this dialog, and
// displays a single button to dismiss.
// displays buttons (or a single button). Two buttons (primary and secondary)
// will be displayed if this is an warning dialog for closing the termimal,
// this allows the users to abondon the closing action. Otherwise, a single
// close button will be displayed.
// - Only one dialog can be visible at a time. If another dialog is visible
// when this is called, nothing happens.
// Arguments:
// - titleElement: the element to use as the title of this ContentDialog
KaiyuWang16 marked this conversation as resolved.
Show resolved Hide resolved
// - contentElement: the element to use as the content of this ContentDialog
// - closeButtonText: The string to use on the close button
// - primaryButtonText: The string to use on the primary or close button
// - secondaryButtonText: The string to use on the secondary button, this is
// currently only used for closing the whole window
// - isClosingWindow: whether this dialog is for closing the whole app window
fire_and_forget App::_ShowDialog(const IInspectable& titleElement,
const IInspectable& contentElement,
const winrt::hstring& closeButtonText)
const winrt::hstring& primaryButtonText,
const winrt::hstring& secondaryButtonText,
bool isClosingWindow)
{
// DON'T release this lock in a wil::scope_exit. The scope_exit will get
// called when we await, which is not what we want.
Expand All @@ -226,9 +234,19 @@ namespace winrt::TerminalApp::implementation
}

Controls::ContentDialog dialog;

if (isClosingWindow)
KaiyuWang16 marked this conversation as resolved.
Show resolved Hide resolved
{
dialog.PrimaryButtonText(primaryButtonText);
dialog.SecondaryButtonText(secondaryButtonText);
auto token = dialog.PrimaryButtonClick({ this, &App::_CloseWarningPrimaryButtonOnClick });
}
else
{
dialog.CloseButtonText(primaryButtonText);
}
dialog.Title(titleElement);
dialog.Content(contentElement);
dialog.CloseButtonText(closeButtonText);

// IMPORTANT: This is necessary as documented in the ContentDialog MSDN docs.
// Since we're hosting the dialog in a Xaml island, we need to connect it to the
Expand Down Expand Up @@ -262,7 +280,7 @@ namespace winrt::TerminalApp::implementation
auto message = _resourceLoader.GetLocalizedString(contentKey);
auto buttonText = _resourceLoader.GetLocalizedString(L"Ok");

_ShowDialog(winrt::box_value(title), winrt::box_value(message), buttonText);
_ShowDialog(winrt::box_value(title), winrt::box_value(message), buttonText, buttonText, false);
}

// Method Description:
Expand Down Expand Up @@ -306,7 +324,7 @@ namespace winrt::TerminalApp::implementation
usingDefaultsRun.Text(usingDefaultsText);
warningsTextBlock.Inlines().Append(usingDefaultsRun);

_ShowDialog(winrt::box_value(title), warningsTextBlock, buttonText);
_ShowDialog(winrt::box_value(title), warningsTextBlock, buttonText, buttonText, false);
}

// Method Description:
Expand Down Expand Up @@ -337,7 +355,33 @@ namespace winrt::TerminalApp::implementation
}
}

_ShowDialog(winrt::box_value(title), warningsTextBlock, buttonText);
_ShowDialog(winrt::box_value(title), warningsTextBlock, buttonText, buttonText, false);
}

// Method Description:
// - Displays a dialog for warnings found while closing the terminal app using
// - key binding with multiple tabs opened. Display messages to warn user
// - that more than 1 tabs are opend, and once the user click the OK button, remove
// - all the tabs and shut down and app. If cancel is clicked, the dialog will close
// - Only one dialog can be visible at a time. If another dialog is visible
// when this is called, nothing happens. See _ShowDialog for details
void App::_ShowCloseWarningDialog()
{
// To do: change these strings to localized strings in resource loader
auto title = _resourceLoader.GetLocalizedString(L"CloseWindowWarningTitle");
auto primaryButtonText = _resourceLoader.GetLocalizedString(L"OK");
auto secondaryButtonText = _resourceLoader.GetLocalizedString(L"Cancel");

Controls::TextBlock warningsTextBlock;
KaiyuWang16 marked this conversation as resolved.
Show resolved Hide resolved
// Make sure you can copy-paste
warningsTextBlock.IsTextSelectionEnabled(true);
// Make sure the lines of text wrap
warningsTextBlock.TextWrapping(TextWrapping::Wrap);

const auto warningText = _resourceLoader.GetLocalizedString(L"CloseWindowWarningText");
warningsTextBlock.Inlines().Append(_BuildErrorRun(warningText, Resources()));

_ShowDialog(winrt::box_value(title), warningsTextBlock, primaryButtonText, secondaryButtonText, true);
}

// Method Description:
Expand Down Expand Up @@ -406,7 +450,7 @@ namespace winrt::TerminalApp::implementation
aboutTextBlock.Inlines().Append(releaseNotesLink);
aboutTextBlock.IsTextSelectionEnabled(true);

_ShowDialog(winrt::box_value(title), aboutTextBlock, buttonText);
_ShowDialog(winrt::box_value(title), aboutTextBlock, buttonText, buttonText, false);
}

// Method Description:
Expand Down Expand Up @@ -643,6 +687,12 @@ namespace winrt::TerminalApp::implementation
_ShowAboutDialog();
}

void App::_CloseWarningPrimaryButtonOnClick(Windows::UI::Xaml::Controls::ContentDialog sender,
KaiyuWang16 marked this conversation as resolved.
Show resolved Hide resolved
Windows::UI::Xaml::Controls::ContentDialogButtonClickEventArgs eventArgs)
{
CloseAllTabs();
}

// Method Description:
// - Register our event handlers with the given keybindings object. This
// should be done regardless of what the events are actually bound to -
Expand All @@ -661,6 +711,7 @@ namespace winrt::TerminalApp::implementation
bindings.DuplicateTab({ this, &App::_HandleDuplicateTab });
bindings.CloseTab({ this, &App::_HandleCloseTab });
bindings.ClosePane({ this, &App::_HandleClosePane });
bindings.CloseWindow({ this, &App::_HandleCloseWindow });
bindings.ScrollUp({ this, &App::_HandleScrollUp });
bindings.ScrollDown({ this, &App::_HandleScrollDown });
bindings.NextTab({ this, &App::_HandleNextTab });
Expand Down Expand Up @@ -1168,6 +1219,34 @@ namespace winrt::TerminalApp::implementation
focusedTab->ClosePane();
}

// Method Description:
// - Close the terminal app with keys. If there are more
// - than one tab opened, show a warning dialog.
void App::_CloseWindow()
{
if (_tabs.size() > 1)
{
_ShowCloseWarningDialog();
}
else
{
CloseAllTabs();
}
}

// Method Description:
// - Close all the tabs opened and this will finally terminate
// - the terminal
void App::CloseAllTabs()
KaiyuWang16 marked this conversation as resolved.
Show resolved Hide resolved
{
int tabCount = _tabs.size();
for (int i = tabCount - 1; i >= 0; i--)
{
std::shared_ptr<Tab> curTab{ _tabs[i] };
_RemoveTabViewItem(curTab->GetTabViewItem());
}
}

// Method Description:
// - Move the viewport of the terminal of the currently focused tab up or
// down a number of lines. Negative values of `delta` will move the
Expand Down
9 changes: 8 additions & 1 deletion src/cascadia/TerminalApp/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ namespace winrt::TerminalApp::implementation

fire_and_forget _ShowDialog(const winrt::Windows::Foundation::IInspectable& titleElement,
const winrt::Windows::Foundation::IInspectable& contentElement,
const winrt::hstring& closeButtonText);
const winrt::hstring& primaryButtonText,
const winrt::hstring& secondaryButtonText,
bool isClosingWindow);
void _ShowOkDialog(const winrt::hstring& titleKey, const winrt::hstring& contentKey);
void _ShowAboutDialog();
void _ShowLoadWarningsDialog();
void _ShowLoadErrorsDialog(const winrt::hstring& titleKey, const winrt::hstring& contentKey);
void _ShowCloseWarningDialog();

[[nodiscard]] HRESULT _TryLoadSettings() noexcept;
void _LoadSettings();
Expand All @@ -98,6 +101,7 @@ namespace winrt::TerminalApp::implementation
void _SettingsButtonOnClick(const IInspectable& sender, const Windows::UI::Xaml::RoutedEventArgs& eventArgs);
void _FeedbackButtonOnClick(const IInspectable& sender, const Windows::UI::Xaml::RoutedEventArgs& eventArgs);
void _AboutButtonOnClick(const IInspectable& sender, const Windows::UI::Xaml::RoutedEventArgs& eventArgs);
void _CloseWarningPrimaryButtonOnClick(Windows::UI::Xaml::Controls::ContentDialog sender, Windows::UI::Xaml::Controls::ContentDialogButtonClickEventArgs eventArgs);

void _UpdateTabView();
void _UpdateTabIcon(std::shared_ptr<Tab> tab);
Expand All @@ -112,6 +116,8 @@ namespace winrt::TerminalApp::implementation
void _DuplicateTabViewItem();
void _CloseFocusedTab();
void _CloseFocusedPane();
void _CloseWindow();
void CloseAllTabs();
void _SelectNextTab(const bool bMoveRight);
bool _SelectTab(const int tabIndex);

Expand Down Expand Up @@ -173,6 +179,7 @@ namespace winrt::TerminalApp::implementation
void _HandleResizePane(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleMoveFocus(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleCopyText(const IInspectable& sender, const TerminalApp::ActionEventArgs& args);
void _HandleCloseWindow(const IInspectable&, const TerminalApp::ActionEventArgs& args);
#pragma endregion
};
}
Expand Down
7 changes: 7 additions & 0 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ namespace winrt::TerminalApp::implementation
args.Handled(true);
}

void App::_HandleCloseWindow(const IInspectable& /*sender*/,
const TerminalApp::ActionEventArgs& args)
{
_CloseWindow();
args.Handled(true);
}

void App::_HandleScrollUp(const IInspectable& /*sender*/,
const TerminalApp::ActionEventArgs& args)
{
Expand Down
65 changes: 37 additions & 28 deletions src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

<!--
Microsoft ResX Schema
KaiyuWang16 marked this conversation as resolved.
Show resolved Hide resolved
Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -193,4 +193,13 @@ Temporarily using the Windows Terminal default settings.
<data name="SettingsMenuItem" xml:space="preserve">
<value>Settings</value>
</data>
</root>
<data name="Cancel" xml:space="preserve">
<value>Cancel</value>
</data>
<data name="CloseWindowWarningText" xml:space="preserve">
<value>You have more than one tab opened</value>
</data>
<data name="CloseWindowWarningTitle" xml:space="preserve">
<value>Close Window Warning</value>
</data>
</root>