Files
WinWARP/WinWARP.Ui/MainWindow.xaml.cs
2026-08-22 19:11:00 +03:00

96 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Input;
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;
using WinWARP.Core.Services;
using WinWARP.Core.ViewModels;
using WinWARP.Ui.Services;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace WinWARP.Ui;
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public MainViewModel ViewModel { get; }
public MainWindow(MainViewModel viewModel)
{
InitializeComponent();
ViewModel = viewModel;
ExtendsContentIntoTitleBar = true;
AppWindow.TitleBar.PreferredHeightOption = Microsoft.UI.Windowing.TitleBarHeightOption.Tall;
SetTitleBar(TopAppBar);
AppWindow.SetIcon("Assets/Square44x44Logo.targetsize-24_altform-unplated.png");
AppWindow.Resize(new Windows.Graphics.SizeInt32(1024, 759));
var navService = (NavigationService)App.Services.GetRequiredService<INavigationService>();
navService.NavigatedToViewModel += NavService_NavigatedToViewModel;
navService.RegisterFrame(RootFrame);
navService.NavigateTo<HomeViewModel>();
RootGrid.AddHandler(UIElement.PointerPressedEvent, new PointerEventHandler(Window_PointerPressed), true);
}
private void NavService_NavigatedToViewModel(object? sender, Type e)
{
var targetItem = RootNavigationView.MenuItems
.Concat(RootNavigationView.FooterMenuItems)
.OfType<NavigationViewItem>()
.FirstOrDefault(item => item.Tag is Type itemVmType && itemVmType == e);
if (targetItem != null)
{
RootNavigationView.SelectedItem = targetItem;
}
}
private void TitleBar_BackRequested(TitleBar sender, object args)
{
if (RootFrame.CanGoBack)
{
RootFrame.GoBack();
}
}
private void TitleBar_PaneToggleRequested(TitleBar sender, object args)
{
RootNavigationView.IsPaneOpen = !RootNavigationView.IsPaneOpen;
}
private void Window_PointerPressed(object sender, PointerRoutedEventArgs e)
{
var properties = e.GetCurrentPoint(sender as UIElement).Properties;
// Check if mouse back button (XButton1) was pressed
if (properties.PointerUpdateKind == PointerUpdateKind.XButton1Pressed)
{
e.Handled = true;
if (RootFrame.CanGoBack)
{
RootFrame.GoBack();
}
}
}
}