-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathStandaloneFileBrowserWindows.cs
More file actions
executable file
·137 lines (116 loc) · 5.23 KB
/
Copy pathStandaloneFileBrowserWindows.cs
File metadata and controls
executable file
·137 lines (116 loc) · 5.23 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
#if UNITY_STANDALONE_WIN
using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Ookii.Dialogs.WinForms;
namespace SFB {
// For fullscreen support
// - WindowWrapper class and GetActiveWindow() are required for modal file dialog.
// - "PlayerSettings/Visible In Background" should be enabled, otherwise when file dialog opened app window minimizes automatically.
public class WindowWrapper : IWin32Window {
private IntPtr _hwnd;
public WindowWrapper(IntPtr handle) { _hwnd = handle; }
public IntPtr Handle { get { return _hwnd; } }
}
public class StandaloneFileBrowserWindows : IStandaloneFileBrowser {
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow();
public string[] OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect) {
var fd = new VistaOpenFileDialog();
fd.Title = title;
if (extensions != null) {
fd.Filter = GetFilterFromFileExtensionList(extensions);
fd.FilterIndex = 1;
}
else {
fd.Filter = string.Empty;
}
fd.Multiselect = multiselect;
if (!string.IsNullOrEmpty(directory)) {
fd.FileName = GetDirectoryPath(directory);
}
var res = fd.ShowDialog(new WindowWrapper(GetActiveWindow()));
var filenames = res == DialogResult.OK ? fd.FileNames : new string[0];
fd.Dispose();
return filenames;
}
public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<string[]> cb) {
cb.Invoke(OpenFilePanel(title, directory, extensions, multiselect));
}
public string[] OpenFolderPanel(string title, string directory, bool multiselect) {
var fd = new VistaFolderBrowserDialog();
fd.Description = title;
if (!string.IsNullOrEmpty(directory)) {
fd.SelectedPath = GetDirectoryPath(directory);
}
var res = fd.ShowDialog(new WindowWrapper(GetActiveWindow()));
var filenames = res == DialogResult.OK ? new []{ fd.SelectedPath } : new string[0];
fd.Dispose();
return filenames;
}
public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<string[]> cb) {
cb.Invoke(OpenFolderPanel(title, directory, multiselect));
}
public string SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions) {
var fd = new VistaSaveFileDialog();
fd.Title = title;
var finalFilename = "";
if (!string.IsNullOrEmpty(directory)) {
finalFilename = GetDirectoryPath(directory);
}
if (!string.IsNullOrEmpty(defaultName)) {
finalFilename += defaultName;
}
fd.FileName = finalFilename;
if (extensions != null) {
fd.Filter = GetFilterFromFileExtensionList(extensions);
fd.FilterIndex = 1;
fd.DefaultExt = extensions[0].Extensions[0];
fd.AddExtension = true;
}
else {
fd.DefaultExt = string.Empty;
fd.Filter = string.Empty;
fd.AddExtension = false;
}
var res = fd.ShowDialog(new WindowWrapper(GetActiveWindow()));
var filename = res == DialogResult.OK ? fd.FileName : "";
fd.Dispose();
return filename;
}
public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<string> cb) {
cb.Invoke(SaveFilePanel(title, directory, defaultName, extensions));
}
// .NET Framework FileDialog Filter format
// https://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.filter
private static string GetFilterFromFileExtensionList(ExtensionFilter[] extensions) {
var filterString = "";
foreach (var filter in extensions) {
filterString += filter.Name + "(";
foreach (var ext in filter.Extensions) {
filterString += "*." + ext + ",";
}
filterString = filterString.Remove(filterString.Length - 1);
filterString += ") |";
foreach (var ext in filter.Extensions) {
filterString += "*." + ext + "; ";
}
filterString += "|";
}
filterString = filterString.Remove(filterString.Length - 1);
return filterString;
}
private static string GetDirectoryPath(string directory) {
var directoryPath = Path.GetFullPath(directory);
if (!directoryPath.EndsWith("\\")) {
directoryPath += "\\";
}
if (Path.GetPathRoot(directoryPath) == directoryPath) {
return directory;
}
return Path.GetDirectoryName(directoryPath) + Path.DirectorySeparatorChar;
}
}
}
#endif