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 9 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
112 changes: 97 additions & 15 deletions src/cascadia/TerminalApp/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,22 @@ 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
fire_and_forget App::_ShowDialog(const IInspectable& titleElement,
const IInspectable& contentElement,
const winrt::hstring& closeButtonText)
// - 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(Controls::ContentDialog dialog)
{
// 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 @@ -225,11 +229,6 @@ namespace winrt::TerminalApp::implementation
return;
}

Controls::ContentDialog dialog;
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
// xaml tree somehow.
Expand Down Expand Up @@ -262,7 +261,13 @@ 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);
Controls::ContentDialog dialog;

dialog.Title(winrt::box_value(title));
dialog.Content(winrt::box_value(message));
dialog.CloseButtonText(buttonText);

_ShowDialog(dialog);
}

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

_ShowDialog(winrt::box_value(title), warningsTextBlock, buttonText);
Controls::ContentDialog dialog;

dialog.Title(winrt::box_value(title));
dialog.Content(warningsTextBlock);
dialog.CloseButtonText(buttonText);

_ShowDialog(dialog);
}

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

_ShowDialog(winrt::box_value(title), warningsTextBlock, buttonText);
Controls::ContentDialog dialog;

dialog.Title(winrt::box_value(title));
dialog.Content(warningsTextBlock);
dialog.CloseButtonText(buttonText);

_ShowDialog(dialog);
}

// 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"Close all");
auto closeButtonText = _resourceLoader.GetLocalizedString(L"Cancel");

Controls::ContentDialog dialog;
dialog.Title(winrt::box_value(title));

dialog.PrimaryButtonText(primaryButtonText);
dialog.CloseButtonText(closeButtonText);
auto token = dialog.PrimaryButtonClick({ this, &App::_CloseWarningPrimaryButtonOnClick });

_ShowDialog(dialog);
}

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

_ShowDialog(winrt::box_value(title), aboutTextBlock, buttonText);
Controls::ContentDialog dialog;

dialog.Title(winrt::box_value(title));
dialog.Content(aboutTextBlock);
dialog.CloseButtonText(buttonText);

_ShowDialog(dialog);
}

// Method Description:
Expand Down Expand Up @@ -643,6 +690,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 +714,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 +1222,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()
{
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: 6 additions & 3 deletions src/cascadia/TerminalApp/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ namespace winrt::TerminalApp::implementation
void _CreateNewTabFlyout();
void _OpenNewTabDropdown();

fire_and_forget _ShowDialog(const winrt::Windows::Foundation::IInspectable& titleElement,
const winrt::Windows::Foundation::IInspectable& contentElement,
const winrt::hstring& closeButtonText);
fire_and_forget _ShowDialog(Windows::UI::Xaml::Controls::ContentDialog dialog);
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 +97,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 +112,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 +175,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
11 changes: 10 additions & 1 deletion src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
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="Close all" xml:space="preserve">
KaiyuWang16 marked this conversation as resolved.
Show resolved Hide resolved
<value>Close all</value>
</data>
<data name="CloseWindowWarningTitle" xml:space="preserve">
<value>Do you want to close all tabs ?</value>
KaiyuWang16 marked this conversation as resolved.
Show resolved Hide resolved
</data>
</root>