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 ;
@@ -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