Skip to content

Commit

Permalink
fix: Make it possible to retrieve instance of ApplicationView earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Feb 8, 2024
1 parent 129e5b2 commit 69718ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Uno.UWP/Microsoft/UI/Windowing/AppWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal AppWindow()
Id = new(Interlocked.Increment(ref _windowIdIterator));

_windowIdMap[Id] = this;
ApplicationView.InitializeForWindowId(Id);
ApplicationView.GetOrCreateForWindowId(Id);
}

public event TypedEventHandler<AppWindow, AppWindowClosingEventArgs> Closing;
Expand Down
39 changes: 22 additions & 17 deletions src/Uno.UWP/UI/ViewManagement/ApplicationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Uno.Devices.Sensors;
using Uno.Foundation.Extensibility;
using Uno.Foundation.Logging;
using Windows.ApplicationModel.Resources.Core;
using Windows.Foundation;
using Windows.Storage;
using Windows.UI.WindowManagement;
using MUXWindowId = Microsoft.UI.WindowId;
using AppWindow = Microsoft.UI.Windowing.AppWindow;
using Windows.ApplicationModel.Core;
using MUXWindowId = Microsoft.UI.WindowId;

namespace Windows.UI.ViewManagement
{
Expand Down Expand Up @@ -94,28 +90,37 @@ internal Rect? VisibleBoundsOverride

public static global::Windows.UI.ViewManagement.ApplicationView GetForCurrentView()
{
if (!CoreApplication.IsFullFledgedApp)
{
// This is specifically needed to provide a stub for Uno Islands.
InitializeForWindowId(AppWindow.MainWindowId);
}

return GetForWindowId(AppWindow.MainWindowId);
// This is needed to ensure for "current view" there is always a corresponding ApplicationView instance.
// This means that Uno Islands and WinUI apps can keep using this API for now until we make the breaking change
// on Uno.WinUI codebase.
return GetOrCreateForWindowId(AppWindow.MainWindowId);
}

#pragma warning disable RS0030 // Do not use banned APIs
public static global::Windows.UI.ViewManagement.ApplicationView GetForCurrentViewSafe() => GetForCurrentView();
#pragma warning restore RS0030 // Do not use banned APIs

internal static global::Windows.UI.ViewManagement.ApplicationView GetForWindowId(MUXWindowId windowId) => _windowIdMap[windowId];
internal static global::Windows.UI.ViewManagement.ApplicationView GetForWindowId(MUXWindowId windowId)
{
if (!_windowIdMap.TryGetValue(windowId, out var appView))
{
throw new InvalidOperationException(
$"ApplicationView corresponding with this window does not exist yet, which usually means " +
$"the API was called too early in the windowing lifecycle. Try to use ApplicationView later.");
}

internal static void InitializeForWindowId(MUXWindowId windowId)
return appView;
}

internal static ApplicationView GetOrCreateForWindowId(MUXWindowId windowId)
{
if (!_windowIdMap.ContainsKey(windowId))
if (!_windowIdMap.TryGetValue(windowId, out var appView))
{
ApplicationView applicationView = new();
_windowIdMap[windowId] = applicationView;
appView = new();
_windowIdMap[windowId] = appView;
}

return appView;
}

[global::Uno.NotImplemented]
Expand Down

0 comments on commit 69718ef

Please sign in to comment.