Files
IPClab1/MainWindow.xaml.cs
T
2026-09-18 14:26:02 +03:00

99 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using IPClab1.Models;
using IPClab1.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Windows.Foundation;
using Windows.Foundation.Collections;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace IPClab1;
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
private readonly DispatcherTimer timer = new() { Interval = TimeSpan.FromMilliseconds(100) };
public MainWindowViewModel ViewModel;
public MainWindow(MainWindowViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
ExtendsContentIntoTitleBar = true;
SetTitleBar(AppTitleBar);
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object? sender, object e)
{
AppTitleBar.Title = Title;
ViewModel.RefreshInfo();
}
private void GridView_SizeChanged(object sender, SizeChangedEventArgs e)
{
var gridView = sender as GridView;
double availableWidth = e.NewSize.Width - gridView.Padding.Left - gridView.Padding.Right;
double minItemWidth = 250; // The absolute minimum width you want an item to be
double baseSpacing = 8; // Your baseline gap width
// Calculate how many columns can mathematically fit
int columns = (int)Math.Floor((availableWidth + baseSpacing) / (minItemWidth + baseSpacing));
columns = Math.Max(1, columns); // Ensure at least 1 column
// Dynamically calculate the item width to absorb the remaining space
double dynamicWidth = ((availableWidth + baseSpacing) / columns) - baseSpacing;
if (WindowGrid.ItemsPanelRoot is ItemsWrapGrid wrapGrid)
{
wrapGrid.ItemWidth = dynamicWidth;
}
}
private void WindowGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0) ViewModel.SelectedWindow = (WindowInfo)e.AddedItems[0];
}
private void RowContent_SizeChanged(object sender, SizeChangedEventArgs e)
{
// 1. Get the actual natural height of the content in both rows
// (Assuming the direct children of Row 0 and Row 1 are FrameworkElements)
double row0Height = ((FrameworkElement)ActionsGrid.Children[0]).ActualHeight;
double row1Height = ((FrameworkElement)ActionsGrid.Children[1]).ActualHeight;
double row2Height = ((FrameworkElement)ActionsGrid.Children[2]).ActualHeight;
double row3Height = ((FrameworkElement)ActionsGrid.Children[3]).ActualHeight;
// 2. Find which one is larger
double maxHeight = Math.Max(Math.Max(row0Height, row1Height), Math.Max(row2Height, row3Height));
// 3. Force both rows to match the largest height
ActionRow0.MinHeight = maxHeight;
ActionRow1.MinHeight = maxHeight;
// Note: Row 2 will automatically shrink to 0 height if Row 0 and Row 1
// consume the entire available height of the parent Grid.
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.Windows.Remove((WindowInfo)WindowGrid.SelectedItem);
WindowGrid.SelectedIndex = -1;
}
}