Skip to content

Commit

Permalink
Base class changed to Rectangle & fix DefaultBackgroundColor
Browse files Browse the repository at this point in the history
Improvement example project
  • Loading branch information
AigioL committed Jul 11, 2022
1 parent 25bfda1 commit 9a01ca0
Show file tree
Hide file tree
Showing 20 changed files with 299 additions and 85 deletions.
25 changes: 25 additions & 0 deletions Avalonia.WebView2.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_Root", "_Root", "{61E7D838
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.WebView2.Sample", "src\Avalonia.WebView2.Sample\Avalonia.WebView2.Sample.csproj", "{A08F349B-7924-440B-A3B6-A06A07BD3568}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Avalonia.Ref", "Avalonia.Ref", "{DC6BC752-4767-4682-92C7-3AE2C9BA5C84}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.X11", "src\Avalonia.Ref\Avalonia.X11\Avalonia.X11.csproj", "{47ECFB81-D3BA-4E50-8CCC-774FD482A9E4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.Native", "src\Avalonia.Ref\Avalonia.Native\Avalonia.Native.csproj", "{C3F8BEC7-5365-4C35-8236-BC853A592106}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.Desktop", "src\Avalonia.Ref\Avalonia.Desktop\Avalonia.Desktop.csproj", "{F482B647-9FF9-4BCC-B8F7-59ACD76D2B51}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -30,10 +38,27 @@ Global
{A08F349B-7924-440B-A3B6-A06A07BD3568}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A08F349B-7924-440B-A3B6-A06A07BD3568}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A08F349B-7924-440B-A3B6-A06A07BD3568}.Release|Any CPU.Build.0 = Release|Any CPU
{47ECFB81-D3BA-4E50-8CCC-774FD482A9E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47ECFB81-D3BA-4E50-8CCC-774FD482A9E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47ECFB81-D3BA-4E50-8CCC-774FD482A9E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47ECFB81-D3BA-4E50-8CCC-774FD482A9E4}.Release|Any CPU.Build.0 = Release|Any CPU
{C3F8BEC7-5365-4C35-8236-BC853A592106}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3F8BEC7-5365-4C35-8236-BC853A592106}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3F8BEC7-5365-4C35-8236-BC853A592106}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3F8BEC7-5365-4C35-8236-BC853A592106}.Release|Any CPU.Build.0 = Release|Any CPU
{F482B647-9FF9-4BCC-B8F7-59ACD76D2B51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F482B647-9FF9-4BCC-B8F7-59ACD76D2B51}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F482B647-9FF9-4BCC-B8F7-59ACD76D2B51}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F482B647-9FF9-4BCC-B8F7-59ACD76D2B51}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{47ECFB81-D3BA-4E50-8CCC-774FD482A9E4} = {DC6BC752-4767-4682-92C7-3AE2C9BA5C84}
{C3F8BEC7-5365-4C35-8236-BC853A592106} = {DC6BC752-4767-4682-92C7-3AE2C9BA5C84}
{F482B647-9FF9-4BCC-B8F7-59ACD76D2B51} = {DC6BC752-4767-4682-92C7-3AE2C9BA5C84}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F80CD13F-5A1C-4F58-BDCA-4C2991EE56F4}
EndGlobalSection
Expand Down
3 changes: 3 additions & 0 deletions src/Avalonia.Ref/AssemblyInfo.Version.Max.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Reflection;

[assembly: AssemblyVersion("255.255.255.255")]
42 changes: 42 additions & 0 deletions src/Avalonia.Ref/Avalonia.Desktop/AppBuilderDesktopExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Avalonia.Controls;
using Avalonia.Platform;

// https://github.com/AvaloniaUI/Avalonia/blob/0.10.13/src/Avalonia.Desktop/AppBuilderDesktopExtensions.cs

namespace Avalonia
{
public static class AppBuilderDesktopExtensions
{
public static TAppBuilder UsePlatformDetect<TAppBuilder>(this TAppBuilder builder)
where TAppBuilder : AppBuilderBase<TAppBuilder>, new()
{
#if WINDOWS
builder.UseWin32();
#else
var os = builder.RuntimePlatform.GetRuntimeInfo().OperatingSystem;

// We don't have the ability to load every assembly right now, so we are
// stuck with manual configuration here
// Helpers are extracted to separate methods to take the advantage of the fact
// that CLR doesn't try to load dependencies before referencing method is jitted
// Additionally, by having a hard reference to each assembly,
// we verify that the assemblies are in the final .deps.json file
// so .NET Core knows where to load the assemblies from,.
switch (os)
{
case OperatingSystemType.WinNT:
builder.UseWin32();
break;
case OperatingSystemType.OSX:
builder.UseAvaloniaNative();
break;
default:
builder.UseX11();
break;
}
#endif
builder.UseSkia();
return builder;
}
}
}
28 changes: 28 additions & 0 deletions src/Avalonia.Ref/Avalonia.Desktop/Avalonia.Desktop.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net6.0-windows</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>..\avalonia.snk</AssemblyOriginatorKeyFile>
<!--https://github.com/AvaloniaUI/Avalonia/blob/0.10.10/build/SharedVersion.props#L18-->
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\AssemblyInfo.Version.Max.cs">
<LinkBase>Properties</LinkBase>
</Compile>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Avalonia.Win32" />
<PackageReference Include="Avalonia.Skia" />
</ItemGroup>

