|
| 1 | +using System.Numerics; |
| 2 | +using ImGuiNET; |
| 3 | +using StArray.ModManager.Manager; |
| 4 | +using StArray.ModManager.Resources; |
| 5 | + |
| 6 | +namespace StArray.ModManager.Android.UI; |
| 7 | + |
| 8 | +/// <summary>Browses local files with the host application's existing storage access.</summary> |
| 9 | +internal sealed class AndroidModFilePicker |
| 10 | +{ |
| 11 | + private sealed record DirectoryListing(string[] Folders, string[] Files); |
| 12 | + |
| 13 | + private readonly string _imports; |
| 14 | + private readonly string _downloads; |
| 15 | + private TaskCompletionSource<string?>? _selection; |
| 16 | + private Task<DirectoryListing>? _listing; |
| 17 | + private string _directory; |
| 18 | + private string? _selectedFile; |
| 19 | + private bool _openPopup; |
| 20 | + private bool _resetListScroll; |
| 21 | + |
| 22 | + public AndroidModFilePicker(string modsPath) |
| 23 | + { |
| 24 | + string mods = Path.GetFullPath(modsPath); |
| 25 | + _imports = Path.Combine(Path.GetDirectoryName(mods) ?? mods, "imports"); |
| 26 | + string? storage = Environment.GetEnvironmentVariable("EXTERNAL_STORAGE"); |
| 27 | + _downloads = Path.Combine(!string.IsNullOrEmpty(storage) && Path.IsPathFullyQualified(storage) |
| 28 | + ? storage |
| 29 | + : "/sdcard", "Download"); |
| 30 | + _directory = _imports; |
| 31 | + } |
| 32 | + |
| 33 | + public async Task<ModInstallFile?> PickAsync() |
| 34 | + { |
| 35 | + if (_selection != null) |
| 36 | + throw new InvalidOperationException(L10n.Get("ModInstall_InProgress")); |
| 37 | + |
| 38 | + var selection = new TaskCompletionSource<string?>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 39 | + _selection = selection; |
| 40 | + _openPopup = true; |
| 41 | + Navigate(_directory); |
| 42 | + |
| 43 | + string? path = await selection.Task.ConfigureAwait(false); |
| 44 | + return path == null ? null : new ModInstallFile(Path.GetFileName(path), File.OpenRead(path)); |
| 45 | + } |
| 46 | + |
| 47 | + public void Render() |
| 48 | + { |
| 49 | + if (_selection == null) |
| 50 | + return; |
| 51 | + |
| 52 | + string title = L10n.Get("ModInstall_PickerTitle") + "###AndroidModFilePicker"; |
| 53 | + if (_openPopup) |
| 54 | + { |
| 55 | + _openPopup = false; |
| 56 | + ImGui.OpenPopup(title); |
| 57 | + } |
| 58 | + |
| 59 | + var viewport = ImGui.GetMainViewport(); |
| 60 | + ImGui.SetNextWindowPos(viewport.GetCenter(), ImGuiCond.Appearing, new Vector2(0.5f)); |
| 61 | + ImGui.SetNextWindowSize(viewport.Size * 0.9f, ImGuiCond.Appearing); |
| 62 | + bool open = true; |
| 63 | + if (ImGui.BeginPopupModal(title, ref open)) |
| 64 | + { |
| 65 | + RenderDirectory(); |
| 66 | + ImGui.BeginDisabled(_selectedFile == null); |
| 67 | + if (ImGui.Button(L10n.Get("ModInstall_Confirm"))) |
| 68 | + { |
| 69 | + Finish(_selectedFile); |
| 70 | + ImGui.CloseCurrentPopup(); |
| 71 | + } |
| 72 | + ImGui.EndDisabled(); |
| 73 | + |
| 74 | + ImGui.SameLine(); |
| 75 | + if (ImGui.Button(L10n.Get("ModInstall_Cancel"))) |
| 76 | + { |
| 77 | + Finish(null); |
| 78 | + ImGui.CloseCurrentPopup(); |
| 79 | + } |
| 80 | + |
| 81 | + ImGui.EndPopup(); |
| 82 | + } |
| 83 | + |
| 84 | + if (!open || !ImGui.IsPopupOpen(title)) |
| 85 | + Finish(null); |
| 86 | + } |
| 87 | + |
| 88 | + private void RenderDirectory() |
| 89 | + { |
| 90 | + if (ImGui.Button(L10n.Get("ModInstall_Downloads"))) |
| 91 | + Navigate(_downloads); |
| 92 | + ImGui.SameLine(); |
| 93 | + if (ImGui.Button(L10n.Get("ModInstall_ImportFolder"))) |
| 94 | + Navigate(_imports); |
| 95 | + |
| 96 | + string? parent = Path.GetDirectoryName(_directory); |
| 97 | + ImGui.BeginDisabled(parent == null); |
| 98 | + if (ImGui.Button(L10n.Get("ModInstall_ParentFolder")) && parent != null) |
| 99 | + Navigate(parent); |
| 100 | + ImGui.EndDisabled(); |
| 101 | + |
| 102 | + ImGui.SameLine(); |
| 103 | + if (ImGui.Button(L10n.Get("ModInstall_Refresh"))) |
| 104 | + Navigate(_directory); |
| 105 | + |
| 106 | + ImGui.PushTextWrapPos(0); |
| 107 | + ImGui.TextUnformatted(_directory); |
| 108 | + ImGui.TextUnformatted(L10n.Get("ModInstall_ImportHint", _imports)); |
| 109 | + ImGui.PopTextWrapPos(); |
| 110 | + |
| 111 | + float availableHeight = ImGui.GetContentRegionAvail().Y - ImGui.GetFrameHeightWithSpacing(); |
| 112 | + if (ImGui.BeginChild("ModFiles", new Vector2(0, Math.Max(1, availableHeight)), ImGuiChildFlags.None)) |
| 113 | + RenderListing(); |
| 114 | + ImGui.EndChild(); |
| 115 | + } |
| 116 | + |
| 117 | + private void RenderListing() |
| 118 | + { |
| 119 | + if (_listing is not { IsCompleted: true } listing) |
| 120 | + { |
| 121 | + ImGui.TextUnformatted(L10n.Get("ModInstall_ReadingFolder")); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (!listing.IsCompletedSuccessfully) |
| 126 | + { |
| 127 | + ImGui.PushTextWrapPos(0); |
| 128 | + string detail = listing.Exception?.GetBaseException().Message ?? _directory; |
| 129 | + ImGui.TextUnformatted(L10n.Get("ModInstall_FolderReadError", detail)); |
| 130 | + ImGui.PopTextWrapPos(); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + if (_resetListScroll) |
| 135 | + { |
| 136 | + ImGui.SetScrollY(0); |
| 137 | + _resetListScroll = false; |
| 138 | + } |
| 139 | + |
| 140 | + DirectoryListing entries = listing.Result; |
| 141 | + if (entries.Folders.Length == 0 && entries.Files.Length == 0) |
| 142 | + ImGui.TextUnformatted(L10n.Get("ModInstall_EmptyFolder")); |
| 143 | + foreach (string folder in entries.Folders) |
| 144 | + { |
| 145 | + ImGui.PushID(folder); |
| 146 | + bool entered = ImGui.Selectable(Path.GetFileName(folder) + "/"); |
| 147 | + ImGui.PopID(); |
| 148 | + if (entered) |
| 149 | + { |
| 150 | + Navigate(folder); |
| 151 | + return; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + foreach (string file in entries.Files) |
| 156 | + { |
| 157 | + ImGui.PushID(file); |
| 158 | + if (ImGui.Selectable(Path.GetFileName(file), _selectedFile == file)) |
| 159 | + _selectedFile = file; |
| 160 | + ImGui.PopID(); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private void Navigate(string directory) |
| 165 | + { |
| 166 | + _directory = directory; |
| 167 | + _selectedFile = null; |
| 168 | + _resetListScroll = true; |
| 169 | + _listing = Task.Run(() => ReadDirectory(directory, directory == _imports)); |
| 170 | + } |
| 171 | + |
| 172 | + private static DirectoryListing ReadDirectory(string directory, bool create) |
| 173 | + { |
| 174 | + if (create) |
| 175 | + Directory.CreateDirectory(directory); |
| 176 | + |
| 177 | + return new DirectoryListing( |
| 178 | + Directory.GetDirectories(directory).Order(StringComparer.OrdinalIgnoreCase).ToArray(), |
| 179 | + Directory.EnumerateFiles(directory) |
| 180 | + .Where(path => Path.GetExtension(path).Equals(".zip", StringComparison.OrdinalIgnoreCase) |
| 181 | + || Path.GetExtension(path).Equals(".dll", StringComparison.OrdinalIgnoreCase)) |
| 182 | + .Order(StringComparer.OrdinalIgnoreCase).ToArray()); |
| 183 | + } |
| 184 | + |
| 185 | + private void Finish(string? path) |
| 186 | + { |
| 187 | + _selection?.TrySetResult(path); |
| 188 | + _selection = null; |
| 189 | + _listing = null; |
| 190 | + _selectedFile = null; |
| 191 | + } |
| 192 | +} |
0 commit comments