WIn_RustTools/RustTools/Views/ShellPage.xaml.cs

123 lines
4.0 KiB
C#

using System;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using RustTools.Contracts.Services;
using RustTools.Helpers;
using RustTools.muqing;
using RustTools.ViewModels;
using Windows.System;
namespace RustTools.Views;
// TODO: Update NavigationViewItem titles and icons in ShellPage.xaml.
public sealed partial class ShellPage : Page
{
public ShellViewModel ViewModel
{
get;
}
public ShellPage(ShellViewModel viewModel)
{
ViewModel = viewModel;
InitializeComponent();
ViewModel.NavigationService.Frame = NavigationFrame;
//ViewModel.NavigationService.Frame.ElementSoundMode.isna = "False";
ViewModel.NavigationViewService.Initialize(NavigationViewControl);
// TODO: Set the title bar icon by updating /Assets/WindowIcon.ico.
// A custom title bar is required for full window theme and Mica support.
// https://docs.microsoft.com/windows/apps/develop/title-bar?tabs=winui3#full-customization
if (App.MainWindow != null)
{
App.MainWindow.SetTitleBar(AppTitleBar);
App.MainWindow.Activated += MainWindow_Activated;
}
//AppTitleBarText.Text = "app_name".GetLocalized() + " " + "app_version".GetLocalized();
//NavigationViewControl.SelectedItem = NavigationViewControl.MenuItems[0];
//NavigationViewControl.SelectionChanged += NavigationViewControl_SelectionChanged;
//NavigateToPage("Home");
}
private void NavigationViewControl_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
System.GC.Collect();
if (args.SelectedItem is NavigationViewItem selectedItem)
{
var tag = selectedItem.Tag.ToString();
gj.sc(tag);
if (tag != null)
{
NavigateToPage(tag);
}
}
}
private void NavigateToPage(string pageTag)
{
var pageType = pageTag switch
{
"Home" => typeof(HomePage),
"Concern" => typeof(ConcernPage),
"Ranking" => typeof(RankingPage),
"User"=>typeof(UserPage),
"Module" => typeof(ModulePage),
"Settings" => typeof(SettingsPage),
_ => typeof(HomePage),
};
NavigationFrame.Navigate(pageType);
}
private void OnLoaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
//gj.sc(RequestedTheme);
TitleBarHelper.UpdateTitleBar(RequestedTheme);
KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.Left, VirtualKeyModifiers.Menu));
KeyboardAccelerators.Add(BuildKeyboardAccelerator(VirtualKey.GoBack));
}
private void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
{
App.AppTitlebar = AppTitleBarText;
}
private void NavigationViewControl_DisplayModeChanged(NavigationView sender, NavigationViewDisplayModeChangedEventArgs args)
{
AppTitleBar.Margin = new Thickness()
{
Left = sender.CompactPaneLength * (sender.DisplayMode == NavigationViewDisplayMode.Minimal ? 2 : 1),
Top = AppTitleBar.Margin.Top,
Right = AppTitleBar.Margin.Right,
Bottom = AppTitleBar.Margin.Bottom
};
}
private static KeyboardAccelerator BuildKeyboardAccelerator(VirtualKey key, VirtualKeyModifiers? modifiers = null)
{
var keyboardAccelerator = new KeyboardAccelerator() { Key = key };
if (modifiers.HasValue)
{
keyboardAccelerator.Modifiers = modifiers.Value;
}
keyboardAccelerator.Invoked += OnKeyboardAcceleratorInvoked;
return keyboardAccelerator;
}
private static void OnKeyboardAcceleratorInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
var navigationService = App.GetService<INavigationService>();
var result = navigationService.GoBack();
args.Handled = result;
}
}