Skip to content

Commit

Permalink
Remove references to an "app window" (#3003)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Oct 19, 2021
1 parent 8bc9373 commit 2e50fce
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 50 deletions.
3 changes: 0 additions & 3 deletions src/Core/src/Platform/Android/ContextExtensions.cs
Expand Up @@ -240,9 +240,6 @@ public static int GetDrawableId(this Resources resources, string packageName, st
{
var nativeWindow = context.GetActivity();

if (nativeWindow is MauiAppCompatActivity mac && mac.VirtualWindow != null)
return mac.VirtualWindow;

foreach (var window in MauiApplication.Current.Application.Windows)
{
if (window?.Handler?.NativeView is AActivity win && win == nativeWindow)
Expand Down
20 changes: 3 additions & 17 deletions src/Core/src/Platform/Android/MauiAppCompatActivity.cs
Expand Up @@ -8,18 +8,6 @@ namespace Microsoft.Maui
{
public partial class MauiAppCompatActivity : AppCompatActivity
{
WeakReference<IWindow>? _virtualWindow;
internal IWindow? VirtualWindow
{
get
{
IWindow? window = null;
_virtualWindow?.TryGetTarget(out window);
return window;
}
}


// Override this if you want to handle the default Android behavior of restoring fragments on an application restart
protected virtual bool AllowFragmentRestore => false;

Expand Down Expand Up @@ -51,13 +39,12 @@ void CreateNativeWindow(Bundle? savedInstanceState = null)
if (mauiApp == null)
throw new InvalidOperationException($"The {nameof(IApplication)} instance was not found.");

var services = MauiApplication.Current.Services;
if (services == null)
if (mauiApp.Handler?.MauiContext is not IMauiContext applicationContext)
throw new InvalidOperationException($"The {nameof(IServiceProvider)} instance was not found.");

var mauiContext = MauiApplication.Current.MauiApplicationContext.MakeScoped(this);
var mauiContext = applicationContext.MakeScoped(this);

services.InvokeLifecycleEvents<AndroidLifecycle.OnMauiContextCreated>(del => del(mauiContext));
applicationContext.Services.InvokeLifecycleEvents<AndroidLifecycle.OnMauiContextCreated>(del => del(mauiContext));

// TODO: Fix once we have multiple windows
IWindow window;
Expand All @@ -73,7 +60,6 @@ void CreateNativeWindow(Bundle? savedInstanceState = null)
window = mauiApp.CreateWindow(state);
}

_virtualWindow = new WeakReference<IWindow>(window);
this.SetWindowHandler(window, mauiContext);
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/Core/src/Platform/Android/MauiApplication.cs
Expand Up @@ -27,15 +27,13 @@ public override void OnCreate()

var applicationContext = new MauiContext(mauiApp.Services, this);

MauiApplicationContext = applicationContext;

Services = mauiApp.Services;

Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationCreating>(del => del(this));

Application = Services.GetRequiredService<IApplication>();

this.SetApplicationHandler(Application, MauiApplicationContext);
this.SetApplicationHandler(Application, applicationContext);

Current.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnApplicationCreate>(del => del(this));

Expand Down Expand Up @@ -65,8 +63,6 @@ public override void OnConfigurationChanged(Configuration newConfig)

public static MauiApplication Current { get; private set; } = null!;

internal IMauiContext MauiApplicationContext { get; private set; } = null!;

public IServiceProvider Services { get; protected set; } = null!;

public IApplication Application { get; protected set; } = null!;
Expand Down
6 changes: 1 addition & 5 deletions src/Core/src/Platform/Windows/MauiWinUIApplication.cs
Expand Up @@ -27,9 +27,7 @@ protected override void OnLaunched(UI.Xaml.LaunchActivatedEventArgs args)

var winuiWndow = CreateNativeWindow(args, applicationContext);

MainWindow = winuiWndow;

MainWindow.Activate();
winuiWndow.Activate();

Services.InvokeLifecycleEvents<WindowsLifecycle.OnLaunched>(del => del(this, args));
}
Expand All @@ -56,8 +54,6 @@ UI.Xaml.Window CreateNativeWindow(UI.Xaml.LaunchActivatedEventArgs args, IMauiCo

public UI.Xaml.LaunchActivatedEventArgs LaunchActivatedEventArgs { get; protected set; } = null!;

public UI.Xaml.Window MainWindow { get; protected set; } = null!;

public IServiceProvider Services { get; protected set; } = null!;

public IApplication Application { get; protected set; } = null!;
Expand Down
12 changes: 0 additions & 12 deletions src/Core/src/Platform/iOS/MauiUIApplicationDelegate.cs
Expand Up @@ -10,17 +10,6 @@ namespace Microsoft.Maui
public abstract class MauiUIApplicationDelegate : UIApplicationDelegate, IUIApplicationDelegate
{
MauiContext _applicationContext = null!;
WeakReference<IWindow>? _virtualWindow;

internal IWindow? VirtualWindow
{
get
{
IWindow? window = null;
_virtualWindow?.TryGetTarget(out window);
return window;
}
}

protected MauiUIApplicationDelegate()
{
Expand Down Expand Up @@ -69,7 +58,6 @@ UIWindow CreateNativeWindow()

var activationState = new ActivationState(mauiContext);
var window = Application.CreateWindow(activationState);
_virtualWindow = new WeakReference<IWindow>(window);
uiWindow.SetWindowHandler(window, mauiContext);

return uiWindow;
Expand Down
9 changes: 3 additions & 6 deletions src/Core/src/Platform/iOS/UIApplicationExtensions.cs
@@ -1,4 +1,3 @@
using System;
using UIKit;

namespace Microsoft.Maui
Expand All @@ -21,17 +20,15 @@ internal static class UIApplicationExtensions

public static IWindow? GetWindow(this UIApplication application)
{
if (MauiUIApplicationDelegate.Current.VirtualWindow != null)
return MauiUIApplicationDelegate.Current.VirtualWindow;
var windows = application.Windows;

var nativeWindow = application.GetKeyWindow();
foreach (var window in MauiUIApplicationDelegate.Current.Application.Windows)
{
if (window?.Handler?.NativeView is UIWindow win && win == nativeWindow)
if (window?.Handler?.NativeView is UIWindow win && windows.IndexOf(win) != -1)
return window;
}

return null;
}
}
}
}
8 changes: 6 additions & 2 deletions src/TestUtils/src/DeviceTests/TestMainThread.cs
Expand Up @@ -11,7 +11,9 @@ public static Task<T> InvokeOnMainThreadAsync<T>(Func<T> func)
#if WINDOWS
var tcs = new TaskCompletionSource<T>();

var didQueue = MauiWinUIApplication.Current.MainWindow.DispatcherQueue.TryEnqueue(() =>
// TODO: this all needs to be cleaned up with the IDispatcher
var winuiApp = (UI.Xaml.Window)MauiWinUIApplication.Current.Application.Windows[0].Handler!.NativeView!;
var didQueue = winuiApp.DispatcherQueue.TryEnqueue(() =>
{
try
{
Expand Down Expand Up @@ -56,7 +58,9 @@ public static Task<T> InvokeOnMainThreadAsync<T>(Func<Task<T>> func)
#if WINDOWS
var tcs = new TaskCompletionSource<T>();

var didQueue = MauiWinUIApplication.Current.MainWindow.DispatcherQueue.TryEnqueue(async () =>
// TODO: this all needs to be cleaned up with the IDispatcher
var winuiApp = (UI.Xaml.Window)MauiWinUIApplication.Current.Application.Windows[0].Handler!.NativeView!;
var didQueue = winuiApp.DispatcherQueue.TryEnqueue(async () =>
{
try
{
Expand Down

0 comments on commit 2e50fce

Please sign in to comment.