1
- using Files . Filesystem ;
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 ;
5
6
using System ;
6
7
using System . Collections . Generic ;
8
+ using System . Collections . Immutable ;
7
9
using System . ComponentModel ;
8
10
using System . IO ;
9
11
using System . Linq ;
10
12
using System . Runtime . CompilerServices ;
13
+ using System . Threading . Tasks ;
11
14
using Windows . ApplicationModel . DataTransfer ;
12
15
using Windows . ApplicationModel . Resources ;
13
16
using Windows . Storage ;
14
17
using Windows . System ;
15
18
using Windows . UI . Core ;
16
19
using Windows . UI . Xaml ;
17
20
using Windows . UI . Xaml . Controls ;
21
+ using Windows . UI . Xaml . Controls . Primitives ;
18
22
using Windows . UI . Xaml . Input ;
23
+ using Windows . UI . Xaml . Media ;
24
+ using Windows . UI . Xaml . Media . Imaging ;
19
25
using Windows . UI . Xaml . Navigation ;
20
26
21
27
namespace Files
@@ -28,6 +34,7 @@ public abstract class BaseLayout : Page, INotifyPropertyChanged
28
34
public SelectedItemsPropertiesViewModel SelectedItemsPropertiesViewModel { get ; }
29
35
public DirectoryPropertiesViewModel DirectoryPropertiesViewModel { get ; }
30
36
public bool IsQuickLookEnabled { get ; set ; } = false ;
37
+ public MenuFlyout BaseLayoutItemContextFlyout { get ; set ; }
31
38
32
39
public ItemViewModel AssociatedViewModel = null ;
33
40
public Interaction AssociatedInteractions = null ;
@@ -111,10 +118,6 @@ public BaseLayout()
111
118
}
112
119
}
113
120
114
- public abstract void SetSelectedItemOnUi ( ListedItem item ) ;
115
-
116
- public abstract void SetSelectedItemsOnUi ( List < ListedItem > items ) ;
117
-
118
121
public abstract void SelectAllItems ( ) ;
119
122
120
123
public abstract void InvertSelection ( ) ;
@@ -127,6 +130,42 @@ public BaseLayout()
127
130
128
131
public abstract int GetSelectedIndex ( ) ;
129
132
133
+ public abstract void SetSelectedItemOnUi ( ListedItem selectedItem ) ;
134
+ public abstract void SetSelectedItemsOnUi ( List < ListedItem > selectedItems ) ;
135
+
136
+ private void ClearShellContextMenus ( )
137
+ {
138
+ var contextMenuItems = BaseLayoutItemContextFlyout . Items . Where ( c => c . Tag != null && ParseContextMenuTag ( c . Tag ) . commandKey != null ) . ToList ( ) ;
139
+ for ( int i = 0 ; i < contextMenuItems . Count ; i ++ )
140
+ {
141
+ BaseLayoutItemContextFlyout . Items . RemoveAt ( BaseLayoutItemContextFlyout . Items . IndexOf ( contextMenuItems [ i ] ) ) ;
142
+ }
143
+ if ( BaseLayoutItemContextFlyout . Items [ 0 ] is MenuFlyoutSeparator flyoutSeperator )
144
+ {
145
+ BaseLayoutItemContextFlyout . Items . RemoveAt ( BaseLayoutItemContextFlyout . Items . IndexOf ( flyoutSeperator ) ) ;
146
+ }
147
+ }
148
+
149
+ public virtual void SetShellContextmenu ( )
150
+ {
151
+ ClearShellContextMenus ( ) ;
152
+ if ( _SelectedItems != null && _SelectedItems . Count > 0 )
153
+ {
154
+ var currentBaseLayoutItemCount = BaseLayoutItemContextFlyout . Items . Count ;
155
+ var isDirectory = ! _SelectedItems . Any ( c=> c . PrimaryItemAttribute == StorageItemTypes . File || c . PrimaryItemAttribute == StorageItemTypes . None ) ;
156
+ foreach ( var selectedItem in _SelectedItems )
157
+ {
158
+ var menuFlyoutItems = Task . Run ( ( ) => new RegistryReader ( ) . GetExtensionContextMenuForFiles ( isDirectory , selectedItem . FileExtension ) ) ;
159
+ LoadMenuFlyoutItem ( menuFlyoutItems . Result ) ;
160
+ }
161
+ var totalFlyoutItems = BaseLayoutItemContextFlyout . Items . Count - currentBaseLayoutItemCount ;
162
+ if ( totalFlyoutItems > 0 && ! ( BaseLayoutItemContextFlyout . Items [ totalFlyoutItems ] is MenuFlyoutSeparator ) )
163
+ {
164
+ BaseLayoutItemContextFlyout . Items . Insert ( totalFlyoutItems , new MenuFlyoutSeparator ( ) ) ;
165
+ }
166
+ }
167
+ }
168
+
130
169
public abstract void FocusSelectedItems ( ) ;
131
170
132
171
public abstract void StartRenameItem ( ) ;
@@ -209,8 +248,71 @@ private void UnloadMenuFlyoutItemByName(string nameToUnload)
209
248
( menuItem as MenuFlyoutItemBase ) . Visibility = Visibility . Collapsed ;
210
249
}
211
250
251
+ private void LoadMenuFlyoutItem ( IEnumerable < ( string commandKey , string commandName , string commandIcon , string command ) > menuFlyoutItems )
252
+ {
253
+ foreach ( var menuFlyoutItem in menuFlyoutItems )
254
+ {
255
+ if ( BaseLayoutItemContextFlyout . Items . Any ( c => ParseContextMenuTag ( c . Tag ) . commandKey == menuFlyoutItem . commandKey ) )
256
+ {
257
+ continue ;
258
+ }
259
+
260
+ var menuLayoutItem = new MenuFlyoutItem ( )
261
+ {
262
+ Text = menuFlyoutItem . commandName ,
263
+ Tag = menuFlyoutItem
264
+ } ;
265
+ menuLayoutItem . Click += MenuLayoutItem_Click ;
266
+
267
+ BaseLayoutItemContextFlyout . Items . Insert ( 0 , menuLayoutItem ) ;
268
+ }
269
+ }
270
+
271
+ private ( string commandKey , string commandName , string commandIcon , string command ) ParseContextMenuTag ( object tag )
272
+ {
273
+ if ( tag is ValueTuple < string , string , string , string > )
274
+ {
275
+ ( string commandKey , string commandName , string commandIcon , string command ) = ( ValueTuple < string , string , string , string > ) tag ;
276
+ return ( commandKey , commandName , commandIcon , command ) ;
277
+ }
278
+
279
+ return ( null , null , null , null ) ;
280
+ }
281
+
282
+ private async void MenuLayoutItem_Click ( object sender , RoutedEventArgs e )
283
+ {
284
+ var selectedFileSystemItems = ( App . CurrentInstance . ContentPage as BaseLayout ) . SelectedItems ;
285
+ var currentMenuLayoutItem = ( MenuFlyoutItem ) sender ;
286
+ if ( currentMenuLayoutItem != null )
287
+ {
288
+ var ( _, _, _, command ) = ParseContextMenuTag ( currentMenuLayoutItem . Tag ) ;
289
+ if ( selectedFileSystemItems . Count > 1 )
290
+ {
291
+ foreach ( var selectedDataItem in selectedFileSystemItems )
292
+ {
293
+ var commandToExecute = await new ShellCommandParser ( ) . ParseShellCommand ( command , selectedDataItem . ItemPath ) ;
294
+ if ( ! string . IsNullOrEmpty ( commandToExecute . command ) )
295
+ {
296
+ await Interaction . InvokeWin32Component ( commandToExecute . command , commandToExecute . arguments ) ;
297
+ }
298
+ }
299
+ }
300
+ else if ( selectedFileSystemItems . Count == 1 )
301
+ {
302
+ var selectedDataItem = selectedFileSystemItems [ 0 ] as ListedItem ;
303
+
304
+ var commandToExecute = await new ShellCommandParser ( ) . ParseShellCommand ( command , selectedDataItem . ItemPath ) ;
305
+ if ( ! string . IsNullOrEmpty ( commandToExecute . command ) )
306
+ {
307
+ await Interaction . InvokeWin32Component ( commandToExecute . command , commandToExecute . arguments ) ;
308
+ }
309
+ }
310
+ }
311
+ }
312
+
212
313
public void RightClickContextMenu_Opening ( object sender , object e )
213
314
{
315
+ SetShellContextmenu ( ) ;
214
316
if ( App . CurrentInstance . FilesystemViewModel . WorkingDirectory . StartsWith ( App . AppSettings . RecycleBinPath ) )
215
317
{
216
318
( this . FindName ( "EmptyRecycleBin" ) as MenuFlyoutItemBase ) . Visibility = Visibility . Visible ;
0 commit comments