-
Notifications
You must be signed in to change notification settings - Fork 399
/
DirectorySelector.cs
228 lines (193 loc) · 8.08 KB
/
DirectorySelector.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
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Graphics.Containers;
using osu.Framework.Platform;
using osuTK;
namespace osu.Framework.Graphics.UserInterface
{
/// <summary>
/// A component which allows a user to select a directory.
/// </summary>
public abstract partial class DirectorySelector : CompositeDrawable
{
private FillFlowContainer directoryFlow;
protected readonly BindableBool ShowHiddenItems = new BindableBool();
protected abstract ScrollContainer<Drawable> CreateScrollContainer();
/// <summary>
/// Create the breadcrumb part of the control.
/// </summary>
protected abstract DirectorySelectorBreadcrumbDisplay CreateBreadcrumb();
/// <summary>
/// Create a button that toggles the display of hidden items.
/// </summary>
/// <remarks>
/// Unless overridden, a toggle button will not be added.
/// </remarks>
protected virtual Drawable CreateHiddenToggleButton() => Empty();
protected abstract DirectorySelectorDirectory CreateDirectoryItem(DirectoryInfo directory, string displayName = null);
/// <summary>
/// Create the directory item that resolves the parent directory.
/// </summary>
protected abstract DirectorySelectorDirectory CreateParentDirectoryItem(DirectoryInfo directory);
[Cached]
public readonly Bindable<DirectoryInfo> CurrentPath = new Bindable<DirectoryInfo>();
private string initialPath;
protected DirectorySelector(string initialPath = null)
{
this.initialPath = initialPath;
}
protected override void LoadComplete()
{
base.LoadComplete();
CurrentPath.Value = new DirectoryInfo(initialPath);
}
[BackgroundDependencyLoader]
private void load(GameHost gameHost)
{
initialPath ??= gameHost.InitialFileSelectorPath;
InternalChild = new GridContainer
{
RelativeSizeAxes = Axes.Both,
RowDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize),
new Dimension(),
},
Content = new[]
{
new Drawable[]
{
new GridContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
ColumnDimensions = new[]
{
new Dimension(),
new Dimension(GridSizeMode.AutoSize),
},
RowDimensions = new[] { new Dimension(GridSizeMode.AutoSize) },
Content = new[]
{
new[]
{
CreateBreadcrumb(),
CreateHiddenToggleButton()
}
}
}
},
new Drawable[]
{
CreateScrollContainer().With(d =>
{
d.RelativeSizeAxes = Axes.Both;
d.Child = directoryFlow = new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Direction = FillDirection.Vertical,
Spacing = new Vector2(2),
};
})
}
}
};
ShowHiddenItems.ValueChanged += _ => updateDisplay();
CurrentPath.BindValueChanged(_ => updateDisplay(), true);
}
/// <summary>
/// Because <see cref="CurrentPath"/> changes may not necessarily lead to directories that exist/are accessible,
/// <see cref="updateDisplay"/> may need to change <see cref="CurrentPath"/> again to lead to a directory that is actually accessible.
/// This flag intends to prevent recursive <see cref="updateDisplay"/> calls from taking place during the process of finding an accessible directory.
/// </summary>
private bool directoryChanging;
private void updateDisplay()
{
if (directoryChanging)
return;
try
{
directoryChanging = true;
var newDirectory = CurrentPath.Value;
bool notifyError = false;
ICollection<DirectorySelectorItem> items = Array.Empty<DirectorySelectorItem>();
while (newDirectory != null)
{
newDirectory.Refresh();
if (TryGetEntriesForPath(newDirectory, out items))
break;
notifyError = true;
newDirectory = newDirectory.Parent;
}
if (notifyError)
NotifySelectionError();
if (newDirectory == null)
{
try
{
// This will throw on AOT platforms (System.ExecutionEngineException: Attempting to JIT compile method).
var drives = DriveInfo.GetDrives();
directoryFlow.Clear();
foreach (var drive in drives)
directoryFlow.Add(CreateDirectoryItem(drive.RootDirectory));
}
catch
{
NotifySelectionError();
}
return;
}
CurrentPath.Value = newDirectory;
directoryFlow.Clear();
directoryFlow.Add(CreateParentDirectoryItem(newDirectory.Parent));
directoryFlow.AddRange(items);
}
finally
{
directoryChanging = false;
}
}
/// <summary>
/// Attempts to create entries to display for the given <paramref name="path"/>.
/// A return value of <see langword="false"/> is used to indicate a non-specific I/O failure, signaling to the selector that it should attempt
/// to find another directory to display (since <paramref name="path"/> is inaccessible).
/// </summary>
/// <param name="path">The directory to create entries for.</param>
/// <param name="items">
/// The created <see cref="DirectorySelectorItem"/>s, provided that the <paramref name="path"/> could be entered.
/// Not valid for reading if the return value of the method is <see langword="false"/>.
/// </param>
protected virtual bool TryGetEntriesForPath(DirectoryInfo path, out ICollection<DirectorySelectorItem> items)
{
items = new List<DirectorySelectorItem>();
try
{
foreach (var dir in path.GetDirectories().OrderBy(d => d.Name))
{
if (ShowHiddenItems.Value || !dir.Attributes.HasFlagFast(FileAttributes.Hidden))
items.Add(CreateDirectoryItem(dir));
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Called when an error has occured. Usually happens when trying to access protected directories.
/// </summary>
protected virtual void NotifySelectionError()
{
}
}
}