Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
123 changes: 123 additions & 0 deletions UWP/WebViewRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
namespace Zebble;

using System;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using controls = Windows.UI.Xaml.Controls;
using Olive;

partial class WebViewRenderer : INativeRenderer
{
WebView View;
controls.WebView Result;

public async Task<FrameworkElement> Render(Renderer renderer)
{
View = (WebView)renderer.View;

View.SourceChanged.HandleOn(Thread.UI, () => Reload());
View.EvaluatedJavascript += x => Thread.UI.Run(() => EvaluateJavascript(x));
View.InvokeJavascriptFunction += (s, a) => Thread.UI.Post(() => EvaluateJavascriptFunction(s, a));
CreateBrowser();
Reload();

return Result;
}

void CreateBrowser()
{
SetDefaultUserAgent();

Result = new controls.WebView(controls.WebViewExecutionMode.SeparateThread);
Result.Loaded += async (s, e) => await View.LoadFinished.RaiseOn(Thread.Pool);

Result.NavigationStarting += (s, e) =>
{
if (e.Uri != null && View.OnBrowserNavigating(e.Uri.ToString())) Result.Stop();
};

Result.NavigationCompleted += async (s, e) =>
{
if (View.BrowserNavigated.IsHandled())
{
var html = (await EvaluateJavascript("document.documentElement.outerHTML")).ToStringOrEmpty();
var url = e.Uri.ToStringOrEmpty();

Thread.Pool.RunAction(() => View.OnBrowserNavigated(url, html));
}
};

Result.LoadCompleted += Browser_LoadCompleted;
Result.NavigationFailed += Browser_NavigationFailed;
}

async void Browser_NavigationFailed(object sender, controls.WebViewNavigationFailedEventArgs args)
{
var error = args.WebErrorStatus.ToString();

await View.LoadingError.RaiseOn(Thread.Pool, error);
}

void Browser_LoadCompleted(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs args)
{
// TODO: Find if it was an error, and raise the event.
}

Task<string> EvaluateJavascript(string script) => Result.InvokeScriptAsync("eval", new string[] { script }).AsTask();

async void EvaluateJavascriptFunction(string function, string[] args)
{
try
{
await Result.InvokeScriptAsync(function, args).AsTask();
}
catch (Exception ex)
{
if (ex.Message == "Exception from HRESULT: 0x80020101")
{
Log.For(this).Error(ex, "Syntax error in the javascript invoking function '" + function + "' with params:\n" +
args.ToLinesString());
}
else
{
Log.For(this).Error(ex, "EvaluateJavascriptFunction() failed.");
}
}
}

public controls.WebView Render() => Result;

void Reload()
{
if (View.Url?.Contains(":") == true) Result.Navigate(GetUri());
else Result.NavigateToString(View.GetExecutableHtml().OrEmpty());
}

public Uri GetUri()
{
var path = View.Url;

if (path.Contains(":")) return path.AsUri();

var notFond = Device.IO.AbsolutePath("Images/Icons/not-found.png").AsUri();

path = path.OrEmpty();

var file = Device.IO.File(path.TrimStart("/").Replace("/", "\\"));
if (!file.Exists() && file.Extension.OrEmpty().ToLower().IsAnyOf(".gif", ".png", ".jpg", ".jpeg", ".webp"))
{
Log.For(this).Error("Image file does not exist: " + file);
return notFond;
}

return file.FullName.AsUri();
}

public void Dispose()
{
Result = null;
View = null;

GC.SuppressFinalize(this);
}
}
21 changes: 12 additions & 9 deletions WebView.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="MSBuild.Sdk.Extras/3.0.44">
<PropertyGroup>
<TargetFrameworks>net8.0-android;net8.0-ios</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net8.0-windows10.0.19041</TargetFrameworks>
<TargetFrameworks>net9.0-android;net9.0-ios</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net9.0-windows10.0.26100</TargetFrameworks>
<AssemblyName>Zebble.WebView</AssemblyName>
<RootNamespace>Zebble</RootNamespace>
<PackageId>Zebble.WebView</PackageId>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<Version>5.1.1.0</Version>
<Version>5.1.2.0</Version>
<PackOnBuild>true</PackOnBuild>
<NeutralLanguage>en</NeutralLanguage>
<DefineConstants>$(DefineConstants)</DefineConstants>
Expand Down Expand Up @@ -35,9 +35,10 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" $(TargetFramework.Contains('windows')) ">
<PropertyGroup Condition=" $(TargetFramework.Contains('net9.0-windows')) ">
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<DefineConstants>$(DefineConstants);WINUI</DefineConstants>
<DefineConstants>$(DefineConstants);UWP</DefineConstants>
<UseUwp>true</UseUwp>
</PropertyGroup>
<PropertyGroup Condition=" $(TargetFramework.Contains('android')) ">
<DefineConstants>$(DefineConstants);ANDROID</DefineConstants>
Expand All @@ -49,8 +50,10 @@
<ItemGroup>
<Compile Include="Shared\**\*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(DefineConstants.Contains('WINUI')) ">
<Compile Include="WinUI\**\*.cs" />
<ItemGroup Condition=" $(DefineConstants.Contains('UWP')) ">
<Compile Include="UWP\**\*.cs" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.250108002" IncludeAssets="build" PrivateAssets="all" />
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.2.0" />
</ItemGroup>
<ItemGroup Condition=" $(DefineConstants.Contains('ANDROID')) ">
<Compile Include="Android\**\*.cs" />
Expand All @@ -65,8 +68,8 @@
<None Remove="Zebble-LicenseAgreement.txt" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.65" />
<PackageReference Include="Zebble" Version="5.1.2" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.72" />
<PackageReference Include="Zebble" Version="5.1.7" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="'$(Running)'=='local'">
<Exec Command="update-local-nuget-cache $(TargetDir)" />
Expand Down
112 changes: 0 additions & 112 deletions WinUI/WebViewRenderer.cs

This file was deleted.