-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIFilePicker.cs
73 lines (59 loc) · 2.35 KB
/
UIFilePicker.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WinForMono {
public class UIFilePicker<T> {
private const string ALL_FILETYPES = "All Files (*.*)|*.*";
public const string IMAGE_FILTER = ALL_FILETYPES + "|GDI+ Supported Images (*.bmp;*.gif;*.jpeg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.png;*.tiff";
public UIFilePicker() : this("Pick a file") {}
public UIFilePicker(
string title="Pick a file",
string default_dir=null,
string filter=ALL_FILETYPES,
bool show_readonly=false
) {
underlying = new OpenFileDialog();
underlying.Title = title;
underlying.InitialDirectory = default_dir;
underlying.ShowReadOnly = show_readonly;
underlying.Filter = filter;
}
// Will run until a valid input is given
public string run(bool existing_file) {
while (true) {
if (try_run(existing_file, out string path)) return path;
}
}
// Will run once, if nothing is selected returns false and path is null
public bool try_run(bool existing_file, out string path) {
path = null;
underlying.CheckFileExists = existing_file;
underlying.CheckPathExists = existing_file;
underlying.Multiselect = false;
switch (underlying.ShowDialog() == DialogResult.OK) {
case true:
path = underlying.FileName;
return true;
default:
return false;
}
}
public string[] run_mult(bool existing_file) {
while (true) {
if (try_run_mult(existing_file, out string[] paths)) return paths;
}
}
public bool try_run_mult(bool existing_file, out string[] paths) {
// Default to null to make compiler happy
paths = null;
underlying.CheckFileExists = existing_file;
underlying.CheckPathExists = existing_file;
underlying.Multiselect = true;
bool success = underlying.ShowDialog() == DialogResult.OK;
if (success) paths = underlying.FileNames;
return success;
}
private OpenFileDialog underlying;
}
}