-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyDialogs.cs
239 lines (210 loc) · 11.2 KB
/
TinyDialogs.cs
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
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
namespace TinyDialogsNet;
/// <summary>
/// The entrypoint to all dialogs provided by the wrapper.
/// </summary>
public static class TinyDialogs
{
private const char MultipleFileSeparator = '|';
/// <summary>
/// Produces a short beep sound effect (when possible).
/// </summary>
public static void Beep()
{
NativeDialogs.tinyfd_beep();
}
/// <summary>
/// Shows a notification popup.
/// </summary>
/// <param name="icon">The icon of the notification. See <see cref="NotificationIconType" /> for values.</param>
/// <param name="title">The title of the notification.</param>
/// <param name="message">The body of the notification.</param>
/// <exception cref="ArgumentException">The title string was ill-formed.</exception>
// Intentional, as the return value is only useful for tinyfd_query, which is not included in this library.
[SuppressMessage("Performance", "CA1806")]
public static void NotifyPopup(NotificationIconType icon, string title, string message)
{
DialogHelpers.RequireValidTitleOrThrow(title);
if (OperatingSystem.IsWindows()) NativeDialogs.tinyfd_notifyPopupW(title, message, icon.ToNativeString());
else NativeDialogs.tinyfd_notifyPopup(title, message, icon.ToNativeString());
}
/// <summary>
/// Shows a message box.
/// </summary>
/// <param name="title">The title of the message box.</param>
/// <param name="message">The body of the message box.</param>
/// <param name="type">The buttons displayed in the message box. See <see cref="MessageBoxDialogType" /> for values.</param>
/// <param name="icon">The icon of the message box. See <see cref="MessageBoxIconType" /> for values.</param>
/// <param name="defaultButton">The button which will be focused by default.</param>
/// <remarks>The <see cref="title" /> parameter cannot contain new line or tab characters. </remarks>
/// <returns>The button which the user clicked.</returns>
/// <exception cref="ArgumentException">The title string was ill-formed.</exception>
public static MessageBoxButton MessageBox(string title, string message,
MessageBoxDialogType type,
MessageBoxIconType icon, MessageBoxButton defaultButton)
{
DialogHelpers.RequireValidTitleOrThrow(title);
var response = OperatingSystem.IsWindows()
? NativeDialogs.tinyfd_messageBoxW(title, message, type.ToNativeString(), icon.ToNativeString(),
defaultButton.ToNativeValue(type))
: NativeDialogs.tinyfd_messageBox(title, message, type.ToNativeString(), icon.ToNativeString(),
defaultButton.ToNativeValue(type));
return response.ToButton(type);
}
/// <summary>
/// Shows an input box (prompt).
/// </summary>
/// <param name="type">Defines what information should be inputted. Can be regular text or a password.</param>
/// <param name="title">The title of the input box.</param>
/// <param name="message">The body of the input box.</param>
/// <param name="placeholder">The placeholder.</param>
/// <remarks>The placeholder will always be empty if <see cref="type" /> is <see cref="InputBoxType.Password" />.</remarks>
/// <returns>
/// If canceled, returns an empty string and "true" for the "canceled" parameter. Otherwise, returns the text the
/// user inputted.
/// </returns>
/// <exception cref="ArgumentException">The title string was ill-formed.</exception>
public static (bool Canceled, string Text) InputBox(InputBoxType type, string title, string message,
string placeholder = "")
{
DialogHelpers.RequireValidTitleOrThrow(title);
var nativePlaceholder = type == InputBoxType.Password ? null : placeholder;
var response = OperatingSystem.IsWindows()
? NativeDialogs.tinyfd_inputBoxW(title, message, nativePlaceholder)
: NativeDialogs.tinyfd_inputBox(title, message, nativePlaceholder);
return response == IntPtr.Zero ? (true, "") : (false, NativeDialogs.StringFromPointer(response));
}
/// <summary>
/// Shows a save file dialog.
/// </summary>
/// <param name="title">The title of the dialog.</param>
/// <param name="defaultPath">The default file/directory which will be shown on startup.</param>
/// <param name="filter">The filter which will control the output path.</param>
/// <remarks>
/// A forward slash ("/") may be added to <see cref="defaultPath" /> to show the default directory instead of a
/// file.
/// </remarks>
/// <returns>
/// If canceled, returns an empty path and "true" for the "canceled" parameter. Otherwise, returns the path the
/// user selected.
/// </returns>
/// <exception cref="ArgumentException">The title string was ill-formed.</exception>
public static (bool Canceled, string Path) SaveFileDialog(string title, string defaultPath = "",
FileFilter filter = default)
{
DialogHelpers.RequireValidTitleOrThrow(title);
var patternCount = filter?.Patterns?.Count() ?? 0;
var patterns = filter?.Patterns?.ToArray() ?? Array.Empty<string>();
var response = OperatingSystem.IsWindows()
? NativeDialogs.tinyfd_saveFileDialogW(title, defaultPath, patternCount, patterns, filter?.Name)
: NativeDialogs.tinyfd_saveFileDialog(title, defaultPath, patternCount, patterns, filter?.Name);
return response == IntPtr.Zero ? (true, "") : (false, NativeDialogs.StringFromPointer(response));
}
/// <summary>
/// Shows an open file dialog.
/// </summary>
/// <param name="title">The title of the dialog.</param>
/// <param name="defaultPath">The default file/directory which will be shown on startup.</param>
/// <param name="allowMultipleSelections">Defines whether the user can pick multiple files in the dialog.</param>
/// <param name="filter">The filter which will control the output path.</param>
/// <remarks>
/// A forward slash ("/") may be added to <see cref="defaultPath" /> to show the default directory instead of a
/// file.
/// </remarks>
/// <returns>
/// If canceled, returns an empty array and "true" for the "canceled" parameter. Otherwise, returns a list of
/// paths the user selected.
/// </returns>
/// <exception cref="ArgumentException">The title string was ill-formed.</exception>
public static (bool Canceled, IEnumerable<string> Paths) OpenFileDialog(string title, string defaultPath = "",
bool allowMultipleSelections = false, FileFilter filter = default)
{
DialogHelpers.RequireValidTitleOrThrow(title);
var patternCount = filter?.Patterns?.Count() ?? 0;
var patterns = filter?.Patterns?.ToArray() ?? Array.Empty<string>();
var response = OperatingSystem.IsWindows()
? NativeDialogs.tinyfd_openFileDialogW(title, defaultPath, patternCount, patterns, filter?.Name,
allowMultipleSelections ? 1 : 0)
: NativeDialogs.tinyfd_openFileDialog(title, defaultPath, patternCount, patterns, filter?.Name,
allowMultipleSelections ? 1 : 0);
return response == IntPtr.Zero
? (true, Array.Empty<string>())
: (false, NativeDialogs.StringFromPointer(response).Split(MultipleFileSeparator));
}
/// <summary>
/// Shows a select folder dialog.
/// </summary>
/// <param name="title">The title of the dialog.</param>
/// <param name="defaultPath">The default directory which will be shown on startup.</param>
/// <returns>
/// If canceled, returns an empty path and "true" for the "canceled" parameter. Otherwise, returns the path the
/// user selected.
/// </returns>
/// <exception cref="ArgumentException">The title string was ill-formed.</exception>
public static (bool Canceled, string Path) SelectFolderDialog(string title, string defaultPath = "")
{
DialogHelpers.RequireValidTitleOrThrow(title);
var response = OperatingSystem.IsWindows()
? NativeDialogs.tinyfd_selectFolderDialogW(title, defaultPath)
: NativeDialogs.tinyfd_selectFolderDialog(title, defaultPath);
return response == IntPtr.Zero ? (true, "") : (false, NativeDialogs.StringFromPointer(response));
}
/// <summary>
/// Shows a color chooser dialog.
/// </summary>
/// <param name="title">The title of the dialog.</param>
/// <param name="defaultColor">
/// The default color which will be shown on startup. If not specified, black (#000000) will be
/// used.
/// </param>
/// <remarks>
/// The <see cref="defaultColor" /> parameter must be a valid #RRGGBB string, otherwise this method will throw an
/// <see cref="ArgumentException" />.
/// </remarks>
/// <returns>
/// If canceled, returns an empty string and "true" for the "canceled" parameter. Otherwise, returns the color the
/// user selected in #RRGGBB format.
/// </returns>
/// <exception cref="ArgumentException">The <see cref="defaultColor" /> parameter was in an invalid format.</exception>
/// <exception cref="ArgumentException">The title string was ill-formed.</exception>
public static (bool Canceled, string Color) ColorChooser(string title, string defaultColor = "#000000")
{
DialogHelpers.RequireValidTitleOrThrow(title);
var rgb = DialogHelpers.HexToRgb(defaultColor);
var response = OperatingSystem.IsWindows()
? NativeDialogs.tinyfd_colorChooserW(title, defaultColor, rgb, rgb)
: NativeDialogs.tinyfd_colorChooser(title, defaultColor, rgb, rgb);
return response == IntPtr.Zero ? (true, "") : (false, NativeDialogs.StringFromPointer(response));
}
/// <summary>
/// Gets a native tinyfiledialog string variable.
/// </summary>
/// <param name="variable">The type of the variable to get.</param>
/// <returns>The value of the variable.</returns>
public static string GetGlobalStringVariable(StringVariable variable)
{
// The getGlobalChar method doesn't have a Windows variant, so the returned string is always UTF-8.
return Marshal.PtrToStringAnsi(NativeDialogs.tinyfd_getGlobalChar(variable.ToNativeName()));
}
/// <summary>
/// Gets a native tinyfiledialog integer variable.
/// </summary>
/// <param name="variable">The type of the variable to get.</param>
/// <returns>The value of the variable.</returns>
public static int GetGlobalIntegerVariable(IntegerVariable variable)
{
return NativeDialogs.tinyfd_getGlobalInt(variable.ToNativeName());
}
/// <summary>
/// Sets the value of a native tinyfiledialog integer variable.
/// </summary>
/// <param name="variable">The type of the variable to change.</param>
/// <param name="value">The new value of the variable.</param>
// Intentional, as the returned integer value is the same as the "value" parameter.
[SuppressMessage("Performance", "CA1806")]
public static void SetGlobalIntegerVariable(IntegerVariable variable, int value)
{
NativeDialogs.tinyfd_setGlobalInt(variable.ToNativeName(), value);
}
}