13
13
using System ;
14
14
using System . Collections . Generic ;
15
15
using System . IO ;
16
+ using System . Linq ;
16
17
17
18
namespace GeonBit . UI . Utils
18
19
{
@@ -260,6 +261,81 @@ public static MessageBoxHandle ShowMsgBox(string header, string text, string clo
260
261
} , size : size ?? DefaultMsgBoxSize , extraEntities : extraEntities , onDone : onDone ) ;
261
262
}
262
263
264
+ /// <summary>
265
+ /// How to sort files when presenting them in files dialog.
266
+ /// </summary>
267
+ public enum FilesSortingMode
268
+ {
269
+ /// <summary>
270
+ /// Files will be presented in whatever order the OS returns them.
271
+ /// </summary>
272
+ Unsorted ,
273
+
274
+ /// <summary>
275
+ /// Sort files by name, ascending (0 to 9, A to Z).
276
+ /// </summary>
277
+ ByNameAscending ,
278
+
279
+ /// <summary>
280
+ /// Sort files by name, descending (9 to 0, Z to A).
281
+ /// </summary>
282
+ ByNameDescending ,
283
+
284
+ /// <summary>
285
+ /// Sort files by creation time, ascending.
286
+ /// </summary>
287
+ ByCreationDateAscending ,
288
+
289
+ /// <summary>
290
+ /// Sort files by creation time, descending.
291
+ /// </summary>
292
+ ByCreationDateDescending ,
293
+
294
+ /// <summary>
295
+ /// Sort files by modify time, ascending.
296
+ /// </summary>
297
+ ByModifyDateAscending ,
298
+
299
+ /// <summary>
300
+ /// Sort files by modify time, descending.
301
+ /// </summary>
302
+ ByModifyDateDescending ,
303
+ }
304
+
305
+ /// <summary>
306
+ /// Sort list of files / folder names.
307
+ /// </summary>
308
+ private static void _SortFilesList ( ref List < string > filenames , string path , FilesSortingMode sorting )
309
+ {
310
+ switch ( sorting )
311
+ {
312
+ case FilesSortingMode . ByNameAscending :
313
+ filenames . Sort ( ) ;
314
+ break ;
315
+
316
+ case FilesSortingMode . ByNameDescending :
317
+ filenames . Sort ( ) ;
318
+ filenames . Reverse ( ) ;
319
+ break ;
320
+
321
+ case FilesSortingMode . ByCreationDateAscending :
322
+ filenames = filenames . OrderBy ( x => new FileInfo ( Path . Combine ( path , x ) ) . CreationTime ) . ToList ( ) ;
323
+ break ;
324
+
325
+ case FilesSortingMode . ByCreationDateDescending :
326
+ filenames = filenames . OrderByDescending ( x => new FileInfo ( Path . Combine ( path , x ) ) . CreationTime ) . ToList ( ) ;
327
+ break ;
328
+
329
+ case FilesSortingMode . ByModifyDateAscending :
330
+ filenames = filenames . OrderBy ( x => new FileInfo ( Path . Combine ( path , x ) ) . LastWriteTime ) . ToList ( ) ;
331
+ break ;
332
+
333
+ case FilesSortingMode . ByModifyDateDescending :
334
+ filenames = filenames . OrderByDescending ( x => new FileInfo ( Path . Combine ( path , x ) ) . LastWriteTime ) . ToList ( ) ;
335
+ break ;
336
+ }
337
+ }
338
+
263
339
/// <summary>
264
340
/// Open a dialog to select file for saving.
265
341
/// </summary>
@@ -274,8 +350,9 @@ public static MessageBoxHandle ShowMsgBox(string header, string text, string clo
274
350
/// <param name="saveButtonTxt">String to show on the save file button.</param>
275
351
/// <param name="cancelButtonTxt">String to show on the cancel button.</param>
276
352
/// <param name="overrideWarning">If not null, will show this warning in a Yes/No prompt if the user tries to select an existing file.</param>
353
+ /// <param name="sorting">How to sort files.</param>
277
354
/// <returns>Message box handle.</returns>
278
- public static MessageBoxHandle OpenSaveFileDialog ( string path , Func < FileDialogResponse , bool > onSelected , Action onCancel = null ! , FileDialogOptions options = FileDialogOptions . Default , Func < string , bool > filterFiles = null ! , Func < string , bool > filterFolders = null ! , string title = "Save File As.." , string message = null ! , string saveButtonTxt = "Save File" , string cancelButtonTxt = "Cancel" , string overrideWarning = "File '<filename>' already exists!\n Are you sure you want to override it?" )
355
+ public static MessageBoxHandle OpenSaveFileDialog ( string path , Func < FileDialogResponse , bool > onSelected , Action onCancel = null ! , FileDialogOptions options = FileDialogOptions . Default , Func < string , bool > filterFiles = null ! , Func < string , bool > filterFolders = null ! , string title = "Save File As.." , string message = null ! , string saveButtonTxt = "Save File" , string cancelButtonTxt = "Cancel" , string overrideWarning = "File '<filename>' already exists!\n Are you sure you want to override it?" , FilesSortingMode sorting = FilesSortingMode . Unsorted )
279
356
{
280
357
// current path
281
358
var currPath = string . IsNullOrEmpty ( path ) ? Path . GetFullPath ( Directory . GetCurrentDirectory ( ) ) : Path . GetFullPath ( path ) ;
@@ -346,25 +423,37 @@ void UpdateFilesList()
346
423
}
347
424
348
425
// add folders
426
+ List < string > folders = new List < string > ( ) ;
349
427
foreach ( var dir in Directory . GetDirectories ( currPath ) )
350
428
{
351
429
if ( filterFolders == null || filterFolders ( dir ) )
352
430
{
353
- filesList . AddItem ( Path . GetFileName ( dir ) ) ;
354
- filesList . SetIcon ( "textures/folder_icon" , filesList . Count - 1 ) ;
431
+ folders . Add ( Path . GetFileName ( dir ) ) ;
355
432
}
356
433
}
434
+ _SortFilesList ( ref folders , currPath , sorting ) ;
435
+ foreach ( var dir in folders )
436
+ {
437
+ filesList . AddItem ( dir ) ;
438
+ filesList . SetIcon ( "textures/folder_icon" , filesList . Count - 1 ) ;
439
+ }
357
440
}
358
441
359
442
// add files
443
+ List < string > files = new List < string > ( ) ;
360
444
foreach ( var file in Directory . GetFiles ( currPath ) )
361
445
{
362
446
if ( filterFiles == null || filterFiles ( file ) )
363
447
{
364
- filesList . AddItem ( Path . GetFileName ( file ) ) ;
365
- filesList . SetIcon ( "textures/file_icon" , filesList . Count - 1 ) ;
448
+ files . Add ( Path . GetFileName ( file ) ) ;
366
449
}
367
450
}
451
+ _SortFilesList ( ref files , currPath , sorting ) ;
452
+ foreach ( var file in files )
453
+ {
454
+ filesList . AddItem ( file ) ;
455
+ filesList . SetIcon ( "textures/file_icon" , filesList . Count - 1 ) ;
456
+ }
368
457
}
369
458
370
459
// click on files list - check if enter or exit folder
@@ -520,11 +609,12 @@ void UpdateFilesList()
520
609
/// <param name="message">Optional message to show above files.</param>
521
610
/// <param name="loadButtonTxt">String to show on the load file button.</param>
522
611
/// <param name="cancelButtonTxt">String to show on the cancel button.</param>
612
+ /// <param name="sorting">How to sort files.</param>
523
613
/// <returns>Message box handle.</returns>
524
- public static MessageBoxHandle OpenLoadFileDialog ( string path , Func < FileDialogResponse , bool > onSelected , Action onCancel = null ! , FileDialogOptions options = FileDialogOptions . Default , Func < string , bool > filterFiles = null ! , Func < string , bool > filterFolders = null ! , string title = "Open File.." , string message = null ! , string loadButtonTxt = "Open File" , string cancelButtonTxt = "Cancel" )
614
+ public static MessageBoxHandle OpenLoadFileDialog ( string path , Func < FileDialogResponse , bool > onSelected , Action onCancel = null ! , FileDialogOptions options = FileDialogOptions . Default , Func < string , bool > filterFiles = null ! , Func < string , bool > filterFolders = null ! , string title = "Open File.." , string message = null ! , string loadButtonTxt = "Open File" , string cancelButtonTxt = "Cancel" , FilesSortingMode sorting = FilesSortingMode . Unsorted )
525
615
{
526
616
options |= FileDialogOptions . MustSelectExistingFile ;
527
- return OpenSaveFileDialog ( path , onSelected , onCancel , options , filterFiles , filterFolders , title , message , loadButtonTxt , cancelButtonTxt , null ) ;
617
+ return OpenSaveFileDialog ( path , onSelected , onCancel , options , filterFiles , filterFolders , title , message , loadButtonTxt , cancelButtonTxt , null , sorting ) ;
528
618
}
529
619
}
530
620
0 commit comments