Skip to content

Commit d4d5b70

Browse files
committed
Another small FileDialog refactoring
1 parent b515166 commit d4d5b70

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

src/Myra/Graphics2D/UI/File/FileDialog.PlatformDependent.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static string SystemUsername
7777
/// Append <see cref="Location"/> directories under the user's HOME directory.
7878
/// </summary>
7979
/// <param name="placeList">What folders to try to add relative to the HOME directory.</param>
80-
public static void AppendUserPlacesOnSystem(List<Location> appendResult, IReadOnlyList<string> placeList)
80+
public static void AppendUserPlacesOnSystem(List<Location> appendResult, IReadOnlyList<string> placeList, FileDialogMode mode, bool showHidden)
8181
{
8282
ThrowIfNull(appendResult);
8383

@@ -97,8 +97,10 @@ public static void AppendUserPlacesOnSystem(List<Location> appendResult, IReadOn
9797

9898
foreach (string path in places)
9999
{
100-
if (!TryGetFileAttributes(path, out _))
100+
if (!CheckAccess(path, mode, showHidden))
101+
{
101102
continue;
103+
}
102104

103105
appendResult.Add(new Location(string.Empty, Path.GetFileName(path), path, false));
104106
}

src/Myra/Graphics2D/UI/File/FileDialog.Util.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ private static bool TryEnumerateDirectoryFiles(string path, string searchPattern
6161
/// <summary>
6262
/// Attempt to retreive permissions about a file or directory. Return value is indicative of success and a basic attribute filter.
6363
/// </summary>
64-
private static bool TryGetFileAttributes(string path, out FileAttributes attributes)
64+
private static bool CheckAccess(string path, FileDialogMode mode, bool showHidden)
6565
{
66+
FileAttributes attributes;
6667
try
6768
{
6869
attributes = new FileInfo(path).Attributes;
@@ -73,15 +74,6 @@ private static bool TryGetFileAttributes(string path, out FileAttributes attribu
7374
return false;
7475
}
7576

76-
bool discard =
77-
attributes.HasFlag(FileAttributes.System) |
78-
attributes.HasFlag(FileAttributes.Offline);
79-
80-
return !discard;
81-
}
82-
83-
private static bool FileAttributeFilter(FileAttributes attributes, FileDialogMode mode, bool showHidden)
84-
{
8577
bool discard =
8678
attributes.HasFlag(FileAttributes.System) |
8779
attributes.HasFlag(FileAttributes.Offline) |
@@ -98,6 +90,7 @@ private static bool FileAttributeFilter(FileAttributes attributes, FileDialogMod
9890
default:
9991
break;
10092
}
93+
10194
return !discard;
10295
}
10396
}

src/Myra/Graphics2D/UI/File/FileDialog.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ protected virtual void PopulatePlacesListUI(ListView listView)
194194
int index = 0;
195195

196196
//Add user directories
197-
Platform.AppendUserPlacesOnSystem(placeList, Platform.SystemUserPlacePaths);
197+
Platform.AppendUserPlacesOnSystem(placeList, Platform.SystemUserPlacePaths, _mode, ShowHiddenFiles);
198198
for (; index < placeList.Count; index++)
199199
listView.Widgets.Add(CreateListItem(placeList[index]));
200200

@@ -241,9 +241,7 @@ protected bool TryAccessDirectory(string path)
241241
return true;
242242
}
243243

244-
if (!TryGetFileAttributes(path, out FileAttributes att))
245-
return false;
246-
return FileAttributeFilter(att, _mode, ShowHiddenFiles);
244+
return CheckAccess(path, _mode, ShowHiddenFiles);
247245
}
248246

249247
protected void ShowIOError(string path, string exceptionMsg)
@@ -414,11 +412,10 @@ private void UpdateFolder()
414412
var gridY = 0;
415413
foreach (string folder in collection)
416414
{
417-
success = TryGetFileAttributes(folder, out FileAttributes att);
418-
if (success)
419-
success &= FileAttributeFilter(att, _mode, ShowHiddenFiles);
420-
if (!success)
415+
if (!CheckAccess(folder, _mode, ShowHiddenFiles))
416+
{
421417
continue;
418+
}
422419

423420
_gridFiles.RowsProportions.Add(new Proportion());
424421

@@ -461,11 +458,10 @@ private void UpdateFolder()
461458

462459
foreach (string file in collection)
463460
{
464-
success = TryGetFileAttributes(file, out FileAttributes att);
465-
if (success)
466-
success &= FileAttributeFilter(att, _mode, ShowHiddenFiles);
467-
if (!success)
461+
if (!CheckAccess(file, _mode, ShowHiddenFiles))
462+
{
468463
continue;
464+
}
469465

470466
_gridFiles.RowsProportions.Add(new Proportion());
471467

0 commit comments

Comments
 (0)