@@ -74,7 +74,7 @@ bool filterMenuItemsImpl(string menuItem) => !string.IsNullOrEmpty(menuItem)
7474 return menuItemsList ;
7575 }
7676
77- public static void LoadMenuFlyoutItem ( IList < ContextMenuFlyoutItemViewModel > menuItemsListLocal ,
77+ private static void LoadMenuFlyoutItem ( IList < ContextMenuFlyoutItemViewModel > menuItemsListLocal ,
7878 ContextMenu contextMenu ,
7979 IEnumerable < Win32ContextMenuItem > menuFlyoutItems ,
8080 CancellationToken cancellationToken ,
@@ -136,7 +136,7 @@ public static void LoadMenuFlyoutItem(IList<ContextMenuFlyoutItemViewModel> menu
136136 } ;
137137 menuItemsListLocal . Insert ( 0 , menuLayoutItem ) ;
138138 }
139- else if ( ! string . IsNullOrEmpty ( menuFlyoutItem . Label ) && menuFlyoutItem . SubItems . Where ( x => x . Type != MenuItemType . MFT_SEPARATOR ) . Any ( ) )
139+ else if ( ! string . IsNullOrEmpty ( menuFlyoutItem . Label ) && menuFlyoutItem . SubItems is not null )
140140 {
141141 if ( string . Equals ( menuFlyoutItem . Label , Win32API . ExtractStringFromDLL ( "shell32.dll" , 30312 ) ) )
142142 menuFlyoutItem . CommandString = "sendto" ;
@@ -145,9 +145,23 @@ public static void LoadMenuFlyoutItem(IList<ContextMenuFlyoutItemViewModel> menu
145145 {
146146 Text = menuFlyoutItem . Label . Replace ( "&" , "" , StringComparison . Ordinal ) ,
147147 Tag = menuFlyoutItem ,
148+ BitmapIcon = image ,
148149 Items = new List < ContextMenuFlyoutItemViewModel > ( ) ,
149150 } ;
150- LoadMenuFlyoutItem ( menuLayoutSubItem . Items , contextMenu , menuFlyoutItem . SubItems , cancellationToken , showIcons ) ;
151+
152+ if ( menuFlyoutItem . SubItems . Any ( ) )
153+ {
154+ LoadMenuFlyoutItem ( menuLayoutSubItem . Items , contextMenu , menuFlyoutItem . SubItems , cancellationToken , showIcons ) ;
155+ }
156+ else
157+ {
158+ menuLayoutSubItem . LoadSubMenuAction = async ( ) =>
159+ {
160+ if ( await contextMenu . LoadSubMenu ( menuFlyoutItem . SubItems ) )
161+ LoadMenuFlyoutItem ( menuLayoutSubItem . Items , contextMenu , menuFlyoutItem . SubItems , cancellationToken , showIcons ) ;
162+ } ;
163+ }
164+
151165 menuItemsListLocal . Insert ( 0 , menuLayoutSubItem ) ;
152166 }
153167 else if ( ! string . IsNullOrEmpty ( menuFlyoutItem . Label ) )
@@ -243,37 +257,13 @@ public static async Task LoadShellMenuItems(
243257 showOpenMenu : false ,
244258 default ) ;
245259
246- if ( showOpenWithMenu )
247- {
248- var openWithItem = shellMenuItems . Where ( x => ( x . Tag as Win32ContextMenuItem ) ? . CommandString == "openas" ) . ToList ( ) . FirstOrDefault ( ) ;
249- if ( openWithItem is not null )
250- {
251- openWithItem . OpacityIcon = new OpacityIconModel ( )
252- {
253- OpacityIconStyle = "ColorIconOpenWith" ,
254- } ;
255- var ( _, openWithItems ) = ItemModelListToContextFlyoutHelper . GetAppBarItemsFromModel ( new List < ContextMenuFlyoutItemViewModel > ( ) { openWithItem } ) ;
256- var placeholder = itemContextMenuFlyout . SecondaryCommands . Where ( x => Equals ( ( x as AppBarButton ) ? . Tag , "OpenWithPlaceholder" ) ) . FirstOrDefault ( ) as AppBarButton ;
257- if ( placeholder is not null )
258- placeholder . Visibility = Visibility . Collapsed ;
259- itemContextMenuFlyout . SecondaryCommands . Insert ( 0 , openWithItems . FirstOrDefault ( ) ) ;
260- shellMenuItems . Remove ( openWithItem ) ;
261- }
262- }
260+ var openWithItem = showOpenWithMenu ? shellMenuItems . Where ( x => ( x . Tag as Win32ContextMenuItem ) ? . CommandString == "openas" ) . ToList ( ) . FirstOrDefault ( ) : null ;
261+ if ( openWithItem is not null )
262+ shellMenuItems . Remove ( openWithItem ) ;
263263
264- if ( showSendToMenu )
265- {
266- var sendToItem = shellMenuItems . Where ( x => ( x . Tag as Win32ContextMenuItem ) ? . CommandString == "sendto" ) . ToList ( ) . FirstOrDefault ( ) ;
267- if ( sendToItem is not null )
268- {
269- var ( _, sendToItems ) = ItemModelListToContextFlyoutHelper . GetAppBarItemsFromModel ( new List < ContextMenuFlyoutItemViewModel > ( ) { sendToItem } ) ;
270- var placeholder = itemContextMenuFlyout . SecondaryCommands . Where ( x => Equals ( ( x as AppBarButton ) ? . Tag , "SendToPlaceholder" ) ) . FirstOrDefault ( ) as AppBarButton ;
271- if ( placeholder is not null )
272- placeholder . Visibility = Visibility . Collapsed ;
273- itemContextMenuFlyout . SecondaryCommands . Insert ( 1 , sendToItems . FirstOrDefault ( ) ) ;
274- shellMenuItems . Remove ( sendToItem ) ;
275- }
276- }
264+ var sendToItem = showSendToMenu ? shellMenuItems . Where ( x => ( x . Tag as Win32ContextMenuItem ) ? . CommandString == "sendto" ) . ToList ( ) . FirstOrDefault ( ) : null ;
265+ if ( sendToItem is not null )
266+ shellMenuItems . Remove ( sendToItem ) ;
277267
278268 if ( ! UserSettingsService . PreferencesSettingsService . MoveShellExtensionsToSubMenu )
279269 {
@@ -317,8 +307,78 @@ public static async Task LoadShellMenuItems(
317307 overflowItem . Label = "ShowMoreOptions" . GetLocalizedResource ( ) ;
318308 overflowItem . IsEnabled = true ;
319309 }
310+
311+ // Add items to openwith dropdown
312+ if ( openWithItem is not null )
313+ {
314+ await openWithItem . LoadSubMenuAction . Invoke ( ) ;
315+
316+ openWithItem . OpacityIcon = new OpacityIconModel ( )
317+ {
318+ OpacityIconStyle = "ColorIconOpenWith" ,
319+ } ;
320+ var ( _, openWithItems ) = ItemModelListToContextFlyoutHelper . GetAppBarItemsFromModel ( new List < ContextMenuFlyoutItemViewModel > ( ) { openWithItem } ) ;
321+ var placeholder = itemContextMenuFlyout . SecondaryCommands . Where ( x => Equals ( ( x as AppBarButton ) ? . Tag , "OpenWithPlaceholder" ) ) . FirstOrDefault ( ) as AppBarButton ;
322+ if ( placeholder is not null )
323+ placeholder . Visibility = Visibility . Collapsed ;
324+ itemContextMenuFlyout . SecondaryCommands . Insert ( 0 , openWithItems . FirstOrDefault ( ) ) ;
325+ }
326+
327+ // Add items to sendto dropdown
328+ if ( sendToItem is not null )
329+ {
330+ await sendToItem . LoadSubMenuAction . Invoke ( ) ;
331+
332+ var ( _, sendToItems ) = ItemModelListToContextFlyoutHelper . GetAppBarItemsFromModel ( new List < ContextMenuFlyoutItemViewModel > ( ) { sendToItem } ) ;
333+ var placeholder = itemContextMenuFlyout . SecondaryCommands . Where ( x => Equals ( ( x as AppBarButton ) ? . Tag , "SendToPlaceholder" ) ) . FirstOrDefault ( ) as AppBarButton ;
334+ if ( placeholder is not null )
335+ placeholder . Visibility = Visibility . Collapsed ;
336+ itemContextMenuFlyout . SecondaryCommands . Insert ( 1 , sendToItems . FirstOrDefault ( ) ) ;
337+ }
338+
339+ // Add items to shell submenu
340+ shellMenuItems . Where ( x => x . LoadSubMenuAction is not null ) . ForEach ( async x => {
341+ await x . LoadSubMenuAction . Invoke ( ) ;
342+
343+ if ( ! UserSettingsService . PreferencesSettingsService . MoveShellExtensionsToSubMenu )
344+ {
345+ AddItemsToMainMenu ( itemContextMenuFlyout . SecondaryCommands , x ) ;
346+ }
347+ else if ( itemContextMenuFlyout . SecondaryCommands . FirstOrDefault ( x => x is AppBarButton appBarButton && ( appBarButton . Tag as string ) == "ItemOverflow" ) is AppBarButton overflowItem )
348+ {
349+ AddItemsToOverflowMenu ( overflowItem , x ) ;
350+ }
351+ } ) ;
320352 }
321353 catch { }
322354 }
355+
356+ public static void AddItemsToMainMenu ( IEnumerable < ICommandBarElement > mainMenu , ContextMenuFlyoutItemViewModel viewModel )
357+ {
358+ var appBarButton = mainMenu . FirstOrDefault ( x => ( x as AppBarButton ) ? . Tag == viewModel . Tag ) as AppBarButton ;
359+
360+ if ( appBarButton is not null )
361+ {
362+ var ctxFlyout = new MenuFlyout ( ) ;
363+ ItemModelListToContextFlyoutHelper . GetMenuFlyoutItemsFromModel ( viewModel . Items ) ? . ForEach ( i => ctxFlyout . Items . Add ( i ) ) ;
364+ appBarButton . Flyout = ctxFlyout ;
365+ appBarButton . Visibility = Visibility . Collapsed ;
366+ appBarButton . Visibility = Visibility . Visible ;
367+ }
368+ }
369+
370+ public static void AddItemsToOverflowMenu ( AppBarButton ? overflowItem , ContextMenuFlyoutItemViewModel viewModel )
371+ {
372+ if ( overflowItem ? . Flyout is MenuFlyout flyout )
373+ {
374+ var flyoutSubItem = flyout . Items . FirstOrDefault ( x => x . Tag == viewModel . Tag ) as MenuFlyoutSubItem ;
375+ if ( flyoutSubItem is not null )
376+ {
377+ viewModel . Items . ForEach ( i => flyoutSubItem . Items . Add ( ItemModelListToContextFlyoutHelper . GetMenuItem ( i ) ) ) ;
378+ flyout . Items [ flyout . Items . IndexOf ( flyoutSubItem ) + 1 ] . Visibility = Visibility . Collapsed ;
379+ flyoutSubItem . Visibility = Visibility . Visible ;
380+ }
381+ }
382+ }
323383 }
324384}
0 commit comments