From 3a1c299867b4d0d6510c64502d9b096cdebafad0 Mon Sep 17 00:00:00 2001 From: Maiko Date: Sun, 28 Jan 2024 02:51:59 +0900 Subject: [PATCH] Save recent open singer/project directory --- OpenUtau.Core/Util/Preferences.cs | 2 + OpenUtau/FilePicker.cs | 75 +++++++++++++++++++++-- OpenUtau/Views/MainWindow.axaml.cs | 28 ++++----- OpenUtau/Views/PreferencesDialog.axaml.cs | 2 +- 4 files changed, 87 insertions(+), 20 deletions(-) diff --git a/OpenUtau.Core/Util/Preferences.cs b/OpenUtau.Core/Util/Preferences.cs index 3b0833224..b0c5a1a76 100644 --- a/OpenUtau.Core/Util/Preferences.cs +++ b/OpenUtau.Core/Util/Preferences.cs @@ -182,6 +182,8 @@ public class SerializablePreferences { public bool RememberVsqx = true; public int ImportTempo = 0; public string PhoneticAssistant = string.Empty; + public string RecentOpenSingerDirectory = string.Empty; + public string RecentOpenProjectDirectory = string.Empty; } } } diff --git a/OpenUtau/FilePicker.cs b/OpenUtau/FilePicker.cs index b540ec955..00cd9fddb 100644 --- a/OpenUtau/FilePicker.cs +++ b/OpenUtau/FilePicker.cs @@ -1,7 +1,9 @@ -using System.Linq; +using System.IO; +using System.Linq; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Platform.Storage; +using OpenUtau.Core.Util; namespace OpenUtau.App { internal class FilePicker { @@ -44,7 +46,7 @@ internal class FilePicker { public static FilePickerFileType DS { get; } = new("DS") { Patterns = new[] { "*.ds" }, }; - public static FilePickerFileType OUDEP { get; } = new("OpenUtau dependency"){ + public static FilePickerFileType OUDEP { get; } = new("OpenUtau dependency") { Patterns = new[] { "*.oudep" }, }; @@ -53,6 +55,28 @@ internal class FilePicker { return await OpenFile(window, titleKey, null, types); } + public async static Task OpenFileAboutSinger( + Window window, string titleKey, params FilePickerFileType[] types) { + var path = await OpenFile(window, titleKey, Preferences.Default.RecentOpenSingerDirectory, types); + var dir = Path.GetDirectoryName(path); + if (dir != null) { + Preferences.Default.RecentOpenSingerDirectory = dir; + Preferences.Save(); + } + return path; + } + + public async static Task OpenFileAboutProject( + Window window, string titleKey, params FilePickerFileType[] types) { + var path = await OpenFile(window, titleKey, Preferences.Default.RecentOpenProjectDirectory, types); + var dir = Path.GetDirectoryName(path); + if (dir != null) { + Preferences.Default.RecentOpenProjectDirectory = dir; + Preferences.Save(); + } + return path; + } + public async static Task OpenFile( Window window, string titleKey, string? startLocation, params FilePickerFileType[] types) { var location = startLocation == null @@ -71,13 +95,30 @@ internal class FilePicker { ?.FirstOrDefault(); } + public async static Task OpenFilesAboutProject( + Window window, string titleKey, params FilePickerFileType[] types) { + var result = await OpenFiles(window, titleKey, Preferences.Default.RecentOpenProjectDirectory, types); + if (result != null) { + var dir = Path.GetDirectoryName(result.FirstOrDefault()); + if (dir != null) { + Preferences.Default.RecentOpenProjectDirectory = dir; + Preferences.Save(); + } + } + return result; + } + public async static Task OpenFiles( - Window window, string titleKey, params FilePickerFileType[] types) { + Window window, string titleKey, string? startLocation, params FilePickerFileType[] types) { + var location = startLocation == null + ? null + : await window.StorageProvider.TryGetFolderFromPathAsync(startLocation); var files = await window.StorageProvider.OpenFilePickerAsync( new FilePickerOpenOptions() { Title = ThemeManager.GetString(titleKey), AllowMultiple = true, - FileTypeFilter = types + FileTypeFilter = types, + SuggestedStartLocation = location }); return files ?.Select(f => f.TryGetLocalPath()) @@ -85,11 +126,24 @@ internal class FilePicker { ?.ToArray(); } - public async static Task OpenFolder(Window window, string titleKey) { + public async static Task OpenFolderAboutSinger(Window window, string titleKey) { + var dir = await OpenFolder(window, titleKey, Preferences.Default.RecentOpenSingerDirectory); + if (dir != null) { + Preferences.Default.RecentOpenSingerDirectory = dir; + Preferences.Save(); + } + return dir; + } + + public async static Task OpenFolder(Window window, string titleKey, string? startLocation) { + var location = startLocation == null + ? null + : await window.StorageProvider.TryGetFolderFromPathAsync(startLocation); var dirs = await window.StorageProvider.OpenFolderPickerAsync( new FolderPickerOpenOptions { Title = ThemeManager.GetString(titleKey), AllowMultiple = false, + SuggestedStartLocation = location }); return dirs ?.Select(f => f.TryGetLocalPath()) @@ -102,6 +156,17 @@ public async static Task SaveFile return await SaveFile(window, titleKey, null, null, types); } + public async static Task SaveFileAboutProject + (Window window, string titleKey, params FilePickerFileType[] types) { + var path = await SaveFile(window, titleKey, Preferences.Default.RecentOpenProjectDirectory, null, types); + var dir = Path.GetDirectoryName(path); + if (dir != null) { + Preferences.Default.RecentOpenProjectDirectory = dir; + Preferences.Save(); + } + return path; + } + public async static Task SaveFile (Window window, string titleKey, string? startLocation, string? filename, params FilePickerFileType[] types) { var location = startLocation == null diff --git a/OpenUtau/Views/MainWindow.axaml.cs b/OpenUtau/Views/MainWindow.axaml.cs index cd49a5b44..40280f5e6 100644 --- a/OpenUtau/Views/MainWindow.axaml.cs +++ b/OpenUtau/Views/MainWindow.axaml.cs @@ -182,7 +182,7 @@ async void Open() { if (!DocManager.Inst.ChangesSaved && !await AskIfSaveAndContinue()) { return; } - var files = await FilePicker.OpenFiles( + var files = await FilePicker.OpenFilesAboutProject( this, "menu.file.open", FilePicker.ProjectFiles, FilePicker.USTX, @@ -251,7 +251,7 @@ public async Task Save() { async void OnMenuSaveAs(object sender, RoutedEventArgs args) => await SaveAs(); async Task SaveAs() { - var file = await FilePicker.SaveFile( + var file = await FilePicker.SaveFileAboutProject( this, "menu.file.saveas", FilePicker.USTX); if (!string.IsNullOrEmpty(file)) { viewModel.SaveProject(file); @@ -276,7 +276,7 @@ void OnMenuSaveTemplate(object sender, RoutedEventArgs args) { } async void OnMenuImportTracks(object sender, RoutedEventArgs args) { - var files = await FilePicker.OpenFiles( + var files = await FilePicker.OpenFilesAboutProject( this, "menu.file.importtracks", FilePicker.ProjectFiles, FilePicker.USTX, @@ -325,7 +325,7 @@ async void OnMenuImportTracks(object sender, RoutedEventArgs args) { } async void OnMenuImportAudio(object sender, RoutedEventArgs args) { - var file = await FilePicker.OpenFile( + var file = await FilePicker.OpenFileAboutProject( this, "menu.file.importaudio", FilePicker.AudioFiles); if (file == null) { return; @@ -339,7 +339,7 @@ async void OnMenuImportAudio(object sender, RoutedEventArgs args) { } async void OnMenuImportMidi(bool UseDrywetmidi = false) { - var file = await FilePicker.OpenFile( + var file = await FilePicker.OpenFileAboutProject( this, "menu.file.importmidi", FilePicker.MIDI); if (file == null) { return; @@ -362,7 +362,7 @@ void OnMenuImportMidiDrywetmidi(object sender, RoutedEventArgs args) { async void OnMenuExportMixdown(object sender, RoutedEventArgs args) { var project = DocManager.Inst.Project; - var file = await FilePicker.SaveFile( + var file = await FilePicker.SaveFileAboutProject( this, "menu.file.exportmixdown", FilePicker.WAV); if (!string.IsNullOrEmpty(file)) { await PlaybackManager.Inst.RenderMixdown(project, file); @@ -381,7 +381,7 @@ async void OnMenuExportWav(object sender, RoutedEventArgs args) { async void OnMenuExportWavTo(object sender, RoutedEventArgs args) { var project = DocManager.Inst.Project; - var file = await FilePicker.SaveFile( + var file = await FilePicker.SaveFileAboutProject( this, "menu.file.exportwavto", FilePicker.WAV); if (!string.IsNullOrEmpty(file)) { await PlaybackManager.Inst.RenderToFiles(project, file); @@ -390,7 +390,7 @@ async void OnMenuExportWavTo(object sender, RoutedEventArgs args) { async void OnMenuExportDsTo(object sender, RoutedEventArgs e) { var project = DocManager.Inst.Project; - var file = await FilePicker.SaveFile( + var file = await FilePicker.SaveFileAboutProject( this, "menu.file.exportds", FilePicker.DS); if (!string.IsNullOrEmpty(file)) { for (var i = 0; i < project.parts.Count; i++) { @@ -406,7 +406,7 @@ async void OnMenuExportDsTo(object sender, RoutedEventArgs e) { async void OnMenuExportDsV2To(object sender, RoutedEventArgs e) { var project = DocManager.Inst.Project; - var file = await FilePicker.SaveFile( + var file = await FilePicker.SaveFileAboutProject( this, "menu.file.exportds.v2", FilePicker.DS); if (!string.IsNullOrEmpty(file)) { for (var i = 0; i < project.parts.Count; i++) { @@ -422,7 +422,7 @@ async void OnMenuExportDsV2To(object sender, RoutedEventArgs e) { async void OnMenuExportDsV2WithoutPitchTo(object sender, RoutedEventArgs e) { var project = DocManager.Inst.Project; - var file = await FilePicker.SaveFile( + var file = await FilePicker.SaveFileAboutProject( this, "menu.file.exportds.v2withoutpitch", FilePicker.DS); if (!string.IsNullOrEmpty(file)) { for (var i = 0; i < project.parts.Count; i++) { @@ -455,7 +455,7 @@ async void OnMenuExportUst(object sender, RoutedEventArgs e) { async void OnMenuExportUstTo(object sender, RoutedEventArgs e) { var project = DocManager.Inst.Project; - var file = await FilePicker.SaveFile( + var file = await FilePicker.SaveFileAboutProject( this, "menu.file.exportustto", FilePicker.UST); if (!string.IsNullOrEmpty(file)) { for (var i = 0; i < project.parts.Count; i++) { @@ -471,7 +471,7 @@ async void OnMenuExportUstTo(object sender, RoutedEventArgs e) { async void OnMenuExportMidi(object sender, RoutedEventArgs e) { var project = DocManager.Inst.Project; - var file = await FilePicker.SaveFile( + var file = await FilePicker.SaveFileAboutProject( this, "menu.file.exportmidi", FilePicker.MIDI); if (!string.IsNullOrEmpty(file)) { MidiWriter.Save(file, project); @@ -554,7 +554,7 @@ public void OpenSingersWindow() { } async void OnMenuInstallSinger(object sender, RoutedEventArgs args) { - var file = await FilePicker.OpenFile( + var file = await FilePicker.OpenFileAboutSinger( this, "menu.tools.singer.install", FilePicker.ArchiveFiles); if (file == null) { return; @@ -1124,7 +1124,7 @@ void GotoFile(UPart part) { } async void ReplaceAudio(UPart part) { - var file = await FilePicker.OpenFile( + var file = await FilePicker.OpenFileAboutProject( this, "context.part.replaceaudio", FilePicker.AudioFiles); if (file == null) { return; diff --git a/OpenUtau/Views/PreferencesDialog.axaml.cs b/OpenUtau/Views/PreferencesDialog.axaml.cs index fdd83e584..6af188dce 100644 --- a/OpenUtau/Views/PreferencesDialog.axaml.cs +++ b/OpenUtau/Views/PreferencesDialog.axaml.cs @@ -15,7 +15,7 @@ void ResetAddlSingersPath(object sender, RoutedEventArgs e) { } async void SelectAddlSingersPath(object sender, RoutedEventArgs e) { - var path = await FilePicker.OpenFolder(this, "prefs.paths.addlsinger"); + var path = await FilePicker.OpenFolderAboutSinger(this, "prefs.paths.addlsinger"); if (string.IsNullOrEmpty(path)) { return; }