-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathFileBrowser.cs
More file actions
3012 lines (2499 loc) · 86.8 KB
/
FileBrowser.cs
File metadata and controls
3012 lines (2499 loc) · 86.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#define WIN_DIR_CHECK_WITHOUT_TIMEOUT // When uncommented, Directory.Exists won't be wrapped inside a Task/Thread on Windows but we won't be able to set a timeout for unreachable directories/drives
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.InputSystem;
#endif
namespace SimpleFileBrowser
{
public class FileBrowser : MonoBehaviour, IListViewAdapter
{
public enum Permission { Denied = 0, Granted = 1, ShouldAsk = 2 };
public enum PickMode { Files = 0, Folders = 1, FilesAndFolders = 2 };
#region Structs
#pragma warning disable 0649
[Serializable]
private struct FiletypeIcon
{
public string extension;
public Sprite icon;
}
[Serializable]
private struct QuickLink
{
#if UNITY_EDITOR || ( !UNITY_WSA && !UNITY_WSA_10_0 )
public Environment.SpecialFolder target;
#endif
public string name;
public Sprite icon;
}
#pragma warning restore 0649
#endregion
#region Inner Classes
public interface IFilter {
/// <summary>Default extension for this filter.</summary>
string defaultExtension { get; }
/// <summary>'false' when some extensions have multiple suffixes like ".tar.gz"</summary>
bool allExtensionsHaveSingleSuffix { get; }
/// <summary>
/// Returns true if this filter is the default 'all files' filter. All custom filters should return false here.
/// </summary>
bool isAllFilesFilter { get; }
/// <summary>Returns true if this is one of the supported extensions for this filter.</summary>
bool isValidExtension( string extension );
/// <summary>
/// Returns true if the filter matches the given <see cref="extension"/>.
/// </summary>
/// <param name="extension">a file extension, like '.tar' or '.tar.gz'</param>
/// <param name="extensionMayHaveMultipleSuffixes">
/// true if <see cref="extension"/> may have multiple suffixes, like '.tar.gz'
/// </param>
bool MatchesExtension( string extension, bool extensionMayHaveMultipleSuffixes );
/// <summary>Shown in the user interface.</summary>
string ToString();
}
public class Filter : IFilter
{
public readonly string name;
public readonly string[] extensions;
public readonly HashSet<string> extensionsSet;
public string defaultExtension { get; private set; }
public bool allExtensionsHaveSingleSuffix { get; private set; }
internal Filter( string name )
{
this.name = name;
extensions = null;
extensionsSet = null;
defaultExtension = null;
allExtensionsHaveSingleSuffix = true;
}
public Filter( string name, string extension )
{
this.name = name;
extension = extension.ToLowerInvariant();
if( extension[0] != '.' )
extension = "." + extension;
extensions = new string[1] { extension };
extensionsSet = new HashSet<string>() { extension };
defaultExtension = extension;
allExtensionsHaveSingleSuffix = ( extension.LastIndexOf( '.' ) == 0 );
}
public Filter( string name, params string[] extensions )
{
this.name = name;
allExtensionsHaveSingleSuffix = true;
for( int i = 0; i < extensions.Length; i++ )
{
extensions[i] = extensions[i].ToLowerInvariant();
if( extensions[i][0] != '.' )
extensions[i] = "." + extensions[i];
allExtensionsHaveSingleSuffix &= ( extensions[i].LastIndexOf( '.' ) == 0 );
}
this.extensions = extensions;
extensionsSet = new HashSet<string>( extensions );
defaultExtension = extensions[0];
}
public bool isAllFilesFilter
{
get
{
return extensions == null;
}
}
public bool isValidExtension( string extension )
{
return extensionsSet != null && extensionsSet.Contains( extension );
}
public bool MatchesExtension( string extension, bool extensionMayHaveMultipleSuffixes )
{
if( extensionsSet == null || extensionsSet.Contains( extension ) )
return true;
// When the provided extension may have multiple suffixes (e.g. ".tar.gz"), check if it ends with any of the
// extensions in this filter (e.g. return true when this Filter has ".gz" and the provided extension is ".tar.gz")
if( extensionMayHaveMultipleSuffixes )
{
for( int i = 0; i < extensions.Length; i++ )
{
if( extension.EndsWith( extensions[i], StringComparison.Ordinal ) )
{
extensionsSet.Add( extension );
return true;
}
}
}
return false;
}
public override string ToString()
{
string result = string.Empty;
if( name != null )
result += name;
if( extensions != null )
{
if( name != null )
result += " (";
for( int i = 0; i < extensions.Length; i++ )
{
if( i > 0 )
result += ", " + extensions[i];
else
result += extensions[i];
}
if( name != null )
result += ")";
}
return result;
}
}
#endregion
#region Constants
private const int FILENAME_INPUT_FIELD_MAX_FILE_COUNT = 7;
private const string SAF_PICK_FOLDER_QUICK_LINK_PATH = "SAF_PICK_FOLDER";
#endregion
#region Static Variables
public static bool IsOpen { get; private set; }
public static bool Success { get; private set; }
public static string[] Result { get; private set; }
[SerializeField]
private UISkin m_skin;
#if UNITY_EDITOR
private UISkin prevSkin;
#endif
private int m_skinVersion = 0;
private Sprite m_skinPrevDriveIcon, m_skinPrevFolderIcon;
public static UISkin Skin
{
get { return Instance.m_skin; }
set
{
if( value && Instance.m_skin != value )
{
Instance.m_skin = value;
Instance.m_skinVersion = Instance.m_skin.Version;
Instance.RefreshSkin();
}
}
}
private static bool m_askPermissions = true;
public static bool AskPermissions
{
get { return m_askPermissions; }
set { m_askPermissions = value; }
}
private static bool m_singleClickMode = false;
public static bool SingleClickMode
{
get { return m_singleClickMode; }
set { m_singleClickMode = value; }
}
private static FileSystemEntryFilter m_displayedEntriesFilter;
public static event FileSystemEntryFilter DisplayedEntriesFilter
{
add
{
m_displayedEntriesFilter -= value;
m_displayedEntriesFilter += value;
if( m_instance )
{
m_instance.PersistFileEntrySelection();
m_instance.RefreshFiles( false );
}
}
remove
{
m_displayedEntriesFilter -= value;
if( m_instance )
{
m_instance.PersistFileEntrySelection();
m_instance.RefreshFiles( false );
}
}
}
#if UNITY_EDITOR || ( !UNITY_ANDROID && !UNITY_IOS && !UNITY_WSA && !UNITY_WSA_10_0 )
private static float m_drivesRefreshInterval = 5f;
#else
private static float m_drivesRefreshInterval = -1f;
#endif
public static float DrivesRefreshInterval
{
get { return m_drivesRefreshInterval; }
set { m_drivesRefreshInterval = value; }
}
public static bool ShowHiddenFiles
{
get { return Instance.showHiddenFilesToggle.isOn; }
set { Instance.showHiddenFilesToggle.isOn = value; }
}
private static bool m_displayHiddenFilesToggle = true;
public static bool DisplayHiddenFilesToggle
{
get { return m_displayHiddenFilesToggle; }
set
{
if( m_displayHiddenFilesToggle != value )
{
m_displayHiddenFilesToggle = value;
if( m_instance )
{
if( !value )
m_instance.showHiddenFilesToggle.gameObject.SetActive( false );
else if( m_instance.windowTR.sizeDelta.x >= m_instance.narrowScreenWidth )
{
#if !UNITY_EDITOR && UNITY_ANDROID
if( !FileBrowserHelpers.ShouldUseSAF )
#endif
m_instance.showHiddenFilesToggle.gameObject.SetActive( true );
}
}
}
}
}
private static string m_allFilesFilterText = "All Files (.*)";
public static string AllFilesFilterText
{
get { return m_allFilesFilterText; }
set
{
if( m_allFilesFilterText != value )
{
string oldValue = m_allFilesFilterText;
m_allFilesFilterText = value;
if( m_instance )
{
IFilter oldAllFilesFilter = m_instance.allFilesFilter;
m_instance.allFilesFilter = new Filter( value );
if( m_instance.filters.Count > 0 && m_instance.filters[0] == oldAllFilesFilter )
m_instance.filters[0] = m_instance.allFilesFilter;
if( m_instance.filtersDropdown.options[0].text == oldValue )
m_instance.filtersDropdown.options[0].text = value;
}
}
}
}
private static string m_foldersFilterText = "Folders";
public static string FoldersFilterText
{
get { return m_foldersFilterText; }
set
{
if( m_foldersFilterText != value )
{
string oldValue = m_foldersFilterText;
m_foldersFilterText = value;
if( m_instance && m_instance.filtersDropdown.options[0].text == oldValue )
m_instance.filtersDropdown.options[0].text = value;
}
}
}
private static string m_pickFolderQuickLinkText = "Browse...";
public static string PickFolderQuickLinkText
{
get { return m_pickFolderQuickLinkText; }
set
{
if( m_pickFolderQuickLinkText != value )
{
m_pickFolderQuickLinkText = value;
if( m_instance )
{
for( int i = 0; i < m_instance.allQuickLinks.Count; i++ )
{
FileBrowserQuickLink quickLink = m_instance.allQuickLinks[i];
if( quickLink && quickLink.TargetPath == SAF_PICK_FOLDER_QUICK_LINK_PATH )
{
quickLink.SetQuickLink( Skin.DriveIcon, value, SAF_PICK_FOLDER_QUICK_LINK_PATH );
break;
}
}
}
}
}
}
private static FileBrowser m_instance = null;
private static FileBrowser Instance
{
get
{
if( !m_instance )
{
m_instance = Instantiate( Resources.Load<GameObject>( "SimpleFileBrowserCanvas" ) ).GetComponent<FileBrowser>();
DontDestroyOnLoad( m_instance.gameObject );
m_instance.gameObject.SetActive( false );
}
return m_instance;
}
}
#endregion
#region Variables
#pragma warning disable 0649
[Header( "Settings" )]
[SerializeField]
internal int minWidth = 380;
[SerializeField]
internal int minHeight = 300;
[SerializeField]
private float narrowScreenWidth = 380f;
[SerializeField]
private float quickLinksMaxWidthPercentage = 0.4f;
[SerializeField]
private bool sortFilesByName = true;
[SerializeField, UnityEngine.Serialization.FormerlySerializedAs( "excludeExtensions" )]
private string[] excludedExtensions;
#pragma warning disable 0414
[SerializeField]
private QuickLink[] quickLinks;
private static bool quickLinksInitialized;
#pragma warning restore 0414
private readonly HashSet<string> excludedExtensionsSet = new HashSet<string>();
[SerializeField]
private bool generateQuickLinksForDrives = true;
[SerializeField]
private bool contextMenuShowDeleteButton = true;
[SerializeField]
private bool contextMenuShowRenameButton = true;
[SerializeField]
private bool showResizeCursor = true;
[Header( "Internal References" )]
[SerializeField]
private FileBrowserMovement window;
private RectTransform windowTR;
[SerializeField]
private RectTransform topViewNarrowScreen;
[SerializeField]
private RectTransform middleView;
private Vector2 middleViewOriginalPosition;
private Vector2 middleViewOriginalSize;
[SerializeField]
private RectTransform middleViewQuickLinks;
private Vector2 middleViewQuickLinksOriginalSize;
[SerializeField]
private RectTransform middleViewFiles;
[SerializeField]
private RectTransform middleViewSeparator;
[SerializeField]
private FileBrowserItem itemPrefab;
private readonly List<FileBrowserItem> allItems = new List<FileBrowserItem>( 16 );
[SerializeField]
private FileBrowserQuickLink quickLinkPrefab;
private readonly List<FileBrowserQuickLink> allQuickLinks = new List<FileBrowserQuickLink>( 8 );
[SerializeField]
private Text titleText;
[SerializeField]
private Button backButton;
[SerializeField]
private Button forwardButton;
[SerializeField]
private Button upButton;
[SerializeField]
private Button moreOptionsButton;
[SerializeField]
private InputField pathInputField;
[SerializeField]
private RectTransform pathInputFieldSlotTop;
[SerializeField]
private RectTransform pathInputFieldSlotBottom;
[SerializeField]
private InputField searchInputField;
[SerializeField]
private RectTransform quickLinksContainer;
[SerializeField]
private ScrollRect quickLinksScrollRect;
[SerializeField]
private RectTransform filesContainer;
[SerializeField]
private ScrollRect filesScrollRect;
[SerializeField]
private RecycledListView listView;
[SerializeField]
private InputField filenameInputField;
[SerializeField]
private Text filenameInputFieldOverlayText;
[SerializeField]
private Image filenameImage;
[SerializeField]
private Dropdown filtersDropdown;
[SerializeField]
private RectTransform filtersDropdownContainer;
[SerializeField]
private Text filterItemTemplate;
[SerializeField]
private Toggle showHiddenFilesToggle;
[SerializeField]
private Text submitButtonText;
[SerializeField]
private Button[] allButtons;
[SerializeField]
private RectTransform moreOptionsContextMenuPosition;
[SerializeField]
private FileBrowserRenamedItem renameItem;
[SerializeField]
private FileBrowserContextMenu contextMenu;
[SerializeField]
private FileBrowserDeleteConfirmationPanel deleteConfirmationPanel;
[SerializeField]
private FileBrowserCursorHandler resizeCursorHandler;
#pragma warning restore 0649
internal RectTransform rectTransform;
private Canvas canvas;
private FileAttributes ignoredFileAttributes = FileAttributes.System;
private FileSystemEntry[] allFileEntries;
private readonly List<FileSystemEntry> validFileEntries = new List<FileSystemEntry>();
private readonly List<int> selectedFileEntries = new List<int>( 4 );
private readonly List<string> pendingFileEntrySelection = new List<string>();
#pragma warning disable 0414 // Value is assigned but never used on Android & iOS
private int multiSelectionPivotFileEntry;
#pragma warning restore 0414
private StringBuilder multiSelectionFilenameBuilder;
private readonly List<IFilter> filters = new List<IFilter>();
private IFilter allFilesFilter;
private bool showAllFilesFilter = true;
// Single suffix: ".mp4", ".txt", etc.
// Multiple suffixes: ".tar.gz", etc.
private bool allFiltersHaveSingleSuffix = true;
private bool allExcludedExtensionsHaveSingleSuffix = true;
// When its value is 'true', file extensions will be handled in a more optimized way
private bool AllExtensionsHaveSingleSuffix { get { return allFiltersHaveSingleSuffix && allExcludedExtensionsHaveSingleSuffix && m_skin.AllIconExtensionsHaveSingleSuffix; } }
private string defaultInitialPath;
private int currentPathIndex = -1;
private readonly List<string> pathsFollowed = new List<string>();
private HashSet<char> invalidFilenameChars;
private float drivesNextRefreshTime;
#if !UNITY_EDITOR && UNITY_ANDROID
private string driveQuickLinks;
#else
private string[] driveQuickLinks;
#endif
private int numberOfDriveQuickLinks;
#if !WIN_DIR_CHECK_WITHOUT_TIMEOUT && ( UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN )
private readonly List<string> timedOutDirectoryExistsRequests = new List<string>( 2 );
#endif
private bool canvasDimensionsChanged;
private readonly CompareInfo textComparer = new CultureInfo( "en-US" ).CompareInfo;
private readonly CompareOptions textCompareOptions = CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace;
// Required in RefreshFiles() function
private PointerEventData nullPointerEventData;
#endregion
#region Properties
private string m_currentPath = string.Empty;
private string CurrentPath
{
get { return m_currentPath; }
set
{
if( value != null )
{
value = value.Trim();
#if !UNITY_EDITOR && UNITY_ANDROID
if( !FileBrowserHelpers.ShouldUseSAFForPath( value ) )
#endif
value = GetPathWithoutTrailingDirectorySeparator( value );
}
if( string.IsNullOrEmpty( value ) )
{
pathInputField.text = m_currentPath;
return;
}
if( m_currentPath != value )
{
if( !FileBrowserHelpers.DirectoryExists( value ) )
{
pathInputField.text = m_currentPath;
return;
}
m_currentPath = value;
pathInputField.text = m_currentPath;
if( currentPathIndex == -1 || pathsFollowed[currentPathIndex] != m_currentPath )
{
currentPathIndex++;
if( currentPathIndex < pathsFollowed.Count )
{
pathsFollowed[currentPathIndex] = value;
for( int i = pathsFollowed.Count - 1; i >= currentPathIndex + 1; i-- )
pathsFollowed.RemoveAt( i );
}
else
pathsFollowed.Add( m_currentPath );
}
backButton.interactable = currentPathIndex > 0;
forwardButton.interactable = currentPathIndex < pathsFollowed.Count - 1;
#if !UNITY_EDITOR && UNITY_ANDROID
if( FileBrowserHelpers.ShouldUseSAF )
{
string parentPath = FileBrowserHelpers.GetDirectoryName( m_currentPath );
upButton.interactable = !string.IsNullOrEmpty( parentPath ) && ( FileBrowserHelpers.ShouldUseSAFForPath( parentPath ) || FileBrowserHelpers.DirectoryExists( parentPath ) ); // DirectoryExists: Directory may not be accessible on Android 10+, this function checks that
}
else
#endif
{
try // When "C:/" or "C:" is typed instead of "C:\", an exception is thrown
{
upButton.interactable = Directory.GetParent( m_currentPath ) != null;
}
catch
{
upButton.interactable = false;
}
}
m_searchString = string.Empty;
searchInputField.text = m_searchString;
multiSelectionPivotFileEntry = 0;
filesScrollRect.verticalNormalizedPosition = 1;
filenameImage.color = m_skin.InputFieldNormalBackgroundColor;
if( m_pickerMode != PickMode.Files )
{
filenameInputField.text = string.Empty;
filenameInputField.interactable = true;
}
// If a quick link points to this directory, highlight it
#if !UNITY_EDITOR && UNITY_ANDROID
// Path strings aren't deterministic on Storage Access Framework but the paths' absolute parts usually are
if( FileBrowserHelpers.ShouldUseSAFForPath( m_currentPath ) )
{
int SAFAbsolutePathSeparatorIndex = m_currentPath.LastIndexOf( '/' );
if( SAFAbsolutePathSeparatorIndex >= 0 )
{
string absoluteSAFPath = m_currentPath.Substring( SAFAbsolutePathSeparatorIndex );
for( int i = 0; i < allQuickLinks.Count; i++ )
allQuickLinks[i].SetSelected( allQuickLinks[i].TargetPath == m_currentPath || allQuickLinks[i].TargetPath.EndsWith( absoluteSAFPath ) );
}
else
{
for( int i = 0; i < allQuickLinks.Count; i++ )
allQuickLinks[i].SetSelected( allQuickLinks[i].TargetPath == m_currentPath );
}
}
else
#endif
{
for( int i = 0; i < allQuickLinks.Count; i++ )
allQuickLinks[i].SetSelected( allQuickLinks[i].TargetPath == m_currentPath );
}
}
m_multiSelectionToggleSelectionMode = false;
RefreshFiles( true );
}
}
private string m_searchString = string.Empty;
private string SearchString
{
get { return m_searchString; }
set
{
if( m_searchString != value )
{
m_searchString = value;
searchInputField.text = m_searchString;
RefreshFiles( false );
}
}
}
private bool m_acceptNonExistingFilename = false;
private bool AcceptNonExistingFilename
{
get { return m_acceptNonExistingFilename; }
set { m_acceptNonExistingFilename = value; }
}
private PickMode m_pickerMode = PickMode.Files;
internal PickMode PickerMode
{
get { return m_pickerMode; }
private set
{
m_pickerMode = value;
if( m_pickerMode == PickMode.Folders )
{
filtersDropdown.options[0].text = FoldersFilterText;
filtersDropdown.value = 0;
filtersDropdown.RefreshShownValue();
filtersDropdown.interactable = false;
}
else
{
filtersDropdown.options[0].text = filters[0].ToString();
filtersDropdown.interactable = true;
}
Text placeholder = filenameInputField.placeholder as Text;
if( placeholder )
placeholder.gameObject.SetActive( m_pickerMode != PickMode.Folders );
}
}
private bool m_allowMultiSelection;
internal bool AllowMultiSelection
{
get { return m_allowMultiSelection; }
private set { m_allowMultiSelection = value; }
}
private bool m_multiSelectionToggleSelectionMode;
internal bool MultiSelectionToggleSelectionMode
{
get { return m_multiSelectionToggleSelectionMode; }
private set
{
if( m_multiSelectionToggleSelectionMode != value )
{
m_multiSelectionToggleSelectionMode = value;
for( int i = 0; i < allItems.Count; i++ )
{
if( allItems[i].gameObject.activeSelf )
allItems[i].SetSelected( selectedFileEntries.Contains( allItems[i].Position ) );
}
}
}
}
private string Title
{
get { return titleText.text; }
set { titleText.text = value; }
}
private string SubmitButtonText
{
get { return submitButtonText.text; }
set { submitButtonText.text = value; }
}
private string LastBrowsedFolder
{
get { return PlayerPrefs.GetString( "FBLastPath", null ); }
set { PlayerPrefs.SetString( "FBLastPath", value ); }
}
#endregion
#region Delegates
public delegate void OnSuccess( string[] paths );
public delegate void OnCancel();
public delegate bool FileSystemEntryFilter( FileSystemEntry entry );
#if UNITY_EDITOR || UNITY_ANDROID
public delegate void AndroidSAFDirectoryPickCallback( string rawUri, string name );
#endif
private OnSuccess onSuccess;
private OnCancel onCancel;
#endregion
#region Messages
private void Awake()
{
m_instance = this;
rectTransform = (RectTransform) transform;
windowTR = (RectTransform) window.transform;
canvas = GetComponent<Canvas>();
middleViewOriginalPosition = middleView.anchoredPosition;
middleViewOriginalSize = middleView.sizeDelta;
middleViewQuickLinksOriginalSize = middleViewQuickLinks.sizeDelta;
nullPointerEventData = new PointerEventData( null );
#if !UNITY_EDITOR && ( UNITY_ANDROID || UNITY_IOS || UNITY_WSA || UNITY_WSA_10_0 )
defaultInitialPath = Application.persistentDataPath;
#else
defaultInitialPath = Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments );
#endif
#if !UNITY_EDITOR && UNITY_ANDROID
if( FileBrowserHelpers.ShouldUseSAF )
{
// These UI elements have no use in Storage Access Framework mode (Android 10+)
pathInputField.gameObject.SetActive( false );
showHiddenFilesToggle.gameObject.SetActive( false );
}
#endif
SetExcludedExtensions( excludedExtensions );
backButton.interactable = false;
forwardButton.interactable = false;
upButton.interactable = false;
filenameInputField.onValidateInput += OnValidateFilenameInput;
filenameInputField.onValueChanged.AddListener( OnFilenameInputChanged );
allFilesFilter = new Filter( AllFilesFilterText );
filters.Add( allFilesFilter );
invalidFilenameChars = new HashSet<char>( Path.GetInvalidFileNameChars() )
{
Path.DirectorySeparatorChar,
Path.AltDirectorySeparatorChar
};
window.Initialize( this );
listView.SetAdapter( this );
// Refresh the skin immediately
m_skinVersion = m_skin.Version;
RefreshSkin();
if( !showResizeCursor )
Destroy( resizeCursorHandler );
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
// On new Input System, scroll sensitivity is much higher than legacy Input system
filesScrollRect.scrollSensitivity *= 0.25f;
quickLinksContainer.GetComponentInParent<ScrollRect>().scrollSensitivity *= 0.25f;
filtersDropdownContainer.GetComponent<ScrollRect>().scrollSensitivity *= 0.25f;
#endif
}
private void OnRectTransformDimensionsChange()
{
canvasDimensionsChanged = true;
}
#if UNITY_EDITOR
protected virtual void OnValidate()
{
// Refresh the skin in the next Update if it is changed via Unity Inspector at runtime
if( UnityEditor.EditorApplication.isPlaying && m_skin != prevSkin )
{
if( !m_skin ) // Don't allow null UISkin
m_skin = prevSkin;
else
m_skinVersion = m_skin.Version - 1;
}
}
#endif
private void Update()
{
if( m_skin && m_skinVersion != m_skin.Version )
{
m_skinVersion = m_skin.Version;
RefreshSkin();
#if UNITY_EDITOR
prevSkin = m_skin;
#endif
}
}
private void LateUpdate()
{
if( canvasDimensionsChanged )
{
canvasDimensionsChanged = false;
Vector2 windowSize = windowTR.sizeDelta;
EnsureWindowIsWithinBounds();
if( windowTR.sizeDelta != windowSize )
OnWindowDimensionsChanged( windowTR.sizeDelta );
deleteConfirmationPanel.OnCanvasDimensionsChanged( rectTransform.sizeDelta );
if( contextMenu.gameObject.activeSelf )
contextMenu.Hide();
}
#if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WEBGL || UNITY_WSA || UNITY_WSA_10_0
// Handle keyboard shortcuts
if( !EventSystem.current.currentSelectedGameObject )
{
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current != null )
#endif
{
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current[Key.Delete].wasPressedThisFrame )
#else
if( Input.GetKeyDown( KeyCode.Delete ) )
#endif
DeleteSelectedFiles();
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current[Key.F2].wasPressedThisFrame )
#else
if( Input.GetKeyDown( KeyCode.F2 ) )
#endif
RenameSelectedFile();
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
if( Keyboard.current[Key.A].wasPressedThisFrame && Keyboard.current.ctrlKey.isPressed )
#else
if( Input.GetKeyDown( KeyCode.A ) && ( Input.GetKey( KeyCode.LeftControl ) || Input.GetKey( KeyCode.LeftCommand ) ) )
#endif
SelectAllFiles();
}
}
#endif
// 2 Text objects are used in the filename input field:
// filenameInputField.textComponent: visible when editing the text, has Horizontal Overflow set to Wrap (cuts out words, ugly)
// filenameInputFieldOverlayText: visible when not editing the text, has Horizontal Overflow set to Overflow (doesn't cut out words)
if( EventSystem.current.currentSelectedGameObject == filenameInputField.gameObject )
{
if( filenameInputFieldOverlayText.enabled )
{
filenameInputFieldOverlayText.enabled = false;
filenameInputField.textComponent.color = m_skin.InputFieldTextColor;
}
}
else if( !filenameInputFieldOverlayText.enabled )
{
filenameInputFieldOverlayText.enabled = true;
Color c = m_skin.InputFieldTextColor;
c.a = 0f;
filenameInputField.textComponent.color = c;
}
// Refresh drive quick links
#if UNITY_EDITOR || ( !UNITY_IOS && !UNITY_WSA && !UNITY_WSA_10_0 )
#if !UNITY_EDITOR && UNITY_ANDROID
if( !FileBrowserHelpers.ShouldUseSAF )
#endif
if( quickLinksInitialized && generateQuickLinksForDrives && m_drivesRefreshInterval >= 0f && Time.realtimeSinceStartup >= drivesNextRefreshTime )
{
drivesNextRefreshTime = Time.realtimeSinceStartup + m_drivesRefreshInterval;
RefreshDriveQuickLinks();
}
#endif
}
private void OnApplicationFocus( bool focus )
{