From 4fae5fd7f45b8583571c526bb961c9ab7fcc95a0 Mon Sep 17 00:00:00 2001 From: aiqinxuancai Date: Mon, 12 Aug 2024 11:08:54 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E9=98=85=E5=88=97=E8=A1=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9A=E9=80=89=E5=90=8E=E5=8F=B3=E9=94=AE=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aria2Fast/View/WkySubscriptionListView.xaml | 1 + .../View/WkySubscriptionListView.xaml.cs | 115 ++++++++++-------- 2 files changed, 67 insertions(+), 49 deletions(-) diff --git a/Aria2Fast/View/WkySubscriptionListView.xaml b/Aria2Fast/View/WkySubscriptionListView.xaml index 7e892e6..a059d56 100644 --- a/Aria2Fast/View/WkySubscriptionListView.xaml +++ b/Aria2Fast/View/WkySubscriptionListView.xaml @@ -44,6 +44,7 @@ HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding ViewModel, ElementName=Self, UpdateSourceTrigger=PropertyChanged}" LoadingRow="MainDataGrid_LoadingRow" + ContextMenuOpening="MainDataGrid_ContextMenuOpening" ScrollViewer.CanContentScroll="True"> diff --git a/Aria2Fast/View/WkySubscriptionListView.xaml.cs b/Aria2Fast/View/WkySubscriptionListView.xaml.cs index 213ee9b..fa030b6 100644 --- a/Aria2Fast/View/WkySubscriptionListView.xaml.cs +++ b/Aria2Fast/View/WkySubscriptionListView.xaml.cs @@ -19,6 +19,7 @@ using System.Windows.Navigation; using System.Windows.Shapes; using System.IO; +using Newtonsoft.Json; @@ -57,8 +58,7 @@ public WkySubscriptionListView(ObservableCollection viewModel }); } - - private SubscriptionModel _lastSubscriptionModel; + List _selectedItems; public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register("ViewModel", typeof(ObservableCollection), typeof(WkySubscriptionListView)); @@ -89,60 +89,70 @@ private void UIElement_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e) } } - - - private void MainDataGrid_LoadingRow(object sender, DataGridRowEventArgs e) + private void MainDataGrid_ContextMenuOpening(object sender, ContextMenuEventArgs e) { - ContextMenu menu = new ContextMenu(); + //展开时,需要暂停刷新? + var contextMenu = MainDataGrid.ContextMenu; + contextMenu.Items.Clear(); - e.Row.MouseRightButtonDown += (s, a) => { - a.Handled = true; + var selectedItems = new List(); + foreach (var item in MainDataGrid.SelectedItems) + { + var myItem = item as SubscriptionModel; + if (myItem != null) + { + selectedItems.Add(myItem); + } + } - SubscriptionModel model = (SubscriptionModel)((DataGridRow)s).DataContext; - _lastSubscriptionModel = model; + var jsonString = JsonConvert.SerializeObject(selectedItems); - menu.Items.Clear(); - if (_lastSubscriptionModel != null) - { - if (AppConfig.Instance.ConfigData.Aria2UseLocal) - { - MenuItem menuOpenPath = new MenuItem() { Header = "打开目录" }; - menuOpenPath.Click += MenuOpenPath_Click; - menu.Items.Add(menuOpenPath); - } + //需要深拷贝 + _selectedItems = JsonConvert.DeserializeObject>(jsonString); + if (_selectedItems.Count > 0) + { + if (AppConfig.Instance.ConfigData.Aria2UseLocal && _selectedItems.Count == 1) + { + MenuItem menuOpenPath = new MenuItem() { Header = "打开目录" }; + menuOpenPath.Click += MenuOpenPath_Click; + contextMenu.Items.Add(menuOpenPath); + } + if (_selectedItems.Count == 1) + { MenuItem menuEdit = new MenuItem() { Header = "编辑" }; menuEdit.Click += MenuEdit_Click; + contextMenu.Items.Add(menuEdit); + } - MenuItem menuCopyUrl = new MenuItem() { Header = "复制URL" }; - menuCopyUrl.Click += MenuCopyUrl_Click; ; - MenuItem menuReDownload = new MenuItem() { Header = "重新下载" }; - menuReDownload.Click += MenuReDownload_Click; ; + MenuItem menuCopyUrl = new MenuItem() { Header = "复制URL" }; + menuCopyUrl.Click += MenuCopyUrl_Click; ; - MenuItem menuDelete = new MenuItem() { Header = "删除" }; - menuDelete.Click += MenuDelete_Click; + MenuItem menuReDownload = new MenuItem() { Header = "重新下载" }; + menuReDownload.Click += MenuReDownload_Click; ; + MenuItem menuDelete = new MenuItem() { Header = "删除" }; + menuDelete.Click += MenuDelete_Click; - menu.Items.Add(menuEdit); - menu.Items.Add(menuCopyUrl); - menu.Items.Add(menuReDownload); - menu.Items.Add(menuDelete); - DataGrid row = sender as DataGrid; + contextMenu.Items.Add(menuCopyUrl); + contextMenu.Items.Add(menuReDownload); + contextMenu.Items.Add(menuDelete); + DataGrid row = sender as DataGrid; + row.ContextMenu = contextMenu; + } - row.ContextMenu = menu; - } + - - }; } + private void MenuEdit_Click(object sender, RoutedEventArgs e) { - var model = _lastSubscriptionModel; + var model = _selectedItems.FirstOrDefault(); //编辑 MainWindow.Instance.RootNavigation.Navigate(typeof(AddSubscriptionView), (model.Url, model.Name, new MikanAnime(), model)); @@ -151,7 +161,7 @@ private void MenuEdit_Click(object sender, RoutedEventArgs e) private void MenuOpenPath_Click(object sender, RoutedEventArgs e) { - var model = _lastSubscriptionModel; + var model = _selectedItems.FirstOrDefault(); if (Directory.Exists(model.SavePath)) { @@ -159,43 +169,50 @@ private void MenuOpenPath_Click(object sender, RoutedEventArgs e) Process.Start("explorer.exe", correctedPath); } } - + private void MenuCopyUrl_Click(object sender, RoutedEventArgs e) { - Clipboard.SetDataObject(_lastSubscriptionModel.Url); - + var urls = _selectedItems.Select(item => item.Url); + Clipboard.SetDataObject(string.Join("\r\n", urls)); } - private void MenuReDownload_Click(object sender, RoutedEventArgs e) { - //SubscriptionManager.Instance.SubscriptionModel.Remove(_lastSubscriptionModel); - //SubscriptionManager.Instance.Save(); - if (!SubscriptionManager.Instance.Subscribing) { - _lastSubscriptionModel.AlreadyAddedDownloadModel = new ObservableCollection { }; - SubscriptionManager.Instance.CheckSubscriptionOne(_lastSubscriptionModel, Aria2ApiManager.Instance.CurrentRpc); + Task.Run(async () => + { + foreach (var item in _selectedItems) + { + item.AlreadyAddedDownloadModel = new ObservableCollection { }; + await SubscriptionManager.Instance.CheckSubscriptionOne(item, Aria2ApiManager.Instance.CurrentRpc); + } + }); } else { - MainWindow.Instance.ShowSnackbar("当前无法操作", $"正在执行订阅中..."); + MainWindow.Instance.ShowSnackbar("无法操作", $"正在执行订阅中"); } } private void MenuDelete_Click(object sender, RoutedEventArgs e) { - SubscriptionManager.Instance.SubscriptionModel.Remove(_lastSubscriptionModel); + foreach (var item in _selectedItems) + { + SubscriptionManager.Instance.SubscriptionModel.Remove(item); + } SubscriptionManager.Instance.Save(); } private void SubscriptionButton_Click(object sender, RoutedEventArgs e) { - //WindowAddSubscription.Show(Application.Current.MainWindow); + MainWindow.Instance.RootNavigation.Navigate(typeof(AddSubscriptionView), null); + } - MainWindow.Instance.RootNavigation.Navigate(typeof(AddSubscriptionView), null); + private void MainDataGrid_LoadingRow(object sender, DataGridRowEventArgs e) + { } }