using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using SpdUp.ViewModels;
namespace SpdUp.Views;
///
/// Floating desktop widget – borderless, transparent, always-on-top.
///
public partial class WidgetWindow : Window
{
private bool _expanded;
// ── Win32 click-through support ───────────────────────────────────
private const int GWL_EXSTYLE = -20;
private const int WS_EX_TRANSPARENT = 0x00000020;
private const int WS_EX_TOOLWINDOW = 0x00000080;
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
public WidgetWindow(MainViewModel vm)
{
InitializeComponent();
DataContext = vm;
// Restore position from settings
Left = vm.Settings.WidgetOpacity > 0 ? 100 : 100; // default
Top = 100;
Loaded += (_, _) =>
{
// Make it a tool window (no taskbar entry)
var hwnd = new WindowInteropHelper(this).Handle;
var exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TOOLWINDOW);
Opacity = vm.Settings.WidgetOpacity;
if (!vm.Settings.WidgetCompactMode)
ToggleExpand();
};
}
private void Widget_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
DragMove();
}
private void Widget_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
ToggleExpand();
}
private void Toggle_Click(object sender, RoutedEventArgs e) => ToggleExpand();
private void Close_Click(object sender, RoutedEventArgs e) => Close();
private void ToggleExpand()
{
_expanded = !_expanded;
ExpandedPanel.Visibility = _expanded ? Visibility.Visible : Visibility.Collapsed;
}
///
/// Enable click-through: all mouse events pass through to windows below.
///
public void SetClickThrough(bool enable)
{
var hwnd = new WindowInteropHelper(this).Handle;
var exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
if (enable)
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TRANSPARENT);
else
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle & ~WS_EX_TRANSPARENT);
}
}