using System.IO; using Chronos.App.Editor; using Chronos.App.Infrastructure; using Chronos.App.Shell; using Chronos.Core.Diagnostics; using Chronos.Core.Localization; namespace Chronos.App.Workspace; /// /// Choosing another workspace folder (owner review 2, R8): Settings › Workspace › Change, and the refusal of a folder /// inside a library, where Edit cannot be used until one is chosen. The folder is picked, its plan shown and confirmed /// (the projects the folder in use keeps, those the new one holds), a workspace project open in the editor closes (its /// last edit stored first), and only then does the setting change. Nothing is moved or deleted: the projects of the /// folder left stay there, listed again once it is chosen back. Moving a workspace with its projects comes in phase 5c. /// public static class WorkspaceFolderChange { /// The shell's flow: its folder picker and confirmation, its editor, its toasts. public static async Task ChooseAsync(ShellViewModel shell) { ArgumentNullException.ThrowIfNull(shell); var changed = await ChooseAsync(shell.Services.Workspace, shell.Services.Dialogs, async () => { if (shell.Sequence.Editor?.Session.Source is not WorkspaceSequenceSource) return true; if (!await shell.Sequence.ConfirmCloseAsync()) return false; await shell.Sequence.CloseAsync(); shell.KeepSequence(null); return true; }, message => shell.Notify(Loc.L("Settings.Workspace.NotChanged"), message, isError: true)); if (changed) shell.Notify(Loc.L("Settings.Workspace.Changed"), shell.Services.Workspace.Root); return changed; } /// /// Picks a folder through , confirms its plan, lets close a /// workspace project open in the editor (false keeps everything as it is), then makes it the workspace's folder. /// hears why a folder was refused. True when the folder changed. /// public static async Task ChooseAsync(WorkspaceHost host, IAppDialogs dialogs, Func> closeProject, Action failed) { ArgumentNullException.ThrowIfNull(host); ArgumentNullException.ThrowIfNull(dialogs); ArgumentNullException.ThrowIfNull(closeProject); ArgumentNullException.ThrowIfNull(failed); if (dialogs.PickFolder(Loc.L("Settings.Workspace.ChooseTitle"), Directory.Exists(host.Root) ? host.Root : null) is not { } folder) return false; try { var plan = await host.PlanFolderAsync(folder); if (plan.IsSame) return false; if (plan.InsideLibrary) { failed(Loc.L("Errors.Workspace.InsideLibraryFolder", plan.Folder)); return false; } if (!dialogs.Confirm(Request(plan))) return false; if (!await closeProject()) return false; await host.UseFolderAsync(plan.Folder); return true; } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or InvalidDataException or ArgumentException or WorkspaceException) { Log.Warning("workspace", $"The workspace folder was not changed to {folder}", ex); failed(ex is WorkspaceException ? ex.Message : Loc.L("Errors.Workspace.FolderUnusable", folder)); return false; } } /// The plan as a confirmation: where Edit's projects come from now, and what stays where it is. public static ConfirmRequest Request(WorkspaceFolderPlan plan) { ArgumentNullException.ThrowIfNull(plan); return new ConfirmRequest { Heading = Loc.L("Settings.Workspace.ChangeTitle"), Message = Loc.L("Settings.Workspace.ChangeMessage", plan.Folder, plan.Current), Detail = string.Join(Environment.NewLine, Loc.L("Settings.Workspace.ProjectsLeft", Formatting.Count(plan.ProjectsLeft)), Loc.L("Settings.Workspace.ProjectsFound", Formatting.Count(plan.ProjectsFound))), IsDetailMonospaced = false, SafeText = Loc.L("Archive.Dialog.Cancel"), SafeToolTip = Loc.L("Tooltips.Settings.KeepWorkspace"), ActionText = Loc.L("Settings.Workspace.Use"), ActionToolTip = Loc.L("Tooltips.Settings.UseWorkspace"), }; } }