Skip to content

Commit

Permalink
Merge pull request stakira#1024 from maiko3tattun/0128_RecentOpenDor
Browse files Browse the repository at this point in the history
Save recent open singer/project directory
  • Loading branch information
stakira authored Jan 28, 2024
2 parents fae27c8 + 3a1c299 commit 94213d7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 20 deletions.
2 changes: 2 additions & 0 deletions OpenUtau.Core/Util/Preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,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;
}
}
}
75 changes: 70 additions & 5 deletions OpenUtau/FilePicker.cs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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" },
};

Expand All @@ -53,6 +55,28 @@ internal class FilePicker {
return await OpenFile(window, titleKey, null, types);
}

public async static Task<string?> 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<string?> 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<string?> OpenFile(
Window window, string titleKey, string? startLocation, params FilePickerFileType[] types) {
var location = startLocation == null
Expand All @@ -71,25 +95,55 @@ internal class FilePicker {
?.FirstOrDefault();
}

public async static Task<string[]?> 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<string[]?> 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())
?.OfType<string>()
?.ToArray();
}

public async static Task<string?> OpenFolder(Window window, string titleKey) {
public async static Task<string?> 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<string?> 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())
Expand All @@ -102,6 +156,17 @@ public async static Task<string?> SaveFile
return await SaveFile(window, titleKey, null, null, types);
}

public async static Task<string?> 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<string?> SaveFile
(Window window, string titleKey, string? startLocation, string? filename, params FilePickerFileType[] types) {
var location = startLocation == null
Expand Down
28 changes: 14 additions & 14 deletions OpenUtau/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand All @@ -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++) {
Expand Down Expand Up @@ -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++) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion OpenUtau/Views/PreferencesDialog.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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;
}
Expand Down

0 comments on commit 94213d7

Please sign in to comment.