Skip to content

Commit

Permalink
improvement WebView2
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Jul 11, 2022
1 parent 6abcd3d commit 021957b
Show file tree
Hide file tree
Showing 20 changed files with 270 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
<PackageVersion Include="Avalonia.Win32" Version="0.10.16" />
<PackageVersion Include="Avalonia.X11" Version="0.10.16" />
<!--<PackageVersion Include="Avalonia.HtmlRenderer" Version="0.10.0" />-->
<PackageVersion Include="WebView2.Avalonia" Version="1.0.1264.42-preview.220711.5" />
<PackageVersion Include="WebView2.Avalonia" Version="1.0.1264.42-preview.220711.6" />
<!--Avalonia End-->

<!-- Xamarin.Android -->
Expand Down
33 changes: 32 additions & 1 deletion SteamToolsV2+.sln
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ST.Client.ReverseProxy.Yarp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ST.Client.Windows.ScheduledTasks", "src\ST.Client.Windows.ScheduledTasks\ST.Client.Windows.ScheduledTasks.csproj", "{0EEA90D8-FAB7-48C9-8C15-1E61AEF8AB81}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.WebView2", "src\Avalonia.WebView2\Avalonia.WebView2.csproj", "{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -2625,6 +2627,34 @@ Global
{0EEA90D8-FAB7-48C9-8C15-1E61AEF8AB81}.Release|x64.Build.0 = Release|Any CPU
{0EEA90D8-FAB7-48C9-8C15-1E61AEF8AB81}.Release|x86.ActiveCfg = Release|Any CPU
{0EEA90D8-FAB7-48C9-8C15-1E61AEF8AB81}.Release|x86.Build.0 = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|ARM.ActiveCfg = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|ARM.Build.0 = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|ARM64.Build.0 = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|iPhone.Build.0 = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|x64.ActiveCfg = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|x64.Build.0 = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|x86.ActiveCfg = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Debug|x86.Build.0 = Debug|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|Any CPU.Build.0 = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|ARM.ActiveCfg = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|ARM.Build.0 = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|ARM64.ActiveCfg = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|ARM64.Build.0 = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|iPhone.ActiveCfg = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|iPhone.Build.0 = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|x64.ActiveCfg = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|x64.Build.0 = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|x86.ActiveCfg = Release|Any CPU
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2713,9 +2743,10 @@ Global
{92DEBBE5-FF82-4251-ABC6-75FF285E7149} = {327FDEE7-C4C8-4AA4-A7B0-8BCE3D75346F}
{D8ED7A17-D693-45FF-B75A-D9B12DAB5A42} = {327FDEE7-C4C8-4AA4-A7B0-8BCE3D75346F}
{0EEA90D8-FAB7-48C9-8C15-1E61AEF8AB81} = {E50441E8-75F3-406E-9921-30BFC211B5CE}
{8C996C50-B29F-4CB6-90A7-77C9CD3B0B70} = {910D6A0F-42AB-4B65-B532-852E864EC006}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9313F2B4-10DA-43AA-A481-CAA7571C740C}
RESX_NeutralResourcesLanguage = zh-Hans
SolutionGuid = {9313F2B4-10DA-43AA-A481-CAA7571C740C}
EndGlobalSection
EndGlobal
22 changes: 22 additions & 0 deletions src/Avalonia.WebView2/Avalonia.WebView2.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>$(MSBuildProjectDirectory)\..\..\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>
23 changes: 23 additions & 0 deletions src/Avalonia.WebView2/WebView2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using WebView2BaseType = Avalonia.Controls.Shapes.Rectangle;

namespace Avalonia.Controls;

public partial class WebView2 : WebView2BaseType
{
public static bool IsSupported { get; }

public static readonly DirectProperty<WebView2, Uri?> SourceProperty =
AvaloniaProperty.RegisterDirect<WebView2, Uri?>(
nameof(Uri),
x => x.source,
(x, y) => x.Source = y);

Uri? source;

public Uri? Source
{
get => source;
set => SetAndRaise(SourceProperty, ref source, value);
}
}
3 changes: 0 additions & 3 deletions src/ST.Client.Desktop.Avalonia/Application/UI/App.WebView2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ static string GetUserDataFolder()
}
}
}

