Skip to content

Commit 0e6096b

Browse files
Address PR feedback on dialog fallback handling
1 parent 8b477bf commit 0e6096b

1 file changed

Lines changed: 25 additions & 28 deletions

File tree

NAPS2.Lib/EtoForms/EtoDialogHelper.cs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,56 +92,60 @@ public override bool PromptToSaveImage(string? defaultPath, out string? savePath
9292

9393
private static SaveFileDialog CreateSaveFileDialog()
9494
{
95-
EnsureCurrentDirectoryIsAccessible();
9695
try
9796
{
9897
return new SaveFileDialog();
9998
}
100-
catch (FileNotFoundException)
99+
catch (FileNotFoundException) when (TrySetCurrentDirectoryForDialogFallback())
101100
{
102-
SetSafeCurrentDirectory();
103101
return new SaveFileDialog();
104102
}
105103
}
106104

107-
private static void EnsureCurrentDirectoryIsAccessible()
105+
private static OpenFileDialog CreateOpenFileDialog()
108106
{
109107
try
110108
{
111-
var currentDirectory = Directory.GetCurrentDirectory();
112-
if (!Directory.Exists(currentDirectory))
113-
{
114-
SetSafeCurrentDirectory();
115-
}
109+
return new OpenFileDialog();
116110
}
117-
catch
111+
catch (FileNotFoundException) when (TrySetCurrentDirectoryForDialogFallback())
118112
{
119-
SetSafeCurrentDirectory();
113+
return new OpenFileDialog();
120114
}
121115
}
122116

123-
private static void SetSafeCurrentDirectory()
117+
private static bool TrySetCurrentDirectoryForDialogFallback()
124118
{
125-
var fallbackDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
126-
if (string.IsNullOrWhiteSpace(fallbackDirectory) || !Directory.Exists(fallbackDirectory))
119+
var fallbackDirectory = GetDialogFallbackDirectory();
120+
if (fallbackDirectory == null)
127121
{
128-
fallbackDirectory = Path.GetTempPath();
122+
return false;
129123
}
130124
Environment.CurrentDirectory = fallbackDirectory;
125+
return true;
131126
}
132127

133-
private void SetDir(SaveFileDialog dialog, string? defaultPath)
128+
private static string? GetDialogFallbackDirectory()
129+
{
130+
var fallbackDirectory = Environment.GetFolderPath(OperatingSystem.IsWindows()
131+
? Environment.SpecialFolder.MyDocuments
132+
: Environment.SpecialFolder.UserProfile);
133+
return Directory.Exists(fallbackDirectory) ? fallbackDirectory : null;
134+
}
135+
136+
private void SetDir(FileDialog dialog, string? defaultPath)
134137
{
135138
string? path = null;
136139
if (Paths.IsTestAppDataPath)
137140
{
138141
// For UI test automation we choose the appdata folder for test isolation and consistency
139142
path = Paths.AppData;
140143
}
141-
else if (Path.IsPathRooted(defaultPath))
144+
else if (!string.IsNullOrEmpty(defaultPath) && Path.IsPathRooted(defaultPath))
142145
{
143146
path = Path.GetDirectoryName(NormalizePath(defaultPath));
144147
}
148+
path ??= GetDialogFallbackDirectory();
145149
if (path != null)
146150
{
147151
dialog.Directory = UriHelper.FilePathToFileUri(Path.GetFullPath(path));
@@ -162,19 +166,12 @@ private static string NormalizePath(string path)
162166

163167
public override bool PromptToImport(out string[]? filePaths)
164168
{
165-
EnsureCurrentDirectoryIsAccessible();
166-
var ofd = new OpenFileDialog
167-
{
168-
MultiSelect = true,
169-
CheckFileExists = true
170-
};
169+
var ofd = CreateOpenFileDialog();
170+
ofd.MultiSelect = true;
171+
ofd.CheckFileExists = true;
171172
_fileFilters.Set(ofd,
172173
FileFilterGroup.AllFiles | FileFilterGroup.Pdf | FileFilterGroup.AllImages | FileFilterGroup.Image);
173-
if (Paths.IsTestAppDataPath)
174-
{
175-
// For UI test automation we choose the appdata folder to find the prepared files to import
176-
ofd.Directory = UriHelper.FilePathToFileUri(Path.GetFullPath(Paths.AppData));
177-
}
174+
SetDir(ofd, defaultPath: null);
178175
EtoPlatform.Current.ConfigureFileDialog(ofd);
179176
if (ofd.ShowDialog(null) == DialogResult.Ok)
180177
{

0 commit comments

Comments
 (0)