Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion OpenUtau/FilePicker.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using OpenUtau.Core;
using OpenUtau.Core.Util;
#if MACOS
using MonoMac.Foundation;
using MonoMac.AppKit;
#endif

namespace OpenUtau.App {
internal class FilePicker {
#if MACOS
private static int NSAppInitState;
#endif
public static FilePickerFileType ProjectFiles { get; } = new("Project Files") {
Patterns = new[] { "*.ustx", "*.vsqx", "*.ust", "*.mid", "*.midi", "*.ufdata", "*.musicxml" },
};
Expand Down Expand Up @@ -45,6 +54,11 @@ internal class FilePicker {
Patterns = new[] { "*.exe" },
};
public static FilePickerFileType APP { get; } = new("APP") {
AppleUniformTypeIdentifiers = new[] {
"com.apple.application-bundle",
"com.apple.package",
"public.executable"
},
Patterns = new[] { "*.app" },
};
public static FilePickerFileType PrefixMap { get; } = new("Prefix Map") {
Expand Down Expand Up @@ -93,6 +107,45 @@ internal class FilePicker {
var location = startLocation == null
? null
: await window.StorageProvider.TryGetFolderFromPathAsync(startLocation);
#if MACOS
// Due to an avalonia bug, we need to call the native API for looking for APP
if (types.Contains(APP)) {
var tcs = new TaskCompletionSource<string?>();
var title = ThemeManager.GetString(titleKey);
if (NSAppInitState.Equals(0)) {
NSAppInitState = 1;
NSApplication.Init();
}

NSApplication.SharedApplication.InvokeOnMainThread(() => {
try {
var panel = new NSOpenPanel();
panel.Title = title;
panel.CanChooseFiles = true;
panel.CanChooseDirectories = false;
panel.AllowsMultipleSelection = false;
panel.TreatsFilePackagesAsDirectories = false;

if (!string.IsNullOrEmpty(startLocation)) {
panel.DirectoryUrl = NSUrl.FromFilename(startLocation);
}

var result = panel.RunModal();
if (result == 1) {
tcs.SetResult(panel.Urls.FirstOrDefault()?.Path);
} else {
tcs.SetResult(null);
}
} catch (Exception ex) {
Console.WriteLine($"Exception: {ex}");
tcs.SetException(ex);
}
});

return await tcs.Task;
}
#endif

var files = await window.StorageProvider.OpenFilePickerAsync(
new FilePickerOpenOptions() {
Title = ThemeManager.GetString(titleKey),
Expand Down
5 changes: 5 additions & 0 deletions OpenUtau/OpenUtau.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@
<AvaloniaResource Include="Assets\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia.Native" Version="11.2.4" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.2.4" />
<PackageReference Include="Dotnet.Bundle" Version="0.9.13" />
<PackageReference Include="Material.Avalonia" Version="3.13.3" />
<PackageReference Include="MonoMac.Core" Version="0.0.23728" />
<PackageReference Include="NetSparkleUpdater.SparkleUpdater" Version="2.2.3" />
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41" />
<PackageReference Include="Avalonia" Version="11.2.4" />
Expand Down Expand Up @@ -125,4 +127,7 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<PropertyGroup>
<DefineConstants Condition="$([MSBuild]::IsOSPlatform('OSX'))">$(DefineConstants);MACOS</DefineConstants>
</PropertyGroup>
</Project>
Loading