18 changed files with 163 additions and 111 deletions
@ -1,51 +1,54 @@ |
|||||
using System.Runtime.InteropServices.JavaScript; |
using System; |
||||
using System; |
|
||||
using Avalonia.Controls; |
using Avalonia.Controls; |
||||
using Avalonia.Controls.ApplicationLifetimes; |
using Avalonia.Controls.ApplicationLifetimes; |
||||
using Avalonia.Media; |
|
||||
using Avalonia.Web.Skia; |
using Avalonia.Web.Skia; |
||||
using System.Runtime.Versioning; |
using System.Runtime.Versioning; |
||||
|
|
||||
namespace Avalonia.Web |
namespace Avalonia.Web; |
||||
|
|
||||
|
[SupportedOSPlatform("browser")] |
||||
|
public class BrowserSingleViewLifetime : ISingleViewApplicationLifetime |
||||
|
{ |
||||
|
public AvaloniaView? View; |
||||
|
|
||||
|
public Control? MainView |
||||
|
{ |
||||
|
get => View!.Content; |
||||
|
set => View!.Content = value; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class BrowserPlatformOptions |
||||
|
{ |
||||
|
public Func<string, string> FrameworkAssetPathResolver { get; set; } = new(fileName => $"./{fileName}"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
[SupportedOSPlatform("browser")] |
||||
|
public static class WebAppBuilder |
||||
{ |
{ |
||||
[SupportedOSPlatform("browser")] |
public static T SetupBrowserApp<T>( |
||||
public class BrowserSingleViewLifetime : ISingleViewApplicationLifetime |
this T builder, string mainDivId) |
||||
|
where T : AppBuilderBase<T>, new() |
||||
{ |
{ |
||||
public AvaloniaView? View; |
var lifetime = new BrowserSingleViewLifetime(); |
||||
|
|
||||
public Control? MainView |
return builder |
||||
{ |
.UseBrowser() |
||||
get => View!.Content; |
.AfterSetup(b => |
||||
set => View!.Content = value; |
{ |
||||
} |
lifetime.View = new AvaloniaView(mainDivId); |
||||
|
}) |
||||
|
.SetupWithLifetime(lifetime); |
||||
} |
} |
||||
|
|
||||
[SupportedOSPlatform("browser")] |
public static T UseBrowser<T>( |
||||
public static partial class WebAppBuilder |
this T builder) |
||||
|
where T : AppBuilderBase<T>, new() |
||||
{ |
{ |
||||
public static T SetupBrowserApp<T>( |
return builder |
||||
this T builder, string mainDivId) |
.UseWindowingSubsystem(BrowserWindowingPlatform.Register) |
||||
where T : AppBuilderBase<T>, new() |
.UseSkia() |
||||
{ |
.With(new SkiaOptions { CustomGpuFactory = () => new BrowserSkiaGpu() }); |
||||
var lifetime = new BrowserSingleViewLifetime(); |
|
||||
|
|
||||
return builder |
|
||||
.UseBrowser() |
|
||||
.AfterSetup(b => |
|
||||
{ |
|
||||
lifetime.View = new AvaloniaView(mainDivId); |
|
||||
}) |
|
||||
.SetupWithLifetime(lifetime); |
|
||||
} |
|
||||
|
|
||||
public static T UseBrowser<T>( |
|
||||
this T builder) |
|
||||
where T : AppBuilderBase<T>, new() |
|
||||
{ |
|
||||
return builder |
|
||||
.UseWindowingSubsystem(BrowserWindowingPlatform.Register) |
|
||||
.UseSkia() |
|
||||
.With(new SkiaOptions { CustomGpuFactory = () => new BrowserSkiaGpu() }); |
|
||||
} |
|
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,22 @@ |
|||||
|
using System.Runtime.InteropServices.JavaScript; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Avalonia.Web.Interop; |
||||
|
|
||||
|
internal static class AvaloniaModule |
||||
|
{ |
||||
|
public const string MainModuleName = "avalonia"; |
||||
|
public const string StorageModuleName = "storage"; |
||||
|
|
||||
|
public static Task ImportMain() |
||||
|
{ |
||||
|
var options = AvaloniaLocator.Current.GetService<BrowserPlatformOptions>() ?? new BrowserPlatformOptions(); |
||||
|
return JSHost.ImportAsync(MainModuleName, options.FrameworkAssetPathResolver("avalonia.js")); |
||||
|
} |
||||
|
|
||||
|
public static Task ImportStorage() |
||||
|
{ |
||||
|
var options = AvaloniaLocator.Current.GetService<BrowserPlatformOptions>() ?? new BrowserPlatformOptions(); |
||||
|
return JSHost.ImportAsync(StorageModuleName, options.FrameworkAssetPathResolver("storage.js")); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue