Who ever said nested lambdas is a bad thing?

This commit is contained in:
Mike Griese 2021-09-01 16:42:36 -05:00
parent 7fb7d64b91
commit 631cdf7b18
7 changed files with 83 additions and 14 deletions

View file

@ -10,7 +10,9 @@ using namespace winrt::Windows::UI::Xaml;
namespace winrt::TerminalApp::implementation
{
AdminWarningPlaceholder::AdminWarningPlaceholder()
AdminWarningPlaceholder::AdminWarningPlaceholder(const winrt::Microsoft::Terminal::Control::TermControl& control, const winrt::hstring& cmdline) :
_control{ control },
_cmdline{ cmdline }
{
InitializeComponent();
}
@ -24,4 +26,9 @@ namespace winrt::TerminalApp::implementation
{
_CancelButtonClickedHandlers(*this, e);
}
winrt::Windows::UI::Xaml::Controls::UserControl AdminWarningPlaceholder::Control()
{
return _control;
}
}

View file

@ -10,12 +10,19 @@ namespace winrt::TerminalApp::implementation
{
struct AdminWarningPlaceholder : AdminWarningPlaceholderT<AdminWarningPlaceholder>
{
AdminWarningPlaceholder();
AdminWarningPlaceholder(const winrt::Microsoft::Terminal::Control::TermControl& control, const winrt::hstring& cmdline);
winrt::Windows::UI::Xaml::Controls::UserControl Control();
TYPED_EVENT(PrimaryButtonClicked, TerminalApp::AdminWarningPlaceholder, winrt::Windows::UI::Xaml::RoutedEventArgs);
TYPED_EVENT(CancelButtonClicked, TerminalApp::AdminWarningPlaceholder, winrt::Windows::UI::Xaml::RoutedEventArgs);
private:
friend struct AdminWarningPlaceholderT<AdminWarningPlaceholder>; // friend our parent so it can bind private event handlers
winrt::Microsoft::Terminal::Control::TermControl _control{ nullptr };
winrt::hstring _cmdline;
void _primaryButtonClick(winrt::Windows::Foundation::IInspectable const& sender,
winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
void _cancelButtonClick(winrt::Windows::Foundation::IInspectable const& sender,
@ -23,7 +30,7 @@ namespace winrt::TerminalApp::implementation
};
}
namespace winrt::TerminalApp::factory_implementation
{
BASIC_FACTORY(AdminWarningPlaceholder);
}
// namespace winrt::TerminalApp::factory_implementation
// {
// BASIC_FACTORY(AdminWarningPlaceholder);
// }

View file

@ -5,6 +5,6 @@ namespace TerminalApp
{
[default_interface] runtimeclass AdminWarningPlaceholder : Windows.UI.Xaml.Controls.UserControl
{
AdminWarningPlaceholder();
// AdminWarningPlaceholder();
}
}

View file

@ -544,6 +544,33 @@ bool Pane::SwapPanes(std::shared_ptr<Pane> first, std::shared_ptr<Pane> second)
return false;
}
winrt::Windows::UI::Xaml::Controls::UserControl Pane::ReplaceControl(const winrt::Windows::UI::Xaml::Controls::UserControl& control)
{
if (!_IsLeaf())
{
return nullptr;
}
const auto& oldControl = _control;
const auto& oldTermControl{ _control.try_as<TermControl>() };
if (oldTermControl)
{
oldTermControl.ConnectionStateChanged(_connectionStateChangedToken);
oldTermControl.WarningBell(_warningBellToken);
}
_control = control;
_border.Child(_control);
const auto& termControl{ _control.try_as<TermControl>() };
if (termControl)
{
_connectionStateChangedToken = termControl.ConnectionStateChanged({ this, &Pane::_ControlConnectionStateChangedHandler });
_warningBellToken = termControl.WarningBell({ this, &Pane::_ControlWarningBellHandler });
}
return oldControl;
}
// Method Description:
// - Given two panes, test whether the `direction` side of first is adjacent to second.
// Arguments:

View file

@ -101,6 +101,8 @@ public:
winrt::Microsoft::Terminal::Settings::Model::SplitState splitType);
std::shared_ptr<Pane> DetachPane(std::shared_ptr<Pane> pane);
winrt::Windows::UI::Xaml::Controls::UserControl ReplaceControl(const winrt::Windows::UI::Xaml::Controls::UserControl& control);
int GetLeafPaneCount() const noexcept;
void Maximize(std::shared_ptr<Pane> zoomedPane);

