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. '取消由于高度变化被迫触发的高度动画
  93. Dim RawUseAnimation As Boolean = UseAnimation
  94. UseAnimation = False
  95. Height = SwapedHeight
  96. AniStop("MyCard Height " & Uuid)
  97. IsHeightAnimating = False
  98. RunInUi(Sub() UseAnimation = RawUseAnimation, True)
  99. End If
  100. End Sub
  101. Public Sub StackInstall()
  102. StackInstall(SwapControl, SwapType, Title)
  103. TriggerForceResize()
  104. End Sub
  105. Public Shared Sub StackInstall(ByRef Stack As StackPanel, Type As Integer, Optional CardTitle As String = "")
  106. '这一部分的代码是好几年前留下的究极屎坑,当时还不知道该咋正确调用这种方法,就写了这么一坨屎
  107. '但是现在……反正勉强能用……懒得改了就这样吧.jpg
  108. '别骂了别骂了.jpg
  109. If IsNothing(Stack.Tag) Then Return
  110. '排序
  111. Select Case Type
  112. Case 3
  113. Stack.Tag = CType(Stack.Tag, List(Of DlOptiFineListEntry)).Sort(Function(a, b) VersionSortBoolean(a.NameDisplay, b.NameDisplay))
  114. Case 4, 10
  115. Stack.Tag = CType(Stack.Tag, List(Of DlLiteLoaderListEntry)).Sort(Function(a, b) VersionSortBoolean(a.Inherit, b.Inherit))
  116. Case 6
  117. Stack.Tag = CType(Stack.Tag, List(Of DlForgeVersionEntry)).Sort(Function(a, b) a.Version > b.Version)
  118. Case 8, 9
  119. Stack.Tag = CType(Stack.Tag, List(Of CompFile)).Sort(Function(a, b) a.ReleaseDate > b.ReleaseDate)
  120. End Select
  121. '控件转换
  122. Select Case Type
  123. Case 6
  124. ForgeDownloadListItemPreload(Stack, Stack.Tag, AddressOf ForgeSave_Click, True)
  125. Case 8
  126. CompFilesCardPreload(Stack, Stack.Tag)
  127. End Select
  128. '实现控件虚拟化
  129. For Each Data As Object In Stack.Tag
  130. Select Case Type
  131. Case 0
  132. Dim VersionItem As MyListItem = CType(Data, McVersion).ToListItem
  133. VersionItem.ContentHandler = AddressOf PageSelectRight.McVersionListContent
  134. Stack.Children.Add(VersionItem)
  135. Case 2
  136. Stack.Children.Add(McDownloadListItem(Data, AddressOf McDownloadMenuSave, True))
  137. Case 3
  138. Stack.Children.Add(OptiFineDownloadListItem(Data, AddressOf OptiFineSave_Click, True))
  139. Case 6
  140. Stack.Children.Add(ForgeDownloadListItem(Data, AddressOf ForgeSave_Click, True))
  141. Case 7
  142. '不能使用 AddressOf,这导致了 #535,原因完全不明,疑似是编译器 Bug
  143. Stack.Children.Add(McDownloadListItem(Data, Sub(sender, e) FrmDownloadInstall.MinecraftSelected(sender, e), False))
  144. Case 8
  145. If CType(Stack.Tag, List(Of CompFile)).Distinct(Function(a, b) a.DisplayName = b.DisplayName).Count <>
  146. CType(Stack.Tag, List(Of CompFile)).Count Then
  147. '存在重复的名称(#1344)
  148. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Save_Click, BadDisplayName:=True))
  149. Else
  150. '不存在重复的名称,正常加载
  151. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Save_Click))
  152. End If
  153. Case 9
  154. If CType(Stack.Tag, List(Of CompFile)).Distinct(Function(a, b) a.DisplayName = b.DisplayName).Count <>
  155. CType(Stack.Tag, List(Of CompFile)).Count Then
  156. '存在重复的名称(#1344)
  157. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Install_Click, AddressOf FrmDownloadCompDetail.Save_Click, BadDisplayName:=True))
  158. Else
  159. '不存在重复的名称,正常加载
  160. Stack.Children.Add(CType(Data, CompFile).ToListItem(AddressOf FrmDownloadCompDetail.Install_Click, AddressOf FrmDownloadCompDetail.Save_Click))
  161. End If
  162. Case 10
  163. Stack.Children.Add(LiteLoaderDownloadListItem(Data, AddressOf LiteLoaderSave_Click, True))
  164. Case 11
  165. Stack.Children.Add(CType(Data, HelpEntry).ToListItem)
  166. Case 12
  167. Stack.Children.Add(FabricDownloadListItem(CType(Data, JObject), AddressOf FrmDownloadInstall.Fabric_Selected))
  168. Case 13
  169. Stack.Children.Add(NeoForgeDownloadListItem(Data, AddressOf NeoForgeSave_Click, True))
  170. Case Else
  171. Log("未知的虚拟化种类:" & Type, LogLevel.Feedback)
  172. End Select
  173. Next
  174. Stack.Children.Add(New FrameworkElement With {.Height = 18}) '下边距,同时适应折叠
  175. Stack.Tag = Nothing
  176. End Sub
  177. '动画
  178. Private Const DropShadowIdleOpacity As Double = 0.07
  179. Private Const DropShadowHoverOpacity As Double = 0.4
  180. Public Property HasMouseAnimation As Boolean = True
  181. Private Sub MyCard_MouseEnter(sender As Object, e As MouseEventArgs) Handles Me.MouseEnter
  182. If Not HasMouseAnimation Then Return
  183. Dim AniList As New List(Of AniData)
  184. If Not IsNothing(MainTextBlock) Then AniList.Add(AaColor(MainTextBlock, TextBlock.ForegroundProperty, "ColorBrush2", 90))
  185. If Not IsNothing(MainSwap) Then AniList.Add(AaColor(MainSwap, Shapes.Path.FillProperty, "ColorBrush2", 90))
  186. AniList.AddRange({
  187. AaColor(MainChrome, MyDropShadow.ColorProperty, "ColorObject4", 90),
  188. AaOpacity(MainChrome, DropShadowHoverOpacity - MainChrome.Opacity, 90)
  189. })
  190. AniStart(AniList, "MyCard Mouse " & Uuid)
  191. End Sub
  192. Private Sub MyCard_MouseLeave(sender As Object, e As MouseEventArgs) Handles Me.MouseLeave
  193. If Not HasMouseAnimation Then Return
  194. Dim AniList As New List(Of AniData)
  195. If Not IsNothing(MainTextBlock) Then AniList.Add(AaColor(MainTextBlock, TextBlock.ForegroundProperty, "ColorBrush1", 90))
  196. If Not IsNothing(MainSwap) Then AniList.Add(AaColor(MainSwap, Shapes.Path.FillProperty, "ColorBrush1", 90))
  197. AniList.AddRange({
  198. AaColor(MainChrome, MyDropShadow.ColorProperty, "ColorObject1", 90),
  199. AaOpacity(MainChrome, DropShadowIdleOpacity - MainChrome.Opacity, 90)
  200. })
  201. AniStart(AniList, "MyCard Mouse " & Uuid)
  202. End Sub
  203. #Region "高度改变动画"
  204. ''' <summary>
  205. ''' 是否启用高度改变动画。
  206. ''' </summary>
  207. Public Property UseAnimation As Boolean = True
  208. Private IsHeightAnimating As Boolean = False
  209. Private ActualUsedHeight As Double '回滚实际高度(例如 NaN)
  210. Private Sub MySizeChanged(sender As Object, e As SizeChangedEventArgs) Handles Me.SizeChanged
  211. If Not UseAnimation Then Return
  212. Dim DeltaHeight As Double = If(IsSwapped, SwapedHeight, e.NewSize.Height) - e.PreviousSize.Height
  213. '卡片的进入时动画已被页面通用切换动画替代
  214. If e.PreviousSize.Height = 0 OrElse IsHeightAnimating OrElse Math.Abs(DeltaHeight) < 1 OrElse ActualHeight = 0 Then Return
  215. StartHeightAnimation(DeltaHeight, e.PreviousSize.Height, False)
  216. End Sub
  217. Private Sub StartHeightAnimation(Delta As Double, PreviousHeight As Double, IsLoadAnimation As Boolean)
  218. If IsHeightAnimating OrElse FrmMain Is Nothing Then Return '避免 XAML 设计器出错
  219. Dim AnimList As New List(Of AniData)
  220. Dim AbsDelta = Math.Abs(Delta)
  221. If AbsDelta <= 800 Then
  222. '短距离,直接使用 150ms 的缓动动画
  223. AnimList.Add(AaHeight(Me, Delta, 150,, New AniEaseOutFluent(AniEasePower.ExtraStrong)))
  224. Else
  225. Dim EaseLength As Integer, EaseTime As Integer
  226. Dim InitSpeed As Integer '到达缓动区前的初速度
  227. If Delta < 0 AndAlso AbsDelta - EaseLength > 5000 * 0.1 Then
  228. '收回距离过长 (>0.1s),强制以 100ms 完成匀速段,然后让减速段更长
  229. EaseLength = 200
  230. EaseTime = 150
  231. InitSpeed = (AbsDelta - EaseLength) / 0.1
  232. ElseIf Delta > 0 AndAlso AbsDelta - EaseLength > 5000 * 0.6 Then
  233. '展开距离过长 (>0.6s),以 5000 速度展示 300ms 匀速段,剩下的距离全部归入减速段
  234. InitSpeed = 5000
  235. EaseLength = AbsDelta - InitSpeed * 0.3
  236. EaseTime = 400
  237. Else
  238. '中程,匀速地快速展开(或收回)
  239. EaseLength = 150
  240. EaseTime = 200
  241. InitSpeed = 4000
  242. End If
  243. '匀速段
  244. AnimList.Add(AaHeight(Me, (AbsDelta - EaseLength) * Math.Sign(Delta),
  245. (AbsDelta - EaseLength) / InitSpeed * 1000))
  246. '减速段
  247. AnimList.Add(AaHeight(Me, EaseLength * Math.Sign(Delta),
  248. EaseTime,, New AniEaseOutFluentWithInitial(InitSpeed, EaseTime / 1000, EaseLength), True))
  249. End If
  250. AnimList.Add(AaCode(
  251. Sub()
  252. IsHeightAnimating = False
  253. Height = ActualUsedHeight
  254. If IsSwapped AndAlso SwapControl IsNot Nothing Then SwapControl.Visibility = Visibility.Collapsed
  255. End Sub,, True))
  256. AniStart(AnimList, "MyCard Height " & Uuid)
  257. IsHeightAnimating = True
  258. ActualUsedHeight = If(IsSwapped, SwapedHeight, Height)
  259. Height = PreviousHeight
  260. End Sub
  261. ''' <summary>
  262. ''' 通知 MyCard,控件内容已改变,需要中断动画并更新高度。
  263. ''' </summary>
  264. Public Sub TriggerForceResize()
  265. Height = If(IsSwapped, SwapedHeight, Double.NaN)
  266. AniStop("MyCard Height " & Uuid)
  267. IsHeightAnimating = False
  268. End Sub
  269. #End Region
  270. #Region "折叠"
  271. '若设置了 CanSwap,或 SwapControl 不为空,则判定为会进行折叠
  272. '这是因为不能直接在 XAML 中设置 SwapControl
  273. Public SwapControl As Object
  274. Public Property CanSwap As Boolean = False
  275. ''' <summary>
  276. ''' 被折叠的种类,用于控件虚拟化。
  277. ''' </summary>
  278. Public Property SwapType As Integer
  279. ''' <summary>
  280. ''' 是否已被折叠。
  281. ''' </summary>
  282. Public Property IsSwapped As Boolean
  283. Get
  284. Return _IsSwapped
  285. End Get
  286. Set(value As Boolean)
  287. If _IsSwapped = value Then Return
  288. _IsSwapped = value
  289. If SwapControl Is Nothing Then Return
  290. '展开
  291. If Not IsSwapped AndAlso TypeOf SwapControl Is StackPanel Then StackInstall(SwapControl, SwapType, Title)
  292. '若尚未加载,会在 Loaded 事件中触发无动画的折叠,不需要在这里进行
  293. If Not IsLoaded Then Return
  294. '更新高度
  295. SwapControl.Visibility = Visibility.Visible
  296. TriggerForceResize()
  297. '改变箭头
  298. AniStart(AaRotateTransform(MainSwap, If(_IsSwapped, If(SwapLogoRight, 270, 0), 180) - CType(MainSwap.RenderTransform, RotateTransform).Angle, 250,, New AniEaseOutFluent(AniEasePower.ExtraStrong)), "MyCard Swap " & Uuid, True)
  299. End Set
  300. End Property
  301. Private _IsSwapped As Boolean = False
  302. <Obsolete("IsSwaped 存在拼写错误,请换用 IsSwapped 属性。")>
  303. Public Property IsSwaped As Boolean
  304. Get
  305. Return GetValue(IsSwapedProperty)
  306. End Get
  307. Set(value As Boolean)
  308. SetValue(IsSwapedProperty, value)
  309. End Set
  310. End Property
  311. Public Shared ReadOnly IsSwapedProperty As DependencyProperty = DependencyProperty.Register("IsSwaped", GetType(Boolean), GetType(MyCard), New PropertyMetadata(False,
  312. Sub(sender, e) If sender IsNot Nothing AndAlso TypeOf sender Is MyCard Then CType(sender, MyCard).IsSwapped = CType(e.NewValue, Boolean)))
  313. Public Property SwapLogoRight As Boolean = False
  314. Private IsSwapMouseDown As Boolean = False '用于触发卡片展开/折叠的 MouseDown
  315. Private IsCustomMouseDown As Boolean = False '用于触发自定义事件的 MouseDown
  316. Public Event PreviewSwap(sender As Object, e As RouteEventArgs)
  317. Public Event Swap(sender As Object, e As RouteEventArgs)
  318. Public Const SwapedHeight As Integer = 40
  319. Private Sub MyCard_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
  320. Dim Pos As Double = Mouse.GetPosition(Me).Y
  321. If Not IsSwapped AndAlso
  322. (Pos > If(IsSwapped, SwapedHeight, SwapedHeight - 6) OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Return
  323. IsCustomMouseDown = True
  324. If Not IsSwapped AndAlso
  325. (SwapControl Is Nothing OrElse Pos > If(IsSwapped, SwapedHeight, SwapedHeight - 6) OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Return '检测点击位置;或已经不在可视树上的误判
  326. IsSwapMouseDown = True
  327. End Sub
  328. Private Sub MyCard_MouseLeftButtonUp() Handles Me.MouseLeftButtonUp
  329. If Not IsCustomMouseDown Then Return
  330. IsCustomMouseDown = False
  331. RaiseCustomEvent() '触发自定义事件
  332. If Not IsSwapMouseDown Then Return
  333. IsSwapMouseDown = False
  334. Dim Pos As Double = Mouse.GetPosition(Me).Y
  335. If Not IsSwapped AndAlso
  336. (SwapControl Is Nothing OrElse Pos > If(IsSwapped, SwapedHeight, SwapedHeight - 6) OrElse (Pos = 0 AndAlso Not IsMouseDirectlyOver)) Then Return '检测点击位置;或已经不在可视树上的误判
  337. Dim e = New RouteEventArgs(True)
  338. RaiseEvent PreviewSwap(Me, e)
  339. If e.Handled Then
  340. IsSwapMouseDown = False
  341. Return
  342. End If
  343. IsSwapped = Not IsSwapped
  344. Log("[Control] " & If(IsSwapped, "折叠卡片", "展开卡片") & If(Title Is Nothing, "", ":" & Title))
  345. RaiseEvent Swap(Me, e)
  346. End Sub
  347. Private Sub MyCard_MouseLeave_Swap(sender As Object, e As MouseEventArgs) Handles Me.MouseLeave
  348. IsSwapMouseDown = False
  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