1
1
using Files . Filesystem ;
2
+ using Files . Helpers ;
2
3
using Files . Interacts ;
3
4
using Files . View_Models ;
4
5
using Files . Views . Pages ;
@@ -24,6 +25,7 @@ namespace Files
24
25
public abstract class BaseLayout : Page , INotifyPropertyChanged
25
26
{
26
27
public bool IsQuickLookEnabled { get ; set ; } = false ;
28
+ public MenuFlyout BaseLayoutItemContextFlyout { get ; set ; }
27
29
28
30
public ItemViewModel AssociatedViewModel = null ;
29
31
public Interaction AssociatedInteractions = null ;
@@ -123,9 +125,40 @@ public BaseLayout()
123
125
}
124
126
}
125
127
126
- protected abstract void SetSelectedItemOnUi ( ListedItem selectedItem ) ;
128
+ protected virtual void SetSelectedItemOnUi ( ListedItem selectedItem )
129
+ {
130
+ ClearShellContextMenus ( ) ;
131
+ if ( selectedItem != null )
132
+ {
133
+
134
+ var menuFlyoutItems = new RegistryReader ( ) . GetExtensionContextMenuForFiles ( selectedItem . FileExtension ) ;
135
+ LoadMenuFlyoutItem ( menuFlyoutItems ) ;
136
+ }
127
137
128
- protected abstract void SetSelectedItemsOnUi ( List < ListedItem > selectedItems ) ;
138
+ }
139
+
140
+ private void ClearShellContextMenus ( )
141
+ {
142
+ var contextMenuItems = BaseLayoutItemContextFlyout . Items . Where ( c => c . Tag != null && ParseContextMenuTag ( c . Tag ) . commandKey != null ) ;
143
+
144
+ foreach ( var contextMenuItem in contextMenuItems )
145
+ {
146
+ BaseLayoutItemContextFlyout . Items . Remove ( contextMenuItem ) ;
147
+ }
148
+ }
149
+
150
+ protected virtual void SetSelectedItemsOnUi ( List < ListedItem > selectedItems )
151
+ {
152
+ ClearShellContextMenus ( ) ;
153
+ if ( selectedItems != null )
154
+ {
155
+ foreach ( var selectedItem in selectedItems )
156
+ {
157
+ var menuFlyoutItems = new RegistryReader ( ) . GetExtensionContextMenuForFiles ( selectedItem . FileExtension ) ;
158
+ LoadMenuFlyoutItem ( menuFlyoutItems ) ;
159
+ }
160
+ }
161
+ }
129
162
130
163
public abstract void FocusSelectedItems ( ) ;
131
164
@@ -206,6 +239,73 @@ private void UnloadMenuFlyoutItemByName(string nameToUnload)
206
239
Windows . UI . Xaml . Markup . XamlMarkupHelper . UnloadObject ( this . FindName ( nameToUnload ) as DependencyObject ) ;
207
240
}
208
241
242
+ private void LoadMenuFlyoutItem ( IEnumerable < ( string commandKey , string commandName , string commandIcon , string command ) > menuFlyoutItems )
243
+ {
244
+ foreach ( var menuFlyoutItem in menuFlyoutItems )
245
+ {
246
+ if ( BaseLayoutItemContextFlyout . Items . Any ( c => ParseContextMenuTag ( c . Tag ) . commandKey == menuFlyoutItem . commandKey ) )
247
+ {
248
+ continue ;
249
+ }
250
+
251
+ var menuLayoutItem = new MenuFlyoutItem ( )
252
+ {
253
+ Text = menuFlyoutItem . commandName ,
254
+ Tag = menuFlyoutItem
255
+ } ;
256
+ menuLayoutItem . Click += MenuLayoutItem_Click ;
257
+
258
+ BaseLayoutItemContextFlyout . Items . Add ( menuLayoutItem ) ;
259
+ }
260
+ }
261
+
262
+ private ( string commandKey , string commandName , string commandIcon , string command ) ParseContextMenuTag ( object tag )
263
+ {
264
+ if ( tag is ValueTuple < string , string , string , string > )
265
+ {
266
+ ( string commandKey , string commandName , string commandIcon , string command ) = ( ValueTuple < string , string , string , string > ) tag ;
267
+ return ( commandKey , commandName , commandIcon , command ) ;
268
+ }
269
+
270
+ return ( null , null , null , null ) ;
271
+ }
272
+
273
+ private async void MenuLayoutItem_Click ( object sender , RoutedEventArgs e )
274
+ {
275
+ var selectedFileSystemItems = ( App . CurrentInstance . ContentPage as BaseLayout ) . SelectedItems ;
276
+ var currentMenuLayoutItem = ( MenuFlyoutItem ) sender ;
277
+ if ( currentMenuLayoutItem != null )
278
+ {
279
+ var ( _, _, _, command ) = ParseContextMenuTag ( currentMenuLayoutItem . Tag ) ;
280
+ if ( selectedFileSystemItems . Count > 1 )
281
+ {
282
+ var commandsToExecute = new List < string > ( ) ;
283
+ foreach ( var selectedDataItem in selectedFileSystemItems )
284
+ {
285
+ var commandToExecute = command ? . Replace ( "%1" , selectedDataItem . ItemPath ) ;
286
+ if ( ! string . IsNullOrEmpty ( commandToExecute ) )
287
+ {
288
+ commandsToExecute . Add ( commandToExecute ) ;
289
+ }
290
+ }
291
+ if ( commandsToExecute . Count > 0 )
292
+ {
293
+ await Interaction . InvokeWin32Components ( commandsToExecute ) ;
294
+ }
295
+ }
296
+ else if ( selectedFileSystemItems . Count == 1 )
297
+ {
298
+ var selectedDataItem = selectedFileSystemItems [ 0 ] as ListedItem ;
299
+
300
+ var commandToExecute = command ? . Replace ( "%1" , selectedDataItem . ItemPath ) ;
301
+ if ( ! string . IsNullOrEmpty ( commandToExecute ) )
302
+ {
303
+ await Interaction . InvokeWin32Component ( commandToExecute ) ;
304
+ }
305
+ }
306
+ }
307
+ }
308
+
209
309
public void RightClickContextMenu_Opening ( object sender , object e )
210
310
{
211
311
var selectedFileSystemItems = ( App . CurrentInstance . ContentPage as BaseLayout ) . SelectedItems ;
0 commit comments