// WebView2AutoInstaller
// https://github.com/ProKn1fe/WebView2.Runtime/blob/62011b09436944143996fdb0039cd2c5dbb5c300/WebView2.Runtime.AutoInstaller/WebView2.Runtime.AutoInstaller/WebView2AutoInstaller.cs
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Rotator()
// Prepare render loop task for use.
_loopTask = new RenderLoopClock();
_loopTask.Subscribe(
delegate(TimeSpan renderTime)
renderTime =>
{
var delta = renderTime - _prev;
_rotateDegree += _speed * delta.TotalMilliseconds;
Expand All @@ -54,8 +54,8 @@ public double Speed

public static readonly DirectProperty<Rotator, double> SpeedProperty =
AvaloniaProperty.RegisterDirect(nameof(Speed),
delegate(Rotator rotator) { return rotator._speed; },
delegate(Rotator rotator, double v)
rotator => rotator._speed,
(Rotator rotator, double v) =>
{
rotator._speed = v;
OnSpeedChanged(rotator, v);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<UserControl
x:Class="System.Application.UI.Views.Controls.WebView2Compat"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:resx="using:System.Application.UI.Resx"
xmlns:wv2="clr-namespace:Avalonia.Controls;assembly=Avalonia.WebView2"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Panel>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock x:Name="TbContent"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="24" />
<Button x:Name="BtnDownloadAndInstall"
Margin="0,24,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{ReflectionBinding Path=Res.DownloadAndInstall,
Mode=OneWay,
Source={x:Static resx:R.Current}}" />
</StackPanel>
<wv2:WebView2 x:Name="WebView2" />
</Panel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using Avalonia;
using Avalonia.Interactivity;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using System.Application.UI.Resx;
#if WINDOWS
using Microsoft.Web.WebView2.Core;
#endif

namespace System.Application.UI.Views.Controls;

public partial class WebView2Compat : UserControl
{
public readonly TextBlock TbContent;
public readonly Button BtnDownloadAndInstall;
public readonly WebView2 WebView2;

public WebView2Compat()
{
InitializeComponent();

TbContent = this.FindControl<TextBlock>("TbContent");
BtnDownloadAndInstall = this.FindControl<Button>("BtnDownloadAndInstall");
WebView2 = this.FindControl<WebView2>("WebView2");
WebView2.IsVisible = false;
#if WINDOWS
WebView2.DOMContentLoaded += WebView2_DOMContentLoaded;
#endif

var webView2IsSupported = WebView2.IsSupported;
TbContent.Text = webView2IsSupported ? AppResources.Loading : AppResources.YouNeedInstallWebView2Runtime;
if (BtnDownloadAndInstall.IsVisible = !webView2IsSupported)
{
BtnDownloadAndInstall.Click += BtnDownloadAndInstall_Click;
}

this.GetPropertyChangedObservable(SourceProperty).Subscribe(OnSourceChanged);
this.GetPropertyChangedObservable(HtmlSourceProperty).Subscribe(OnHtmlSourceChanged);
}

bool isNotFirstLoad;

#if WINDOWS
void WebView2_DOMContentLoaded(object? sender, CoreWebView2DOMContentLoadedEventArgs e)
{
if (!isNotFirstLoad) isNotFirstLoad = true;
WebView2.IsVisible = true;
}
#endif

void BtnDownloadAndInstall_Click(object? sender, RoutedEventArgs e)
{
// WebView2AutoInstaller
// https://github.com/ProKn1fe/WebView2.Runtime/blob/62011b09436944143996fdb0039cd2c5dbb5c300/WebView2.Runtime.AutoInstaller/WebView2.Runtime.AutoInstaller/WebView2AutoInstaller.cs
}

void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

public static readonly DirectProperty<WebView2Compat, Uri?> SourceProperty =
AvaloniaProperty.RegisterDirect<WebView2Compat, Uri?>(
nameof(Source),
x => x._Source,
(x, y) => x.Source = y);

Uri? _Source;

public Uri? Source
{
get => _Source;
set => SetAndRaise(SourceProperty, ref _Source, value);
}

void OnSourceChanged(EventArgs e)
{
if (!isNotFirstLoad) WebView2.IsVisible = true;
WebView2.Source = Source;
if (!isNotFirstLoad) WebView2.IsVisible = false;
}

public static readonly DirectProperty<WebView2Compat, string?> HtmlSourceProperty =
AvaloniaProperty.RegisterDirect<WebView2Compat, string?>(
nameof(HtmlSource),
x => x._HtmlSource,
(x, y) => x.HtmlSource = y);

string? _HtmlSource;

public string? HtmlSource
{
get => _HtmlSource;
set => SetAndRaise(HtmlSourceProperty, ref _HtmlSource, value);
}

void OnHtmlSourceChanged(EventArgs e)
{
#if WINDOWS
var htmlString = _HtmlSource;
if (!string.IsNullOrWhiteSpace(htmlString))
{
if (!isNotFirstLoad) WebView2.IsVisible = true;
WebView2.NavigateToString(htmlString);
if (!isNotFirstLoad) WebView2.IsVisible = false;
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wv2="clr-namespace:Avalonia.Controls;assembly=Avalonia.WebView2"
xmlns:controls="using:System.Application.UI.Views.Controls"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="System.Application.UI.Views.Pages.DebugWebViewPage">
<Grid RowDefinitions="Auto,*">
<TextBox x:Name="urlTextBox" Watermark="Input Url Press Enter..."></TextBox>
<wv2:WebView2 Grid.Row="1" x:Name="webView" />
<controls:WebView2Compat Grid.Row="1" x:Name="webView" />
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml;
using Avalonia.ReactiveUI;
using Microsoft.Web.WebView2.Core;
using System.Application.Services;
using System.Application.UI.Resx;
using System.Application.UI.ViewModels;
using System.Application.UI.Views.Controls;
using static System.Common.Constants;
Expand All @@ -12,20 +15,28 @@ namespace System.Application.UI.Views.Pages
{
public class DebugWebViewPage : ReactiveUserControl<DebugWebViewPageViewModel>/*, IDisposable*/
{
readonly WebView2 webView;
readonly WebView2Compat webViewCompat;
readonly TextBox urlTextBox;

public DebugWebViewPage()
{
InitializeComponent();

webView = this.FindControl<WebView2>("webView");
webView.Source = new(UrlConstants.GitHub_Repository);
webViewCompat = this.FindControl<WebView2Compat>("webView");
webViewCompat.Source = new(R.IsChineseSimplified ? UrlConstants.Gitee_Repository : UrlConstants.GitHub_Repository);
//webViewCompat.WebView2.DOMContentLoaded += WebView2_DOMContentLoaded;

urlTextBox = this.FindControl<TextBox>("urlTextBox");
urlTextBox.KeyUp += UrlTextBox_KeyUp;
}

//async void WebView2_DOMContentLoaded(object? sender, CoreWebView2DOMContentLoadedEventArgs e)
//{
// webViewCompat.WebView2.DOMContentLoaded -= WebView2_DOMContentLoaded;
// var userAgent = await webViewCompat.WebView2.ExecuteScriptAsync("window.navigator.userAgent");
// // "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.49"
//}

private void UrlTextBox_KeyUp(object? sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
Expand All @@ -35,15 +46,10 @@ private void UrlTextBox_KeyUp(object? sender, KeyEventArgs e)
var array = url.Split('/');
if (array.Length < 3) return;
if (!array[2].Contains('.')) return;
webView.CoreWebView2?.Navigate(url);
webViewCompat.Source = new(url);
}
}

//private async void WebView_BrowserCreated(object sender, EventArgs e)
//{
// var value = await webView.GetUserAgentAsync();
//}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
Expand Down
15 changes: 2 additions & 13 deletions src/ST.Client.Desktop.Avalonia/ST.Client.Avalonia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,8 @@
<PackageReference Include="WebView2.Avalonia" />
</ItemGroup>

<ItemGroup>
<AvaloniaResource Update="Application\UI\Styles\Controls\TreeView.xaml">
<Generator>MSBuild:Compile</Generator>
</AvaloniaResource>
</ItemGroup>

<ItemGroup>
<Compile Update="Application\UI\Views\Controls\AdDialog.axaml.cs">
<DependentUpon>AdDialog.axaml</DependentUpon>
</Compile>
<Compile Update="Application\UI\Views\Windows\GameListPage\CloudArchiveWindow.axaml.cs">
<DependentUpon>CloudArchiveWindow.axaml</DependentUpon>
</Compile>
<ItemGroup Condition=" !$(TargetFramework.Contains('-windows')) ">
<ProjectReference Include="..\Avalonia.WebView2\Avalonia.WebView2.csproj" />
</ItemGroup>

<Import Condition=" $(TargetFramework.Contains('-windows')) " Project="..\..\WindowsPlatform.props" />
Expand Down
Loading

0 comments on commit 021957b

Please sign in to comment.