using Moli.Desktop.Configuration;
using Moli.Desktop.Diagnostics;
using Moli.Desktop.Layout;
namespace Moli.Desktop.Interop;
/// Fallback strategy that keeps the wallpaper window at the bottom of normal Z-order.
public sealed class BottomDesktopLayerStrategy : IDesktopLayerStrategy
{
private readonly IDiagnosticsService _diagnostics;
public BottomDesktopLayerStrategy(IDiagnosticsService diagnostics)
{
_diagnostics = diagnostics;
}
public string Name => "BottomDesktopLayer";
public bool IsSupported() => true;
public bool TryAttachDesktopWindow(IntPtr hwnd, MonitorInfo monitor, RectD bounds, out string? error)
{
error = null;
var scale = monitor.Scale;
var x = (int)(bounds.X * scale);
var y = (int)(bounds.Y * scale);
var w = (int)(bounds.Width * scale);
var h = (int)(bounds.Height * scale);
if (!NativeMethods.SetWindowPos(
hwnd,
NativeMethods.HwndBottom,
x,
y,
w,
h,
NativeMethods.SwpNoActivate | NativeMethods.SwpShowWindow))
{
error = $"SetWindowPos failed with {NativeMethods.GetLastError()}";
_diagnostics.Record(
DiagnosticSeverity.Warning,
DiagnosticCodes.DesktopLayerAttachFailed,
"Failed to place window at bottom desktop layer.",
new Dictionary { ["reason"] = error });
return false;
}
_diagnostics.Record(
DiagnosticSeverity.Info,
DiagnosticCodes.DesktopLayerStrategySelected,
"Desktop layer strategy selected.",
new Dictionary { ["strategy"] = Name });
return true;
}
}