Skip to content

Commit

Permalink
chore:拦截新窗口都用ShellWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
GiantappMan committed Feb 6, 2024
1 parent 6fe2aa0 commit 21b26f1
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/giantapp-wallpaper-client/Client/UI/ShellWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,31 +156,36 @@ public static void SetTheme(string theme, string mode)

public static async void ShowShell(string? url)
{
_logger.Info($"ShowShell {url}");
Instance ??= new ShellWindow();
await Instance.ShowUrl(url);
}

public async Task ShowUrl(string? url)
{
_logger.Info($"ShowShell {url}");

bool ok = await Task.Run(CheckWebView2);
if (!ok)
{
//没装webview2
Instance.loading.Visibility = Visibility.Collapsed;
Instance.tips.Visibility = Visibility.Visible;
loading.Visibility = Visibility.Collapsed;
tips.Visibility = Visibility.Visible;

LoopCheckWebView2(url);
}
else
{
Instance.loading.Visibility = Visibility.Visible;
loading.Visibility = Visibility.Visible;
}

if (Instance.WindowState == WindowState.Minimized)
Instance.WindowState = WindowState.Normal;
if (WindowState == WindowState.Minimized)
WindowState = WindowState.Normal;

Instance.Activate();
Activate();

Instance.webview2.Source = new Uri(url);
Instance.webview2.NavigationCompleted += NavigationCompleted;
Instance.Show();
webview2.Source = new Uri(url);
webview2.NavigationCompleted += NavigationCompleted;
Show();
}

public static void ApplyCustomFolderMapping(Dictionary<string, string> mapping, Microsoft.Web.WebView2.Wpf.WebView2? webview2 = null)
Expand Down Expand Up @@ -317,6 +322,8 @@ protected override void OnClosed(EventArgs e)
SizeChanged -= ShellWindow_SizeChanged;
StateChanged -= ShellWindow_StateChanged;
webview2.CoreWebView2InitializationCompleted -= Webview2_CoreWebView2InitializationCompleted;
webview2.CoreWebView2.NavigationStarting -= CoreWebView2_NavigationStarting;
webview2.CoreWebView2.NewWindowRequested -= CoreWebView2_NewWindowRequested;

//webview2.CoreWebView2.WebMessageReceived -= CoreWebView2_WebMessageReceived;
//强制回收webview2
Expand All @@ -337,6 +344,7 @@ private void Webview2_CoreWebView2InitializationCompleted(object? sender, CoreWe
webview2.CoreWebView2.AddHostObjectToScript("shell", new ShellApiObject());
webview2.CoreWebView2.NavigationStarting += CoreWebView2_NavigationStarting;
//webview2.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived;
webview2.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;

if (!AllowDragFile)
DisableDragFile();
Expand All @@ -352,6 +360,14 @@ private void Webview2_CoreWebView2InitializationCompleted(object? sender, CoreWe
#endif
}

private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
e.Handled = true;

var window = new ShellWindow();
_ = window.ShowUrl(e.Uri);
}

private void CoreWebView2_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
{
var uri = new Uri(e.Uri);
Expand Down

0 comments on commit 21b26f1

Please sign in to comment.