Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add doNotActivate parameter to NavigateAsync method from Navigation Extensions #2397

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Expand Up @@ -6,9 +6,9 @@ private struct ToolkitViewInitializer : IRootViewInitializer
{
public ContentControl CreateDefaultView() => new LoadingView();

public void InitializeViewHost(Window window, FrameworkElement element, Task loadingTask)
public void InitializeViewHost(Window window, FrameworkElement element, Task loadingTask, bool doNotActivate)
{
window.ApplyLoadingTask(element, loadingTask);
window.ApplyLoadingTask(element, loadingTask, doNotActivate);
}

public void PreInitialize(FrameworkElement element, IApplicationBuilder builder)
Expand Down
14 changes: 9 additions & 5 deletions src/Uno.Extensions.Navigation.Toolkit/WindowExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class WindowExtensions
/// <param name="initialView">[optional] Initial navigation view</param>
/// <param name="initialViewModel">[optional] Initial navigation viewmodel</param>
/// <param name="initialNavigate">[optional] Callback to drive initial navigation for app</param>
/// <param name="doNotActivate">[optional] Do not activate the window after initializing navigation</param>
/// <returns>The created IHost</returns>
public static Task<IHost> InitializeNavigationAsync(
this Window window,
Expand All @@ -24,18 +25,20 @@ public static Task<IHost> InitializeNavigationAsync(
string? initialRoute = "",
Type? initialView = null,
Type? initialViewModel = null,
Func<IServiceProvider, INavigator, Task>? initialNavigate = null)
Func<IServiceProvider, INavigator, Task>? initialNavigate = null,
bool doNotActivate = false)
{
return window.InternalInitializeNavigationAsync(
buildHost,
navigationRoot,
initialRoute, initialView, initialViewModel,
ApplyLoadingTask,
initialNavigate
initialNavigate,
doNotActivate
);
}

internal static void ApplyLoadingTask(this Window window, FrameworkElement root, Task navInit)
internal static void ApplyLoadingTask(this Window window, FrameworkElement root, Task navInit, bool doNotActivate)
{
var activate = true;
if (root is LoadingView lv)
Expand All @@ -53,15 +56,16 @@ internal static void ApplyLoadingTask(this Window window, FrameworkElement root,
loadingTask = new Func<Task>(async () =>
{
await navInit;
window.Activate();
if (!doNotActivate)
window.Activate();
})();
}
}
var loading = new LoadingTask(loadingTask, root);
lv.Source = loading;
}

if (activate)
if (activate && !doNotActivate)
{
// Activate immediately to show the splash screen
window.Activate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ public static class ApplicationBuilderExtensions
/// <typeparam name="TShell">The <see cref="UIElement" /> to use for the App Shell.</typeparam>
/// <param name="appBuilder">The <see cref="IApplicationBuilder" />.</param>
/// <param name="initialNavigate">An optional Navigation Delegate to conditionally control where the app should navigate on launch.</param>
/// <param name="doNotActivate">An optional flag to prevent the Window from activating right after initialization but only when host is completely loaded.</param>
/// <returns>The <see cref="IHost" />.</returns>
public static async Task<IHost> NavigateAsync<TShell>(this IApplicationBuilder appBuilder,
Func<IServiceProvider, INavigator, Task>? initialNavigate = null)
Func<IServiceProvider, INavigator, Task>? initialNavigate = null, bool doNotActivate = false)
where TShell : UIElement, new()
{
var appRoot = new TShell();
Expand All @@ -21,7 +22,7 @@ public static async Task<IHost> NavigateAsync<TShell>(this IApplicationBuilder a
navRoot = contentProvider.ContentControl;
}

Action<Window, FrameworkElement, Task> initializeViewHost = (_, _, _) => { };
Action<Window, FrameworkElement, Task, bool> initializeViewHost = (_, _, _, _) => { };
if (appBuilder.Properties.TryGetValue(typeof(IRootViewInitializer), out var propValue) && propValue is IRootViewInitializer initializer)
{
navRoot ??= initializer.CreateDefaultView();
Expand All @@ -43,7 +44,8 @@ public static async Task<IHost> NavigateAsync<TShell>(this IApplicationBuilder a
buildHost: () => Task.FromResult(appBuilder.Build()),
navigationRoot: navRoot,
initializeViewHost: initializeViewHost,
initialNavigate: initialNavigate
initialNavigate: initialNavigate,
doNotActivate: doNotActivate
);
}
}
3 changes: 2 additions & 1 deletion src/Uno.Extensions.Navigation.UI/IRootViewInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ internal interface IRootViewInitializer
/// <param name="window"></param>
/// <param name="element"></param>
/// <param name="loadingTask"></param>
void InitializeViewHost(Window window, FrameworkElement element, Task loadingTask);
/// <param name="doNotActivate"></param>
void InitializeViewHost(Window window, FrameworkElement element, Task loadingTask, bool doNotActivate = false);
Copy link
Member

Choose a reason for hiding this comment

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

That's odd using a double negative like that. That's confusing.

}
14 changes: 8 additions & 6 deletions src/Uno.Extensions.Navigation.UI/ServiceProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal static IServiceProvider CreateNavigationScope(this IServiceProvider ser
.CloneScopedInstance<IDispatcher>(services);
}



/// <summary>
/// Initializes navigation for an application using a ContentControl
Expand Down Expand Up @@ -91,16 +91,17 @@ internal static Task<IHost> InternalInitializeNavigationAsync(
string? initialRoute = "",
Type? initialView = null,
Type? initialViewModel = null,
Action<Window, FrameworkElement, Task>? initializeViewHost = null,
Func<IServiceProvider, INavigator, Task>? initialNavigate = null)
Action<Window, FrameworkElement, Task, bool>? initializeViewHost = null,
Func<IServiceProvider, INavigator, Task>? initialNavigate = null,
bool doNotActivate = false)
{
if (window.Content is null)
{
window.Content = navigationRoot;
}

var buildTask = window.BuildAndInitializeHostAsync(navigationRoot, buildHost, initialRoute, initialView, initialViewModel, initialNavigate);
initializeViewHost?.Invoke(window, navigationRoot, buildTask);
initializeViewHost?.Invoke(window, navigationRoot, buildTask, doNotActivate);
return buildTask;
}

Expand All @@ -124,8 +125,9 @@ private static async Task<IHost> BuildAndInitializeHostAsync(

await startup;

// Fallback to make sure the window is activated
window.Activate();
var hostLifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
// Activate the window after the application has started
hostLifetime.ApplicationStarted.Register(() => window.Activate());

return host;
}
Expand Down