1- //
1+ //
22// MacSelectFileDialogHandler.cs
33//
44// Author:
2828using System . Collections . Generic ;
2929using System . Linq ;
3030using System . Text ;
31-
31+ using System . Threading ;
3232using AppKit ;
3333using Foundation ;
3434using 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