// 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 Microsoft.Maui.Platform.Linux; using Microsoft.Maui.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(); // Collection Controls handlers.AddHandler(); // Navigation Controls handlers.AddHandler(); handlers.AddHandler(); handlers.AddHandler(); handlers.AddHandler(); // Page Controls handlers.AddHandler(); handlers.AddHandler(); // Graphics handlers.AddHandler(); // Search handlers.AddHandler(); // Web 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; } }