Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CleanupElement like CefSharp.Wpf. #14

Merged
merged 3 commits into from
Sep 19, 2021
Merged
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
80 changes: 80 additions & 0 deletions CefSharp.Wpf.HwndHost/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,12 @@ private void PresentationSourceChangedHandler(object sender, SourceChangedEventA
window.StateChanged += OnWindowStateChanged;
window.LocationChanged += OnWindowLocationChanged;
sourceWindow = window;

// If CleanupElement is null, set the CleanupElement to the new window that the browser is moved in.
if (CleanupElement == null)
{
CleanupElement = window;
}
}
}
else if (args.OldSource != null)
Expand All @@ -676,6 +682,12 @@ private void PresentationSourceChangedHandler(object sender, SourceChangedEventA
window.StateChanged -= OnWindowStateChanged;
window.LocationChanged -= OnWindowLocationChanged;
sourceWindow = null;

// If CleanupElement is the old Window that the browser is moved out of, set CleanupElement to null.
if (CleanupElement == window)
{
CleanupElement = null;
}
}
}
}
Expand Down Expand Up @@ -898,6 +910,11 @@ private void InternalDispose(bool disposing)

browser = null;

if (CleanupElement != null)
{
CleanupElement.Unloaded -= OnCleanupElementUnloaded;
}

managedCefBrowserAdapter?.Dispose();
managedCefBrowserAdapter = null;
}
Expand Down Expand Up @@ -1333,6 +1350,69 @@ public double ZoomLevelIncrement
public static readonly DependencyProperty ZoomLevelIncrementProperty =
DependencyProperty.Register(nameof(ZoomLevelIncrement), typeof(double), typeof(ChromiumWebBrowser), new PropertyMetadata(0.10));

/// <summary>
/// The CleanupElement Controls when the BrowserResources will be cleaned up.
/// The ChromiumWebBrowser will register on Unloaded of the provided Element and dispose all resources when that handler is called.
/// By default the cleanup element is the Window that contains the ChromiumWebBrowser.
/// if you want cleanup to happen earlier provide another FrameworkElement.
/// Be aware that this Control is not usable anymore after cleanup is done.
/// </summary>
/// <value>The cleanup element.</value>
public FrameworkElement CleanupElement
{
get { return (FrameworkElement)GetValue(CleanupElementProperty); }
set { SetValue(CleanupElementProperty, value); }
}

/// <summary>
/// The cleanup element property
/// </summary>
public static readonly DependencyProperty CleanupElementProperty =
DependencyProperty.Register(nameof(CleanupElement), typeof(FrameworkElement), typeof(ChromiumWebBrowser), new PropertyMetadata(null, OnCleanupElementChanged));

/// <summary>
/// Handles the <see cref="E:CleanupElementChanged" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="args">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void OnCleanupElementChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
var owner = (ChromiumWebBrowser)sender;
var oldValue = (FrameworkElement)args.OldValue;
var newValue = (FrameworkElement)args.NewValue;

owner.OnCleanupElementChanged(oldValue, newValue);
}

/// <summary>
/// Called when [cleanup element changed].
/// </summary>
/// <param name="oldValue">The old value.</param>
/// <param name="newValue">The new value.</param>
protected virtual void OnCleanupElementChanged(FrameworkElement oldValue, FrameworkElement newValue)
{
if (oldValue != null)
{
oldValue.Unloaded -= OnCleanupElementUnloaded;
}

if (newValue != null)
{
newValue.Unloaded += OnCleanupElementUnloaded;
}
}


/// <summary>
/// Handles the <see cref="E:CleanupElementUnloaded" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
private void OnCleanupElementUnloaded(object sender, RoutedEventArgs e)
{
Dispose();
}

/// <summary>
/// The text that will be displayed as a ToolTip
/// </summary>
Expand Down