diff --git a/Hosting/MauiAppBuilderExtensions.cs b/Hosting/MauiAppBuilderExtensions.cs new file mode 100644 index 0000000..4cc3b75 --- /dev/null +++ b/Hosting/MauiAppBuilderExtensions.cs @@ -0,0 +1,193 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// Copyright (c) 2025 MarketAlly LLC + +using Microsoft.Maui; +using Microsoft.Maui.Controls; +using Microsoft.Maui.Controls.Hosting; +using Microsoft.Maui.Hosting; +using OpenMaui.Platform.Linux.Handlers; + +namespace OpenMaui.Platform.Linux.Hosting; + +/// +/// Extension methods for configuring OpenMaui Linux platform in a MAUI application. +/// This enables full XAML support by registering Linux-specific handlers. +/// +public static class MauiAppBuilderExtensions +{ + /// + /// Configures the application to use OpenMaui Linux platform with full XAML support. + /// + /// The MAUI app builder. + /// The configured MAUI app builder. + /// + /// + /// var builder = MauiApp.CreateBuilder(); + /// builder + /// .UseMauiApp<App>() + /// .UseOpenMauiLinux(); // Enable Linux support with XAML + /// + /// + public static MauiAppBuilder UseOpenMauiLinux(this MauiAppBuilder builder) + { + builder.ConfigureMauiHandlers(handlers => + { + // Register all Linux platform handlers + // These map MAUI virtual views to our Skia platform views + + // Basic Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Selection Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Display Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Layout Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Collection Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Navigation Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Page Controls + handlers.AddHandler(); + handlers.AddHandler(); + + // Graphics + handlers.AddHandler(); + + // Search + handlers.AddHandler(); + + // Window + handlers.AddHandler(); + }); + + // Register Linux-specific services + builder.Services.AddSingleton(); + + return builder; + } + + /// + /// Configures the application to use OpenMaui Linux with custom handler configuration. + /// + /// The MAUI app builder. + /// Action to configure additional handlers. + /// The configured MAUI app builder. + public static MauiAppBuilder UseOpenMauiLinux( + this MauiAppBuilder builder, + Action? configureHandlers) + { + builder.UseOpenMauiLinux(); + + if (configureHandlers != null) + { + builder.ConfigureMauiHandlers(configureHandlers); + } + + return builder; + } +} + +/// +/// Interface for Linux platform services. +/// +public interface ILinuxPlatformServices +{ + /// + /// Gets the display server type (X11 or Wayland). + /// + DisplayServerType DisplayServer { get; } + + /// + /// Gets the current DPI scale factor. + /// + float ScaleFactor { get; } + + /// + /// Gets whether high contrast mode is enabled. + /// + bool IsHighContrastEnabled { get; } +} + +/// +/// Display server types supported by OpenMaui. +/// +public enum DisplayServerType +{ + /// X11 display server. + X11, + /// Wayland display server. + Wayland, + /// Auto-detected display server. + Auto +} + +/// +/// Implementation of Linux platform services. +/// +internal class LinuxPlatformServices : ILinuxPlatformServices +{ + public DisplayServerType DisplayServer => DetectDisplayServer(); + public float ScaleFactor => DetectScaleFactor(); + public bool IsHighContrastEnabled => DetectHighContrast(); + + private static DisplayServerType DetectDisplayServer() + { + var waylandDisplay = Environment.GetEnvironmentVariable("WAYLAND_DISPLAY"); + if (!string.IsNullOrEmpty(waylandDisplay)) + return DisplayServerType.Wayland; + + var display = Environment.GetEnvironmentVariable("DISPLAY"); + if (!string.IsNullOrEmpty(display)) + return DisplayServerType.X11; + + return DisplayServerType.Auto; + } + + private static float DetectScaleFactor() + { + // Try GDK_SCALE first + var gdkScale = Environment.GetEnvironmentVariable("GDK_SCALE"); + if (float.TryParse(gdkScale, out var scale)) + return scale; + + // Default to 1.0 + return 1.0f; + } + + private static bool DetectHighContrast() + { + var highContrast = Environment.GetEnvironmentVariable("GTK_THEME"); + return highContrast?.Contains("HighContrast", StringComparison.OrdinalIgnoreCase) ?? false; + } +} diff --git a/README.md b/README.md index 9495615..a4a9675 100644 --- a/README.md +++ b/README.md @@ -27,14 +27,14 @@ This project brings .NET MAUI to Linux desktops with native X11/Wayland support, ### Installation ```bash -# Install the template +# Install the templates dotnet new install OpenMaui.Linux.Templates -# Create a new project -dotnet new openmaui-linux -n MyApp -cd MyApp +# Create a new project (choose one): +dotnet new openmaui-linux -n MyApp # Code-based UI +dotnet new openmaui-linux-xaml -n MyApp # XAML-based UI (recommended) -# Run +cd MyApp dotnet run ``` @@ -44,6 +44,32 @@ dotnet run dotnet add package OpenMaui.Controls.Linux --prerelease ``` +## XAML Support + +OpenMaui fully supports standard .NET MAUI XAML syntax. Use the familiar XAML workflow: + +```xml + + + +