diff --git a/CefGlue.Avalonia/CefGlue.Avalonia.csproj b/CefGlue.Avalonia/CefGlue.Avalonia.csproj
index 8a729079..422be6e6 100644
--- a/CefGlue.Avalonia/CefGlue.Avalonia.csproj
+++ b/CefGlue.Avalonia/CefGlue.Avalonia.csproj
@@ -13,7 +13,7 @@
-
+
diff --git a/CefGlue.Avalonia/Platform/AvaloniaControl.cs b/CefGlue.Avalonia/Platform/AvaloniaControl.cs
index 90ad246a..4f5c3998 100644
--- a/CefGlue.Avalonia/Platform/AvaloniaControl.cs
+++ b/CefGlue.Avalonia/Platform/AvaloniaControl.cs
@@ -43,15 +43,6 @@ namespace Xilium.CefGlue.Avalonia.Platform
_control.LayoutUpdated += OnLayoutUpdated;
}
- public void Dispose()
- {
- if (CefRuntime.Platform == CefRuntimePlatform.MacOSX && _browserHandle != null)
- {
- NativeExtensions.OSX.objc_release(_browserHandle.Value);
- }
- _browserHandle = null;
- }
-
private void OnLayoutUpdated(object sender, EventArgs e)
{
SizeChanged?.Invoke(new CefSize((int)_control.Bounds.Width, (int)_control.Bounds.Height));
@@ -143,25 +134,27 @@ namespace Xilium.CefGlue.Avalonia.Platform
NativeExtensions.OSX.objc_retain(browserHandle);
}
- Dispatcher.UIThread.Post(() =>
+ Dispatcher.UIThread.Post(() => SetContent(new ExtendedAvaloniaNativeControlHost(browserHandle)));
+ }
+
+ public void DestroyRender()
+ {
+ if (_browserHandle == null)
{
- var nativeHost = new ExtendedAvaloniaNativeControlHost(browserHandle);
+ return;
+ }
- if (CefRuntime.Platform == CefRuntimePlatform.MacOSX)
- {
- // in osx we need to force an extra update, otherwise the browser will have wrong dimensions when initialized
- IDisposable observable = null;
- void UpdateNativeControlBounds(AvaloniaPropertyChangedEventArgs e)
- {
- observable.Dispose();
- nativeHost.TryUpdateNativeControlPosition();
- }
+ switch (CefRuntime.Platform) {
+ case CefRuntimePlatform.Windows:
+ NativeExtensions.Windows.DestroyWindow(_browserHandle.Value);
+ break;
- observable = nativeHost.GetPropertyChangedObservable(Control.TransformedBoundsProperty).Subscribe(UpdateNativeControlBounds);
- }
+ case CefRuntimePlatform.MacOSX:
+ NativeExtensions.OSX.objc_release(_browserHandle.Value);
+ break;
+ }
- SetContent(nativeHost);
- });
+ _browserHandle = null;
}
protected void SetContent(Control content)
diff --git a/CefGlue.Avalonia/Platform/ExtendedAvaloniaNativeControlHost.cs b/CefGlue.Avalonia/Platform/ExtendedAvaloniaNativeControlHost.cs
index 63dcb594..72dff979 100644
--- a/CefGlue.Avalonia/Platform/ExtendedAvaloniaNativeControlHost.cs
+++ b/CefGlue.Avalonia/Platform/ExtendedAvaloniaNativeControlHost.cs
@@ -1,4 +1,5 @@
using System;
+using Avalonia;
using Avalonia.Controls;
using Avalonia.Platform;
@@ -13,6 +14,23 @@ namespace Xilium.CefGlue.Avalonia.Platform
_browserHandle = browserHandle;
}
+ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
+ {
+ base.OnAttachedToVisualTree(e);
+ if(CefRuntime.Platform == CefRuntimePlatform.MacOSX)
+ {
+ // in osx we need to force an extra update, otherwise the browser will have wrong dimensions when initialized
+ IDisposable observable = null;
+ void UpdateNativeControlBounds(AvaloniaPropertyChangedEventArgs _)
+ {
+ observable.Dispose();
+ TryUpdateNativeControlPosition();
+ }
+
+ observable = this.GetPropertyChangedObservable(Control.TransformedBoundsProperty).Subscribe(UpdateNativeControlBounds);
+ }
+ }
+
protected override IPlatformHandle CreateNativeControlCore(IPlatformHandle handle)
{
return new PlatformHandle(_browserHandle, "HWND");
diff --git a/CefGlue.Avalonia/Platform/NativeExtensions.cs b/CefGlue.Avalonia/Platform/NativeExtensions.cs
index ed0f3805..e1e156ff 100644
--- a/CefGlue.Avalonia/Platform/NativeExtensions.cs
+++ b/CefGlue.Avalonia/Platform/NativeExtensions.cs
@@ -26,8 +26,8 @@ namespace Xilium.CefGlue.Avalonia.Platform
public static class Windows
{
- [DllImport("user32.dll")]
- public static extern bool EnableWindow(IntPtr hWnd, bool bEnable);
+ [DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Unicode)]
+ public static extern bool DestroyWindow(IntPtr hwnd);
}
}
}
diff --git a/CefGlue.Common/CommonBrowserAdapter.cs b/CefGlue.Common/CommonBrowserAdapter.cs
index bac9fd98..b09f067d 100644
--- a/CefGlue.Common/CommonBrowserAdapter.cs
+++ b/CefGlue.Common/CommonBrowserAdapter.cs
@@ -64,8 +64,6 @@ namespace Xilium.CefGlue.Common
_logger.Info($"Browser adapter disposed (Id:{GetHashCode()}");
}
- Control.Dispose();
-
var browserHost = BrowserHost;
if (browserHost != null)
{
@@ -414,10 +412,17 @@ namespace Xilium.CefGlue.Common
});
}
- protected virtual void OnBrowserHostCreated(CefBrowserHost browserHost) {
+ protected virtual void OnBrowserHostCreated(CefBrowserHost browserHost)
+ {
Control.InitializeRender(browserHost.GetWindowHandle());
}
+ protected virtual bool OnBrowserClose()
+ {
+ Control.DestroyRender();
+ return true;
+ }
+
#region ICefBrowserHost
void ICefBrowserHost.HandleBrowserCreated(CefBrowser browser)
@@ -437,6 +442,17 @@ namespace Xilium.CefGlue.Common
});
}
+ bool ICefBrowserHost.HandleBrowserClose(CefBrowser browser)
+ {
+ var result = false;
+ WithErrorHandling((nameof(ICefBrowserHost.HandleBrowserClose)), () =>
+ {
+ result = OnBrowserClose();
+ });
+
+ return result;
+ }
+
bool ICefBrowserHost.HandleTooltip(CefBrowser browser, string text)
{
WithErrorHandling((nameof(ICefBrowserHost.HandleTooltip)), () =>
diff --git a/CefGlue.Common/CommonOffscreenBrowserAdapter.cs b/CefGlue.Common/CommonOffscreenBrowserAdapter.cs
index d28bdd7e..a529c514 100644
--- a/CefGlue.Common/CommonOffscreenBrowserAdapter.cs
+++ b/CefGlue.Common/CommonOffscreenBrowserAdapter.cs
@@ -307,6 +307,14 @@ namespace Xilium.CefGlue.Common
_logger.Debug($"Browser resized {newWidth}x{newHeight}");
}
+
+ protected override bool OnBrowserClose()
+ {
+ // According to cef documentation:
+ // If no OS window exists (window rendering disabled) returning false will cause the browser object to be destroyed immediately
+ return false;
+ }
+
#region IOffscreenCefBrowserHost
void IOffscreenCefBrowserHost.GetViewRect(out CefRectangle rect)
diff --git a/CefGlue.Common/ICefBrowserHost.cs b/CefGlue.Common/ICefBrowserHost.cs
index 20352f50..109cce98 100644
--- a/CefGlue.Common/ICefBrowserHost.cs
+++ b/CefGlue.Common/ICefBrowserHost.cs
@@ -7,6 +7,7 @@ namespace Xilium.CefGlue.Common
{
void HandleBrowserCreated(CefBrowser browser);
void HandleBrowserDestroyed(CefBrowser browser);
+ bool HandleBrowserClose(CefBrowser browser);
void HandleAddressChange(CefBrowser browser, CefFrame frame, string url);
void HandleTitleChange(CefBrowser browser, string title);
diff --git a/CefGlue.Common/InternalHandlers/CommonCefLifeSpanHandler.cs b/CefGlue.Common/InternalHandlers/CommonCefLifeSpanHandler.cs
index eba0d077..4eaa9730 100644
--- a/CefGlue.Common/InternalHandlers/CommonCefLifeSpanHandler.cs
+++ b/CefGlue.Common/InternalHandlers/CommonCefLifeSpanHandler.cs
@@ -24,7 +24,8 @@ namespace Xilium.CefGlue.Common.InternalHandlers
protected override bool DoClose(CefBrowser browser)
{
- return (_owner.LifeSpanHandler?.HandleDoClose(browser) ?? false) || base.DoClose(browser);
+ return (_owner.LifeSpanHandler?.HandleDoClose(browser) ?? false) ||
+ _owner.HandleBrowserClose(browser);
}
protected override void OnBeforeClose(CefBrowser browser)
diff --git a/CefGlue.Common/Platform/IControl.cs b/CefGlue.Common/Platform/IControl.cs
index a42ef8ba..5b62222c 100644
--- a/CefGlue.Common/Platform/IControl.cs
+++ b/CefGlue.Common/Platform/IControl.cs
@@ -4,7 +4,7 @@ using Xilium.CefGlue.Common.Helpers;
namespace Xilium.CefGlue.Common.Platform
{
- internal interface IControl : IDisposable
+ internal interface IControl
{
event Action SizeChanged;
@@ -16,5 +16,7 @@ namespace Xilium.CefGlue.Common.Platform
void SetTooltip(string text);
void InitializeRender(IntPtr browserHandle);
+
+ void DestroyRender();
}
}
\ No newline at end of file
diff --git a/CefGlue.Demo.Avalonia/BrowserView.xaml.cs b/CefGlue.Demo.Avalonia/BrowserView.xaml.cs
index 1ec91ba0..e45c878c 100644
--- a/CefGlue.Demo.Avalonia/BrowserView.xaml.cs
+++ b/CefGlue.Demo.Avalonia/BrowserView.xaml.cs
@@ -102,5 +102,10 @@ namespace Xilium.CefGlue.Demo.Avalonia
{
browser.ShowDeveloperTools();
}
+
+ public void Dispose()
+ {
+ browser.Dispose();
+ }
}
}
\ No newline at end of file
diff --git a/CefGlue.Demo.Avalonia/CefGlue.Demo.Avalonia.csproj b/CefGlue.Demo.Avalonia/CefGlue.Demo.Avalonia.csproj
index 7a1af150..034da0c1 100644
--- a/CefGlue.Demo.Avalonia/CefGlue.Demo.Avalonia.csproj
+++ b/CefGlue.Demo.Avalonia/CefGlue.Demo.Avalonia.csproj
@@ -28,7 +28,7 @@
-
+
diff --git a/CefGlue.Demo.Avalonia/MainWindow.xaml.cs b/CefGlue.Demo.Avalonia/MainWindow.xaml.cs
index 0f4de0b4..afb5fc22 100644
--- a/CefGlue.Demo.Avalonia/MainWindow.xaml.cs
+++ b/CefGlue.Demo.Avalonia/MainWindow.xaml.cs
@@ -42,19 +42,45 @@ namespace Xilium.CefGlue.Demo.Avalonia
private void CreateNewTab()
{
- var tabItems = ((IList)this.FindControl("tabControl").Items);
-
- var tab = new TabItem();
- tab.Header = "New Tab";
-
- var view = new BrowserView();
- view.TitleChanged += title => Dispatcher.UIThread.Post(() =>
- {
- tab.Header = title;
- ToolTip.SetTip(tab, title);
- });
-
- tab.Content = view;
+ var tabItems = ((IList)this.FindControl("tabControl").Items);
+
+ var view = new BrowserView();
+ var tab = new TabItem();
+
+ var headerPanel = new DockPanel();
+ tab.Header = headerPanel;
+
+ var closeButton = new Button()
+ {
+ Content = "X",
+ Padding = new Thickness(2),
+ Margin = new Thickness(5, 0, 0, 0)
+ };
+ closeButton.Click += delegate
+ {
+ view.Dispose();
+ tabItems.Remove(tab);
+ };
+ DockPanel.SetDock(closeButton, Dock.Right);
+
+ var tabTitle = new TextBlock()
+ {
+ Text = "New Tab"
+ };
+ headerPanel.Children.Add(tabTitle);
+ headerPanel.Children.Add(closeButton);
+
+ view.TitleChanged += title =>
+ {
+ Dispatcher.UIThread.Post((Action)(() =>
+ {
+ tabTitle.Text = title;
+ ToolTip.SetTip(tab, title);
+ }));
+ };
+
+ tab.Content = view;
+
tabItems.Add(tab);
}
diff --git a/CefGlue.Demo.WPF/MainWindow.xaml.cs b/CefGlue.Demo.WPF/MainWindow.xaml.cs
index ce83b851..50277b43 100644
--- a/CefGlue.Demo.WPF/MainWindow.xaml.cs
+++ b/CefGlue.Demo.WPF/MainWindow.xaml.cs
@@ -23,15 +23,37 @@ namespace Xilium.CefGlue.Demo.WPF
private void CreateNewTab()
{
- var tab = new TabItem();
- tab.Header = "New Tab";
-
var view = new BrowserView();
+ var tab = new TabItem();
+
+ var headerPanel = new DockPanel();
+ tab.Header = headerPanel;
+
+ var closeButton = new Button()
+ {
+ Content = "X",
+ Padding = new Thickness(2),
+ Margin = new Thickness(5, 0, 0, 0)
+ };
+ closeButton.Click += delegate
+ {
+ view.Dispose();
+ tabControl.Items.Remove(tab);
+ };
+ DockPanel.SetDock(closeButton, Dock.Right);
+
+ var tabTitle = new TextBlock()
+ {
+ Text = "New Tab"
+ };
+ headerPanel.Children.Add(tabTitle);
+ headerPanel.Children.Add(closeButton);
+
view.TitleChanged += title =>
{
Application.Current.Dispatcher.BeginInvoke((Action)(() =>
{
- tab.Header = title;
+ tabTitle.Text = title;
tab.ToolTip = title;
}));
};
diff --git a/CefGlue.Tests/Helpers/DummyControl.cs b/CefGlue.Tests/Helpers/DummyControl.cs
index b4d83888..a47ba075 100644
--- a/CefGlue.Tests/Helpers/DummyControl.cs
+++ b/CefGlue.Tests/Helpers/DummyControl.cs
@@ -122,6 +122,11 @@ namespace CefGlue.Tests.Helpers
throw new NotImplementedException();
}
+ public void DestroyRender()
+ {
+ throw new NotImplementedException();
+ }
+
public Task StartDrag(CefDragData dragData, CefDragOperationsMask allowedOps, int x, int y)
{
throw new NotImplementedException();
diff --git a/CefGlue.WPF/Platform/ExtendedWpfNativeControlHost.cs b/CefGlue.WPF/Platform/ExtendedWpfNativeControlHost.cs
index 093f9a14..55a9ab9e 100644
--- a/CefGlue.WPF/Platform/ExtendedWpfNativeControlHost.cs
+++ b/CefGlue.WPF/Platform/ExtendedWpfNativeControlHost.cs
@@ -6,6 +6,9 @@ namespace Xilium.CefGlue.WPF.Platform
{
internal class ExtendedWpfNativeControlHost : HwndHost
{
+ [DllImport("user32.dll", EntryPoint = "DestroyWindow", CharSet = CharSet.Unicode)]
+ private static extern bool DestroyWindow(IntPtr hwnd);
+
private readonly IntPtr _browserHandle;
public ExtendedWpfNativeControlHost(IntPtr browserHandle)
@@ -20,7 +23,12 @@ namespace Xilium.CefGlue.WPF.Platform
protected override void DestroyWindowCore(HandleRef hwnd)
{
- // do nothing
+ // nothing to do
+ }
+
+ public void DestroyWindow()
+ {
+ DestroyWindow(_browserHandle);
}
}
}
diff --git a/CefGlue.WPF/Platform/WpfControl.cs b/CefGlue.WPF/Platform/WpfControl.cs
index 89c84374..552f9289 100644
--- a/CefGlue.WPF/Platform/WpfControl.cs
+++ b/CefGlue.WPF/Platform/WpfControl.cs
@@ -16,6 +16,7 @@ namespace Xilium.CefGlue.WPF.Platform
internal class WpfControl : IControl
{
protected readonly FrameworkElement _control;
+ private ExtendedWpfNativeControlHost _nativeControl;
public event Action SizeChanged;
@@ -26,8 +27,6 @@ namespace Xilium.CefGlue.WPF.Platform
control.LayoutUpdated += OnControlLayoutUpdated;
}
- public void Dispose() { }
-
private void OnControlLayoutUpdated(object sender, EventArgs e)
{
SizeChanged?.Invoke(new CefSize((int)_control.RenderSize.Width, (int)_control.RenderSize.Height));
@@ -109,7 +108,21 @@ namespace Xilium.CefGlue.WPF.Platform
DispatcherPriority.Input,
new Action(() =>
{
- ((ContentControl) _control).Content = new ExtendedWpfNativeControlHost(browserHandle);
+ _nativeControl = new ExtendedWpfNativeControlHost(browserHandle);
+ ((ContentControl) _control).Content = _nativeControl;
+ })
+ );
+ }
+
+ public void DestroyRender()
+ {
+ _nativeControl.DestroyWindow(); // must be destroyed on current thread
+ _control.Dispatcher.BeginInvoke(
+ DispatcherPriority.Normal,
+ new Action(() =>
+ {
+ // dispose remaining resources
+ _nativeControl.Dispose();
})
);
}