Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Android.Content;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Views.Animations;
using AndroidX.Activity;
using AndroidX.AppCompat.App;
using AndroidX.Fragment.App;
using Microsoft.Maui.LifecycleEvents;
using AAnimation = Android.Views.Animations.Animation;
using AColor = Android.Graphics.Color;
using AView = Android.Views.View;

Expand All @@ -23,9 +17,6 @@ namespace Microsoft.Maui.Controls.Platform
internal partial class ModalNavigationManager
{
ViewGroup? _modalParentView;
bool _navAnimationInProgress;
internal const string CloseContextActionsSignalName = "Xamarin.CloseContextActions";
AAnimation? _dismissAnimation;
bool _platformActivated;

readonly Stack<string> _modals = [];
Expand All @@ -44,8 +35,6 @@ void OnWindowPropertyChanging(object sender, PropertyChangingEventArgs e)
return;
}

var handler = _currentPage?.Handler;
var windowP = _window.Page;
if (CurrentPage is not null &&
_window.Page != CurrentPage)
{
Expand Down Expand Up @@ -89,76 +78,45 @@ internal void SetModalParentView(ViewGroup viewGroup)
_modalParentView = viewGroup;
}

ViewGroup GetModalParentView()
{
return _modalParentView ??
_window?.PlatformActivity?.Window?.DecorView as ViewGroup ??
throw new InvalidOperationException("Root View Needs to be set");
}

// AFAICT this is specific to ListView and Context Items
internal bool NavAnimationInProgress
{
get { return _navAnimationInProgress; }
set
{
if (_navAnimationInProgress == value)
return;
_navAnimationInProgress = value;

#pragma warning disable CS0618 // TODO: Remove when we internalize/replace MessagingCenter
if (value)
MessagingCenter.Send(this, CloseContextActionsSignalName);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this was originally intended for

//TODO: MAUI
//MessagingCenter.Subscribe<ListViewAdapter>(this, Platform.CloseContextActionsSignalName, lva => CloseContextActions());

which clearly isn’t subscribing anymore.
And since ListView is about to be kicked out of the house, I’m removing it.

#pragma warning restore CS0618 // Type or member is obsolete
}
}

Task<Page> PopModalPlatformAsync(bool animated)
{
Page modal = CurrentPlatformModalPage;
_platformModalPages.Remove(modal);

TaskCompletionSource<Page> tcs = new();

var fragmentManager = WindowMauiContext.GetFragmentManager();

var dialogFragmentId = _modals.Pop();
var dialogFragment = (ModalFragment?)fragmentManager.FindFragmentByTag(dialogFragmentId);

// If for the dialog is null what we want to do?

if (dialogFragment is null)
{
return Task.FromResult(modal);
tcs.TrySetResult(modal);
return tcs.Task;
}

var source = new TaskCompletionSource<Page>();
EventHandler? OnDialogDismiss = null;

if (animated && dialogFragment.View is not null)
OnDialogDismiss = (_, _) =>
{
_dismissAnimation ??= AnimationUtils.LoadAnimation(WindowMauiContext.Context, Resource.Animation.nav_modal_default_exit_anim)!;
dialogFragment.DialogDismissEvent -= OnDialogDismiss;
tcs.TrySetResult(modal);
};

_dismissAnimation.AnimationEnd += OnAnimationEnded;
dialogFragment.DialogDismissEvent += OnDialogDismiss;

dialogFragment.View.StartAnimation(_dismissAnimation);
if (animated)
{
dialogFragment.Dialog?.Window?.SetWindowAnimations(Resource.Style.modal_exit_animation);
modal.Dispatcher.Dispatch(() => dialogFragment.Dismiss());
}
else
{
dialogFragment.Dismiss();
source.TrySetResult(modal);
}

return source.Task;

void OnAnimationEnded(object? sender, AAnimation.AnimationEndEventArgs e)
{
if (sender is not AAnimation animation)
{
return;
}

animation.AnimationEnd -= OnAnimationEnded;
dialogFragment.Dismiss();
source.TrySetResult(modal);
_dismissAnimation = null;
}
return tcs.Task;
}

