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

Add support for running profiles from Add tab menu as admin without a keyboard #15679

Merged
merged 2 commits into from Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Expand Up @@ -216,7 +216,7 @@
</data>
<data name="SearchWebText" xml:space="preserve">
<value>Web Search</value>
</data>
</data>
<data name="TabColorChoose" xml:space="preserve">
<value>Color...</value>
</data>
Expand Down Expand Up @@ -836,4 +836,8 @@
<data name="MoveTabToNewWindowToolTip" xml:space="preserve">
<value>Moves tab to a new window </value>
</data>
</root>
<data name="RunAsAdminFlyout.Text" xml:space="preserve">
<value>Run as Administrator</value>
<comment>This text is displayed on context menu for profile entries in add new tab button.</comment>
</data>
</root>
47 changes: 47 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Expand Up @@ -1057,6 +1057,15 @@ 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;
}

Expand Down Expand Up @@ -4899,4 +4908,42 @@ namespace winrt::TerminalApp::implementation
// _RemoveTab will make sure to null out the _stashed.draggedTab
_RemoveTab(*_stashed.draggedTab);
}

/// <summary>
/// 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
/// </summary>
/// <param name="profileIndex">The index for the profileMenuItem</param>
/// <returns>MenuFlyout that will show when the context is request on a profileMenuItem</returns>
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;
}

}
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalPage.h
Expand Up @@ -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);
Expand Down