Skip to content

Commit

Permalink
Add HtmlSourceProperty & Remove VerifyInitializedGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Jul 12, 2022
1 parent 90fcc0c commit 02146ed
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Avalonia.WebView2/Avalonia.WebView2.csproj
Expand Up @@ -10,7 +10,7 @@
This package is necessary for Avalonia applications. To learn more about package versions checkout https://aka.ms/wv2-packageversion. To learn best practices checkout: https://aka.ms/wv2-bestpractices.
</Description>
<PackageTags>Web WebView Native native package Edge avalonia avaloniaui dotnet framework core Webview2</PackageTags>
<Version>1.0.1264.42-preview.220712.8</Version>
<Version>1.0.1264.42-preview.220712.9</Version>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>

Expand Down
2 changes: 2 additions & 0 deletions src/Avalonia.WebView2/WebView2.AvaloniaProperty.cs
Expand Up @@ -17,6 +17,8 @@ partial class WebView2
/// </summary>
public static readonly DirectProperty<WebView2, Uri?> SourceProperty = AvaloniaProperty.RegisterDirect<WebView2, Uri?>(nameof(Source), x => x._source, (x, y) => x.Source = y);

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

/// <summary>
/// The <see cref="AvaloniaProperty" /> which backs the <see cref="CanGoBack" /> property.
/// </summary>
Expand Down
88 changes: 58 additions & 30 deletions src/Avalonia.WebView2/WebView2.cs
Expand Up @@ -149,6 +149,7 @@ protected override void OnInitialized()
static readonly Color _defaultBackgroundColorDefaultValue = Color.White;
Color _defaultBackgroundColor;
Uri? _source;
string? _htmlSource;
bool _browserCrashed;
readonly ImplicitInitGate _implicitInitGate = new();

Expand All @@ -158,8 +159,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (IsInitialized)
UnsubscribeHandlersAndCloseController();
UnsubscribeHandlersAndCloseController();
}

disposedValue = true;
Expand All @@ -174,7 +174,6 @@ public void Dispose()

void UnsubscribeHandlersAndCloseController(bool browserCrashed = false)
{
IsInitialized = false;
_browserCrashed = browserCrashed;
if (!_browserCrashed)
{
Expand Down Expand Up @@ -397,14 +396,17 @@ async Task InitCoreWebView2Async(CoreWebView2Environment? environment = null, Co
sender.CoreWebView2.ProcessFailed += new EventHandler<CoreWebView2ProcessFailedEventArgs>(sender.CoreWebView2_ProcessFailed);
if (sender.Focusable)
sender._coreWebView2Controller.MoveFocus(CoreWebView2MoveFocusReason.Programmatic);
int num = sender._source != null ? 1 : 0;
if (sender._source == null)
sender._source = new Uri(sender.CoreWebView2.Source);
sender.IsInitialized = true;

sender.CoreWebView2InitializationCompleted?.Invoke(sender, new CoreWebView2InitializationCompletedEventArgs());
if (num == 0)
return;
sender.CoreWebView2.Navigate(sender._source.AbsoluteUri);

if (sender._source != null)
{
sender.CoreWebView2.Navigate(sender._source.AbsoluteUri);
}
else if (sender._htmlSource != null)
{
sender.CoreWebView2.NavigateToString(sender._htmlSource);
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -478,7 +480,7 @@ protected virtual void IsVisibleChanged(EventArgs e)
protected override void OnGotFocus(GotFocusEventArgs e)
{
base.OnGotFocus(e);
if (IsInitialized)
if (_coreWebView2Controller != null)
{
if (!_browserCrashed)
{
Expand All @@ -500,11 +502,6 @@ protected override void OnGotFocus(GotFocusEventArgs e)
#endif
}

/// <summary>
/// True if initialization finished successfully and the control is not disposed yet.
/// </summary>
protected new bool IsInitialized { get; set; }

#if !DISABLE_WEBVIEW2_CORE
/// <summary>
/// The underlying CoreWebView2. Use this property to perform more operations on the WebView2 content than is exposed
Expand Down Expand Up @@ -608,6 +605,7 @@ public override void EndInit()
}
if (_source == null || _source.AbsoluteUri != value.AbsoluteUri)
{
_htmlSource = null;
SetAndRaise(SourceProperty, ref _source, value);
#if !DISABLE_WEBVIEW2_CORE
if (CoreWebView2 != null) CoreWebView2.Navigate(value.AbsoluteUri);
Expand All @@ -620,6 +618,37 @@ public override void EndInit()
}
}

[Browsable(true)]
public string? HtmlSource
{
get => _htmlSource;
set
{
if (value == null)
{
if (_htmlSource == null)
{
return;
}
throw new NotImplementedException("The HtmlSource property cannot be set to null.");
}
else
{
if (_htmlSource == null || _htmlSource != value)
{
_source = null;
SetAndRaise(HtmlSourceProperty, ref _htmlSource, value);
#if !DISABLE_WEBVIEW2_CORE
if (CoreWebView2 != null) CoreWebView2.NavigateToString(value);
#endif
}
#if !DISABLE_WEBVIEW2_CORE
_implicitInitGate.RunWhenOpen(() => EnsureCoreWebView2Async());
#endif
}
}
}

/// <summary>
/// Returns true if the webview can navigate to a next page in the
/// navigation history via the <see cref="M:Avalonia.Controls.WebView2.GoForward" /> method.
Expand Down Expand Up @@ -689,13 +718,14 @@ public Color DefaultBackgroundColor
/// <seealso cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.ExecuteScriptAsync(System.String)" />
public async Task<string> ExecuteScriptAsync(string script)
{
VerifyInitializedGuard();
VerifyBrowserNotCrashedGuard();
#if !DISABLE_WEBVIEW2_CORE
return await CoreWebView2!.ExecuteScriptAsync(script);
#else
return await Task.FromResult(string.Empty);
if (CoreWebView2 != null)
{
return await CoreWebView2.ExecuteScriptAsync(script);
}
#endif
return await Task.FromResult(string.Empty);
}

/// <summary>
Expand All @@ -707,10 +737,12 @@ public async Task<string> ExecuteScriptAsync(string script)
/// <seealso cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.Reload" />
public void Reload()
{
VerifyInitializedGuard();
VerifyBrowserNotCrashedGuard();
#if !DISABLE_WEBVIEW2_CORE
CoreWebView2!.Reload();
if (CoreWebView2 != null)
{
CoreWebView2!.Reload();
}
#endif
}

Expand Down Expand Up @@ -750,10 +782,12 @@ public void GoBack()
/// <seealso cref="M:Microsoft.Web.WebView2.Core.CoreWebView2.NavigateToString(System.String)" />
public void NavigateToString(string htmlContent)
{
VerifyInitializedGuard();
VerifyBrowserNotCrashedGuard();
#if !DISABLE_WEBVIEW2_CORE
CoreWebView2!.NavigateToString(htmlContent);
if (CoreWebView2 != null)
{
CoreWebView2.NavigateToString(htmlContent);
}
#endif
}

Expand All @@ -770,12 +804,6 @@ public void Stop()
{ }
#endif

void VerifyInitializedGuard()
{
if (!IsInitialized)
throw new InvalidOperationException("The instance of CoreWebView2 is uninitialized and unable to complete this operation. See EnsureCoreWebView2Async.");
}

#if !DISABLE_WEBVIEW2_CORE
void VerifyNotClosedGuard()
{
Expand Down

0 comments on commit 02146ed

Please sign in to comment.