Skip to content

Commit 4021bcd

Browse files
authored
Merge pull request PCL-Community#273 from PCL-Community/feat#236
feat: mod manage sort
2 parents 0c27e0e + 27a2046 commit 4021bcd

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-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

+95
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.FileName)
103104
Catch ex As Exception
104105
Log(ex, "加载 Mod 列表 UI 失败", LogLevel.Feedback)
105106
End Try
@@ -109,6 +110,7 @@
109110
Dim NewItem As New MyLocalModItem With {.SnapsToDevicePixels = True, .Entry = Entry,
110111
.ButtonHandler = AddressOf McModContent, .Checked = SelectedMods.Contains(Entry.RawFileName)}
111112
AddHandler Entry.OnCompUpdate, AddressOf NewItem.Refresh
113+
'AddHandler Entry.OnCompUpdate, Sub() RunInUi(Sub() DoSort())
112114
NewItem.Refresh()
113115
AniControlEnabled -= 1
114116
Return NewItem
@@ -486,10 +488,103 @@ Install:
486488
Private Sub ChangeFilter(sender As MyRadioButton, raiseByMouse As Boolean) Handles BtnFilterAll.Check, BtnFilterCanUpdate.Check, BtnFilterDisabled.Check, BtnFilterEnabled.Check, BtnFilterError.Check
487489
Filter = sender.Tag
488490
RefreshUI()
491+
DoSort()
489492
End Sub
490493

491494
#End Region
492495

496+
#Region "排序"
497+
Private CurrentSortMethod As SortMethod = SortMethod.FileName
498+
499+
Private Sub SetSortMethod(Target As SortMethod)
500+
CurrentSortMethod = Target
501+
BtnSort.Text = $"排序:{GetSortName(Target)}"
502+
RefreshUI()
503+
DoSort()
504+
End Sub
505+
506+
Private Enum SortMethod
507+
FileName
508+
ModName
509+
TagNums
510+
CreateTime
511+
End Enum
512+
513+
Private Function GetSortName(Method As SortMethod) As String
514+
Select Case Method
515+
Case SortMethod.FileName : Return "文件名"
516+
Case SortMethod.ModName : Return "模组名称"
517+
Case SortMethod.TagNums : Return "标签数量"
518+
Case SortMethod.CreateTime : Return "加入时间"
519+
End Select
520+
Return ""
521+
End Function
522+
523+
Private Sub BtnSortClick(sender As Object, e As RouteEventArgs) Handles BtnSort.Click
524+
Dim Body As New ContextMenu
525+
For Each i As SortMethod In [Enum].GetValues(GetType(SortMethod))
526+
Dim Item As New MyMenuItem
527+
Item.Header = GetSortName(i)
528+
AddHandler Item.Click, Sub()
529+
SetSortMethod(i)
530+
End Sub
531+
Body.Items.Add(Item)
532+
Next
533+
Body.PlacementTarget = sender
534+
Body.Placement = Primitives.PlacementMode.Bottom
535+
Body.IsOpen = True
536+
End Sub
537+
538+
Private ReadOnly SortLock As New Object
539+
Private Sub DoSort()
540+
SyncLock SortLock
541+
If PanList Is Nothing Then Exit Sub
542+
Dim Length As Integer = PanList.Children.Count
543+
Dim MS = GetSortMethod(CurrentSortMethod)
544+
For i As Integer = 0 To Length - 1
545+
For j As Integer = 0 To Length - i - 2
546+
Dim Comp1 As MyLocalModItem = PanList.Children(j)
547+
Dim Comp2 As MyLocalModItem = PanList.Children(j + 1)
548+
549+
If CurrentSortMethod = SortMethod.TagNums AndAlso (Comp1.Entry.Comp Is Nothing OrElse Comp2.Entry.Comp Is Nothing) Then
550+
Continue For
551+
End If
552+
If MS(Comp2.Entry, Comp1.Entry) Then
553+
' 交换元素
554+
Dim temp1 As MyLocalModItem = PanList.Children(j)
555+
Dim temp2 As MyLocalModItem = PanList.Children(j + 1)
556+
PanList.Children.Remove(temp1)
557+
PanList.Children.Remove(temp2)
558+
PanList.Children.Insert(j, temp2)
559+
PanList.Children.Insert(j + 1, temp1)
560+
End If
561+
Next
562+
Next
563+
End SyncLock
564+
End Sub
565+
566+
Private Function GetSortMethod(Method As SortMethod) As Func(Of McMod, McMod, Boolean)
567+
Select Case Method
568+
Case SortMethod.FileName
569+
Return Function(a As McMod, b As McMod) As Boolean
570+
Return StrComp(a.FileName, b.FileName) < 0
571+
End Function
572+
Case SortMethod.ModName
573+
Return Function(a As McMod, b As McMod) As Boolean
574+
Return StrComp(a.Name, b.Name) < 0
575+
End Function
576+
Case SortMethod.TagNums
577+
Return Function(a As McMod, b As McMod) As Boolean
578+
Return a.Comp.Tags.Count > b.Comp.Tags.Count
579+
End Function
580+
Case SortMethod.CreateTime
581+
Return Function(a As McMod, b As McMod) As Boolean
582+
Return (New FileInfo(a.Path)).CreationTime > (New FileInfo(b.Path)).CreationTime
583+
End Function
584+
End Select
585+
End Function
586+
#End Region
587+
493588
#Region "下边栏"
494589

495590
'启用 / 禁用

0 commit comments

Comments
 (0)