From 1c83323c96f7d4cba99388b58ddffd529464438b Mon Sep 17 00:00:00 2001 From: Arturas Slajus Date: Wed, 13 Apr 2022 14:55:53 +0300 Subject: [PATCH] Make file filters extensible by turning them into an interface, instead of concrete class. --- .../SimpleFileBrowser/Scripts/FileBrowser.cs | 63 +++++++++++++++---- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/Plugins/SimpleFileBrowser/Scripts/FileBrowser.cs b/Plugins/SimpleFileBrowser/Scripts/FileBrowser.cs index c94ff80..727cc43 100644 --- a/Plugins/SimpleFileBrowser/Scripts/FileBrowser.cs +++ b/Plugins/SimpleFileBrowser/Scripts/FileBrowser.cs @@ -42,13 +42,42 @@ private struct QuickLink #endregion #region Inner Classes - public class Filter + + public interface IFilter { + /// Default extension for this filter. + string defaultExtension { get; } + + /// 'false' when some extensions have multiple suffixes like ".tar.gz" + bool allExtensionsHaveSingleSuffix { get; } + + /// + /// Returns true if this filter is the default 'all files' filter. All custom filters should return false here. + /// + bool isAllFilesFilter { get; } + + /// Returns true if this is one of the supported extensions for this filter. + bool isValidExtension( string extension ); + + /// + /// Returns true if the filter matches the given . + /// + /// a file extension, like '.tar' or '.tar.gz' + /// + /// true if may have multiple suffixes, like '.tar.gz' + /// + bool MatchesExtension( string extension, bool extensionMayHaveMultipleSuffixes ); + + /// Shown in the user interface. + string ToString(); + } + + public class Filter : IFilter { public readonly string name; public readonly string[] extensions; public readonly HashSet extensionsSet; - public readonly string defaultExtension; - public readonly bool allExtensionsHaveSingleSuffix; // 'false' when some extensions have multiple suffixes like ".tar.gz" + public string defaultExtension { get; private set; } + public bool allExtensionsHaveSingleSuffix { get; private set; } internal Filter( string name ) { @@ -92,6 +121,19 @@ public Filter( string name, params 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 ) ) @@ -271,7 +313,7 @@ public static string AllFilesFilterText if( m_instance ) { - Filter oldAllFilesFilter = m_instance.allFilesFilter; + IFilter oldAllFilesFilter = m_instance.allFilesFilter; m_instance.allFilesFilter = new Filter( value ); if( m_instance.filters.Count > 0 && m_instance.filters[0] == oldAllFilesFilter ) @@ -516,8 +558,8 @@ private static FileBrowser Instance #pragma warning restore 0414 private StringBuilder multiSelectionFilenameBuilder; - private readonly List filters = new List(); - private Filter allFilesFilter; + private readonly List filters = new List(); + private IFilter allFilesFilter; private bool showAllFilesFilter = true; @@ -1470,7 +1512,7 @@ public void OnSubmitButtonClicked() { // This is a nonexisting file string filename = filenameInput.Substring( startIndex, filenameLength ); - if( m_pickerMode != PickMode.Folders && filters[filtersDropdown.value].extensions != null ) + if( m_pickerMode != PickMode.Folders && !filters[filtersDropdown.value].isAllFilesFilter ) { // In file selection mode, make sure that nonexisting files' extensions match one of the required extensions string fileExtension = GetExtensionFromFilename( filename, AllExtensionsHaveSingleSuffix ); @@ -2840,7 +2882,7 @@ public static void SetFilters( bool showAllFilesFilter, IEnumerable filt SetFiltersPostProcessing(); } - public static void SetFilters( bool showAllFilesFilter, params Filter[] filters ) + public static void SetFilters( bool showAllFilesFilter, params IFilter[] filters ) { SetFiltersPreProcessing( showAllFilesFilter ); @@ -2868,7 +2910,7 @@ private static void SetFiltersPreProcessing( bool showAllFilesFilter ) private static void SetFiltersPostProcessing() { - List filters = Instance.filters; + List filters = Instance.filters; if( filters.Count == 0 ) filters.Add( Instance.allFilesFilter ); @@ -2915,8 +2957,7 @@ public static bool SetDefaultFilter( string defaultFilter ) for( int i = 0; i < Instance.filters.Count; i++ ) { - HashSet extensions = Instance.filters[i].extensionsSet; - if( extensions != null && extensions.Contains( defaultFilter ) ) + if( Instance.filters[i].isValidExtension( defaultFilter ) ) { Instance.filtersDropdown.value = i; Instance.filtersDropdown.RefreshShownValue();