@@ -73,30 +73,33 @@ 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 {
77- using ( var panel = CreatePanel ( data , out var state ) )
78- using ( var panelClosedSource = new CancellationTokenSource ( ) ) {
78+ using ( var panel = CreatePanel ( data , out var state ) ) {
7979 bool pathAlreadySet = false ;
80- var panelClosedToken = panelClosedSource . Token ;
81- panel . DidChangeToDirectory += ( sender , e ) => {
82- var directoryPath = e . NewDirectoryUrl ? . AbsoluteString ;
83- if ( string . IsNullOrEmpty ( directoryPath ) )
84- return ;
85- var selectedPath = data . OnDirectoryChanged ( this , directoryPath ) ;
86- if ( selectedPath . IsNull )
87- return ;
88- data . SelectedFiles = new FilePath [ ] { selectedPath } ;
89- pathAlreadySet = true ;
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 ;
9092
91- // We need to call Cancel on 1ms delay so it's executed after DidChangeToDirectory event handler is finished
92- // this is needed because it's possible that DidChangeToDirectory event is executed while dialog is opening
93- // in that case calling .Cancel() leaves dialog in weird state...
94- // Fun fact: DidChangeToDirectory event is called from Open on 10.12 but not on 10.13
95- System . Threading . Tasks . Task . Delay ( 1 ) . ContinueWith ( delegate {
96- if ( ! panelClosedToken . IsCancellationRequested )
97- panel . Cancel ( panel ) ;
98- } , panelClosedToken , System . Threading . Tasks . TaskContinuationOptions . None , Runtime . MainTaskScheduler ) ;
99- } ;
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+ }
100103
101104 panel . SelectionDidChange += delegate {
102105 var selection = MacSelectFileDialogHandler . GetSelectedFiles ( panel ) ;
@@ -116,11 +119,11 @@ public bool Run (OpenFileDialogData data)
116119
117120 // TODO: support for data.CenterToParent, we could use sheeting.
118121 if ( panel . RunModal ( ) == 0 && ! pathAlreadySet ) {
119- panelClosedSource . Cancel ( ) ;
122+ panelClosedSource ? . Cancel ( ) ;
120123 IdeServices . DesktopService . FocusWindow ( parent ) ;
121124 return false ;
122125 }
123- panelClosedSource . Cancel ( ) ;
126+ panelClosedSource ? . Cancel ( ) ;
124127 if ( ! pathAlreadySet )
125128 data . SelectedFiles = MacSelectFileDialogHandler . GetSelectedFiles ( panel ) ;
126129
@@ -138,6 +141,8 @@ public bool Run (OpenFileDialogData data)
138141 }
139142 } catch ( Exception ex ) {
140143 LoggingService . LogInternalError ( "Error in Open File dialog" , ex ) ;
144+ } finally {
145+ panelClosedSource ? . Dispose ( ) ;
141146 }
142147 return true ;
143148 }
0 commit comments