Fix file dialogs crashing on macOS

This commit is contained in:
Pieter-Jan Briers
2022-03-20 16:46:33 +01:00
parent 650eceea7e
commit c9ccd0b873
6 changed files with 50 additions and 6 deletions

View File

@@ -437,6 +437,13 @@ namespace Robust.Client.Graphics.Clyde
_windowing!.WindowSetVisible(reg, visible);
}
public void RunOnWindowThread(Action a)
{
DebugTools.AssertNotNull(_windowing);
_windowing!.RunOnWindowThread(a);
}
private abstract class WindowReg
{
public bool IsDisposed;

View File

@@ -251,6 +251,11 @@ namespace Robust.Client.Graphics.Clyde
// Nada.
}
public void RunOnWindowThread(Action action)
{
action();
}
private sealed class DummyCursor : ICursor
{
public void Dispose()

View File

@@ -118,6 +118,10 @@ namespace Robust.Client.Graphics.Clyde
case CmdWinCursorSet cmd:
WinThreadWinCursorSet(cmd);
break;
case CmdRunAction cmd:
cmd.Action();
break;
}
}
@@ -169,6 +173,11 @@ namespace Robust.Client.Graphics.Clyde
}
}
public void RunOnWindowThread(Action action)
{
SendCmd(new CmdRunAction(action));
}
private abstract record CmdBase;
private sealed record CmdTerminate : CmdBase;
@@ -245,6 +254,10 @@ namespace Robust.Client.Graphics.Clyde
private sealed record CmdCursorDestroy(
ClydeHandle Cursor
) : CmdBase;
private sealed record CmdRunAction(
Action Action
) : CmdBase;
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using Robust.Client.Input;
using Robust.Shared.Maths;
using SixLabors.ImageSharp;
@@ -58,6 +59,9 @@ namespace Robust.Client.Graphics.Clyde
void GLMakeContextCurrent(WindowReg? reg);
void GLSwapInterval(int interval);
unsafe void* GLGetProcAddress(string procName);
// Misc
void RunOnWindowThread(Action a);
}
}
}

View File

@@ -63,5 +63,7 @@ namespace Robust.Client.Graphics
uint? GetX11WindowId();
void RegisterGridEcsEvents();
void RunOnWindowThread(Action action);
}
}

View File

@@ -8,6 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using Robust.Client.Graphics;
using Robust.Shared;
using Robust.Shared.Console;
using Robust.Shared.Asynchronous;
using Robust.Shared.IoC;
using Robust.Shared.Log;
@@ -164,12 +165,11 @@ namespace Robust.Client.UserInterface
{
if (OperatingSystem.IsMacOS())
{
// macOS seems pretty annoying about having the file dialog opened from the main thread.
// So we are forced to execute this synchronously on the main thread.
// Also I'm calling RunOnMainThread here to provide safety in case this is ran from a different thread.
// nativefiledialog doesn't provide any form of async API, so this WILL lock up the client.
// macOS seems pretty annoying about having the file dialog opened from the main windowing thread.
// So we are forced to execute this synchronously on the main windowing thread.
// nativefiledialog doesn't provide any form of async API, so this WILL lock up half the client.
var tcs = new TaskCompletionSource<string?>();
_taskManager.RunOnMainThread(() => tcs.SetResult(action()));
_clyde.RunOnWindowThread(() => tcs.SetResult(action()));
return tcs.Task;
}
@@ -388,4 +388,17 @@ namespace Robust.Client.UserInterface
SW_NFD_CANCEL,
}
}
public sealed class OpenFileCommand : IConsoleCommand
{
public string Command => "testopenfile";
public string Description => "";
public string Help => "";
public async void Execute(IConsoleShell shell, string argStr, string[] args)
{
var stream = await IoCManager.Resolve<IFileDialogManager>().OpenFile();
stream?.Dispose();
}
}
}