Files
Febe/src/Febe.Desktop/DesktopShell.cs
T
2026-10-14 00:59:18 +00:00

108 lines
3.6 KiB
C#

using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using Febe.App;
using Febe.Desktop.Browser;
using Febe.Desktop.Core;
using Febe.Desktop.Native;
using Febe.Web.Native;
using Microsoft.Web.WebView2.Core;
using Microsoft.Win32;
namespace Febe.Desktop;
/// <summary>The Windows-only part the desktop platform needs (WebView2, Edge's window, native shell with notifications).</summary>
internal sealed class DesktopShell(Dispatcher ui, ImageSource? icon, INativeShell native) : IDesktopShell
{
public INativeShell Native { get; } = native;
public string? WebView2Version()
{
try
{
return CoreWebView2Environment.GetAvailableBrowserVersionString();
}
catch (WebView2RuntimeNotFoundException)
{
return null;
}
}
public IFebeBrowser CreateWebView2Browser(WebView2BrowserSettings settings, FebePlatformContext context) =>
new WebView2Browser(ui, settings, context, icon);
public string? FindEdge()
{
string? appPath = null;
foreach (var hive in new[] { Registry.CurrentUser, Registry.LocalMachine })
{
using var key = hive.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe", writable: false);
if (key?.GetValue(null) is string value && !string.IsNullOrWhiteSpace(value))
{
appPath = value;
break;
}
}
return EdgeLocator.Find(EdgeLocator.Candidates(
appPath,
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)));
}
public Task ShowProcessWindowAsync(int processId, CancellationToken ct) => ui.InvokeAsync(() =>
{
if (MainWindowOf(processId) is not { } window)
{
return;
}
if (Win32.IsIconic(window))
{
Win32.ShowWindow(window, Win32.SwRestore);
}
var area = SystemParameters.WorkArea;
var dpi = Win32.SystemScale;
Win32.SetWindowPos(window, IntPtr.Zero, (int)(area.Left * dpi) + 40, (int)(area.Top * dpi) + 40, 0, 0, Win32.SwpNoSize | Win32.SwpNoZOrder);
Win32.SetForegroundWindow(window);
}).Task;
public Task HideProcessWindowAsync(int processId, CancellationToken ct) => ui.InvokeAsync(() =>
{
if (MainWindowOf(processId) is not { } window)
{
return;
}
// Out of view, never minimised (S2): just right of every screen.
var dpi = Win32.SystemScale;
var x = (int)((SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth) * dpi) + 64;
var y = (int)(SystemParameters.VirtualScreenTop * dpi);
if (Win32.IsIconic(window))
{
Win32.ShowWindow(window, Win32.SwShowNoActivate);
}
Win32.SetWindowPos(window, IntPtr.Zero, x, y, 0, 0, Win32.SwpNoSize | Win32.SwpNoZOrder | Win32.SwpNoActivate);
}).Task;
public bool EndWithFebe(Process process) => ProcessJob.Add(process);
private static IntPtr? MainWindowOf(int processId)
{
try
{
using var process = Process.GetProcessById(processId);
process.Refresh();
return process.MainWindowHandle == IntPtr.Zero ? null : process.MainWindowHandle;
}
catch (Exception ex) when (ex is ArgumentException or InvalidOperationException)
{
return null;
}
}
}