Skip to content
This repository was archived by the owner on Oct 4, 2021. It is now read-only.

Commit d5e8667

Browse files
authored
Merge pull request #9212 from mono/backport-pr-9210-to-release-8.3
[release-8.3] [Mac][985660] Fix NSSavePanel crash on Catalina
2 parents c2a65dd + 9ad92b6 commit d5e8667

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

main/src/addins/MacPlatform/Dialogs/MacOpenFileDialogHandler.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// MacSelectFileDialogHandler.cs
33
//
44
// Author:
@@ -28,7 +28,7 @@
2828
using System.Collections.Generic;
2929
using System.Linq;
3030
using System.Text;
31-
31+
using System.Threading;
3232
using AppKit;
3333
using Foundation;
3434
using MonoDevelop.Components;
@@ -72,11 +72,16 @@ protected override NSSavePanel OnCreatePanel (OpenFileDialogData data)
7272

7373
public bool Run (OpenFileDialogData data)
7474
{
75+
var panelClosedSource = MacSystemInformation.OsVersion < MacSystemInformation.Catalina ? new CancellationTokenSource () : null;
7576
try {
7677
using (var panel = CreatePanel (data, out var state)) {
7778
bool pathAlreadySet = false;
79+
var panelClosedToken = panelClosedSource.Token;
7880
panel.DidChangeToDirectory += (sender, e) => {
79-
var directoryPath = e.NewDirectoryUrl?.AbsoluteString;
81+
// HACK: On Catalina e.NewDirectoryUrl might be NSNull instead of null
82+
if (e.NewDirectoryUrl == null || ((NSObject)e.NewDirectoryUrl) is NSNull)
83+
return;
84+
var directoryPath = e.NewDirectoryUrl.AbsoluteString;
8085
if (string.IsNullOrEmpty (directoryPath))
8186
return;
8287
var selectedPath = data.OnDirectoryChanged (this, directoryPath);
@@ -89,7 +94,10 @@ public bool Run (OpenFileDialogData data)
8994
// this is needed because it's possible that DidChangeToDirectory event is executed while dialog is opening
9095
// in that case calling .Cancel() leaves dialog in weird state...
9196
// Fun fact: DidChangeToDirectory event is called from Open on 10.12 but not on 10.13
92-
System.Threading.Tasks.Task.Delay (1).ContinueWith (delegate { panel.Cancel (panel); }, Runtime.MainTaskScheduler);
97+
System.Threading.Tasks.Task.Delay (1).ContinueWith (delegate {
98+
if (!panelClosedToken.IsCancellationRequested)
99+
panel.Cancel (panel);
100+
}, panelClosedToken, System.Threading.Tasks.TaskContinuationOptions.None, Runtime.MainTaskScheduler);
93101
};
94102

95103
panel.SelectionDidChange += delegate {
@@ -110,9 +118,11 @@ public bool Run (OpenFileDialogData data)
110118

111119
// TODO: support for data.CenterToParent, we could use sheeting.
112120
if (panel.RunModal () == 0 && !pathAlreadySet) {
121+
panelClosedSource?.Cancel ();
113122
IdeServices.DesktopService.FocusWindow (parent);
114123
return false;
115124
}
125+
panelClosedSource?.Cancel ();
116126
if (!pathAlreadySet)
117127
data.SelectedFiles = MacSelectFileDialogHandler.GetSelectedFiles (panel);
118128

@@ -130,6 +140,8 @@ public bool Run (OpenFileDialogData data)
130140
}
131141
} catch (Exception ex) {
132142
LoggingService.LogInternalError ("Error in Open File dialog", ex);
143+
} finally {
144+
panelClosedSource?.Dispose ();
133145
}
134146
return true;
135147
}

0 commit comments

Comments
 (0)