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

Commit 222fcf2

Browse files
authored
Merge pull request #9210 from mono/fix985660-nssavepanel-delegate-catalina
[Mac][985660] Fix NSSavePanel crash on Catalina
2 parents a58bb42 + f611b9c commit 222fcf2

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;
@@ -73,11 +73,16 @@ protected override NSSavePanel OnCreatePanel (OpenFileDialogData data)
7373

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

96104
panel.SelectionDidChange += delegate {
@@ -111,9 +119,11 @@ public bool Run (OpenFileDialogData data)
111119

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

@@ -131,6 +141,8 @@ public bool Run (OpenFileDialogData data)
131141
}
132142
} catch (Exception ex) {
133143
LoggingService.LogInternalError ("Error in Open File dialog", ex);
144+
} finally {
145+
panelClosedSource?.Dispose ();
134146
}
135147
return true;
136148
}

0 commit comments

Comments
 (0)