using XFEExtension.NetCore.WinUIHelper.Utilities.Helper; using XFEWorkflow.Profiles.CrossVersionProfiles; namespace XFEWorkflow.Views { /// /// AppShell /// public sealed partial class AppShellPage : Page { public static AppShellPage? Current { get; set; } public AppShellPageViewModel ViewModel { get; set; } = new(); public AppShellPage() { Current = this; this.InitializeComponent(); UserAccountMenu.ActionInvoked += AccountMenu_ActionInvoked; ActualThemeChanged += (_, _) => UpdateAccountMenuTheme(); if (Microsoft.UI.Composition.SystemBackdrops.MicaController.IsSupported()) { HeaderAccountFlyout.SystemBackdrop = new Microsoft.UI.Xaml.Media.MicaBackdrop(); HeaderAccountFlyout.FlyoutPresenterStyle = (Style)Application.Current.Resources["AccountMicaFlyoutPresenterStyle"]; } navigationView.RegisterPropertyChangedCallback(NavigationView.IsPaneOpenProperty, (_, _) => UpdateNavigationSplitter()); var services = Services.AppServices.Current; NavigationSplitter.Bind("shell.navigation", 320, 216, 400, () => navigationView.OpenPaneLength, value => { navigationView.OpenPaneLength = value; if (navigationView.IsPaneOpen) rightPanelGrid.Margin = new(value, 50, 0, 0); UpdateNavigationSplitter(); }, () => ActualWidth - 640); ViewModel.ApplyUser(services.Space.User, services.State.SpaceAddress); Bindings.Update(); services.Space.UserChanged += () => { if (DispatcherQueue.HasThreadAccess) ViewModel.ApplyUser(services.Space.User, services.State.SpaceAddress); else DispatcherQueue.TryEnqueue(() => ViewModel.ApplyUser(services.Space.User, services.State.SpaceAddress)); }; App.MainWindow.SetTitleBar(appTitleBar); ViewModel.NavigationViewService.Initialize(navigationView, navigationFrame); ViewModel.MessageService.Initialize(messageStackPanel, DispatcherQueue); ViewModel.LoadingService.Initialize(loadingGrid, globalLoadingGrid, globalLoadingTextBlock, DispatcherQueue, ViewModel.NavigationViewService.NavigationService); Services.AppServices.Current.Start(browserHost); navigationView.ItemInvoked += (_, args) => { if (args.InvokedItemContainer?.Tag is Services.PluginPageReference page) { ViewModel.NavigationViewService.NavigateTo(page); navigationView.SelectedItem = args.InvokedItemContainer; } }; services.Plugins.Changed += () => DispatcherQueue.TryEnqueue(async () => await RefreshPluginPagesAsync()); _ = RefreshPluginPagesAsync(); ViewModel.NavigationViewService.NavigateTo(); } private bool accountMenuOpen, accountActionBusy; internal void SelectPluginPage(Services.PluginPageReference reference) { if (navigationView.MenuItems.OfType().FirstOrDefault(x => x.Tag is Services.PluginPageReference page && page == reference) is { } item) navigationView.SelectedItem = item; } private async Task RefreshPluginPagesAsync() { try { await Services.AppServices.Current.Ready; var selected = (navigationView.SelectedItem as NavigationViewItem)?.Tag as Services.PluginPageReference; foreach (var item in navigationView.MenuItems.OfType().Where(x => x.Tag is Services.PluginPageReference).ToArray()) navigationView.MenuItems.Remove(item); var offset = 3; foreach (var (plugin, page) in Services.AppServices.Current.Plugins.PageCatalog()) { var item = new NavigationViewItem { Content = page.Name, Icon = new FontIcon { Glyph = page.IconGlyph }, Tag = new Services.PluginPageReference(plugin.Manifest.Id, plugin.Manifest.Version, page.Id) }; Microsoft.UI.Xaml.Controls.ToolTipService.SetToolTip(item, plugin.Manifest.Name + " · " + page.Description); navigationView.MenuItems.Insert(offset++, item); if (selected == item.Tag as Services.PluginPageReference) navigationView.SelectedItem = item; } await Services.AppServices.Current.PluginPages.RefreshAsync(); } catch (Exception ex) { ViewModel.MessageService.ShowMessage(ex.Message, "插件页面", InfoBarSeverity.Error); } } private TaskCompletionSource? accountMenuClosed; private void Account_Click(object sender, RoutedEventArgs e) => Microsoft.UI.Xaml.Controls.Primitives.FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender); private void AccountFlyout_Opened(object sender, object e) { accountMenuOpen = true; UpdateAccountMenuTheme(); } private void UpdateAccountMenuTheme() { UserAccountMenu.RequestedTheme = ActualTheme; if (XamlRoot is not { } root) return; foreach (var popup in Microsoft.UI.Xaml.Media.VisualTreeHelper.GetOpenPopupsForXamlRoot(root)) if (popup.Child is FlyoutPresenter { Content: Controls.AccountMenu } presenter) presenter.RequestedTheme = ActualTheme; } private void AccountFlyout_Closed(object sender, object e) { accountMenuOpen = false; accountMenuClosed?.TrySetResult(); } private async void AccountMenu_ActionInvoked(Controls.AccountMenuAction action) { if (accountActionBusy) return; accountActionBusy = true; try { if (accountMenuOpen) { accountMenuClosed = new(TaskCreationOptions.RunContinuationsAsynchronously); HeaderAccountFlyout.Hide(); await accountMenuClosed.Task; } switch (action) { case Controls.AccountMenuAction.Profile: ViewModel.NavigationViewService.NavigateTo(); break; case Controls.AccountMenuAction.Space: ViewModel.NavigationViewService.NavigateTo(); break; case Controls.AccountMenuAction.SignIn: case Controls.AccountMenuAction.Switch: await Services.AccountDialog.ShowAsync(XamlRoot); break; case Controls.AccountMenuAction.SignOut: await Services.AppServices.Current.Space.LogoutAsync(); break; } } catch (Exception ex) { ViewModel.MessageService.ShowMessage(ex.Message, "用户账号", InfoBarSeverity.Error); } finally { accountActionBusy = false; accountMenuClosed = null; } } internal void SetNavigationOpenForSmoke(bool value) => navigationView.IsPaneOpen = value; internal bool NavigationIsOpenForSmoke => navigationView.IsPaneOpen; internal Controls.PaneSplitter NavigationSplitterForSmoke => NavigationSplitter; internal double NavigationWidthForSmoke => navigationView.OpenPaneLength; internal string NavigationStateForSmoke => $"open={navigationView.IsPaneOpen}, mode={navigationView.DisplayMode}, width={navigationView.ActualWidth}, splitter={NavigationSplitter.Visibility}/{NavigationSplitter.ActualWidth}x{NavigationSplitter.ActualHeight}"; private void ShellLayout_SizeChanged(object sender, SizeChangedEventArgs e) { if (NavigationSplitter is not null) { NavigationSplitter.ApplyPreferredSize(); UpdateNavigationSplitter(); } } private void UpdateNavigationSplitter() { if (NavigationSplitter is null || navigationView is null) return; NavigationSplitter.Visibility = navigationView.IsPaneOpen && navigationView.DisplayMode == NavigationViewDisplayMode.Expanded ? Visibility.Visible : Visibility.Collapsed; NavigationSplitter.Margin = new(navigationView.OpenPaneLength - 4, Math.Max(48, appTitleBar.ActualHeight), 0, 0); } internal void AssertAccountPresentation(string name, bool hasAvatar) { if (navigationView.MenuItems[0] is not NavigationViewItem { Icon: FontIcon { Glyph: "\uEF90" } }) throw new InvalidOperationException("工作流图标未使用 EF90"); if (navigationView.PaneDisplayMode != NavigationViewPaneDisplayMode.Auto || ViewModel.UserName != name || HeaderAvatar.DisplayName != name) throw new InvalidOperationException("用户显示或导航展开方式错误"); if (hasAvatar) { if (ViewModel.Avatar is not Microsoft.UI.Xaml.Media.Imaging.BitmapImage bitmap || bitmap.PixelWidth <= 0 || HeaderAvatar.ProfilePicture != bitmap || AccountNavigationItem.Icon is not ImageIcon icon || icon.Source != bitmap || AccountNavigationItem.Content?.ToString() != name) throw new InvalidOperationException("标题栏和用户选项卡未使用真实头像"); } else if (HeaderAvatar.ProfilePicture is not null || AccountNavigationItem.Content?.ToString() != "登录账号") throw new InvalidOperationException("退出账号后用户信息未清除"); } public void ShowBrowser(bool show) { browserHost.Opacity = show ? 1 : 0; browserHost.IsHitTestVisible = show; navigationFrame.Opacity = show ? 0 : 1; navigationFrame.IsHitTestVisible = !show; } private void NavigationView_DisplayModeChanged(NavigationView sender, NavigationViewDisplayModeChangedEventArgs args) { switch (sender.PaneDisplayMode) { case NavigationViewPaneDisplayMode.Auto: case NavigationViewPaneDisplayMode.Left: navigationView.Margin = new(); appTitleBar.Margin = new() { Left = sender.CompactPaneLength * (sender.DisplayMode == NavigationViewDisplayMode.Minimal ? 2 : 1), Top = appTitleBar.Margin.Top, Right = appTitleBar.Margin.Right, Bottom = appTitleBar.Margin.Bottom }; break; case NavigationViewPaneDisplayMode.Top: navigationView.Margin = new(0, 48, 0, 0); appTitleBar.Margin = new(16, 0, 0, 0); break; case NavigationViewPaneDisplayMode.LeftCompact: case NavigationViewPaneDisplayMode.LeftMinimal: navigationView.Margin = new(); appTitleBar.Margin = new() { Left = sender.CompactPaneLength * (sender.DisplayMode == NavigationViewDisplayMode.Minimal ? 2 : 1), Top = appTitleBar.Margin.Top, Right = appTitleBar.Margin.Right, Bottom = appTitleBar.Margin.Bottom }; break; default: break; } UpdateNavigationSplitter(); } private void Page_Loaded(object sender, RoutedEventArgs e) { AppThemeHelper.ChangeTheme(SystemProfile.Theme); } private void NavigationView_PaneOpening(NavigationView sender, object args) { rightPanelGrid.TranslationTransition = new(); rightPanelGrid.Translation = new((float)(sender.OpenPaneLength / 2 - sender.CompactPaneLength * (sender.DisplayMode == NavigationViewDisplayMode.Minimal ? 1f : 0.5f)) + rightPanelGrid.Translation.X, 0, 0); } private void NavigationView_PaneOpened(NavigationView sender, object args) { UpdateNavigationSplitter(); rightPanelGrid.TranslationTransition = null; rightPanelGrid.Translation = new(); rightPanelGrid.Margin = new() { Left = sender.OpenPaneLength, Top = 50 }; } private void NavigationView_PaneClosing(NavigationView sender, NavigationViewPaneClosingEventArgs args) { rightPanelGrid.TranslationTransition = new(); rightPanelGrid.Translation = new((float)(sender.CompactPaneLength * (sender.DisplayMode == NavigationViewDisplayMode.Minimal ? 1f : 0.5f) - sender.OpenPaneLength / 2) + rightPanelGrid.Translation.X, 0, 0); } private void NavigationView_PaneClosed(NavigationView sender, object args) { UpdateNavigationSplitter(); rightPanelGrid.TranslationTransition = null; rightPanelGrid.Translation = new(); rightPanelGrid.Margin = new() { Left = sender.CompactPaneLength * (sender.DisplayMode == NavigationViewDisplayMode.Minimal ? 2 : 1), Top = 50 }; } } }