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

Commit f611b9c

Browse files
committed
[Mac] Check for NSOpenSavePanelUrlEventArgs.NewDirectoryUrl being NSNull
If the file search inside an NSSavePanel is triggered on Catalina for some reason NSOpenSavePanelUrlEventArgs.NewDirectoryUrl might be NSNull and not NSUrl. Trying to access any of its properties results in a runtime crash. Fixes VSTS #1015827
1 parent e35794a commit f611b9c

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// MacSelectFileDialogHandler.cs
33
//
44
// Author:
@@ -77,29 +77,29 @@ public bool Run (OpenFileDialogData data)
7777
try {
7878
using (var panel = CreatePanel (data, out var state)) {
7979
bool pathAlreadySet = false;
80-
//TODO: DidChangeToDirectory is broken on Catalina
81-
if (MacSystemInformation.OsVersion < MacSystemInformation.Catalina) {
82-
var panelClosedToken = panelClosedSource.Token;
83-
panel.DidChangeToDirectory += (sender, e) => {
84-
var directoryPath = e.NewDirectoryUrl?.AbsoluteString;
85-
if (string.IsNullOrEmpty (directoryPath))
86-
return;
87-
var selectedPath = data.OnDirectoryChanged (this, directoryPath);
88-
if (selectedPath.IsNull)
89-
return;
90-
data.SelectedFiles = new FilePath [] { selectedPath };
91-
pathAlreadySet = true;
80+
var panelClosedToken = panelClosedSource.Token;
81+
panel.DidChangeToDirectory += (sender, e) => {
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;
86+
if (string.IsNullOrEmpty (directoryPath))
87+
return;
88+
var selectedPath = data.OnDirectoryChanged (this, directoryPath);
89+
if (selectedPath.IsNull)
90+
return;
91+
data.SelectedFiles = new FilePath [] { selectedPath };
92+
pathAlreadySet = true;
9293

93-
// We need to call Cancel on 1ms delay so it's executed after DidChangeToDirectory event handler is finished
94-
// this is needed because it's possible that DidChangeToDirectory event is executed while dialog is opening
95-
// in that case calling .Cancel() leaves dialog in weird state...
96-
// Fun fact: DidChangeToDirectory event is called from Open on 10.12 but not on 10.13
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);
101-
};
102-
}
94+
// We need to call Cancel on 1ms delay so it's executed after DidChangeToDirectory event handler is finished
95+
// this is needed because it's possible that DidChangeToDirectory event is executed while dialog is opening
96+
// in that case calling .Cancel() leaves dialog in weird state...
97+
// Fun fact: DidChangeToDirectory event is called from Open on 10.12 but not on 10.13
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);
102+
};
103103

104104
panel.SelectionDidChange += delegate {
105105
var selection = MacSelectFileDialogHandler.GetSelectedFiles (panel);

0 commit comments

Comments
 (0)