// The CurrentPage doesn't represent the root of the platform hierarchy.
Expand Down Expand Up @@ -192,8 +150,6 @@ async Task PresentModal(Page modal, bool animated)
{
TaskCompletionSource<bool> animationCompletionSource = new();

var parentView = GetModalParentView();

var dialogFragment = new ModalFragment(WindowMauiContext, modal)
{
Cancelable = false,
Expand All @@ -203,39 +159,30 @@ async Task PresentModal(Page modal, bool animated)
var fragmentManager = WindowMauiContext.GetFragmentManager();
var dialogFragmentId = AView.GenerateViewId().ToString();
_modals.Push(dialogFragmentId);
dialogFragment.Show(fragmentManager, dialogFragmentId);

if (animated)
{
NavAnimationInProgress = true;
dialogFragment!.AnimationEnded += OnAnimationEnded;

await animationCompletionSource.Task;
}
else
{
NavAnimationInProgress = false;
animationCompletionSource.TrySetResult(true);
}
EventHandler? OnDialogShown = null;

void OnAnimationEnded(object? sender, EventArgs e)
OnDialogShown = (_, _) =>
{
dialogFragment!.AnimationEnded -= OnAnimationEnded;
NavAnimationInProgress = false;
dialogFragment!.DialogShowEvent -= OnDialogShown;
animationCompletionSource.SetResult(true);
}
};

dialogFragment!.DialogShowEvent += OnDialogShown;

dialogFragment.Show(fragmentManager, dialogFragmentId);

await animationCompletionSource.Task;
}

internal class ModalFragment : DialogFragment
{
Page _modal;
IMauiContext _mauiWindowContext;
NavigationRootManager? _navigationRootManager;
public event EventHandler? DialogShowEvent;
public event EventHandler? DialogDismissEvent;
static readonly ColorDrawable TransparentColorDrawable = new(AColor.Transparent);
bool _pendingAnimation = true;

public event EventHandler? AnimationEnded;


public bool IsAnimated { get; internal set; }

Expand All @@ -262,6 +209,20 @@ public ModalFragment(IMauiContext mauiContext, Page modal)
{
dialog.Window.SetSoftInputMode(attributes.SoftInputMode);
}
EventHandler? OnDialogShown = null;

OnDialogShown = (_, _) =>
{
dialog.ShowEvent -= OnDialogShown;
DialogShowEvent?.Invoke(this, EventArgs.Empty);
};

dialog.ShowEvent += OnDialogShown;

if (IsAnimated)
{
dialog.Window?.SetWindowAnimations(Resource.Style.modal_enter_animation);
}

return dialog;
}
Expand Down Expand Up @@ -345,29 +306,13 @@ public override void OnStart()
int width = ViewGroup.LayoutParams.MatchParent;
int height = ViewGroup.LayoutParams.MatchParent;
dialog.Window.SetLayout(width, height);

if (IsAnimated)
{
var animation = AnimationUtils.LoadAnimation(_mauiWindowContext.Context, Resource.Animation.nav_modal_default_enter_anim)!;
View.StartAnimation(animation);

animation.AnimationEnd += OnAnimationEnded;
}

void OnAnimationEnded(object? sender, AAnimation.AnimationEndEventArgs e)
{
if (sender is not AAnimation animation)
{
return;
}

animation.AnimationEnd -= OnAnimationEnded;
FireAnimationEnded();
}
}

public override void OnDismiss(IDialogInterface dialog)
{
base.OnDismiss(dialog);
DialogDismissEvent?.Invoke(this, EventArgs.Empty);
DialogDismissEvent = null;
_modal.PropertyChanged -= OnModalPagePropertyChanged;
_modal.HandlerChanged -= OnPageHandlerChanged;

Expand All @@ -380,28 +325,9 @@ public override void OnDismiss(IDialogInterface dialog)
_modal = null!;
_mauiWindowContext = null!;
_navigationRootManager?.Disconnect();
_navigationRootManager = null;
base.OnDismiss(dialog);
}

public override void OnDestroy()
{
base.OnDestroy();
FireAnimationEnded();
_navigationRootManager = null;
}

void FireAnimationEnded()
{
if (!_pendingAnimation)
{
return;
}

_pendingAnimation = false;
AnimationEnded?.Invoke(this, EventArgs.Empty);
}


sealed class CustomComponentDialog : ComponentDialog
{
public CustomComponentDialog(Context context, int themeResId) : base(context, themeResId)
Expand Down
7 changes: 7 additions & 0 deletions src/Core/src/Platform/Android/Resources/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,11 @@
<style name="scrollViewTheme">
<item name="scrollViewStyle">@style/scrollViewScrollBars</item>
</style>

<style name="modal_enter_animation" >
<item name="android:windowEnterAnimation">@anim/nav_modal_default_enter_anim</item>
</style>
<style name="modal_exit_animation" >
<item name="android:windowExitAnimation">@anim/nav_modal_default_exit_anim</item>
</style>
</resources>
Loading