/
githubmirror
/
Files
Обзор
Документация
Войти
/
githubmirror
/
Files
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Files.App/ViewModels/UserControls/Widgets/NetworkLocationsWidgetViewModel.cs
300 строк
10 KB
yair100
Fix: Fixed issue where no error was displayed when opening corrupted exe (#18721)
15 июл 2026, 23:06
Не верифицирован
15 июл 2026, 23:06
8674d76
Код
Авторство
О чём код?
// Copyright (c) Files Community // Licensed under the MIT License. using Microsoft.UI.Input; using Microsoft.UI.Xaml.Controls; using System.Collections.Specialized; using System.Windows.Input; using Windows.System; using Windows.UI.Core; namespace Files.App.ViewModels.UserControls.Widgets { /// <summary> /// Represents view model of <see cref="NetworkLocationsWidget"/>. /// </summary> public sealed partial class NetworkLocationsWidgetViewModel : BaseWidgetViewModel, IWidgetViewModel { // Properties public ObservableCollection<WidgetDriveCardItem> Items { get; } = []; public string WidgetName => nameof(NetworkLocationsWidget); public string AutomationProperties => Strings.NetworkLocations.GetLocalizedResource(); public string WidgetHeader => Strings.NetworkLocations.GetLocalizedResource(); public bool IsWidgetSettingEnabled => UserSettingsService.GeneralSettingsService.ShowNetworkLocationsWidget; public bool ShowMenuFlyout => true; public MenuFlyoutItem? MenuFlyoutItem => new() { Icon = new FontIcon() { Glyph = "\uE710" }, Text = Strings.DrivesWidgetOptionsFlyoutMapNetDriveMenuItemText.GetLocalizedResource(), Command = MapNetworkDriveCommand }; private bool _IsNoNetworkLocations; public bool IsNoNetworkLocations { get => _IsNoNetworkLocations; private set => SetProperty(ref _IsNoNetworkLocations, value); } // Commands private ICommand EjectDeviceCommand { get; } = null!; private ICommand MapNetworkDriveCommand { get; } = null!; private ICommand DisconnectNetworkDriveCommand { get; } = null!; // Constructor public NetworkLocationsWidgetViewModel() { Drives_CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); Shortcuts_CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); DrivesViewModel.Drives.CollectionChanged += Drives_CollectionChanged; NetworkService.Shortcuts.CollectionChanged += Shortcuts_CollectionChanged; PinToSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecutePinToSidebarCommand); UnpinFromSidebarCommand = new AsyncRelayCommand<WidgetCardItem>(ExecuteUnpinFromSidebarCommand); EjectDeviceCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteEjectDeviceCommand); OpenPropertiesCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteOpenPropertiesCommand); DisconnectNetworkDriveCommand = new RelayCommand<WidgetDriveCardItem>(ExecuteDisconnectNetworkDriveCommand); MapNetworkDriveCommand = new AsyncRelayCommand(ExecuteMapNetworkDriveCommand); } // Methods public async Task RefreshWidgetAsync() { var updateTasks = Items.Select(item => item.Item.UpdatePropertiesAsync()); await Task.WhenAll(updateTasks); } public async Task NavigateToPath(string path) { if (await DriveHelpers.CheckEmptyDrive(path)) return; var ctrlPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down); if (ctrlPressed) { await NavigationHelpers.OpenPathInNewTab(path); return; } ContentPageContext.ShellPage!.NavigateWithArguments( ContentPageContext.ShellPage!.InstanceViewModel.FolderSettings.GetLayoutType(path), new() { NavPathParam = path }); } public override List<ContextMenuFlyoutItemViewModel> GetItemMenuItems(WidgetCardItem item, bool isPinned, bool isFolder = false) { var drive = Items.Where(x => string.Equals( PathNormalization.NormalizePath(x.Path!), PathNormalization.NormalizePath(item.Path!), StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(); var options = drive?.Item.MenuOptions; return new List<ContextMenuFlyoutItemViewModel>() { new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewTabFromHome) { IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewTab && CommandManager.OpenInNewTabFromHome.IsExecutable }.Build(), new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInNewWindowFromHome) { IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewWindow && CommandManager.OpenInNewWindowFromHome.IsExecutable }.Build(), new ContextMenuFlyoutItemViewModel() { Text = Strings.OpenInNewPane.GetLocalizedResource(), ShowItem = UserSettingsService.GeneralSettingsService.ShowOpenInNewPane && CommandManager.OpenInNewPaneFromHome.IsExecutable, IsEnabled = CommandManager.OpenInNewPaneFromHome.IsExecutable, Items = [ new ContextMenuFlyoutItemViewModel() { Text = Strings.SplitPaneVertically.GetLocalizedResource(), ThemedIconModel = new() { ThemedIconStyle = "App.ThemedIcons.OpenInPaneVertical" }, Command = CommandManager.OpenInNewPaneFromHome, CommandParameter = ShellPaneArrangement.Vertical, }, new ContextMenuFlyoutItemViewModel() { Text = Strings.SplitPaneHorizontally.GetLocalizedResource(), ThemedIconModel = new() { ThemedIconStyle = "App.ThemedIcons.OpenInPaneHorizontal" }, Command = CommandManager.OpenInNewPaneFromHome, CommandParameter = ShellPaneArrangement.Horizontal, }, ] }, new ContextMenuFlyoutItemViewModelBuilder(CommandManager.OpenInOtherPaneFromHome) { IsVisible = UserSettingsService.GeneralSettingsService.ShowOpenInNewPane && CommandManager.OpenInOtherPaneFromHome.IsExecutable }.Build(), new ContextMenuFlyoutItemViewModelBuilder(CommandManager.CopyItemFromHome) { IsPrimary = true, IsVisible = CommandManager.CopyItemFromHome.IsExecutable }.Build(), new() { Text = Strings.Properties.GetLocalizedResource(), ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.Properties" }, Command = OpenPropertiesCommand, CommandParameter = item, IsPrimary = true }, new() { Text = Strings.PinFolderToSidebar.GetLocalizedResource(), ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.FavoritePin" }, Command = PinToSidebarCommand, CommandParameter = item, ShowItem = !isPinned && UserSettingsService.GeneralSettingsService.ShowPinToSideBar }, new() { Text = Strings.UnpinFolderFromSidebar.GetLocalizedResource(), ThemedIconModel = new ThemedIconModel() { ThemedIconStyle = "App.ThemedIcons.FavoritePinRemove" }, Command = UnpinFromSidebarCommand, CommandParameter = item, ShowItem = isPinned && UserSettingsService.GeneralSettingsService.ShowPinToSideBar }, new() { Text = Strings.Eject.GetLocalizedResource(), Command = EjectDeviceCommand, CommandParameter = item, ShowItem = options?.ShowEjectDevice ?? false }, new ContextMenuFlyoutItemViewModelBuilder(CommandManager.FormatDriveFromHome).Build(), new() { Text = Strings.TurnOnBitLocker.GetLocalizedResource(), Tag = "TurnOnBitLockerPlaceholder", IsEnabled = false }, new() { Text = Strings.ManageBitLocker.GetLocalizedResource(), Tag = "ManageBitLockerPlaceholder", IsEnabled = false }, new() { ItemType = ContextMenuFlyoutItemType.Separator, Tag = "OverflowSeparator", }, new() { Text = Strings.Loading.GetLocalizedResource(), Glyph = "\xE712", Items = [], ID = "ItemOverflow", Tag = "ItemOverflow", IsEnabled = false, } }.Where(x => x.ShowItem).ToList(); } public void DisableWidget() { UserSettingsService.GeneralSettingsService.ShowNetworkLocationsWidget = false; } // Command methods private void ExecuteEjectDeviceCommand(WidgetDriveCardItem? item) { if (item is null) return; DriveHelpers.EjectDeviceAsync(item.Item.Path); } private Task ExecuteMapNetworkDriveCommand() { return NetworkService.OpenMapNetworkDriveDialogAsync(); } private void ExecuteFormatDriveCommand(WidgetDriveCardItem? item) { Win32Helper.OpenFormatDriveDialog(item?.Path ?? string.Empty); } private void ExecuteOpenPropertiesCommand(WidgetDriveCardItem? item) { if (!HomePageContext.IsAnyItemRightClicked || item is null) return; var flyout = HomePageContext.ItemContextFlyoutMenu; EventHandler<object> flyoutClosed = null!; flyoutClosed = (s, e) => { flyout!.Closed -= flyoutClosed; FilePropertiesHelpers.OpenPropertiesWindow(item.Item, ContentPageContext.ShellPage!); }; flyout!.Closed += flyoutClosed; } private void ExecuteDisconnectNetworkDriveCommand(WidgetDriveCardItem? item) { if (item is null) return; NetworkService.DisconnectNetworkDrive(item.Item); } private async Task UpdateItems(ObservableCollection<IFolder> source) { await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () => { IsNoNetworkLocations = false; foreach (DriveItem drive in source.ToList().Cast<DriveItem>()) { if (!Items.Any(x => x.Item == drive) && drive.Type is DriveType.Network) { var cardItem = new WidgetDriveCardItem(drive); Items.AddSorted(cardItem); await cardItem.LoadCardThumbnailAsync(); } } foreach (WidgetDriveCardItem driveCard in Items.ToList()) { if (!DrivesViewModel.Drives.Contains(driveCard.Item) && !NetworkService.Shortcuts.Contains(driveCard.Item)) Items.Remove(driveCard); } IsNoNetworkLocations = !Items.Any(); }); } // Event methods private async void Drives_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { await UpdateItems(DrivesViewModel.Drives); } private async void Shortcuts_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) { await UpdateItems(NetworkService.Shortcuts); } // Disposer public void Dispose() { } } }