Skip to content

Commit

Permalink
WPF - Update CleanupElement when PresentationSource changes
Browse files Browse the repository at this point in the history
- If the CleanupElement is a Window then move it to the new Window
  • Loading branch information
amaitland committed Jan 15, 2022
1 parent ec7c4f7 commit 1f79515
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
24 changes: 24 additions & 0 deletions CefSharp.Wpf.Example/ChangeParentWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Window x:Class="CefSharp.Wpf.Example.ChangeParentWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="ChangeParentWindow" Height="600" Width="800" WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Grid.ColumnSpan="2">
<Button Content="Open browser in new Window" Click="OnAddBrowser" />
<Button Content="Re-Parent Browser and Close Window" Click="OnRemoveBrowser" />
</StackPanel>
<Border Grid.Row="1" Grid.Column="0" x:Name="StaticBrowser" />
<Border Grid.Row="1" x:Name="BrowserSite" Grid.Column="1" />
</Grid>
</Window>
57 changes: 57 additions & 0 deletions CefSharp.Wpf.Example/ChangeParentWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright © 2022 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System.Windows;

namespace CefSharp.Wpf.Example
{
/// <summary>
/// Interaction logic for ChangeParentWindow.xaml
/// </summary>
public partial class ChangeParentWindow : Window
{
private ChromiumWebBrowser browser;
private Window newWindow;

public ChangeParentWindow()
{
InitializeComponent();

StaticBrowser.Child = new ChromiumWebBrowser("http://www.msn.net");
}

private void OnAddBrowser(object sender, RoutedEventArgs e)
{
if (browser == null)
{
browser = new ChromiumWebBrowser("http://www.msn.net");
}

if (newWindow == null)
{
newWindow = new Window
{
Owner = this,
Content = browser,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};

newWindow.Show();
}
}

private void OnRemoveBrowser(object sender, RoutedEventArgs e)
{
if (newWindow != null)
{
newWindow.Content = null;
newWindow.Close();
newWindow = null;
}

BrowserSite.Child = browser;
}
}
}

18 changes: 13 additions & 5 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,19 @@ private void PresentationSourceChangedHandler(object sender, SourceChangedEventA
window.StateChanged += OnWindowStateChanged;
window.LocationChanged += OnWindowLocationChanged;
sourceWindow = window;

if (CleanupElement == null)
{
CleanupElement = window;
}
else if(CleanupElement is Window parent)
{
//If the CleanupElement is a window then move it to the new Window
if(parent != window)
{
CleanupElement = window;
}
}
}

browserScreenLocation = GetBrowserScreenLocation();
Expand Down Expand Up @@ -1941,11 +1954,6 @@ private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArg
/// <param name="routedEventArgs">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
if (CleanupElement == null)
{
CleanupElement = Window.GetWindow(this);
}

// TODO: Consider making the delay here configurable.
tooltipTimer = new DispatcherTimer(
TimeSpan.FromSeconds(0.5),
Expand Down

0 comments on commit 1f79515

Please sign in to comment.