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.Interfaces;
using WinWARP.Core.ViewModels;
using WinWARP.Ui.Helpers;
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;
///
/// An empty window that can be used on its own or navigated to within a Frame.
///
public sealed partial class MainWindow : Window
{
public MainViewModel ViewModel { get; }
private XamlRootTracker _xamlRootTracker;
public MainWindow(MainViewModel viewModel, XamlRootTracker xamlRootTracker)
{
InitializeComponent();
ViewModel = viewModel;
_xamlRootTracker = xamlRootTracker;
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();
navService.NavigatedToViewModel += NavService_NavigatedToViewModel;
navService.RegisterFrame(RootFrame);
navService.NavigateTo();
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()
.FirstOrDefault(item => item.Tag is Type itemVmType && itemVmType == e);
if (targetItem != null)
{
RootNavigationView.SelectedItem = targetItem;
}
_xamlRootTracker.XamlRoot = RootFrame.XamlRoot;
}
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();
}
}
}
}