From 1518624c654eff23cd79a0d9c17b71fa3f9ce253 Mon Sep 17 00:00:00 2001 From: Steven He Date: Tue, 8 Feb 2022 21:28:39 +0800 Subject: [PATCH] Add NativeAOT sample build --- Avalonia.sln | 27 ++++ .../ControlCatalog.NetCore.Native.csproj | 33 +++++ .../ControlCatalog.NetCore.Native/Program.cs | 132 ++++++++++++++++++ samples/ControlCatalog.NetCore.Native/rd.xml | 7 + 4 files changed, 199 insertions(+) create mode 100644 samples/ControlCatalog.NetCore.Native/ControlCatalog.NetCore.Native.csproj create mode 100644 samples/ControlCatalog.NetCore.Native/Program.cs create mode 100644 samples/ControlCatalog.NetCore.Native/rd.xml diff --git a/Avalonia.sln b/Avalonia.sln index 0354e20d4f..0a043772b1 100644 --- a/Avalonia.sln +++ b/Avalonia.sln @@ -234,6 +234,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControlSamples", "samples\S EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.PlatformSupport", "src\Avalonia.PlatformSupport\Avalonia.PlatformSupport.csproj", "{E8A597F0-2AB5-4BDA-A235-41162DAF53CF}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ControlCatalog.NetCore.Native", "samples\ControlCatalog.NetCore.Native\ControlCatalog.NetCore.Native.csproj", "{C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Ad-Hoc|Any CPU = Ad-Hoc|Any CPU @@ -2184,6 +2186,30 @@ Global {E8A597F0-2AB5-4BDA-A235-41162DAF53CF}.Release|iPhone.Build.0 = Release|Any CPU {E8A597F0-2AB5-4BDA-A235-41162DAF53CF}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU {E8A597F0-2AB5-4BDA-A235-41162DAF53CF}.Release|iPhoneSimulator.Build.0 = Release|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.AppStore|Any CPU.Build.0 = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.AppStore|iPhone.ActiveCfg = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.AppStore|iPhone.Build.0 = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Debug|iPhone.ActiveCfg = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Debug|iPhone.Build.0 = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Release|Any CPU.Build.0 = Release|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Release|iPhone.ActiveCfg = Release|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Release|iPhone.Build.0 = Release|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90}.Release|iPhoneSimulator.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -2245,6 +2271,7 @@ Global {C08E9894-AA92-426E-BF56-033E262CAD3E} = {9B9E3891-2366-4253-A952-D08BCEB71098} {26A98DA1-D89D-4A95-8152-349F404DA2E2} = {A0CC0258-D18C-4AB3-854F-7101680FC3F9} {A0D0A6A4-5C72-4ADA-9B27-621C7D94F270} = {9B9E3891-2366-4253-A952-D08BCEB71098} + {C865FE0E-88C0-4026-8AC6-97FAB8C0AC90} = {9B9E3891-2366-4253-A952-D08BCEB71098} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {87366D66-1391-4D90-8999-95A620AD786A} diff --git a/samples/ControlCatalog.NetCore.Native/ControlCatalog.NetCore.Native.csproj b/samples/ControlCatalog.NetCore.Native/ControlCatalog.NetCore.Native.csproj new file mode 100644 index 0000000000..1647410b65 --- /dev/null +++ b/samples/ControlCatalog.NetCore.Native/ControlCatalog.NetCore.Native.csproj @@ -0,0 +1,33 @@ + + + + WinExe + net6.0 + true + true + https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json + 7.0.0-* + + + + + + + + + + + + + + + + + + + en + + + + + diff --git a/samples/ControlCatalog.NetCore.Native/Program.cs b/samples/ControlCatalog.NetCore.Native/Program.cs new file mode 100644 index 0000000000..834f2d3ee0 --- /dev/null +++ b/samples/ControlCatalog.NetCore.Native/Program.cs @@ -0,0 +1,132 @@ +using System; +using System.Diagnostics; +using System.Globalization; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Avalonia; +using Avalonia.Controls; +using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Headless; +using Avalonia.LogicalTree; +using Avalonia.Threading; + +namespace ControlCatalog.NetCore.Native +{ + static class Program + { + [STAThread] + static int Main(string[] args) + { + if (args.Contains("--wait-for-attach")) + { + Console.WriteLine("Attach debugger and use 'Set next statement'"); + while (true) + { + Thread.Sleep(100); + if (Debugger.IsAttached) + break; + } + } + + var builder = BuildAvaloniaApp(); + + double GetScaling() + { + var idx = Array.IndexOf(args, "--scaling"); + if (idx != 0 && args.Length > idx + 1 && + double.TryParse(args[idx + 1], NumberStyles.Any, CultureInfo.InvariantCulture, out var scaling)) + return scaling; + return 1; + } + if (args.Contains("--fbdev")) + { + SilenceConsole(); + return builder.StartLinuxFbDev(args, scaling: GetScaling()); + } + else if (args.Contains("--vnc")) + { + return builder.StartWithHeadlessVncPlatform(null, 5901, args, ShutdownMode.OnMainWindowClose); + } + else if (args.Contains("--full-headless")) + { + return builder + .UseHeadless(true) + .AfterSetup(_ => + { + DispatcherTimer.RunOnce(async () => + { + var window = ((IClassicDesktopStyleApplicationLifetime)Application.Current.ApplicationLifetime) + .MainWindow; + var tc = window.GetLogicalDescendants().OfType().First(); + foreach (var page in tc.Items.Cast().ToList()) + { + // Skip DatePicker because of some layout bug in grid + if (page.Header.ToString() == "DatePicker") + continue; + Console.WriteLine("Selecting " + page.Header); + tc.SelectedItem = page; + await Task.Delay(500); + } + Console.WriteLine("Selecting the first page"); + tc.SelectedItem = tc.Items.OfType().First(); + await Task.Delay(500); + Console.WriteLine("Clicked through all pages, triggering GC"); + for (var c = 0; c < 3; c++) + { + GC.Collect(2, GCCollectionMode.Forced); + await Task.Delay(500); + } + + void FormatMem(string metric, long bytes) + { + Console.WriteLine(metric + ": " + bytes / 1024 / 1024 + "MB"); + } + + FormatMem("GC allocated bytes", GC.GetTotalMemory(true)); + FormatMem("WorkingSet64", Process.GetCurrentProcess().WorkingSet64); + + }, TimeSpan.FromSeconds(1)); + }) + .StartWithClassicDesktopLifetime(args); + } + else if (args.Contains("--drm")) + { + SilenceConsole(); + return builder.StartLinuxDrm(args, scaling: GetScaling()); + } + else + return builder.StartWithClassicDesktopLifetime(args); + } + + /// + /// This method is needed for IDE previewer infrastructure + /// + public static AppBuilder BuildAvaloniaApp() + => AppBuilder.Configure() + .UsePlatformDetect() + .With(new X11PlatformOptions + { + EnableMultiTouch = true, + UseDBusMenu = true, + EnableIme = true, + }) + .With(new Win32PlatformOptions + { + EnableMultitouch = true + }) + .UseSkia() + .LogToTrace(); + + static void SilenceConsole() + { + new Thread(() => + { + Console.CursorVisible = false; + while (true) + Console.ReadKey(true); + }) + { IsBackground = true }.Start(); + } + } +} diff --git a/samples/ControlCatalog.NetCore.Native/rd.xml b/samples/ControlCatalog.NetCore.Native/rd.xml new file mode 100644 index 0000000000..27db7f34ca --- /dev/null +++ b/samples/ControlCatalog.NetCore.Native/rd.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file