1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . ComponentModel ;
4
- using System . Linq ;
5
- using System . Runtime . CompilerServices ;
6
4
using System . Threading . Tasks ;
7
5
using Android . Content ;
8
6
using Android . Graphics . Drawables ;
9
7
using Android . OS ;
10
- using Android . Runtime ;
11
8
using Android . Views ;
12
- using Android . Views . Animations ;
13
9
using AndroidX . Activity ;
14
- using AndroidX . AppCompat . App ;
15
10
using AndroidX . Fragment . App ;
16
11
using Microsoft . Maui . LifecycleEvents ;
17
- using AAnimation = Android . Views . Animations . Animation ;
18
12
using AColor = Android . Graphics . Color ;
19
13
using AView = Android . Views . View ;
20
14
@@ -23,9 +17,6 @@ namespace Microsoft.Maui.Controls.Platform
23
17
internal partial class ModalNavigationManager
24
18
{
25
19
ViewGroup ? _modalParentView ;
26
- bool _navAnimationInProgress ;
27
- internal const string CloseContextActionsSignalName = "Xamarin.CloseContextActions" ;
28
- AAnimation ? _dismissAnimation ;
29
20
bool _platformActivated ;
30
21
31
22
readonly Stack < string > _modals = [ ] ;
@@ -44,8 +35,6 @@ void OnWindowPropertyChanging(object sender, PropertyChangingEventArgs e)
44
35
return ;
45
36
}
46
37
47
- var handler = _currentPage ? . Handler ;
48
- var windowP = _window . Page ;
49
38
if ( CurrentPage is not null &&
50
39
_window . Page != CurrentPage )
51
40
{
@@ -89,76 +78,45 @@ internal void SetModalParentView(ViewGroup viewGroup)
89
78
_modalParentView = viewGroup ;
90
79
}
91
80
92
- ViewGroup GetModalParentView ( )
93
- {
94
- return _modalParentView ??
95
- _window ? . PlatformActivity ? . Window ? . DecorView as ViewGroup ??
96
- throw new InvalidOperationException ( "Root View Needs to be set" ) ;
97
- }
98
-
99
- // AFAICT this is specific to ListView and Context Items
100
- internal bool NavAnimationInProgress
101
- {
102
- get { return _navAnimationInProgress ; }
103
- set
104
- {
105
- if ( _navAnimationInProgress == value )
106
- return ;
107
- _navAnimationInProgress = value ;
108
-
109
- #pragma warning disable CS0618 // TODO: Remove when we internalize/replace MessagingCenter
110
- if ( value )
111
- MessagingCenter . Send ( this , CloseContextActionsSignalName ) ;
112
- #pragma warning restore CS0618 // Type or member is obsolete
113
- }
114
- }
115
-
116
81
Task < Page > PopModalPlatformAsync ( bool animated )
117
82
{
118
83
Page modal = CurrentPlatformModalPage ;
119
84
_platformModalPages . Remove ( modal ) ;
120
85
86
+ TaskCompletionSource < Page > tcs = new ( ) ;
87
+
121
88
var fragmentManager = WindowMauiContext . GetFragmentManager ( ) ;
122
89
123
90
var dialogFragmentId = _modals . Pop ( ) ;
124
91
var dialogFragment = ( ModalFragment ? ) fragmentManager . FindFragmentByTag ( dialogFragmentId ) ;
125
-
126
- // If for the dialog is null what we want to do?
92
+
127
93
if ( dialogFragment is null )
128
94
{
129
- return Task . FromResult ( modal ) ;
95
+ tcs . TrySetResult ( modal ) ;
96
+ return tcs . Task ;
130
97
}
131
98
132
- var source = new TaskCompletionSource < Page > ( ) ;
99
+ EventHandler ? OnDialogDismiss = null ;
133
100
134
- if ( animated && dialogFragment . View is not null )
101
+ OnDialogDismiss = ( _ , _ ) =>
135
102
{
136
- _dismissAnimation ??= AnimationUtils . LoadAnimation ( WindowMauiContext . Context , Resource . Animation . nav_modal_default_exit_anim ) ! ;
103
+ dialogFragment . DialogDismissEvent -= OnDialogDismiss ;
104
+ tcs . TrySetResult ( modal ) ;
105
+ } ;
137
106
138
- _dismissAnimation . AnimationEnd += OnAnimationEnded ;
107
+ dialogFragment . DialogDismissEvent += OnDialogDismiss ;
139
108
140
- dialogFragment . View . StartAnimation ( _dismissAnimation ) ;
109
+ if ( animated )
110
+ {
111
+ dialogFragment . Dialog ? . Window ? . SetWindowAnimations ( Resource . Style . modal_exit_animation ) ;
112
+ modal . Dispatcher . Dispatch ( ( ) => dialogFragment . Dismiss ( ) ) ;
141
113
}
142
114
else
143
115
{
144
116
dialogFragment . Dismiss ( ) ;
145
- source . TrySetResult ( modal ) ;
146
117
}
147
118
148
- return source . Task ;
149
-
150
- void OnAnimationEnded ( object ? sender , AAnimation . AnimationEndEventArgs e )
151
- {
152
- if ( sender is not AAnimation animation )
153
- {
154
- return ;
155
- }
156
-
157
- animation . AnimationEnd -= OnAnimationEnded ;
158
- dialogFragment . Dismiss ( ) ;
159
- source . TrySetResult ( modal ) ;
160
- _dismissAnimation = null ;
161
- }
119
+ return tcs . Task ;
162
120
}
163
121
164
122
// The CurrentPage doesn't represent the root of the platform hierarchy.
@@ -192,8 +150,6 @@ async Task PresentModal(Page modal, bool animated)
192
150
{
193
151
TaskCompletionSource < bool > animationCompletionSource = new ( ) ;
194
152
195
- var parentView = GetModalParentView ( ) ;
196
-
197
153
var dialogFragment = new ModalFragment ( WindowMauiContext , modal )
198
154
{
199
155
Cancelable = false ,
@@ -203,39 +159,30 @@ async Task PresentModal(Page modal, bool animated)
203
159
var fragmentManager = WindowMauiContext . GetFragmentManager ( ) ;
204
160
var dialogFragmentId = AView . GenerateViewId ( ) . ToString ( ) ;
205
161
_modals . Push ( dialogFragmentId ) ;
206
- dialogFragment . Show ( fragmentManager , dialogFragmentId ) ;
207
-
208
- if ( animated )
209
- {
210
- NavAnimationInProgress = true ;
211
- dialogFragment ! . AnimationEnded += OnAnimationEnded ;
212
162
213
- await animationCompletionSource . Task ;
214
- }
215
- else
216
- {
217
- NavAnimationInProgress = false ;
218
- animationCompletionSource . TrySetResult ( true ) ;
219
- }
163
+ EventHandler ? OnDialogShown = null ;
220
164
221
- void OnAnimationEnded ( object ? sender , EventArgs e )
165
+ OnDialogShown = ( _ , _ ) =>
222
166
{
223
- dialogFragment ! . AnimationEnded -= OnAnimationEnded ;
224
- NavAnimationInProgress = false ;
167
+ dialogFragment ! . DialogShowEvent -= OnDialogShown ;
225
168
animationCompletionSource . SetResult ( true ) ;
226
- }
169
+ } ;
170
+
171
+ dialogFragment ! . DialogShowEvent += OnDialogShown ;
172
+
173
+ dialogFragment . Show ( fragmentManager , dialogFragmentId ) ;
174
+
175
+ await animationCompletionSource . Task ;
227
176
}
228
177
229
178
internal class ModalFragment : DialogFragment
230
179
{
231
180
Page _modal ;
232
181
IMauiContext _mauiWindowContext ;
233
182
NavigationRootManager ? _navigationRootManager ;
183
+ public event EventHandler ? DialogShowEvent ;
184
+ public event EventHandler ? DialogDismissEvent ;
234
185
static readonly ColorDrawable TransparentColorDrawable = new ( AColor . Transparent ) ;
235
- bool _pendingAnimation = true ;
236
-
237
- public event EventHandler ? AnimationEnded ;
238
-
239
186
240
187
public bool IsAnimated { get ; internal set ; }
241
188
@@ -263,6 +210,20 @@ public ModalFragment(IMauiContext mauiContext, Page modal)
263
210
{
264
211
dialog . Window . SetSoftInputMode ( attributes . SoftInputMode ) ;
265
212
}
213
+ EventHandler ? OnDialogShown = null ;
214
+
215
+ OnDialogShown = ( _ , _ ) =>
216
+ {
217
+ dialog . ShowEvent -= OnDialogShown ;
218
+ DialogShowEvent ? . Invoke ( this , EventArgs . Empty ) ;
219
+ } ;
220
+
221
+ dialog . ShowEvent += OnDialogShown ;
222
+
223
+ if ( IsAnimated )
224
+ {
225
+ dialog . Window ? . SetWindowAnimations ( Resource . Style . modal_enter_animation ) ;
226
+ }
266
227
267
228
if ( mainActivityWindow is not null )
268
229
{
@@ -356,29 +317,13 @@ public override void OnStart()
356
317
int width = ViewGroup . LayoutParams . MatchParent ;
357
318
int height = ViewGroup . LayoutParams . MatchParent ;
358
319
dialog . Window . SetLayout ( width , height ) ;
359
-
360
- if ( IsAnimated )
361
- {
362
- var animation = AnimationUtils . LoadAnimation ( _mauiWindowContext . Context , Resource . Animation . nav_modal_default_enter_anim ) ! ;
363
- View . StartAnimation ( animation ) ;
364
-
365
- animation . AnimationEnd += OnAnimationEnded ;
366
- }
367
-
368
- void OnAnimationEnded ( object ? sender , AAnimation . AnimationEndEventArgs e )
369
- {
370
- if ( sender is not AAnimation animation )
371
- {
372
- return ;
373
- }
374
-
375
- animation . AnimationEnd -= OnAnimationEnded ;
376
- FireAnimationEnded ( ) ;
377
- }
378
320
}
379
321
380
322
public override void OnDismiss ( IDialogInterface dialog )
381
323
{
324
+ base . OnDismiss ( dialog ) ;
325
+ DialogDismissEvent ? . Invoke ( this , EventArgs . Empty ) ;
326
+ DialogDismissEvent = null ;
382
327
_modal . PropertyChanged -= OnModalPagePropertyChanged ;
383
328
_modal . HandlerChanged -= OnPageHandlerChanged ;
384
329
@@ -391,28 +336,9 @@ public override void OnDismiss(IDialogInterface dialog)
391
336
_modal = null ! ;
392
337
_mauiWindowContext = null ! ;
393
338
_navigationRootManager ? . Disconnect ( ) ;
394
- _navigationRootManager = null ;
395
- base . OnDismiss ( dialog ) ;
396
- }
397
-
398
- public override void OnDestroy ( )
399
- {
400
- base . OnDestroy ( ) ;
401
- FireAnimationEnded ( ) ;
339
+ _navigationRootManager = null ;
402
340
}
403
341
404
- void FireAnimationEnded ( )
405
- {
406
- if ( ! _pendingAnimation )
407
- {
408
- return ;
409
- }
410
-
411
- _pendingAnimation = false ;
412
- AnimationEnded ? . Invoke ( this , EventArgs . Empty ) ;
413
- }
414
-
415
-
416
342
sealed class CustomComponentDialog : ComponentDialog
417
343
{
418
344
public CustomComponentDialog ( Context context , int themeResId ) : base ( context , themeResId )
0 commit comments