Skip to content

Commit

Permalink
WebView cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rocksdanister committed May 2, 2024
1 parent 0a907dd commit 8dd989b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/Lively/Lively.UI.WinUI/Services/DialogService.cs
Expand Up @@ -240,10 +240,11 @@ public async Task ShowAboutDialogAsync()

public async Task ShowPatreonSupportersDialogAsync()
{
var page = new PatreonSupportersView();
var dlg = new ContentDialog()
{
Title = i18n.GetString("TitlePatreon/Text"),
Content = new PatreonSupportersView(),
Content = page,
PrimaryButtonText = i18n.GetString("TextBecomePatreonMember/Content"),
SecondaryButtonText = i18n.GetString("Cancel/Content"),
DefaultButton = ContentDialogButton.Primary,
Expand All @@ -253,6 +254,8 @@ public async Task ShowPatreonSupportersDialogAsync()

if (await dlg.ShowAsyncQueue() == ContentDialogResult.Primary)
LinkUtil.OpenBrowser("https://rocksdanister.github.io/lively/coffee/");

page.OnClose();
}

public async Task ShowControlPanelDialogAsync()
Expand Down
16 changes: 15 additions & 1 deletion src/Lively/Lively.UI.WinUI/Views/Pages/AppUpdateView.xaml
Expand Up @@ -9,6 +9,7 @@
xmlns:local="using:Lively.UI.WinUI.Views.Pages"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Loaded="Page_Loaded"
Unloaded="Page_Unloaded"
mc:Ignorable="d">

<Page.Resources>
Expand All @@ -23,9 +24,22 @@
<WebView2
x:Name="WebView"
CoreWebView2Initialized="WebView_CoreWebView2Initialized"
DefaultBackgroundColor="Black"
NavigationCompleted="WebView_NavigationCompleted"
NavigationStarting="WebView_NavigationStarting"
Visibility="{Binding UpdateChangelogError, Mode=OneWay, Converter={StaticResource NullVisibilityConverter}, ConverterParameter=Reverse}" />
<!-- Changelog loading -->
<ProgressBar
x:Name="WebViewProgress"
VerticalAlignment="Top"
IsIndeterminate="True"
Visibility="Collapsed">
<animations:Implicit.ShowAnimations>
<animations:OpacityAnimation
From="0"
To="1.0"
Duration="0:0:1" />
</animations:Implicit.ShowAnimations>
</ProgressBar>
<!-- Changelog error -->
<StackPanel
Margin="5,125,5,0"
Expand Down
17 changes: 17 additions & 0 deletions src/Lively/Lively.UI.WinUI/Views/Pages/AppUpdateView.xaml.cs
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.Web.WebView2.Core;
using System;

Expand All @@ -22,6 +23,9 @@ public AppUpdateView()
this.viewModel = App.Services.GetRequiredService<AppUpdateViewModel>();
this.DataContext = this.viewModel;

// Error when setting in xaml
WebView.DefaultBackgroundColor = ((SolidColorBrush)App.Current.Resources["ApplicationPageBackgroundThemeBrush"]).Color;
// Set website theme to reflect app setting
var pageTheme = App.Services.GetRequiredService<IUserSettingsClient>().Settings.ApplicationTheme switch
{
AppTheme.Auto => string.Empty, // Website handles theme change based on WebView change.
Expand Down Expand Up @@ -59,6 +63,7 @@ private void CoreWebView2_NewWindowRequested(Microsoft.Web.WebView2.Core.CoreWeb
if (!args.IsUserInitiated)
return;

// Open hyperlinks in default browser
args.Handled = true;
LinkUtil.OpenBrowser(args.Uri);
}
Expand All @@ -68,11 +73,23 @@ private void WebView_NavigationStarting(WebView2 sender, Microsoft.Web.WebView2.
// Stay in page
if (args.IsRedirected)
args.Cancel = true;
else
WebViewProgress.Visibility = Visibility.Visible;
}

private void WebView_NavigationCompleted(WebView2 sender, CoreWebView2NavigationCompletedEventArgs args)
{
WebViewProgress.Visibility = Visibility.Collapsed;
}

private void Page_Loaded(object sender, RoutedEventArgs e)
{
BackgroundGridShadow.Receivers.Add(BackgroundGrid);
}

private void Page_Unloaded(object sender, RoutedEventArgs e)
{
WebView.Close();
}
}
}
Expand Up @@ -6,6 +6,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Lively.UI.WinUI.Views.Pages"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Unloaded="Page_Unloaded"
mc:Ignorable="d">

<Page.Resources>
Expand Down
Expand Up @@ -66,5 +66,15 @@ private void WebView_NavigationStarting(WebView2 sender, Microsoft.Web.WebView2.
if (args.IsRedirected)
args.Cancel = true;
}

public void OnClose()
{
WebView.Close();
}

private void Page_Unloaded(object sender, RoutedEventArgs e)
{
// Unloaded is not reliable when used in dialog
}
}
}

0 comments on commit 8dd989b

Please sign in to comment.