From 4946b33491c9ea27b69f98544acb477233253605 Mon Sep 17 00:00:00 2001 From: James Pack Date: Sat, 8 Jul 2023 16:55:46 -0400 Subject: [PATCH 1/2] Add a menu flyout for profile entries in the add tab split button dropdown which when clicked will run the selected profile as administrator --- .../Resources/en-US/Resources.resw | 8 ++- src/cascadia/TerminalApp/TerminalPage.cpp | 50 +++++++++++++++++++ src/cascadia/TerminalApp/TerminalPage.h | 2 +- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/cascadia/TerminalApp/Resources/en-US/Resources.resw b/src/cascadia/TerminalApp/Resources/en-US/Resources.resw index 1a20db37695..80f0a9df574 100644 --- a/src/cascadia/TerminalApp/Resources/en-US/Resources.resw +++ b/src/cascadia/TerminalApp/Resources/en-US/Resources.resw @@ -216,7 +216,7 @@ Web Search - + Color... @@ -836,4 +836,8 @@ Moves tab to a new window - + + Run as Administrator + This text is displayed on context menu for profile entries in add new tab button. + + \ No newline at end of file diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index c0b3f0d5943..02399aea075 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -1057,6 +1057,17 @@ namespace winrt::TerminalApp::implementation } }); + // Using the static method on the base class seems to do what we want in terms of placement. + WUX::Controls::Primitives::FlyoutBase::SetAttachedFlyout(profileMenuItem, _CreateRunAsAdminFlyout(profileIndex)); + + // Since we are not setting the ContextFlyout property of the item we have to handle the ContextRequested event + // and rely on the base class to show our menu. + profileMenuItem.ContextRequested([profileMenuItem](auto&&, auto&&) { + WUX::Controls::Primitives::FlyoutBase::ShowAttachedFlyout(profileMenuItem); + + }); + + return profileMenuItem; } @@ -4899,4 +4910,43 @@ namespace winrt::TerminalApp::implementation // _RemoveTab will make sure to null out the _stashed.draggedTab _RemoveTab(*_stashed.draggedTab); } + + /// + /// Creates a sub flyout menu for profile items in the split button menu that when clicked will show a menu item for + /// Run as Administrator + /// + /// The index for the profileMenuItem + /// MenuFlyout that will show when the context is request on a profileMenuItem + WUX::Controls::MenuFlyout TerminalPage::_CreateRunAsAdminFlyout(int profileIndex) + { + // Create the MenuFlyout and set its placement + WUX::Controls::MenuFlyout profileMenuItemFlyout{}; + profileMenuItemFlyout.Placement(WUX::Controls::Primitives::FlyoutPlacementMode::BottomEdgeAlignedRight); + + + // Create the menu item and an icon to use in the menu + WUX::Controls::MenuFlyoutItem runAsAdminItem{}; + WUX::Controls::FontIcon adminShieldIcon{}; + + adminShieldIcon.Glyph(L"\xEA18"); + adminShieldIcon.FontFamily(Media::FontFamily{ L"Segoe Fluent Icons, Segoe MDL2 Assets" }); + + runAsAdminItem.Icon(adminShieldIcon); + runAsAdminItem.Text(RS_(L"RunAsAdminFlyout/Text")); + + // Click handler for the flyout item + runAsAdminItem.Click([profileIndex, weakThis{ get_weak() }](auto&&, auto&&) { + if (auto page{ weakThis.get() }) + { + NewTerminalArgs args{ profileIndex }; + args.Elevate(true); + page->_OpenNewTerminalViaDropdown(args); + } + }); + + profileMenuItemFlyout.Items().Append(runAsAdminItem); + + return profileMenuItemFlyout; + } + } diff --git a/src/cascadia/TerminalApp/TerminalPage.h b/src/cascadia/TerminalApp/TerminalPage.h index bde39cca453..a7dee2e4ab6 100644 --- a/src/cascadia/TerminalApp/TerminalPage.h +++ b/src/cascadia/TerminalApp/TerminalPage.h @@ -530,7 +530,7 @@ namespace winrt::TerminalApp::implementation void _ContextMenuOpened(const IInspectable& sender, const IInspectable& args); void _SelectionMenuOpened(const IInspectable& sender, const IInspectable& args); void _PopulateContextMenu(const IInspectable& sender, const bool withSelection); - + winrt::Windows::UI::Xaml::Controls::MenuFlyout _CreateRunAsAdminFlyout(int profileIndex); #pragma region ActionHandlers // These are all defined in AppActionHandlers.cpp #define ON_ALL_ACTIONS(action) DECLARE_ACTION_HANDLER(action); From 9f3eaeabd29239eb1267e3355761a6bf6c7ab32d Mon Sep 17 00:00:00 2001 From: James Pack Date: Sat, 8 Jul 2023 17:55:37 -0400 Subject: [PATCH 2/2] Run code format --- src/cascadia/TerminalApp/TerminalPage.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp index 02399aea075..c4266413ddb 100644 --- a/src/cascadia/TerminalApp/TerminalPage.cpp +++ b/src/cascadia/TerminalApp/TerminalPage.cpp @@ -1064,10 +1064,8 @@ namespace winrt::TerminalApp::implementation // and rely on the base class to show our menu. profileMenuItem.ContextRequested([profileMenuItem](auto&&, auto&&) { WUX::Controls::Primitives::FlyoutBase::ShowAttachedFlyout(profileMenuItem); - }); - return profileMenuItem; } @@ -4923,7 +4921,6 @@ namespace winrt::TerminalApp::implementation WUX::Controls::MenuFlyout profileMenuItemFlyout{}; profileMenuItemFlyout.Placement(WUX::Controls::Primitives::FlyoutPlacementMode::BottomEdgeAlignedRight); - // Create the menu item and an icon to use in the menu WUX::Controls::MenuFlyoutItem runAsAdminItem{}; WUX::Controls::FontIcon adminShieldIcon{}; @@ -4933,8 +4930,8 @@ namespace winrt::TerminalApp::implementation runAsAdminItem.Icon(adminShieldIcon); runAsAdminItem.Text(RS_(L"RunAsAdminFlyout/Text")); - - // Click handler for the flyout item + + // Click handler for the flyout item runAsAdminItem.Click([profileIndex, weakThis{ get_weak() }](auto&&, auto&&) { if (auto page{ weakThis.get() }) {