MyCompItem.xaml.vb 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. Public Class MyCompItem
  2. Public Uuid As Integer = GetUuid()
  3. #Region "Logo"
  4. Private _Logo As String = ""
  5. Public Property Logo As String
  6. Get
  7. Return _Logo
  8. End Get
  9. Set(value As String)
  10. If _Logo = value OrElse value Is Nothing Then Exit Property
  11. _Logo = value
  12. Dim FileAddress = PathTemp & "CompLogo\" & GetHash(_Logo) & ".png"
  13. Try
  14. If _Logo.ToLower.StartsWith("http") Then
  15. '网络图片
  16. If File.Exists(FileAddress) Then
  17. PathLogo.Source = New MyBitmap(FileAddress)
  18. ElseIf _Logo.ToLower.EndsWith(".webp") Then 'Modrinth 林业 Mod 使用了不支持的 WebP 格式 Logo
  19. Log($"[Comp] 发现不支持的 WebP 格式图标,已更改为默认图标:{_Logo}")
  20. PathLogo.Source = New MyBitmap("pack://application:,,,/images/Icons/NoIcon.png")
  21. Else
  22. PathLogo.Source = New MyBitmap("pack://application:,,,/images/Icons/NoIcon.png")
  23. RunInNewThread(Sub() LogoLoader(FileAddress), "Comp Logo Loader " & Uuid & "#", ThreadPriority.BelowNormal)
  24. End If
  25. Else
  26. '位图
  27. PathLogo.Source = New MyBitmap(_Logo)
  28. End If
  29. Catch ex As IOException
  30. Log(ex, "加载资源工程图标时读取失败(" & FileAddress & ")")
  31. Catch ex As ArgumentException
  32. '考虑缓存的图片本身可能有误
  33. Log(ex, "可视化资源工程图标失败(" & FileAddress & ")")
  34. Try
  35. File.Delete(FileAddress)
  36. Log("[Comp] 已清理损坏的资源工程图标:" & FileAddress)
  37. Catch exx As Exception
  38. Log(exx, "清理损坏的资源工程图标缓存失败(" & FileAddress & ")", LogLevel.Hint)
  39. End Try
  40. Catch ex As Exception
  41. Log(ex, "加载资源工程图标失败(" & value & ")")
  42. End Try
  43. End Set
  44. End Property
  45. Private Sub LogoLoader(Address As String)
  46. Dim Retry As Boolean = False
  47. Dim DownloadEnd As String = GetUuid()
  48. RetryStart:
  49. Try
  50. NetDownload(_Logo, Address & DownloadEnd)
  51. Dim LoadError As Exception = Nothing
  52. RunInUiWait(Sub()
  53. Try
  54. '在地址更换时取消加载
  55. If Not Address = PathTemp & "CompLogo\" & GetHash(_Logo) & ".png" Then Exit Sub
  56. '在完成正常加载后才保存缓存图片
  57. PathLogo.Source = New MyBitmap(Address & DownloadEnd)
  58. Catch ex As Exception
  59. Log(ex, "读取资源工程图标失败(" & Address & ")")
  60. File.Delete(Address & DownloadEnd)
  61. LoadError = ex
  62. End Try
  63. End Sub)
  64. If LoadError IsNot Nothing Then Throw LoadError
  65. If File.Exists(Address) Then
  66. File.Delete(Address & DownloadEnd)
  67. Else
  68. FileIO.FileSystem.MoveFile(Address & DownloadEnd, Address)
  69. End If
  70. Catch ex As Exception
  71. If Not Retry Then
  72. Retry = True
  73. GoTo RetryStart
  74. Else
  75. Log(ex, $"下载资源工程图标失败({_Logo})")
  76. RunInUi(Sub() PathLogo.Source = New MyBitmap("pack://application:,,,/images/Icons/NoIcon.png"))
  77. End If
  78. End Try
  79. End Sub
  80. #End Region
  81. #Region "点击"
  82. '触发点击事件
  83. Public Event Click(sender As Object, e As MouseButtonEventArgs)
  84. Private Sub Button_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles Me.PreviewMouseLeftButtonUp
  85. If IsMouseDown Then
  86. RaiseEvent Click(sender, e)
  87. If e.Handled Then Exit Sub
  88. Log("[Control] 按下资源工程列表项:" & LabTitle.Text)
  89. End If
  90. End Sub
  91. Private Sub ProjectClick(sender As MyCompItem, e As EventArgs) Handles Me.Click
  92. '记录当前展开的卡片标题(#2712)
  93. Dim Titles As New List(Of String)
  94. If FrmMain.PageCurrent.Page = FormMain.PageType.CompDetail Then
  95. For Each Card As MyCard In FrmDownloadCompDetail.PanMain.Children
  96. If Card.Title <> "" AndAlso Not Card.IsSwaped Then Titles.Add(Card.Title)
  97. Next
  98. Log("[Comp] 记录当前已展开的卡片:" & String.Join("、", Titles))
  99. FrmMain.PageCurrent.Additional(1) = Titles
  100. End If
  101. FrmMain.PageChange(New FormMain.PageStackData With {.Page = FormMain.PageType.CompDetail, .Additional = {sender.Tag, New List(Of String)}})
  102. End Sub
  103. '鼠标点击判定
  104. Private IsMouseDown As Boolean = False
  105. Private Sub Button_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles Me.PreviewMouseLeftButtonDown
  106. If IsMouseOver Then IsMouseDown = True
  107. End Sub
  108. Private Sub Button_MouseLeave(sender As Object, e As Object) Handles Me.MouseLeave, Me.PreviewMouseLeftButtonUp
  109. IsMouseDown = False
  110. End Sub
  111. #End Region
  112. #Region "后加载指向背景"
  113. Private _RectBack As Border = Nothing
  114. Public ReadOnly Property RectBack As Border
  115. Get
  116. If _RectBack Is Nothing Then
  117. Dim Rect As New Border With {
  118. .Name = "RectBack",
  119. .CornerRadius = New CornerRadius(3),
  120. .RenderTransform = New ScaleTransform(0.8, 0.8),
  121. .RenderTransformOrigin = New Point(0.5, 0.5),
  122. .BorderThickness = New Thickness(GetWPFSize(1)),
  123. .SnapsToDevicePixels = True,
  124. .IsHitTestVisible = False,
  125. .Opacity = 0
  126. }
  127. Rect.SetResourceReference(Border.BackgroundProperty, "ColorBrush7")
  128. Rect.SetResourceReference(Border.BorderBrushProperty, "ColorBrush6")
  129. SetColumnSpan(Rect, 999)
  130. SetRowSpan(Rect, 999)
  131. Children.Insert(0, Rect)
  132. _RectBack = Rect
  133. '<!--<Border x:Name = "RectBack" CornerRadius="3" RenderTransformOrigin="0.5,0.5" SnapsToDevicePixels="True"
  134. 'IsHitTestVisible = "False" Opacity="0" BorderThickness="1"
  135. 'Grid.ColumnSpan = "4" Background="{DynamicResource ColorBrush7}" BorderBrush="{DynamicResource ColorBrush6}"/>-->
  136. End If
  137. Return _RectBack
  138. End Get
  139. End Property
  140. #End Region
  141. Private StateLast As String
  142. Public HasAnimation As Boolean = True
  143. Public Sub RefreshColor(sender As Object, e As EventArgs) Handles Me.MouseEnter, Me.MouseLeave, Me.MouseLeftButtonDown, Me.MouseLeftButtonUp
  144. If Not HasAnimation Then Exit Sub
  145. '判断当前颜色
  146. Dim StateNew As String, Time As Integer
  147. If IsMouseOver Then
  148. If IsMouseDown Then
  149. StateNew = "MouseDown"
  150. Time = 120
  151. Else
  152. StateNew = "MouseOver"
  153. Time = 120
  154. End If
  155. Else
  156. StateNew = "Idle"
  157. Time = 180
  158. End If
  159. If StateLast = StateNew Then Exit Sub
  160. StateLast = StateNew
  161. '触发颜色动画
  162. If IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  163. '有动画
  164. Dim Ani As New List(Of AniData)
  165. If IsMouseOver Then
  166. Ani.AddRange({
  167. AaColor(RectBack, Border.BackgroundProperty, If(IsMouseDown, "ColorBrush6", "ColorBrushBg1"), Time),
  168. AaOpacity(RectBack, 1 - RectBack.Opacity, Time,, New AniEaseOutFluent)
  169. })
  170. If IsMouseDown Then
  171. Ani.Add(AaScaleTransform(RectBack, 0.996 - CType(RectBack.RenderTransform, ScaleTransform).ScaleX, Time * 1.2,, New AniEaseOutFluent))
  172. Else
  173. Ani.Add(AaScaleTransform(RectBack, 1 - CType(RectBack.RenderTransform, ScaleTransform).ScaleX, Time * 1.2,, New AniEaseOutFluent))
  174. End If
  175. Else
  176. Ani.AddRange({
  177. AaOpacity(RectBack, -RectBack.Opacity, Time),
  178. AaColor(RectBack, Border.BackgroundProperty, If(IsMouseDown, "ColorBrush6", "ColorBrush7"), Time),
  179. AaScaleTransform(RectBack, 0.996 - CType(RectBack.RenderTransform, ScaleTransform).ScaleX, Time,, New AniEaseOutFluent),
  180. AaScaleTransform(RectBack, -0.196, 1,,, True)
  181. })
  182. End If
  183. AniStart(Ani, "CompItem Color " & Uuid)
  184. Else
  185. '无动画
  186. AniStop("CompItem Color " & Uuid)
  187. If _RectBack IsNot Nothing Then RectBack.Opacity = 0
  188. End If
  189. End Sub
  190. '指向时扩展描述
  191. Private Sub LabInfo_MouseEnter(sender As Object, e As MouseEventArgs) Handles LabInfo.MouseEnter
  192. If IsTextTrimmed(LabInfo) Then
  193. ToolTipInfo.Content = LabInfo.Text
  194. ToolTipInfo.Width = LabInfo.ActualWidth + 25
  195. LabInfo.ToolTip = ToolTipInfo
  196. Else
  197. LabInfo.ToolTip = Nothing
  198. End If
  199. End Sub
  200. Private Function IsTextTrimmed(textBlock As TextBlock) As Boolean
  201. Dim typeface As New Typeface(textBlock.FontFamily, textBlock.FontStyle, textBlock.FontWeight, textBlock.FontStretch)
  202. Dim formattedText As New FormattedText(textBlock.Text, Thread.CurrentThread.CurrentCulture, textBlock.FlowDirection, typeface, textBlock.FontSize, textBlock.Foreground, DPI)
  203. Return formattedText.Width > textBlock.ActualWidth
  204. End Function
  205. End Class