Skip to content

Commit

Permalink
Dispose settings webview when closing tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Guerra24 committed Nov 7, 2023
1 parent 9c164af commit 75a2fd3
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 35 deletions.
4 changes: 1 addition & 3 deletions LRReader.Shared/Models/App.cs
Expand Up @@ -3,16 +3,14 @@

namespace LRReader.Shared.Models
{
public interface ICustomTab
public interface ICustomTab : IDisposable
{
object CustomTabControl { get; set; }

string CustomTabId { get; set; }

bool IsClosable { get; set; }

void Unload();

bool BackRequested();
}

Expand Down
4 changes: 2 additions & 2 deletions LRReader.Shared/Services/Tabs.cs
Expand Up @@ -68,7 +68,7 @@ public void CloseCurrentTab()

public void CloseTab(ICustomTab tab)
{
tab.Unload();
tab.Dispose();
TabItems.Remove(tab);
}

Expand All @@ -82,7 +82,7 @@ public void CloseTabWithId(string? id)
public void CloseAllTabs()
{
foreach (var t in TabItems)
t.Unload();
t.Dispose();
TabItems.Clear();
}

Expand Down
11 changes: 9 additions & 2 deletions LRReader.UWP/Views/Content/WebContent.xaml.cs
@@ -1,4 +1,5 @@
#nullable enable
using System;
using LRReader.Shared.Services;
using LRReader.UWP.Views.Controls;
using Windows.UI.Xaml.Controls;
Expand All @@ -7,7 +8,7 @@
namespace LRReader.UWP.Views.Content
{

public sealed partial class WebContent : ModernBasePage
public sealed partial class WebContent : ModernBasePage, IDisposable
{
public WebContent()
{
Expand All @@ -23,13 +24,19 @@ protected override void OnNavigatedTo(NavigationEventArgs e)

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
WebView.Close();
WebView.Dispose();
base.OnNavigatingFrom(e);
}

private void WebView_OnCloseRequested()
{
Wrapper.ModernPageTab.GoBack((int)((Frame)Parent).Tag);
}

public void Dispose()
{
WebView.Dispose();
}

}
}
8 changes: 7 additions & 1 deletion LRReader.UWP/Views/Controls/ModernPageTab.xaml.cs
Expand Up @@ -11,7 +11,7 @@

namespace LRReader.UWP.Views.Controls
{
public sealed partial class ModernPageTab : UserControl
public sealed partial class ModernPageTab : UserControl, IDisposable
{

private ObservableCollection<ModernPageTabItem> MainBreadcrumbItems = new ObservableCollection<ModernPageTabItem>();
Expand Down Expand Up @@ -166,6 +166,12 @@ private void ExtraBreadcrumb_ItemClicked(BreadcrumbBar sender, BreadcrumbBarItem
}
}

public void Dispose()
{
if (ContentMain.Content is IDisposable main) main.Dispose();
if (ContentExtra.Content is IDisposable extra) extra.Dispose();
}

public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ModernPageTab), new PropertyMetadata(null));
public static readonly DependencyProperty InitialProperty = DependencyProperty.Register("Initial", typeof(Type), typeof(ModernPageTab), new PropertyMetadata(null));
}
Expand Down
2 changes: 1 addition & 1 deletion LRReader.UWP/Views/Controls/ModernTab.cs
Expand Up @@ -29,7 +29,7 @@ public string CustomTabId

public event GoBackTabEvent? GoBack;

public virtual void Unload()
public virtual void Dispose()
{
}

Expand Down
61 changes: 42 additions & 19 deletions LRReader.UWP/Views/Controls/ModernWebView.cs
@@ -1,6 +1,8 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using LRReader.UWP.Extensions;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Web.WebView2.Core;
using Windows.UI;
Expand All @@ -10,7 +12,7 @@

namespace LRReader.UWP.Views.Controls
{
public class ModernWebView : UserControl
public class ModernWebView : UserControl, IDisposable
{
private static List<string> Allowed = new List<string>() { "/upload", "/batch", "/config", "/config/plugins", "/logs" };

Expand Down Expand Up @@ -54,10 +56,12 @@ public void Navigate(string url)

public void Refresh() => WebView.Refresh();

public void Close() => WebView.Close();
public void Dispose() => WebView.Dispose();

public bool NavigationStarting(IWebView sender, Uri uri)
{
if (!Page.Host.Equals(uri.Host))
return true;
var path = uri.AbsolutePath;
if (path.Equals("/login") || Allowed.Contains(path))
{
Expand Down Expand Up @@ -113,12 +117,11 @@ private static bool CanUseWebView2()
public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ModernWebView), new PropertyMetadata(""));
}

public interface IWebView
public interface IWebView : IDisposable
{
public string DocumentTitle { get; }
public void Navigate(Uri page);
public Task Navigate(Uri page);
public void Refresh();
public void Close();

}

Expand All @@ -140,7 +143,7 @@ public EdgeChromeWebView(ModernWebView modern)

public string DocumentTitle => WebView.CoreWebView2.DocumentTitle;

public async void Navigate(Uri page)
public async Task Navigate(Uri page)
{
await WebView.EnsureCoreWebView2Async();
if (!Initialized)
Expand All @@ -151,25 +154,35 @@ public async void Navigate(Uri page)
WebView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
WebView.CoreWebView2.DOMContentLoaded += CoreWebView2_DOMContentLoaded;
}
WebView.Source = page;
WebView.CoreWebView2.Navigate(page.ToString());
}

