Skip to content

Fix: Fixed issues with recent context menu changes #11270

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

Merged
merged 1 commit into from
Feb 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Files.App/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@ public async void BaseContextFlyout_Opening(object? sender, object e)
var shellMenuItems = await ContextFlyoutItemHelper.GetBaseContextShellCommandsAsync(workingDir: ParentShellPageInstance.FilesystemViewModel.WorkingDirectory, shiftPressed: shiftPressed, showOpenMenu: false, shellContextMenuItemCancellationToken.Token);
if (shellMenuItems.Any())
AddShellItemsToMenu(shellMenuItems, BaseContextMenuFlyout, shiftPressed);
else
RemoveOverflow(BaseContextMenuFlyout);
}
}
catch (Exception error)
Expand Down Expand Up @@ -661,6 +663,8 @@ private async Task LoadMenuItemsAsync()
var shellMenuItems = await ContextFlyoutItemHelper.GetItemContextShellCommandsAsync(workingDir: ParentShellPageInstance.FilesystemViewModel.WorkingDirectory, selectedItems: SelectedItems!, shiftPressed: shiftPressed, showOpenMenu: false, shellContextMenuItemCancellationToken.Token);
if (shellMenuItems.Any())
AddShellItemsToMenu(shellMenuItems, ItemContextMenuFlyout, shiftPressed);
else
RemoveOverflow(ItemContextMenuFlyout);
}
}

Expand Down Expand Up @@ -822,6 +826,17 @@ private void AddShellItemsToMenu(List<ContextMenuFlyoutItemViewModel> shellMenuI
}
}

private void RemoveOverflow(CommandBarFlyout contextMenuFlyout)
{
var overflowItem = contextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "ItemOverflow") as AppBarButton;
var overflowSeparator = contextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarSeparator appBarSeparator && (appBarSeparator.Tag as string) == "OverflowSeparator") as AppBarSeparator;

if (overflowItem is not null)
overflowItem.Visibility = Visibility.Collapsed;
if (overflowSeparator is not null)
overflowSeparator.Visibility = Visibility.Collapsed;
}

protected virtual void Page_CharacterReceived(UIElement sender, CharacterReceivedRoutedEventArgs args)
{
if (ParentShellPageInstance!.IsCurrentInstance)
Expand Down
3 changes: 2 additions & 1 deletion src/Files.App/Helpers/ItemModelListToContextFlyoutHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Files.App.UserControls;
using Files.App.ViewModels;
using Files.Shared.Extensions;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
Expand Down Expand Up @@ -30,7 +31,7 @@ public static (List<ICommandBarElement> primaryElements, List<ICommandBarElement
var primaryModels = items.Where(i => i.IsPrimary).ToList();
var secondaryModels = items.Except(primaryModels).ToList();

if (secondaryModels.Last().ItemType is ItemType.Separator)
if (!secondaryModels.IsEmpty() && secondaryModels.Last().ItemType is ItemType.Separator)
secondaryModels.RemoveAt(secondaryModels.Count - 1);

var primary = new List<ICommandBarElement>();
Expand Down
34 changes: 21 additions & 13 deletions src/Files.App/Helpers/ShellContextMenuHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,33 +261,41 @@ public static async Task LoadShellMenuItems(string path, CommandBarFlyout itemCo
if (!UserSettingsService.AppearanceSettingsService.MoveShellExtensionsToSubMenu)
{
var (_, secondaryElements) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(shellMenuItems);
if (!secondaryElements.Any())
return;
if (secondaryElements.Any())
{
var openedPopups = Microsoft.UI.Xaml.Media.VisualTreeHelper.GetOpenPopups(App.Window);
var secondaryMenu = openedPopups.FirstOrDefault(popup => popup.Name == "OverflowPopup");

var openedPopups = Microsoft.UI.Xaml.Media.VisualTreeHelper.GetOpenPopups(App.Window);
var secondaryMenu = openedPopups.FirstOrDefault(popup => popup.Name == "OverflowPopup");
var itemsControl = secondaryMenu?.Child.FindDescendant<ItemsControl>();
if (itemsControl is not null)
{
var maxWidth = itemsControl.ActualWidth - Constants.UI.ContextMenuLabelMargin;
secondaryElements.OfType<FrameworkElement>()
.ForEach(x => x.MaxWidth = maxWidth); // Set items max width to current menu width (#5555)
}

var itemsControl = secondaryMenu?.Child.FindDescendant<ItemsControl>();
if (itemsControl is not null)
secondaryElements.ForEach(i => itemContextMenuFlyout.SecondaryCommands.Add(i));
}
else if (itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarSeparator appBarSeparator && (appBarSeparator.Tag as string) == "OverflowSeparator") is AppBarSeparator overflowSeparator)
{
var maxWidth = itemsControl.ActualWidth - Constants.UI.ContextMenuLabelMargin;
secondaryElements.OfType<FrameworkElement>()
.ForEach(x => x.MaxWidth = maxWidth); // Set items max width to current menu width (#5555)
overflowSeparator.Visibility = Visibility.Collapsed;
}
itemContextMenuFlyout.SecondaryCommands.Add(new AppBarSeparator());
secondaryElements.ForEach(i => itemContextMenuFlyout.SecondaryCommands.Add(i));

if (itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "ItemOverflow") is AppBarButton overflowItem)
overflowItem.Visibility = Visibility.Collapsed;
}
else
{
var overflowItems = ItemModelListToContextFlyoutHelper.GetMenuFlyoutItemsFromModel(shellMenuItems);
if (itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "ItemOverflow") is not AppBarButton overflowItem)
if (itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarButton appBarButton && (appBarButton.Tag as string) == "ItemOverflow") is not AppBarButton overflowItem
|| itemContextMenuFlyout.SecondaryCommands.FirstOrDefault(x => x is AppBarSeparator appBarSeparator && (appBarSeparator.Tag as string) == "OverflowSeparator") is not AppBarSeparator overflowSeparator)
return;

