Skip to content

Commit

Permalink
Add support for running profiles from Add tab menu as admin without a…
Browse files Browse the repository at this point in the history
… keyboard (#15679)

## Summary of the Pull Request
Add support for running profiles in the Add Tab drop down as
administrator without a keyboard.
## References and Relevant Issues
#14517 
## Detailed Description of the Pull Request / Additional comments
This pull request adds a FlyoutMenu to each Profile entry in the Add New
tab drop down. When a profile is right clicked or held for 2 seconds in
the case of no mouse input will present a MenuItem to allow the user to
click and run the selected profile as administrator
## Validation Steps Performed
- Responds to pointer input events (mouse, pointer, touchpad)
- Adjusts to theme changes.
- Only shows when a profile is selected. Will not show on settings or
pallete entries

## PR Checklist
- [x] Closes #14517 
- [ ] Tests added/passed
- [ ] Documentation updated
- If checked, please file a pull request on [our docs
repo](https://github.com/MicrosoftDocs/terminal) and link it here: #xxx
- [ ] Schema updated (if necessary)
  • Loading branch information
jamespack authored Jul 20, 2023
1 parent 648d240 commit 42e9ddc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -4959,4 +4968,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
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,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

0 comments on commit 42e9ddc

Please sign in to comment.