using System.IO;
|
using System.Text.Json;
|
using System.Windows;
|
using System.Windows.Media;
|
using Moli.Desktop.Configuration;
|
using Moli.Desktop.Diagnostics;
|
using Moli.Desktop.Host;
|
using Moli.Desktop.Interop;
|
using Moli.Desktop.Plugins;
|
using Moli.Desktop.Runtime;
|
using Moli.Desktop.WebView;
|
using Brushes = System.Windows.Media.Brushes;
|
|
namespace Moli.Desktop.Windows;
|
|
/// <summary>Borderless desktop wallpaper surface backed by one WebView host.</summary>
|
public sealed class WallpaperWindow : Window
|
{
|
private readonly PluginDescriptor _plugin;
|
private readonly MonitorInfo _monitor;
|
private readonly IWebViewHost _webView;
|
private readonly IWin32InteropService _interop;
|
private readonly DesktopPaths _paths;
|
private readonly IDiagnosticsService _diagnostics;
|
private bool _clickThrough = true;
|
|
public WallpaperWindow(
|
PluginDescriptor plugin,
|
MonitorInfo monitor,
|
IWebViewHost webView,
|
IWin32InteropService interop,
|
DesktopPaths paths,
|
IDiagnosticsService diagnostics)
|
{
|
_plugin = plugin;
|
_monitor = monitor;
|
_webView = webView;
|
_interop = interop;
|
_paths = paths;
|
_diagnostics = diagnostics;
|
|
Title = $"Moli Wallpaper - {plugin.Manifest.Name}";
|
WindowStyle = WindowStyle.None;
|
ResizeMode = ResizeMode.NoResize;
|
AllowsTransparency = true;
|
Background = Brushes.Transparent;
|
ShowInTaskbar = false;
|
Topmost = false;
|
Left = monitor.Bounds.X;
|
Top = monitor.Bounds.Y;
|
Width = monitor.Bounds.Width;
|
Height = monitor.Bounds.Height;
|
Content = webView.View;
|
|
Loaded += (_, _) => ApplyWin32Styles();
|
}
|
|
public async Task LoadAsync(WallpaperRuntimeState state, CancellationToken cancellationToken = default)
|
{
|
_clickThrough = state.ClickThrough;
|
Opacity = Math.Clamp(state.Opacity, 0.1, 1.0);
|
await _webView.InitializeAsync(new WebViewHostOptions
|
{
|
UserDataFolder = Path.Combine(_paths.WebViewDataDirectory, "wallpaper", _plugin.Id.Value),
|
IsTransparent = true,
|
AreDevToolsEnabled = true,
|
IsMuted = state.IsMuted,
|
InitialScript = PluginBridgeScript.Build()
|
}, cancellationToken).ConfigureAwait(true);
|
|
await _webView.InjectBridgeAsync(CreateBridgeContext(), cancellationToken).ConfigureAwait(true);
|
await _webView.NavigateAsync(_plugin.EntryUri, cancellationToken).ConfigureAwait(true);
|
await _webView.DispatchLifecycleEventAsync("moli:wallpaper-enabled", state, cancellationToken).ConfigureAwait(true);
|
}
|
|
public void ApplyState(WallpaperRuntimeState state, bool safeInputMode)
|
{
|
_clickThrough = !safeInputMode && state.ClickThrough;
|
Opacity = Math.Clamp(state.Opacity, 0.1, 1.0);
|
ApplyWin32Styles();
|
}
|
|
public Task PauseAsync(CancellationToken cancellationToken = default)
|
=> _webView.DispatchLifecycleEventAsync("moli:pause", null, cancellationToken);
|
|
public Task ResumeAsync(CancellationToken cancellationToken = default)
|
=> _webView.DispatchLifecycleEventAsync("moli:resume", null, cancellationToken);
|
|
public Task ReloadAsync(CancellationToken cancellationToken = default) => _webView.ReloadAsync(cancellationToken);
|
|
protected override async void OnClosed(EventArgs e)
|
{
|
await _webView.DisposeAsync();
|
base.OnClosed(e);
|
}
|
|
private PluginBridgeContext CreateBridgeContext() => new()
|
{
|
HostId = _webView.HostId,
|
Manifest = _plugin.Manifest,
|
RuntimeInfo = new PluginRuntimeBridgeInfo
|
{
|
HostId = _webView.HostId,
|
PluginId = _plugin.Id.Value,
|
PluginType = _plugin.Type,
|
IsVisible = true
|
},
|
Config = _plugin.Manifest.Config.ValueKind == JsonValueKind.Undefined
|
? default
|
: _plugin.Manifest.Config.Clone()
|
};
|
|
private void ApplyWin32Styles()
|
{
|
var hwnd = _interop.GetWindowHandle(this);
|
if (hwnd == IntPtr.Zero)
|
{
|
return;
|
}
|
|
try
|
{
|
_interop.SetToolWindow(hwnd, true);
|
_interop.SetNoActivate(hwnd, true);
|
_interop.SetLayered(hwnd, true);
|
_interop.SetClickThrough(hwnd, _clickThrough);
|
_interop.MoveToDesktopLayer(hwnd, DesktopLayerMode.WorkerW, _monitor, _monitor.Bounds);
|
}
|
catch (Exception ex)
|
{
|
_diagnostics.Record(
|
DiagnosticSeverity.Warning,
|
DiagnosticCodes.DesktopLayerAttachFailed,
|
"Failed to apply wallpaper window styles.",
|
new Dictionary<string, string> { ["pluginId"] = _plugin.Id.Value, ["exception"] = ex.GetType().Name });
|
}
|
}
|
}
|