Skip to content

Commit

Permalink
Provide the CloseWindow warning experience for the 'X' button (#3049)
Browse files Browse the repository at this point in the history
Related to #1589.
  • Loading branch information
KaiyuWang16 authored and DHowett committed Oct 11, 2019
1 parent 200e90d commit 82dd0b9
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/cascadia/TerminalApp/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,22 @@ namespace winrt::TerminalApp::implementation
}
}

// Method Description:
// - Used to tell the app that the 'X' button has been clicked and
// the user wants to close the app. We kick off the close warning
// experience.
// Arguments:
// - <none>
// Return Value:
// - <none>
void App::WindowCloseButtonClicked()
{
if (_root)
{
_root->CloseWindow();
}
}

// Methods that proxy typed event handlers through TerminalPage
winrt::event_token App::SetTitleBarContent(Windows::Foundation::TypedEventHandler<winrt::Windows::Foundation::IInspectable, winrt::Windows::UI::Xaml::UIElement> const& handler)
{
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace winrt::TerminalApp::implementation
hstring Title();
void TitlebarClicked();

void WindowCloseButtonClicked();

// -------------------------------- 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
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/App.idl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace TerminalApp
Windows.Foundation.Point GetLaunchDimensions(UInt32 dpi);
Boolean GetShowTabsInTitlebar();
void TitlebarClicked();
void WindowCloseButtonClicked();

event Windows.Foundation.TypedEventHandler<Object, Windows.UI.Xaml.UIElement> SetTitleBarContent;
event Windows.Foundation.TypedEventHandler<Object, String> TitleChanged;
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace winrt::TerminalApp::implementation
void TerminalPage::_HandleCloseWindow(const IInspectable& /*sender*/,
const TerminalApp::ActionEventArgs& args)
{
_CloseWindow();
CloseWindow();
args.Handled(true);
}

Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,9 +823,9 @@ namespace winrt::TerminalApp::implementation
}

// Method Description:
// - Close the terminal app with keys. If there is more
// - Close the terminal app. If there is more
// than one tab opened, show a warning dialog.
void TerminalPage::_CloseWindow()
void TerminalPage::CloseWindow()
{
if (_tabs.size() > 1)
{
Expand Down
3 changes: 2 additions & 1 deletion 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 TitlebarClicked();

void CloseWindow();

// -------------------------------- 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 @@ -94,7 +96,6 @@ namespace winrt::TerminalApp::implementation
void _SetFocusedTabIndex(int tabIndex);
void _CloseFocusedTab();
void _CloseFocusedPane();
void _CloseWindow();
void _CloseAllTabs();

// Todo: add more event implementations here
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TitlebarControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace winrt::TerminalApp::implementation

void TitlebarControl::Close_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
::PostQuitMessage(0);
::PostMessage(_window, WM_SYSCOMMAND, SC_CLOSE, 0);
}

void TitlebarControl::SetWindowVisualState(WindowVisualState visualState)
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/WindowsTerminal/AppHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ void AppHost::Initialize()
_app.SetTitleBarContent({ this, &AppHost::_UpdateTitleBarContent });
}

// Register the 'X' button of the window for a warning experience of multiple
// tabs opened, this is consistent with Alt+F4 closing
_window->WindowCloseButtonClicked([this]() { _app.WindowCloseButtonClicked(); });

// Add an event handler to plumb clicks in the titlebar area down to the
// application layer.
_window->DragRegionClicked([this]() { _app.TitlebarClicked(); });
Expand Down
9 changes: 9 additions & 0 deletions src/cascadia/WindowsTerminal/IslandWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ void IslandWindow::OnSize(const UINT width, const UINT height)
// key that does not correspond to any mnemonic or accelerator key,
return MAKELRESULT(0, MNC_CLOSE);
}
case WM_CLOSE:
{
// If the user wants to close the app by clicking 'X' button,
// we hand off the close experience to the app layer. If all the tabs
// are closed, the window will be closed as well.
_windowCloseButtonClickedHandler();
return 0;
}
}

// TODO: handle messages here...
Expand Down Expand Up @@ -285,3 +293,4 @@ void IslandWindow::UpdateTheme(const winrt::Windows::UI::Xaml::ElementTheme& req
}

DEFINE_EVENT(IslandWindow, DragRegionClicked, _DragRegionClickedHandlers, winrt::delegate<>);
DEFINE_EVENT(IslandWindow, WindowCloseButtonClicked, _windowCloseButtonClickedHandler, winrt::delegate<>);
1 change: 1 addition & 0 deletions src/cascadia/WindowsTerminal/IslandWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class IslandWindow :
#pragma endregion

DECLARE_EVENT(DragRegionClicked, _DragRegionClickedHandlers, winrt::delegate<>);
DECLARE_EVENT(WindowCloseButtonClicked, _windowCloseButtonClickedHandler, winrt::delegate<>);

protected:
void ForceResize()
Expand Down

0 comments on commit 82dd0b9

Please sign in to comment.