<ItemGroup Condition=" !$(TargetFramework.Contains('-windows')) ">
<PackageReference Include="Avalonia.X11" />
<PackageReference Include="Avalonia.Native" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions src/Avalonia.Ref/Avalonia.Native/Avalonia.Native.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net6.0-windows</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>..\avalonia.snk</AssemblyOriginatorKeyFile>
<!--https://github.com/AvaloniaUI/Avalonia/blob/0.10.10/build/SharedVersion.props#L18-->
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\AssemblyInfo.Version.Max.cs">
<LinkBase>Properties</LinkBase>
</Compile>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Avalonia" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if !WINDOWS

using System;
using Avalonia.Controls;

namespace Avalonia
{
public static class AvaloniaNativePlatformExtensions
{
public static T UseAvaloniaNative<T>(this T builder) where T : AppBuilderBase<T>, new()
{
throw new PlatformNotSupportedException();
}
}
}

#endif
22 changes: 22 additions & 0 deletions src/Avalonia.Ref/Avalonia.X11/Avalonia.X11.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>..\avalonia.snk</AssemblyOriginatorKeyFile>
<!--https://github.com/AvaloniaUI/Avalonia/blob/0.10.10/build/SharedVersion.props#L18-->
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\AssemblyInfo.Version.Max.cs">
<LinkBase>Properties</LinkBase>
</Compile>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Avalonia" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions src/Avalonia.Ref/Avalonia.X11/AvaloniaX11PlatformExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#if !WINDOWS

using System;
using Avalonia.Controls;

namespace Avalonia
{
public static class AvaloniaX11PlatformExtensions
{
public static T UseX11<T>(this T builder) where T : AppBuilderBase<T>, new()
{
throw new PlatformNotSupportedException();
}
}
}

#endif
Binary file added src/Avalonia.Ref/avalonia.snk
Binary file not shown.
19 changes: 1 addition & 18 deletions src/Avalonia.WebView2.Sample/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Avalonia.Markup.Xaml;
using AvaloniaWebView2 = Avalonia.Controls.WebView2;

namespace Avalonia.WebView2.Sample;

public class App : Application
Expand All @@ -21,23 +18,9 @@ public override void OnFrameworkInitializationCompleted()
base.OnFrameworkInitializationCompleted();
}

public static bool AvailableWebView2 { get; private set; }

public static string? WebView2VersionString { get; private set; }

