LANCommander/LANCommander.Launcher/Views/MainWindow.axaml.cs

152 lines
4.4 KiB
C#
Raw Permalink Normal View History

2026-06-14 21:24:07 -05:00
using System;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using LANCommander.Launcher.Plugins;
using LANCommander.Launcher.ViewModels;
using Microsoft.Extensions.DependencyInjection;
namespace LANCommander.Launcher.Views;
public partial class MainWindow : Window
{
2026-05-28 00:09:53 -05:00
private WindowState _stateBeforeBigScreen = WindowState.Normal;
2026-06-14 21:24:07 -05:00
private bool _allowClose;
/// <summary>Raised after the window hides to the tray (close intercepted).</summary>
public event EventHandler? HiddenToTray;
2026-05-28 00:09:53 -05:00
public MainWindow()
{
InitializeComponent();
2026-05-28 00:09:53 -05:00
// Content templates come from the shared view registry (seeded with the built-in mappings and
// extendable by plugins) rather than inline XAML DataTemplates.
var registry = App.Services?.GetService<IViewRegistry>();
if (registry != null)
MainContent.DataTemplates.Add(registry.AsDataTemplate());
2026-06-14 21:24:07 -05:00
Closing += (_, e) =>
{
// Hide to the system tray instead of closing; the app keeps running.
if (_allowClose)
return;
e.Cancel = true;
Hide();
HiddenToTray?.Invoke(this, EventArgs.Empty);
};
2026-05-28 00:09:53 -05:00
DataContextChanged += (_, _) =>
{
if (DataContext is MainWindowViewModel vm)
{
vm.BigScreenModeChanged += OnBigScreenModeChanged;
2026-06-14 21:24:07 -05:00
vm.ExitLauncherRequested += (_, _) => ExitApplication();
2026-05-28 00:09:53 -05:00
// Apply big screen mode if it was persisted or set via command line
if (vm.IsBigScreenMode)
WindowState = WindowState.FullScreen;
}
};
}
private void OnBigScreenModeChanged(object? sender, System.EventArgs e)
{
if (sender is not MainWindowViewModel vm) return;
if (vm.IsBigScreenMode)
{
_stateBeforeBigScreen = WindowState;
WindowState = WindowState.FullScreen;
}
else
WindowState = _stateBeforeBigScreen;
}
private void ResizeGrip_PointerPressed(object? sender, PointerPressedEventArgs e)
{
2026-06-14 21:24:07 -05:00
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
return;
if (WindowState != WindowState.Normal)
return;
if (sender is not Border { Name: var name })
return;
var edge = name switch
{
"ResizeNW" => WindowEdge.NorthWest,
"ResizeN" => WindowEdge.North,
"ResizeNE" => WindowEdge.NorthEast,
"ResizeW" => WindowEdge.West,
"ResizeE" => WindowEdge.East,
"ResizeSW" => WindowEdge.SouthWest,
"ResizeS" => WindowEdge.South,
"ResizeSE" => WindowEdge.SouthEast,
_ => (WindowEdge?)null
};
2026-06-14 21:24:07 -05:00
if (edge.HasValue)
BeginResizeDrag(edge.Value, e);
}
private void TitleBarDragRegion_PointerPressed(object? sender, PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
BeginMoveDrag(e);
}
private void TitleBarDragRegion_DoubleTapped(object? sender, TappedEventArgs e)
{
WindowState = WindowState == WindowState.Maximized
? WindowState.Normal
: WindowState.Maximized;
}
private void MinimizeButton_Click(object? sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void MaximizeButton_Click(object? sender, RoutedEventArgs e)
{
WindowState = WindowState == WindowState.Maximized
? WindowState.Normal
: WindowState.Maximized;
}
private void CloseButton_Click(object? sender, RoutedEventArgs e)
{
Close();
}
2026-06-14 21:24:07 -05:00
/// <summary>
/// Bring the window back to the foreground after it has been hidden to the
/// tray or minimized. Used by the tray and when a second launcher instance
/// asks the running one to surface.
/// </summary>
public void RestoreFromTray()
{
Show();
if (WindowState == WindowState.Minimized)
WindowState = WindowState.Normal;
Activate();
}
2026-06-14 21:24:07 -05:00
/// <summary>
/// Fully exit the application, bypassing the close-to-tray behavior.
/// </summary>
public void ExitApplication()
{
_allowClose = true;
Close();
}
}