Files
terminal/src/cascadia/TerminalApp/TabHeaderControl.cpp
PankajBhojwani b7bebc765e Fix sending a title change event when the renamer is dismissed (#8393)
Fix for #8365 

Adds a `_renameCancelled` bool that determines whether we raise the event for a title change. We do this because the lost focus handler for the rename box will be called _both_ when the renamer is dismissed by clicking somewhere else and by pressing enter/escape. So, the lost focus handler needs to conditionally raise the event. 
 
Closes #8365
2020-11-26 00:20:12 +00:00

86 lines
3.2 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include "TabHeaderControl.h"
#include "TabHeaderControl.g.cpp"
using namespace winrt;
using namespace winrt::Microsoft::UI::Xaml;
namespace winrt::TerminalApp::implementation
{
TabHeaderControl::TabHeaderControl()
{
InitializeComponent();
// We'll only process the KeyUp event if we received an initial KeyDown event first.
// Avoids issue immediately closing the tab rename when we see the enter KeyUp event that was
// sent to the command palette to trigger the openTabRenamer action in the first place.
HeaderRenamerTextBox().KeyDown([&](auto&&, auto&&) {
_receivedKeyDown = true;
});
// NOTE: (Preview)KeyDown does not work here. If you use that, we'll
// remove the TextBox from the UI tree, then the following KeyUp
// will bubble to the NewTabButton, which we don't want to have
// happen.
HeaderRenamerTextBox().KeyUp([&](auto&&, Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e) {
if (_receivedKeyDown)
{
if (e.OriginalKey() == Windows::System::VirtualKey::Enter)
{
// User is done making changes, close the rename box
_CloseRenameBox();
}
else if (e.OriginalKey() == Windows::System::VirtualKey::Escape)
{
// User wants to discard the changes they made,
// set _renameCancelled to true and close the rename box
_renameCancelled = true;
_CloseRenameBox();
}
}
});
}
// Method Description:
// - Show the tab rename box for the user to rename the tab title
// - We automatically use the previous title as the initial text of the box
void TabHeaderControl::BeginRename()
{
_receivedKeyDown = false;
_renameCancelled = false;
HeaderTextBlock().Visibility(Windows::UI::Xaml::Visibility::Collapsed);
HeaderRenamerTextBox().Visibility(Windows::UI::Xaml::Visibility::Visible);
HeaderRenamerTextBox().Text(Title());
HeaderRenamerTextBox().SelectAll();
HeaderRenamerTextBox().Focus(Windows::UI::Xaml::FocusState::Programmatic);
}
// Method Description:
// - Event handler for when the rename box loses focus
// - When the rename box loses focus, we send a request for the title change depending
// on whether the rename was cancelled
void TabHeaderControl::RenameBoxLostFocusHandler(Windows::Foundation::IInspectable const& /*sender*/,
Windows::UI::Xaml::RoutedEventArgs const& /*e*/)
{
_CloseRenameBox();
if (!_renameCancelled)
{
_TitleChangeRequestedHandlers(HeaderRenamerTextBox().Text());
}
}
// Method Description:
// - Hides the rename box and displays the title text block
void TabHeaderControl::_CloseRenameBox()
{
HeaderRenamerTextBox().Visibility(Windows::UI::Xaml::Visibility::Collapsed);
HeaderTextBlock().Visibility(Windows::UI::Xaml::Visibility::Visible);
}
}