static void InitWebView2()
{
try
{
WebView2VersionString = CoreWebView2Environment.GetAvailableBrowserVersionString();
if (!string.IsNullOrEmpty(WebView2VersionString)) AvailableWebView2 = true;
}
catch (WebView2RuntimeNotFoundException)
{

}

if (AvailableWebView2)
if (AvaloniaWebView2.IsSupported)
{
AvaloniaWebView2.DefaultCreationProperties = new()
{
Expand Down
3 changes: 3 additions & 0 deletions src/Avalonia.WebView2.Sample/Avalonia.WebView2.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Avalonia.Ref\Avalonia.Desktop\Avalonia.Desktop.csproj" />
<ProjectReference Include="..\Avalonia.Ref\Avalonia.Native\Avalonia.Native.csproj" />
<ProjectReference Include="..\Avalonia.Ref\Avalonia.X11\Avalonia.X11.csproj" />
<ProjectReference Include="..\Avalonia.WebView2\Avalonia.WebView2.csproj" />
</ItemGroup>

Expand Down
46 changes: 20 additions & 26 deletions src/Avalonia.WebView2.Sample/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,27 @@
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Grid RowDefinitions="Auto * Auto">
<Grid
<Grid RowDefinitions="Auto Auto * Auto">
<Label
x:Name="Title"
Grid.Row="0"
Margin="6,6,6,6"
HorizontalAlignment="Stretch"
VerticalAlignment="Top">
<Label
x:Name="Title"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="14"
FontWeight="Bold" />
</Grid>
<Grid Grid.Row="1" Background="BlueViolet">
<local:WebView2Compat x:Name="WebView2Compat" />
<!--<wv2:WebView2
x:Name="WebView2"
Source="https://www.bing.com" />-->
</Grid>
<Grid Grid.Row="2">
<Button
x:Name="Button"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="Debug Button"
FontSize="12"
FontWeight="Bold" />
</Grid>
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="14"
FontWeight="Bold" />
<TextBox
x:Name="UrlTextBox"
Grid.Row="1"
Watermark="Enter the URL address and press enter to navigate" />
<local:WebView2Compat x:Name="WebView2Compat" Grid.Row="2" />
<Button
x:Name="Button"
Grid.Row="3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="Debug Button"
FontSize="12"
FontWeight="Bold" />
</Grid>
</Window>
51 changes: 35 additions & 16 deletions src/Avalonia.WebView2.Sample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Avalonia.Markup.Xaml;
using AvaloniaWebView2 = Avalonia.Controls.WebView2;

namespace Avalonia.WebView2.Sample;

public partial class MainWindow : Window
Expand All @@ -9,8 +6,11 @@ public partial class MainWindow : Window
readonly AvaloniaWebView2 WebView2;
readonly WebView2Compat WebView2Compat;
readonly new Label Title;
readonly TextBox UrlTextBox;

public AvaloniaWebView2? WebView => WebView2 ?? WebView2Compat?.WebView2;

public CoreWebView2Environment Environment { get; }
public CoreWebView2Environment? Environment { get; }

public MainWindow()
{
Expand All @@ -23,25 +23,44 @@ public MainWindow()
WebView2 = this.FindControl<AvaloniaWebView2>("WebView2");
WebView2Compat = this.FindControl<WebView2Compat>("WebView2Compat");
Button = this.FindControl<Button>("Button");
UrlTextBox = this.FindControl<TextBox>("UrlTextBox");

Button.Click += Button_Click;
Environment = AvaloniaWebView2.DefaultCreationProperties!.CreateEnvironmentAsync().GetAwaiter().GetResult();
Environment.ProcessInfosChanged += Environment_ProcessInfosChanged;
SetTitle(Environment.BrowserVersionString);
if (AvaloniaWebView2.IsSupported)
{
UrlTextBox.KeyDown += UrlTextBox_KeyDown;
Environment = AvaloniaWebView2.DefaultCreationProperties!.CreateEnvironmentAsync().GetAwaiter().GetResult();
Environment.ProcessInfosChanged += Environment_ProcessInfosChanged;
SetTitle(Environment.BrowserVersionString);
}
else
{
SetTitle(null);
}
}

const Architecture Unknown = (Architecture)int.MinValue;

static string GetTitle(string browserVersion, Architecture architecture = Unknown)
void UrlTextBox_KeyDown(object? sender, KeyEventArgs e)
{
if (architecture != Unknown)
if (e.Key == Key.Enter)
{
return $"Microsoft Edge WebView2 {browserVersion} {architecture} for Avalonia on {System.Environment.OSVersion.VersionString}";
var url = UrlTextBox.Text;
if (!IsHttpUrl(url)) url = $"{Prefix_HTTPS}{url}";
WebView?.CoreWebView2?.Navigate(url);
}
return $"Microsoft Edge WebView2 {browserVersion} for Avalonia on {System.Environment.OSVersion.VersionString}";
}

void SetTitle(string browserVersion, Architecture architecture = Unknown) => Title.Content = base.Title = GetTitle(browserVersion, architecture);
const string Prefix_HTTPS = "https://";
const string Prefix_HTTP = "http://";

static bool IsHttpUrl([NotNullWhen(true)] string? url, bool httpsOnly = false) => url != null &&
(url.StartsWith(Prefix_HTTPS, StringComparison.OrdinalIgnoreCase) ||
(!httpsOnly && url.StartsWith(Prefix_HTTP, StringComparison.OrdinalIgnoreCase)));

const Architecture Unknown = (Architecture)int.MinValue;

static string GetTitle(string? browserVersion, Architecture architecture = Unknown) => $"Microsoft Edge WebView2{(string.IsNullOrEmpty(browserVersion) ? null : $" {browserVersion}")}{(architecture == Unknown ? null : $" {architecture}")} for Avalonia on {System.Environment.OSVersion.VersionString}";

void SetTitle(string? browserVersion, Architecture architecture = Unknown) => Title.Content = base.Title = GetTitle(browserVersion, architecture);

void InitializeComponent()
{
Expand All @@ -50,7 +69,7 @@ void InitializeComponent()

void Environment_ProcessInfosChanged(object? sender, object e)
{
var processInfos = Environment.GetProcessInfos();
var processInfos = Environment!.GetProcessInfos();
foreach (var processInfo in processInfos)
{
try
Expand Down Expand Up @@ -78,7 +97,7 @@ void Environment_ProcessInfosChanged(object? sender, object e)
void Button_Click(object? sender, RoutedEventArgs e)
{
#if DEBUG
(WebView2 ?? WebView2Compat?.WebView2)?.Test();
WebView?.Test();
#endif
}

Expand Down
3 changes: 0 additions & 3 deletions src/Avalonia.WebView2.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using Microsoft.Win32;
using System.Windows;

namespace Avalonia.WebView2.Sample;

static class Program
Expand Down
Loading

0 comments on commit 9a01ca0

Please sign in to comment.