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

Commit 9ad92b6

Browse files
sevokumonojenkins
authored andcommitted
[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 75312a9 commit 9ad92b6

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:
@@ -76,29 +76,29 @@ public bool Run (OpenFileDialogData data)
7676
try {
7777
using (var panel = CreatePanel (data, out var state)) {
7878
bool pathAlreadySet = false;
79-
//TODO: DidChangeToDirectory is broken on Catalina
80-
if (MacSystemInformation.OsVersion < MacSystemInformation.Catalina) {
81-
var panelClosedToken = panelClosedSource.Token;
82-
panel.DidChangeToDirectory += (sender, e) => {
83-
var directoryPath = e.NewDirectoryUrl?.AbsoluteString;
84-
if (string.IsNullOrEmpty (directoryPath))
85-
return;
86-
var selectedPath = data.OnDirectoryChanged (this, directoryPath);
87-
if (selectedPath.IsNull)
88-
return;
89-
data.SelectedFiles = new FilePath [] { selectedPath };
90-
pathAlreadySet = true;
79+
var panelClosedToken = panelClosedSource.Token;
80+
panel.DidChangeToDirectory += (sender, e) => {
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;
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;
9192

92-
// We need to call Cancel on 1ms delay so it's executed after DidChangeToDirectory event handler is finished
93-
// this is needed because it's possible that DidChangeToDirectory event is executed while dialog is opening
94-
// in that case calling .Cancel() leaves dialog in weird state...
95-
// Fun fact: DidChangeToDirectory event is called from Open on 10.12 but not on 10.13
96-
System.Threading.Tasks.Task.Delay (1).ContinueWith (delegate {
97-
if (!panelClosedToken.IsCancellationRequested)
98-
panel.Cancel (panel);
99-
}, panelClosedToken, System.Threading.Tasks.TaskContinuationOptions.None, Runtime.MainTaskScheduler);
100-
};
101-
}
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+
};
102102

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

0 commit comments

Comments
 (0)