Skip to content

Commit d2bb2b7

Browse files
committed
feat: mod sort
1 parent 70636a9 commit d2bb2b7

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

Plain Craft Launcher 2/Pages/PageVersion/PageVersionMod.xaml

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<local:MyRadioButton Tag="3" ColorType="Highlight" VerticalAlignment="Center" Margin="2,0" Text="可更新" x:Name="BtnFilterCanUpdate" />
3434
<local:MyRadioButton Tag="4" ColorType="Highlight" VerticalAlignment="Center" Margin="2,0" Text="错误" x:Name="BtnFilterError" />
3535
</StackPanel>
36+
<StackPanel Margin="0,13,15,0" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right" Height="28">
37+
<local:MyIconTextButton x:Name="BtnSort" Text="排序:文件名" Logo="M673.158095 252.830476v634.026667h-71.923809V172.251429c0-10.142476 4.022857-19.846095 11.215238-26.989715a38.546286 38.546286 0 0 1 54.296381 0L926.47619 403.553524l-50.883047 50.566095-202.435048-201.289143zM350.841905 768.121905V134.095238h71.923809v719.823238l-0.902095 6.460953c-1.26781 8.045714-2.56 9.728-9.703619 18.992761l-8.045714 4.388572c-17.92 9.45981-20.577524 7.996952-43.154286-4.388572L97.52381 617.374476l50.883047-50.541714 202.435048 201.264762z"/>
38+
</StackPanel>
3639
<StackPanel Margin="20,48,18,22" Name="PanList" VerticalAlignment="Top" />
3740
</local:MyCard>
3841
</StackPanel>

Plain Craft Launcher 2/Pages/PageVersion/PageVersionMod.xaml.vb

+97
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
Filter = FilterType.All
101101
SearchBox.Text = "" '这会触发结果刷新,所以需要在 ModItems 更新之后,详见 #3124 的视频
102102
RefreshUI()
103+
SetSortMethod(SortMethod.ModName)
103104
Catch ex As Exception
104105
Log(ex, "加载 Mod 列表 UI 失败", LogLevel.Feedback)
105106
End Try
@@ -486,10 +487,106 @@ Install:
486487
Private Sub ChangeFilter(sender As MyRadioButton, raiseByMouse As Boolean) Handles BtnFilterAll.Check, BtnFilterCanUpdate.Check, BtnFilterDisabled.Check, BtnFilterEnabled.Check, BtnFilterError.Check
487488
Filter = sender.Tag
488489
RefreshUI()
490+
DoSort()
489491
End Sub
490492

491493
#End Region
492494

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+
493590
#Region "下边栏"
494591

495592
'启用 / 禁用

0 commit comments

Comments
 (0)