Skip to content

Commit

Permalink
订阅列表支持多选后右键操作
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Aug 12, 2024
1 parent 2e87d36 commit 4fae5fd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 49 deletions.
1 change: 1 addition & 0 deletions Aria2Fast/View/WkySubscriptionListView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding ViewModel, ElementName=Self, UpdateSourceTrigger=PropertyChanged}"
LoadingRow="MainDataGrid_LoadingRow"
ContextMenuOpening="MainDataGrid_ContextMenuOpening"
ScrollViewer.CanContentScroll="True">

<DataGrid.HorizontalGridLinesBrush>
Expand Down
115 changes: 66 additions & 49 deletions Aria2Fast/View/WkySubscriptionListView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Newtonsoft.Json;



Expand Down Expand Up @@ -57,8 +58,7 @@ public WkySubscriptionListView(ObservableCollection<SubscriptionModel> viewModel
});
}


private SubscriptionModel _lastSubscriptionModel;
List<SubscriptionModel> _selectedItems;

public static readonly DependencyProperty ViewModelProperty =
DependencyProperty.Register("ViewModel", typeof(ObservableCollection<SubscriptionModel>), typeof(WkySubscriptionListView));
Expand Down Expand Up @@ -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<SubscriptionModel>();
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<List<SubscriptionModel>>(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));
Expand All @@ -151,51 +161,58 @@ 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))
{
string correctedPath = System.IO.Path.GetFullPath(model.SavePath);
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<SubscriptionSubTaskModel> { };
SubscriptionManager.Instance.CheckSubscriptionOne(_lastSubscriptionModel, Aria2ApiManager.Instance.CurrentRpc);
Task.Run(async () =>
{
foreach (var item in _selectedItems)
{
item.AlreadyAddedDownloadModel = new ObservableCollection<SubscriptionSubTaskModel> { };
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)
{

}
}
Expand Down

0 comments on commit 4fae5fd

Please sign in to comment.