|
100 | 100 | Filter = FilterType.All
|
101 | 101 | SearchBox.Text = "" '这会触发结果刷新,所以需要在 ModItems 更新之后,详见 #3124 的视频
|
102 | 102 | RefreshUI()
|
| 103 | + SetSortMethod(SortMethod.ModName) |
103 | 104 | Catch ex As Exception
|
104 | 105 | Log(ex, "加载 Mod 列表 UI 失败", LogLevel.Feedback)
|
105 | 106 | End Try
|
@@ -486,10 +487,106 @@ Install:
|
486 | 487 | Private Sub ChangeFilter(sender As MyRadioButton, raiseByMouse As Boolean) Handles BtnFilterAll.Check, BtnFilterCanUpdate.Check, BtnFilterDisabled.Check, BtnFilterEnabled.Check, BtnFilterError.Check
|
487 | 488 | Filter = sender.Tag
|
488 | 489 | RefreshUI()
|
| 490 | + DoSort() |
489 | 491 | End Sub
|
490 | 492 |
|
491 | 493 | #End Region
|
492 | 494 |
|
| 495 | +#Region "排序" |
| 496 | + Private CurrentSortMethod As SortMethod = SortMethod.FileName |
| 497 | + |
| 498 | + Private Sub SetSortMethod(Target As SortMethod) |
| 499 | + CurrentSortMethod = Target |
| 500 | + BtnSort.Text = $"排序:{GetSortName(Target)}" |
| 501 | + RefreshUI() |
| 502 | + DoSort() |
| 503 | + End Sub |
| 504 | + |
| 505 | + Private Enum SortMethod |
| 506 | + FileName |
| 507 | + ModName |
| 508 | + TagNums |
| 509 | + CreateTime |
| 510 | + End Enum |
| 511 | + |
| 512 | + Private Function GetSortName(Method As SortMethod) As String |
| 513 | + Select Case Method |
| 514 | + Case SortMethod.FileName : Return "文件名" |
| 515 | + Case SortMethod.ModName : Return "模组名称" |
| 516 | + Case SortMethod.TagNums : Return "标签数量" |
| 517 | + Case SortMethod.CreateTime : Return "加入时间" |
| 518 | + End Select |
| 519 | + Return "" |
| 520 | + End Function |
| 521 | + |
| 522 | + Private Sub BtnSortClick(sender As Object, e As RouteEventArgs) Handles BtnSort.Click |
| 523 | + Dim Body As New ContextMenu |
| 524 | + For Each i As SortMethod In [Enum].GetValues(GetType(SortMethod)) |
| 525 | + Dim Item As New MyMenuItem |
| 526 | + Item.Header = GetSortName(i) |
| 527 | + AddHandler Item.Click, Sub() |
| 528 | + SetSortMethod(i) |
| 529 | + End Sub |
| 530 | + Body.Items.Add(Item) |
| 531 | + Next |
| 532 | + Body.PlacementTarget = sender |
| 533 | + Body.Placement = Primitives.PlacementMode.Bottom |
| 534 | + Body.IsOpen = True |
| 535 | + End Sub |
| 536 | + |
| 537 | + Private ReadOnly SortLock As New Object |
| 538 | + Private Sub DoSort() |
| 539 | + SyncLock SortLock |
| 540 | + If PanList Is Nothing OrElse PanList.Children.Count < 2 Then Exit Sub |
| 541 | + |
| 542 | + ' 将子元素转换为可排序的列表 |
| 543 | + Dim items = PanList.Children.OfType(Of MyLocalModItem)().ToList() |
| 544 | + Dim Method = GetSortMethod(CurrentSortMethod) |
| 545 | + |
| 546 | + ' 根据排序类型处理特殊逻辑 |
| 547 | + If CurrentSortMethod = SortMethod.TagNums Then |
| 548 | + ' 分离有效和无效项(保持原始相对顺序) |
| 549 | + Dim valid = items.Where(Function(i) i.Entry.Comp IsNot Nothing).ToList() |
| 550 | + Dim invalid = items.Except(valid).ToList() |
| 551 | + |
| 552 | + ' 仅对有效项进行排序 |
| 553 | + valid.Sort(Function(x, y) Method(y.Entry, x.Entry)) |
| 554 | + |
| 555 | + ' 合并保持无效项的原始顺序 |
| 556 | + items = valid.Concat(invalid).ToList() |
| 557 | + Else |
| 558 | + ' 直接进行高效排序 |
| 559 | + items.Sort(Function(x, y) Method(y.Entry, x.Entry)) |
| 560 | + End If |
| 561 | + |
| 562 | + ' 批量更新UI元素 |
| 563 | + PanList.Children.Clear() |
| 564 | + items.ForEach(Sub(i) PanList.Children.Add(i)) |
| 565 | + End SyncLock |
| 566 | + End Sub |
| 567 | + |
| 568 | + Private Function GetSortMethod(Method As SortMethod) As Func(Of McMod, McMod, Integer) |
| 569 | + Select Case Method |
| 570 | + Case SortMethod.FileName |
| 571 | + Return Function(a As McMod, b As McMod) As Integer |
| 572 | + Return -StrComp(a.FileName, b.FileName) |
| 573 | + End Function |
| 574 | + Case SortMethod.ModName |
| 575 | + Return Function(a As McMod, b As McMod) As Integer |
| 576 | + Return -StrComp(a.Name, b.Name) |
| 577 | + End Function |
| 578 | + Case SortMethod.TagNums |
| 579 | + Return Function(a As McMod, b As McMod) As Integer |
| 580 | + Return a.Comp.Tags.Count - b.Comp.Tags.Count |
| 581 | + End Function |
| 582 | + Case SortMethod.CreateTime |
| 583 | + Return Function(a As McMod, b As McMod) As Integer |
| 584 | + Return If((New FileInfo(a.Path)).CreationTime > (New FileInfo(b.Path)).CreationTime, 1, -1) |
| 585 | + End Function |
| 586 | + End Select |
| 587 | + End Function |
| 588 | +#End Region |
| 589 | + |
493 | 590 | #Region "下边栏"
|
494 | 591 |
|
495 | 592 | '启用 / 禁用
|
|
0 commit comments