mirror of
https://github.com/OutSystems/CefGlue.git
synced 2026-12-05 14:22:29 +00:00
Merge branch '3770-properly-close-browser' into '3770'
Properly close browser See merge request joaompneves/cefglue!11
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia" Version="0.9.3-cibuild0006018-beta" />
|
||||
<PackageReference Include="Avalonia" Version="0.9.3-cibuild0006194-beta" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)), () =>
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -4,7 +4,7 @@ using Xilium.CefGlue.Common.Helpers;
|
||||
|
||||
namespace Xilium.CefGlue.Common.Platform
|
||||
{
|
||||
internal interface IControl : IDisposable
|
||||
internal interface IControl
|
||||
{
|
||||
event Action<CefSize> SizeChanged;
|
||||
|
||||
@@ -16,5 +16,7 @@ namespace Xilium.CefGlue.Common.Platform
|
||||
void SetTooltip(string text);
|
||||
|
||||
void InitializeRender(IntPtr browserHandle);
|
||||
|
||||
void DestroyRender();
|
||||
}
|
||||
}
|
||||
@@ -102,5 +102,10 @@ namespace Xilium.CefGlue.Demo.Avalonia
|
||||
{
|
||||
browser.ShowDeveloperTools();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
browser.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia.Desktop" Version="0.9.3-cibuild0006018-beta" />
|
||||
<PackageReference Include="Avalonia.Desktop" Version="0.9.3-cibuild0006194-beta" />
|
||||
<PackageReference Include="cef.redist.x64" Version="75.1.14" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -42,19 +42,45 @@ namespace Xilium.CefGlue.Demo.Avalonia
|
||||
|
||||
private void CreateNewTab()
|
||||
{
|
||||
var tabItems = ((IList)this.FindControl<TabControl>("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>("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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}));
|
||||
};
|
||||
|
||||
@@ -122,6 +122,11 @@ namespace CefGlue.Tests.Helpers
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void DestroyRender()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<CefDragOperationsMask> StartDrag(CefDragData dragData, CefDragOperationsMask allowedOps, int x, int y)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ namespace Xilium.CefGlue.WPF.Platform
|
||||
internal class WpfControl : IControl
|
||||
{
|
||||
protected readonly FrameworkElement _control;
|
||||
private ExtendedWpfNativeControlHost _nativeControl;
|
||||
|
||||
public event Action<CefSize> 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();
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user