var flyoutItems = (overflowItem.Flyout as MenuFlyout)?.Items;
if (flyoutItems is not null)
overflowItems.ForEach(i => flyoutItems.Add(i));
overflowItem.Visibility = overflowItems.Any() ? Visibility.Visible : Visibility.Collapsed;
overflowSeparator.Visibility = overflowItems.Any() ? Visibility.Visible : Visibility.Collapsed;

overflowItem.Label = "ShowMoreOptions".GetLocalizedResource();
overflowItem.IsEnabled = true;
Expand Down
4 changes: 2 additions & 2 deletions src/Files.App/ServicesImplementation/QuickAccessService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ await SafetyExtensions.IgnoreExceptions(async () => {
App.QuickAccessManager.UpdateQuickAccessWidget?.Invoke(this, new ModifyQuickAccessEventArgs(folderPaths, false));
}

public bool IsItemPinned(ILocatableFolder folder)
public bool IsItemPinned(string folderPath)
{
return App.QuickAccessManager.Model.FavoriteItems.Contains(folder.Path);
return App.QuickAccessManager.Model.FavoriteItems.Contains(folderPath);
}
}
}
6 changes: 6 additions & 0 deletions src/Files.App/UserControls/SidebarControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ private List<ContextMenuFlyoutItemViewModel> GetLocationItemMenuItems(INavigatio
ShowItem = options.ShowProperties
},
new ContextMenuFlyoutItemViewModel()
{
ItemType = ItemType.Separator,
Tag = "OverflowSeparator",
IsHidden = !options.ShowShellItems,
},
new ContextMenuFlyoutItemViewModel()
{
Text = "LoadingMoreOptions".GetLocalizedResource(),
Glyph = "\xE712",
Expand Down
5 changes: 5 additions & 0 deletions src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
CommandParameter = item
},
new ContextMenuFlyoutItemViewModel()
{
ItemType = ItemType.Separator,
Tag = "OverflowSeparator",
},
new ContextMenuFlyoutItemViewModel()
{
Text = "LoadingMoreOptions".GetLocalizedResource(),
Glyph = "\xE712",
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/UserControls/Widgets/HomePageWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async void Button_RightTapped(object sender, RightTappedRoutedEventArgs e
if (sender is not Button widgetCardItem || widgetCardItem.DataContext is not WidgetCardItem item)
return;

var menuItems = GetItemMenuItems(item, QuickAccessService.IsItemPinned(await StorageService.GetFolderFromPathAsync(item.Path)));
var menuItems = GetItemMenuItems(item, QuickAccessService.IsItemPinned(item.Path));
var (_, secondaryElements) = ItemModelListToContextFlyoutHelper.GetAppBarItemsFromModel(menuItems);

if (!UserSettingsService.AppearanceSettingsService.MoveShellExtensionsToSubMenu)
Expand Down
5 changes: 5 additions & 0 deletions src/Files.App/UserControls/Widgets/QuickAccessWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
CommandParameter = item
},
new ContextMenuFlyoutItemViewModel()
{
ItemType = ItemType.Separator,
Tag = "OverflowSeparator",
},
new ContextMenuFlyoutItemViewModel()
{
Text = "LoadingMoreOptions".GetLocalizedResource(),
Glyph = "\xE712",
Expand Down
7 changes: 6 additions & 1 deletion src/Files.App/UserControls/Widgets/RecentFilesWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private async void Grid_RightTapped(object sender, RightTappedRoutedEventArgs e)
secondaryElements.ForEach(i => itemContextMenuFlyout.SecondaryCommands.Add(i));
itemContextMenuFlyout.ShowAt(recentItemsGrid, new FlyoutShowOptions { Position = e.GetPosition(recentItemsGrid) });

await ShellContextmenuHelper.LoadShellMenuItems(item.Path, itemContextMenuFlyout, showOpenWithMenu: true);
await ShellContextmenuHelper.LoadShellMenuItems(item.Path, itemContextMenuFlyout);

e.Handled = true;
}
Expand Down Expand Up @@ -152,6 +152,11 @@ public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCard
CommandParameter = item
},
new ContextMenuFlyoutItemViewModel()
{
ItemType = ItemType.Separator,
Tag = "OverflowSeparator",
},
new ContextMenuFlyoutItemViewModel()
{
Text = "LoadingMoreOptions".GetLocalizedResource(),
Glyph = "\xE712",
Expand Down
2 changes: 1 addition & 1 deletion src/Files.Backend/Services/IQuickAccessService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public interface IQuickAccessService
/// </summary>
/// <param name="folderPath">The path of the folder</param>
/// <returns>true if the item is pinned</returns>
bool IsItemPinned(ILocatableFolder folder);
bool IsItemPinned(string folderPath);
}
}