public void Refresh() => WebView.Reload();

public void Close() => WebView.Close();
public void Dispose() => WebView.Close();

private void NavigationStarting(WebView2 sender, CoreWebView2NavigationStartingEventArgs args) => args.Cancel = Modern.NavigationStarting(this, new Uri(args.Uri));
private void NavigationStarting(WebView2 sender, CoreWebView2NavigationStartingEventArgs args)
{
var res = Modern.NavigationStarting(this, new Uri(args.Uri));
//if (!res)
//await WebView.FadeOutAsync();
args.Cancel = res;
}

private void NavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args) => Modern.NavigationCompleted(this, args.IsSuccess);
private void NavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args)
{
Modern.NavigationCompleted(this, args.IsSuccess);
}

private void CoreWebView2_NewWindowRequested(CoreWebView2 sender, CoreWebView2NewWindowRequestedEventArgs args)
{
args.Handled = true;
}

private async void CoreWebView2_DOMContentLoaded(CoreWebView2 sender, CoreWebView2DOMContentLoadedEventArgs args)
private void CoreWebView2_DOMContentLoaded(CoreWebView2 sender, CoreWebView2DOMContentLoadedEventArgs args)
{
await sender.ExecuteScriptAsync("var style = document.createElement('style'); style.innerHTML = 'body, html { background: transparent !important; } p.ip { display: none !important; }'; document.head.appendChild(style);");
//await sender.ExecuteScriptAsync("var style = document.createElement('style'); style.innerHTML = 'body, html { background: transparent !important; } p.ip { display: none !important; } div.ido, .option-flyout { border: 1px solid #00000019; border-radius: 4px; background-color: #FFFFFF0D !important; background-clip: padding-box !important; }'; document.head.appendChild(style);");
//WebView.FadeIn();
}

}
Expand All @@ -191,20 +204,30 @@ public EdgeHTMLWebView(ModernWebView modern)

public string DocumentTitle => WebView.DocumentTitle;

public void Navigate(Uri page) => WebView.Navigate(page);
public Task Navigate(Uri page)
{
WebView.Navigate(page);
return Task.CompletedTask;
}

public void Refresh() => WebView.Refresh();

public void Close() { }
public void Dispose() { }

private void NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args) => args.Cancel = Modern.NavigationStarting(this, args.Uri);
private void NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
args.Cancel = Modern.NavigationStarting(this, args.Uri);
}

private void NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args) => Modern.NavigationCompleted(this, args.IsSuccess);
private void NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
Modern.NavigationCompleted(this, args.IsSuccess);
}

private async void DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
private void DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
{
var func = "var style = document.createElement('style'); style.innerHTML = 'body, html { background: transparent !important; } p.ip { display: none !important; }'; document.head.appendChild(style);";
await sender.InvokeScriptAsync("eval", new string[] { func });
//var func = "var style = document.createElement('style'); style.innerHTML = 'body, html { background: transparent !important; } p.ip { display: none !important; } div.ido, .option-flyout { border: 1px solid #00000019; border-radius: 4px; background-color: #FFFFFF0D !important; background-clip: padding-box !important; }'; document.head.appendChild(style);";
//await sender.InvokeScriptAsync("eval", new string[] { func });
}
}
}
4 changes: 2 additions & 2 deletions LRReader.UWP/Views/Tabs/ArchiveTab.xaml.cs
Expand Up @@ -14,9 +14,9 @@ public ArchiveTab(Archive archive, IList<Archive> next)
TabContent.LoadArchive(archive, next);
}

public override void Unload()
public override void Dispose()
{
base.Unload();
base.Dispose();
TabContent.RemoveEvent();
}
}
Expand Down
5 changes: 3 additions & 2 deletions LRReader.UWP/Views/Tabs/SettingsTab.xaml.cs
Expand Up @@ -25,9 +25,10 @@ public SettingsTab()
DispatcherTimer.Start();
}

public override void Unload()
public override void Dispose()
{
base.Unload();
base.Dispose();
ContentPage.Dispose();
DispatcherTimer.Stop();
}

Expand Down
6 changes: 3 additions & 3 deletions LRReader.UWP/Views/Tabs/WebTab.xaml.cs
Expand Up @@ -26,10 +26,10 @@ private void TabViewItem_Loaded(object sender, RoutedEventArgs e)
TabContent.Navigate(page);
}

public override void Unload()
public override void Dispose()
{
base.Unload();
TabContent.Close();
base.Dispose();
TabContent.Dispose();
}

private void TabContent_OnCloseRequested()
Expand Down

0 comments on commit 75a2fd3

Please sign in to comment.