MyCard.vb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. Public Class MyCard
  2. '控件
  3. Inherits Grid
  4. Private ReadOnly MainGrid As Grid
  5. Public ReadOnly Property MainChrome As MyDropShadow
  6. Private ReadOnly MainBorder As Border
  7. Public Property BorderChild As UIElement
  8. Get
  9. Return MainBorder.Child
  10. End Get
  11. Set(value As UIElement)
  12. MainBorder.Child = value
  13. End Set
  14. End Property
  15. Private _MainTextBlock As TextBlock
  16. Public Property MainTextBlock As TextBlock
  17. Get
  18. Init() '当父级触发 Loaded 时,本卡片可能尚未触发 Loaded(该事件从父级向子级调用),因此这会是 null。手动触发以确保控件已加载。
  19. Return _MainTextBlock
  20. End Get
  21. Set(value As TextBlock)
  22. _MainTextBlock = value
  23. End Set
  24. End Property
  25. Private _MainSwap As Shapes.Path
  26. Public Property MainSwap As Shapes.Path
  27. Get
  28. Init()
  29. Return _MainSwap
  30. End Get
  31. Set(value As Shapes.Path)
  32. _MainSwap = value
  33. End Set
  34. End Property
  35. '属性
  36. Public Uuid As Integer = GetUuid()
  37. Public ReadOnly Property Inlines As InlineCollection
  38. Get
  39. Return MainTextBlock.Inlines
  40. End Get
  41. End Property
  42. Public Property CornerRadius As CornerRadius
  43. Get
  44. Return MainChrome.CornerRadius
  45. End Get
  46. Set(value As CornerRadius)
  47. MainChrome.CornerRadius = value
  48. MainBorder.CornerRadius = value
  49. End Set
  50. End Property
  51. Public Property Title As String
  52. Get
  53. Return GetValue(TitleProperty)
  54. End Get
  55. Set(value As String)
  56. SetValue(TitleProperty, value)
  57. If _MainTextBlock IsNot Nothing Then MainTextBlock.Text = value
  58. End Set
  59. End Property
  60. Public Shared ReadOnly TitleProperty As DependencyProperty = DependencyProperty.Register("Title", GetType(String), GetType(MyCard), New PropertyMetadata(""))
  61. 'UI 建立
  62. Public Sub New()
  63. MainChrome = New MyDropShadow With {
  64. .Margin = New Thickness(-3, -3, -3, -3 - GetWPFSize(1)), .ShadowRadius = 3, .Opacity = DropShadowIdleOpacity, .CornerRadius = New CornerRadius(5)}
  65. MainChrome.SetResourceReference(MyDropShadow.ColorProperty, "ColorObject1")
  66. Children.Insert(0, MainChrome)
  67. MainBorder = New Border With {.Background = New SolidColorBrush(Color.FromArgb(245, 255, 255, 255)), .CornerRadius = New CornerRadius(5), .IsHitTestVisible = False}
  68. Children.Insert(1, MainBorder)
  69. MainGrid = New Grid
  70. Children.Add(MainGrid)
  71. End Sub
  72. Private IsLoad As Boolean = False
  73. Private Sub Init() Handles Me.Loaded
  74. If IsLoad Then Return
  75. IsLoad = True
  76. '初次加载限定
  77. If MainTextBlock Is Nothing Then
  78. MainTextBlock = New TextBlock With {.HorizontalAlignment = HorizontalAlignment.Left, .VerticalAlignment = VerticalAlignment.Top, .Margin = New Thickness(15, 12, 0, 0), .FontWeight = FontWeights.Bold, .FontSize = 13, .IsHitTestVisible = False}
  79. MainTextBlock.SetResourceReference(TextBlock.ForegroundProperty, "ColorBrush1")
  80. MainTextBlock.SetBinding(TextBlock.TextProperty, New Binding("Title") With {.Source = Me, .Mode = BindingMode.OneWay})
  81. MainGrid.Children.Add(MainTextBlock)
  82. End If
  83. If CanSwap OrElse SwapControl IsNot Nothing Then
  84. If SwapControl Is Nothing AndAlso Children.Count > 3 Then SwapControl = Children(3)
  85. MainSwap = New Shapes.Path With {.HorizontalAlignment = HorizontalAlignment.Right, .Stretch = Stretch.Uniform, .Height = 6, .Width = 10, .VerticalAlignment = VerticalAlignment.Top, .Margin = New Thickness(0, 17, 16, 0), .Data = New GeometryConverter().ConvertFromString("M2,4 l-2,2 10,10 10,-10 -2,-2 -8,8 -8,-8 z"), .RenderTransform = New RotateTransform(180), .RenderTransformOrigin = New Point(0.5, 0.5)}
  86. MainSwap.SetResourceReference(Shapes.Path.FillProperty, "ColorBrush1")
  87. MainGrid.Children.Add(MainSwap)
  88. End If
  89. '改变默认的折叠
  90. If IsSwapped AndAlso SwapControl IsNot Nothing Then
  91. MainSwap.RenderTransform = New RotateTransform(If(SwapLogoRight, 270, 0))
  92. SwapControl.Visibility = Visibility.Collapsed
  93. '取消由于高度变化被迫触发的高度动画
  94. Dim RawUseAnimation As Boolean = UseAnimation
  95. UseAnimation = False
  96. Height = SwapedHeight
  97. AniStop("MyCard Height " & Uuid)
  98. IsHeightAnimating = False
  99. RunInUi(Sub() UseAnimation = RawUseAnimation, True)
  100. End If
  101. End Sub
  102. Public Sub StackInstall()
  103. StackInstall(SwapControl, SwapType, Title)
  104. TriggerForceResize()
  105. End Sub
  106. Public Shared Sub StackInstall(ByRef Stack As StackPanel, Type As Integer, Optional CardTitle As String = "")
  107. '这一部分的代码是好几年前留下的究极屎坑,当时还不知道该咋正确调用这种方法,就写了这么一坨屎
  108. '但是现在……反正勉强能用……懒得改了就这样吧.jpg
  109. '别骂了别骂了.jpg
  110. If Stack.Tag Is Nothing Then Return
  111. '排序
  112. Dim HasDuplicates As Boolean = False
  113. Select Case Type
  114. Case 3
  115. Stack.Tag = CType(Stack.Tag, List(Of DlOptiFineListEntry)).SortByComparison(Function(a, b) CompareVersionGE(a.DisplayName, b.DisplayName))
  116. Case 4, 10
  117. Stack.Tag = CType(Stack.Tag, List(Of DlLiteLoaderListEntry)).SortByComparison(Function(a, b) CompareVersionGE(a.Inherit, b.Inherit))
  118. Case 6
  119. Stack.Tag = CType(Stack.Tag, List(Of DlForgeVersionEntry)).OrderByDescending(Function(a) a).ToList
  120. Case 8, 9
  121. Stack.Tag = CType(Stack.Tag, List(Of ResourceVersion)).SortByComparison(Function(a, b) a.ReleaseDate > b.ReleaseDate)
  122. HasDuplicates =
  123. CType(Stack.Tag, List(Of ResourceVersion)).DistinctBy(Function(a) a.Display).Count <>
  124. CType(Stack.Tag, List(Of ResourceVersion)).Count
  125. End Select
  126. '控件转换
  127. Select Case Type
  128. Case 6
  129. ForgeDownloadListItemPreload(Stack, Stack.Tag, AddressOf ForgeSave_Click, True)
  130. Case 8
  131. PageDownloadResourceDetail.ResourceFilesCardPreload(Stack, Stack.Tag)
  132. End Select
  133. '实现控件虚拟化
  134. For Each Data As Object In Stack.Tag
  135. Select Case Type
  136. Case 0
  137. Stack.Children.Add(CType(Data, McInstance).ToListItem(AddressOf PageSelectRight.McInstanceListContent))
  138. Case 2
  139. Stack.Children.Add(McDownloadListItem(Data, AddressOf McDownloadMenuSave, True))
  140. Case 3
  141. Stack.Children.Add(OptiFineDownloadListItem(Data, AddressOf OptiFineSave_Click, True))
  142. Case 6
  143. Stack.Children.Add(ForgeDownloadListItem(Data, AddressOf ForgeSave_Click, True))
  144. Case 7
  145. '不能使用 AddressOf,这导致了 #535,原因完全不明,疑似是编译器 Bug
  146. Stack.Children.Add(McDownloadListItem(Data, Sub(sender, e) FrmDownloadInstall.MinecraftSelected(sender, e), False))
  147. Case 8
  148. '若存在重复的版本名,则显示文件名而非版本名(#1344)
  149. Stack.Children.Add(CType(Data, ResourceVersion).ToListItem(
  150. AddressOf FrmDownloadResourceDetail.Save_Click, IsBadDisplay:=HasDuplicates))
  151. Case 9
  152. '若存在重复的版本名,则显示文件名而非版本名(#1344)
  153. Stack.Children.Add(CType(Data, ResourceVersion).ToListItem(
  154. AddressOf FrmDownloadResourceDetail.Install_Click, AddressOf FrmDownloadResourceDetail.Save_Click, IsBadDisplay:=HasDuplicates))
  155. Case 10
  156. Stack.Children.Add(LiteLoaderDownloadListItem(Data, AddressOf LiteLoaderSave_Click, True))
  157. Case 11
  158. Stack.Children.Add(CType(Data, HelpEntry).ToListItem)
  159. Case 12
  160. Stack.Children.Add(FabricDownloadListItem(CType(Data, JObject), AddressOf FrmDownloadInstall.Fabric_Selected))
  161. Case 13
  162. Stack.Children.Add(NeoForgeDownloadListItem(Data, AddressOf NeoForgeSave_Click, True))
  163. Case Else
  164. Logger.Error($"未知的虚拟化种类:{Type}")
  165. End Select
  166. Next
  167. Stack.Children.Add(New FrameworkElement With {.Height = 18}) '下边距,同时适应折叠
  168. Stack.Tag = Nothing
  169. End Sub
  170. '动画
  171. Private Const DropShadowIdleOpacity As Double = 0.07
  172. Private Const DropShadowHoverOpacity As Double = 0.4
  173. Public Property HasMouseAnimation As Boolean = True
  174. Private Sub MyCard_MouseEnter(sender As Object, e As MouseEventArgs) Handles Me.MouseEnter
  175. If Not HasMouseAnimation Then Return
  176. Dim AniList As New List(Of AniData)
  177. If Not IsNothing(MainTextBlock) Then AniList.Add(AaColor(MainTextBlock, TextBlock.ForegroundProperty, "ColorBrush2", 90))
  178. If Not IsNothing(MainSwap) Then AniList.Add(AaColor(MainSwap, Shapes.Path.FillProperty, "ColorBrush2", 90))
  179. AniList.AddRange({
  180. AaColor(MainChrome, MyDropShadow.ColorProperty, "ColorObject4", 90),
  181. AaOpacity(MainChrome, DropShadowHoverOpacity - MainChrome.Opacity, 90)
  182. })
  183. AniStart(AniList, "MyCard Mouse " & Uuid)
  184. End Sub
  185. Private Sub MyCard_MouseLeave(sender As Object, e As MouseEventArgs) Handles Me.MouseLeave
  186. If Not HasMouseAnimation Then Return
  187. Dim AniList As New List(Of AniData)
  188. If Not IsNothing(MainTextBlock) Then AniList.Add(AaColor(MainTextBlock, TextBlock.ForegroundProperty, "ColorBrush1", 90))
  189. If Not IsNothing(MainSwap) Then AniList.Add(AaColor(MainSwap, Shapes.Path.FillProperty, "ColorBrush1", 90))
  190. AniList.AddRange({
  191. AaColor(MainChrome, MyDropShadow.ColorProperty, "ColorObject1", 90),
  192. AaOpacity(MainChrome, DropShadowIdleOpacity - MainChrome.Opacity, 90)
  193. })
  194. AniStart(AniList, "MyCard Mouse " & Uuid)
  195. End Sub
  196. #Region "高度改变动画"
  197. ''' <summary>
  198. ''' 是否启用高度改变动画。
  199. ''' </summary>
  200. Public Property UseAnimation As Boolean = True
  201. Private IsHeightAnimating As Boolean = False
  202. Private ActualUsedHeight As Double '回滚实际高度(例如 NaN)
  203. Private Sub MySizeChanged(sender As Object, e As SizeChangedEventArgs) Handles Me.SizeChanged
  204. If Not UseAnimation Then Return
  205. Dim DeltaHeight As Double = If(IsSwapped, SwapedHeight, e.NewSize.Height) - e.PreviousSize.Height
  206. '卡片的进入时动画已被页面通用切换动画替代
  207. If e.PreviousSize.Height = 0 OrElse IsHeightAnimating OrElse Math.Abs(DeltaHeight) < 1 OrElse ActualHeight = 0 Then Return
  208. StartHeightAnimation(DeltaHeight, e.PreviousSize.Height, False)
  209. End Sub
  210. Private Sub StartHeightAnimation(Delta As Double, PreviousHeight As Double, IsLoadAnimation As Boolean)
  211. If IsHeightAnimating OrElse FrmMain Is Nothing Then Return '避免 XAML 设计器出错
  212. Dim AnimList As New List(Of AniData)
  213. Dim AbsDelta = Math.Abs(Delta)
  214. If AbsDelta <= 800 Then
  215. '短距离,直接使用 150ms 的缓动动画
  216. AnimList.Add(AaHeight(Me, Delta, 150,, New AniEaseOutFluent(AniEasePower.ExtraStrong)))
  217. Else
  218. Dim EaseLength As Integer, EaseTime As Integer
  219. Dim InitSpeed As Integer '到达缓动区前的初速度
  220. If Delta < 0 AndAlso AbsDelta - EaseLength > 5000 * 0.1 Then
  221. '收回距离过长 (>0.1s),强制以 100ms 完成匀速段,然后让减速段更长
  222. EaseLength = 200
  223. EaseTime = 150
  224. InitSpeed = (AbsDelta - EaseLength) / 0.1
  225. ElseIf Delta > 0 AndAlso AbsDelta - EaseLength > 5000 * 0.6 Then
  226. '展开距离过长 (>0.6s),以 5000 速度展示 300ms 匀速段,剩下的距离全部归入减速段
  227. InitSpeed = 5000
  228. EaseLength = AbsDelta - InitSpeed * 0.3
  229. EaseTime = 400
  230. Else
  231. '中程,匀速地快速展开(或收回)
  232. EaseLength = 150
  233. EaseTime = 200
  234. InitSpeed = 4000
  235. End If
  236. '匀速段
  237. AnimList.Add(AaHeight(Me, (AbsDelta - EaseLength) * Math.Sign(Delta),
  238. (AbsDelta - EaseLength) / InitSpeed * 1000))
  239. '减速段
  240. AnimList.Add(AaHeight(Me, EaseLength * Math.Sign(Delta),
  241. EaseTime,, New AniEaseOutFluentWithInitial(InitSpeed, EaseTime / 1000, EaseLength), True))
  242. End If
  243. AnimList.Add(AaCode(
  244. Sub()
  245. IsHeightAnimating = False
  246. Height = ActualUsedHeight
  247. If IsSwapped AndAlso SwapControl IsNot Nothing Then SwapControl.Visibility = Visibility.Collapsed
  248. End Sub,, True))
  249. AniStart(AnimList, "MyCard Height " & Uuid)
  250. IsHeightAnimating = True
  251. ActualUsedHeight = If(IsSwapped, SwapedHeight, Height)
  252. Height = PreviousHeight
  253. End Sub
  254. ''' <summary>
  255. ''' 通知 MyCard,控件内容已改变,需要中断动画并瞬间更新高度。
  256. ''' </summary>
  257. Public Sub TriggerForceResize()
  258. Height = If(IsSwapped, SwapedHeight, Double.NaN)
  259. AniStop("MyCard Height " & Uuid)
  260. IsHeightAnimating = False
  261. End Sub
  262. #End Region
  263. #Region "折叠"
  264. '若设置了 CanSwap,或 SwapControl 不为空,则判定为会进行折叠
  265. '这是因为不能直接在 XAML 中设置 SwapControl
  266. Public SwapControl As FrameworkElement
  267. Public Property CanSwap As Boolean = False
  268. ''' <summary>
  269. ''' 被折叠的种类,用于控件虚拟化。
  270. ''' </summary>
  271. Public Property SwapType As Integer
  272. ''' <summary>
  273. ''' 是否已被折叠。
  274. ''' </summary>
  275. Public Property IsSwapped As Boolean
  276. Get
  277. Return _IsSwapped
  278. End Get
  279. Set(value As Boolean)
  280. If _IsSwapped = value Then Return
  281. _IsSwapped = value
  282. If SwapControl Is Nothing Then Return
  283. '展开
  284. If Not IsSwapped AndAlso TypeOf SwapControl Is StackPanel Then StackInstall(SwapControl, SwapType, Title)
  285. '若尚未加载,会在 Loaded 事件中触发无动画的折叠,不需要在这里进行
  286. If Not IsLoaded Then Return
  287. '更新样式
  288. SwapControl.Visibility = Visibility.Visible
  289. TriggerForceResize()
  290. UpdateCursorStyle()
  291. '改变箭头
  292. AniStart(AaRotateTransform(MainSwap, If(_IsSwapped, If(SwapLogoRight, 270, 0), 180) - CType(MainSwap.RenderTransform, RotateTransform).Angle, 250,, New AniEaseOutFluent(AniEasePower.ExtraStrong)), "MyCard Swap " & Uuid, True)
  293. End Set
  294. End Property
  295. Private _IsSwapped As Boolean = False
  296. <Obsolete("IsSwaped 存在拼写错误,请换用 IsSwapped 属性。")>
  297. Public Property IsSwaped As Boolean
  298. Get
  299. Return GetValue(IsSwapedProperty)
  300. End Get
  301. Set(value As Boolean)
  302. SetValue(IsSwapedProperty, value)
  303. End Set
  304. End Property
  305. Public Shared ReadOnly IsSwapedProperty As DependencyProperty = DependencyProperty.Register("IsSwaped", GetType(Boolean), GetType(MyCard), New PropertyMetadata(False,
  306. Sub(sender, e) If sender IsNot Nothing AndAlso TypeOf sender Is MyCard Then CType(sender, MyCard).IsSwapped = CType(e.NewValue, Boolean)))
  307. Public Property SwapLogoRight As Boolean = False
  308. Private IsSwapMouseDown As Boolean = False '用于触发卡片展开/折叠的 MouseDown
  309. Private IsCustomMouseDown As Boolean = False '用于触发自定义事件的 MouseDown
  310. Public Event PreviewSwap(sender As Object, e As RouteEventArgs)
  311. Public Event Swap(sender As Object, e As RouteEventArgs)
  312. Public Const SwapedHeight As Integer = 40
  313. Private Sub MyCard_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
  314. Dim Pos As Double = Mouse.GetPosition(Me).Y
  315. If Not IsSwapped AndAlso
  316. (Pos > If(IsSwapped, SwapedHeight, SwapedHeight - 6) OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Return
  317. IsCustomMouseDown = True
  318. If Not IsSwapped AndAlso
  319. (SwapControl Is Nothing OrElse Pos > If(IsSwapped, SwapedHeight, SwapedHeight - 6) OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Return '检测点击位置;或已经不在可视树上的误判
  320. IsSwapMouseDown = True
  321. End Sub
  322. Private Sub MyCard_MouseLeftButtonUp() Handles Me.MouseLeftButtonUp
  323. If Not IsCustomMouseDown Then Return
  324. IsCustomMouseDown = False
  325. RaiseCustomEvent() '触发自定义事件
  326. If Not IsSwapMouseDown Then Return
  327. IsSwapMouseDown = False
  328. Dim Pos As Double = Mouse.GetPosition(Me).Y
  329. If Not IsSwapped AndAlso
  330. (SwapControl Is Nothing OrElse Pos > If(IsSwapped, SwapedHeight, SwapedHeight - 6) OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Return '检测点击位置;或已经不在可视树上的误判
  331. Dim e = New RouteEventArgs(True)
  332. RaiseEvent PreviewSwap(Me, e)
  333. If e.Handled Then
  334. IsSwapMouseDown = False
  335. Return
  336. End If
  337. IsSwapped = Not IsSwapped
  338. Logger.Info($"{If(IsSwapped, "折叠卡片", "展开卡片")}{If(Title Is Nothing, "", ":" & Title)}")
  339. RaiseEvent Swap(Me, e)
  340. End Sub
  341. Private Sub MyCard_MouseLeave_Swap(sender As Object, e As MouseEventArgs) Handles Me.MouseLeave
  342. IsSwapMouseDown = False
  343. End Sub
  344. Private Sub UpdateCursorStyle() Handles Me.MouseMove
  345. If _MainSwap Is Nothing Then Cursor = Nothing : Return
  346. Dim Pos As Double = Mouse.GetPosition(Me).Y
  347. Cursor = If(MainSwap.Visibility = Visibility.Visible AndAlso (IsSwapped OrElse (Pos <= SwapedHeight - 6 AndAlso Not (Pos = 0 AndAlso Not IsMouseDirectlyOver))),
  348. Cursors.Hand, Nothing)
  349. End Sub
  350. #End Region
  351. End Class
  352. Partial Public Module ModAnimation
  353. Public Sub AniDispose(Control As MyCard, RemoveFromChildren As Boolean, Optional CallBack As ParameterizedThreadStart = Nothing)
  354. If Control.IsHitTestVisible Then
  355. Control.IsHitTestVisible = False
  356. AniStart({
  357. AaScaleTransform(Control, -0.08, 200,, New AniEaseInFluent),
  358. AaOpacity(Control, -1, 200,, New AniEaseOutFluent),
  359. AaHeight(Control, -Control.ActualHeight, 150, 100, New AniEaseOutFluent),
  360. AaCode(
  361. Sub()
  362. If RemoveFromChildren Then
  363. If Control.Parent Is Nothing Then Return
  364. CType(Control.Parent, Object).Children.Remove(Control)
  365. Else
  366. Control.Visibility = Visibility.Collapsed
  367. End If
  368. If CallBack IsNot Nothing Then CallBack(Control)
  369. End Sub,, True)
  370. }, "MyCard Dispose " & Control.Uuid)
  371. Else
  372. If RemoveFromChildren Then
  373. If Control.Parent Is Nothing Then Return
  374. CType(Control.Parent, Object).Children.Remove(Control)
  375. Else
  376. Control.Visibility = Visibility.Collapsed
  377. End If
  378. If CallBack IsNot Nothing Then CallBack(Control)
  379. End If
  380. End Sub
  381. End Module