View file

@ -486,16 +486,16 @@
<data name="MultiLinePasteDialog.Title" xml:space="preserve">
<value>Warning</value>
</data>
<data name="ApproveCommandlineWarning_CancelButton.Text" xml:space="preserve">
<data name="ApproveCommandlineWarning_CancelButton.Content" xml:space="preserve">
<value>Cancel</value>
</data>
<data name="ApproveCommandlineWarningTextBlock.Content" xml:space="preserve">
<data name="ApproveCommandlineWarningTextBlock.Text" xml:space="preserve">
<value>You are about t execute the following commandline. {TODO! format this}. Do you wish to continue?</value>
</data>
<data name="ApproveCommandlineWarning_PrimaryButton.Text" xml:space="preserve">
<data name="ApproveCommandlineWarning_PrimaryButton.Content" xml:space="preserve">
<value>Allow Commandline</value>
</data>
<data name="ApproveCommandlineWarningTitle.Content" xml:space="preserve">
<data name="ApproveCommandlineWarningTitle.Text" xml:space="preserve">
<value>Warning</value>
</data>
<data name="CommandPalette_SearchBox.PlaceholderText" xml:space="preserve">

View file

@ -16,6 +16,7 @@
#include <LibraryResources.h>
#include "TabRowControl.h"
#include "AdminWarningPlaceholder.h"
#include "ColorHelper.h"
#include "DebugTapConnection.h"
#include "SettingsTab.h"
@ -250,10 +251,10 @@ namespace winrt::TerminalApp::implementation
// - existingConnection: optionally receives a connection from the outside world instead of attempting to create one
void TerminalPage::_CreateNewTabWithProfileAndSettings(Microsoft::Terminal::Settings::Model::Profile profile, Microsoft::Terminal::Settings::Model::TerminalSettingsCreateResult settings, TerminalConnection::ITerminalConnection existingConnection)
{
bool doAdminWarning = false;
bool doAdminWarning = true;;
const auto& cmdline{ settings.DefaultSettings().Commandline() };
if (_isElevated())
{
auto cmdline{ settings.DefaultSettings().Commandline() };
auto allowedCommandlines{ ElevatedState::SharedInstance().AllowedCommandlines() };
bool commandlineWasAllowed = false;
@ -317,8 +318,33 @@ namespace winrt::TerminalApp::implementation
// Give term control a child of the settings so that any overrides go in the child
// This way, when we do a settings reload we just update the parent and the overrides remain
auto term = _InitControl(settings, connection);
WUX::Controls::UserControl controlToAdd{ term };
if (doAdminWarning)
{
auto warningControl{ winrt::make_self<implementation::AdminWarningPlaceholder>(term, cmdline) };
warningControl->PrimaryButtonClicked([weakThis = get_weak(), warningControl](auto&&, auto&&) {
if (auto page{ weakThis.get() })
{
for (const auto& tab : page->_tabs)
{
if (const auto& tabImpl{ _GetTerminalTabImpl(tab) })
{
tabImpl->GetRootPane()->WalkTree([warningControl](std::shared_ptr<Pane> pane) -> bool {
if (pane->GetUserControl() == *warningControl)
{
pane->ReplaceControl(warningControl->Control());
return true;
}
return false;
});
}
}
}
});
controlToAdd = *warningControl;
}
auto newTabImpl = winrt::make_self<TerminalTab>(profile, term);
auto newTabImpl = winrt::make_self<TerminalTab>(profile, controlToAdd);
_RegisterTerminalEvents(term);
_InitializeTab(newTabImpl);