// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.Maui.Handlers; using Microsoft.Maui.Graphics; using Microsoft.Maui.Controls; using Microsoft.Maui.Platform; using SkiaSharp; namespace Microsoft.Maui.Platform.Linux.Handlers; /// /// Handler for NavigationPage on Linux using Skia rendering. /// public partial class NavigationPageHandler : ViewHandler { public static IPropertyMapper Mapper = new PropertyMapper(ViewHandler.ViewMapper) { [nameof(NavigationPage.BarBackgroundColor)] = MapBarBackgroundColor, [nameof(NavigationPage.BarBackground)] = MapBarBackground, [nameof(NavigationPage.BarTextColor)] = MapBarTextColor, [nameof(IView.Background)] = MapBackground, }; public static CommandMapper CommandMapper = new(ViewHandler.ViewCommandMapper) { [nameof(IStackNavigationView.RequestNavigation)] = MapRequestNavigation, }; public NavigationPageHandler() : base(Mapper, CommandMapper) { } public NavigationPageHandler(IPropertyMapper? mapper, CommandMapper? commandMapper = null) : base(mapper ?? Mapper, commandMapper ?? CommandMapper) { } protected override SkiaNavigationPage CreatePlatformView() { return new SkiaNavigationPage(); } protected override void ConnectHandler(SkiaNavigationPage platformView) { base.ConnectHandler(platformView); platformView.Pushed += OnPushed; platformView.Popped += OnPopped; platformView.PoppedToRoot += OnPoppedToRoot; // Set initial root page if exists if (VirtualView.CurrentPage != null) { SetupInitialPage(); } } protected override void DisconnectHandler(SkiaNavigationPage platformView) { platformView.Pushed -= OnPushed; platformView.Popped -= OnPopped; platformView.PoppedToRoot -= OnPoppedToRoot; base.DisconnectHandler(platformView); } private void SetupInitialPage() { var currentPage = VirtualView.CurrentPage; if (currentPage?.Handler?.PlatformView is SkiaPage skiaPage) { PlatformView.SetRootPage(skiaPage); } } private void OnPushed(object? sender, NavigationEventArgs e) { // Navigation was completed on platform side } private void OnPopped(object? sender, NavigationEventArgs e) { // Sync back to virtual view if needed } private void OnPoppedToRoot(object? sender, NavigationEventArgs e) { // Navigation was reset } public static void MapBarBackgroundColor(NavigationPageHandler handler, NavigationPage navigationPage) { if (handler.PlatformView is null) return; if (navigationPage.BarBackgroundColor is not null) { handler.PlatformView.BarBackgroundColor = navigationPage.BarBackgroundColor.ToSKColor(); } } public static void MapBarBackground(NavigationPageHandler handler, NavigationPage navigationPage) { if (handler.PlatformView is null) return; if (navigationPage.BarBackground is SolidColorBrush solidBrush) { handler.PlatformView.BarBackgroundColor = solidBrush.Color.ToSKColor(); } } public static void MapBarTextColor(NavigationPageHandler handler, NavigationPage navigationPage) { if (handler.PlatformView is null) return; if (navigationPage.BarTextColor is not null) { handler.PlatformView.BarTextColor = navigationPage.BarTextColor.ToSKColor(); } } public static void MapBackground(NavigationPageHandler handler, NavigationPage navigationPage) { if (handler.PlatformView is null) return; if (navigationPage.Background is SolidColorBrush solidBrush) { handler.PlatformView.BackgroundColor = solidBrush.Color.ToSKColor(); } } public static void MapRequestNavigation(NavigationPageHandler handler, NavigationPage navigationPage, object? args) { if (handler.PlatformView is null || args is not NavigationRequest request) return; // Handle navigation request foreach (var page in request.NavigationStack) { if (page.Handler?.PlatformView is SkiaPage skiaPage) { if (handler.PlatformView.StackDepth == 0) { handler.PlatformView.SetRootPage(skiaPage); } else { handler.PlatformView.Push(skiaPage, request.Animated); } } } } }