FlexPanel.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using HandyControl.Data;
  7. using HandyControl.Expression.Drawing;
  8. using HandyControl.Tools;
  9. using HandyControl.Tools.Extension;
  10. namespace HandyControl.Controls;
  11. public class FlexPanel : Panel
  12. {
  13. private UVSize _uvConstraint;
  14. private int _lineCount;
  15. private readonly List<FlexItemInfo> _orderList = new();
  16. #region Item
  17. public static readonly DependencyProperty OrderProperty = DependencyProperty.RegisterAttached(
  18. "Order", typeof(int), typeof(FlexPanel),
  19. new FrameworkPropertyMetadata(ValueBoxes.Int0Box, OnItemPropertyChanged));
  20. public static void SetOrder(DependencyObject element, int value)
  21. => element.SetValue(OrderProperty, value);
  22. public static int GetOrder(DependencyObject element)
  23. => (int) element.GetValue(OrderProperty);
  24. public static readonly DependencyProperty FlexGrowProperty = DependencyProperty.RegisterAttached(
  25. "FlexGrow", typeof(double), typeof(FlexPanel),
  26. new FrameworkPropertyMetadata(ValueBoxes.Double0Box, OnItemPropertyChanged),
  27. ValidateHelper.IsInRangeOfPosDoubleIncludeZero);
  28. public static void SetFlexGrow(DependencyObject element, double value)
  29. => element.SetValue(FlexGrowProperty, value);
  30. public static double GetFlexGrow(DependencyObject element)
  31. => (double) element.GetValue(FlexGrowProperty);
  32. public static readonly DependencyProperty FlexShrinkProperty = DependencyProperty.RegisterAttached(
  33. "FlexShrink", typeof(double), typeof(FlexPanel),
  34. new FrameworkPropertyMetadata(ValueBoxes.Double1Box, OnItemPropertyChanged),
  35. ValidateHelper.IsInRangeOfPosDoubleIncludeZero);
  36. public static void SetFlexShrink(DependencyObject element, double value)
  37. => element.SetValue(FlexShrinkProperty, value);
  38. public static double GetFlexShrink(DependencyObject element)
  39. => (double) element.GetValue(FlexShrinkProperty);
  40. public static readonly DependencyProperty FlexBasisProperty = DependencyProperty.RegisterAttached(
  41. "FlexBasis", typeof(double), typeof(FlexPanel),
  42. new FrameworkPropertyMetadata(double.NaN, OnItemPropertyChanged));
  43. public static void SetFlexBasis(DependencyObject element, double value)
  44. => element.SetValue(FlexBasisProperty, value);
  45. public static double GetFlexBasis(DependencyObject element)
  46. => (double) element.GetValue(FlexBasisProperty);
  47. public static readonly DependencyProperty AlignSelfProperty = DependencyProperty.RegisterAttached(
  48. "AlignSelf", typeof(FlexItemAlignment), typeof(FlexPanel),
  49. new FrameworkPropertyMetadata(default(FlexItemAlignment), OnItemPropertyChanged));
  50. public static void SetAlignSelf(DependencyObject element, FlexItemAlignment value)
  51. => element.SetValue(AlignSelfProperty, value);
  52. #endregion
  53. #region Panel
  54. public static FlexItemAlignment GetAlignSelf(DependencyObject element)
  55. => (FlexItemAlignment) element.GetValue(AlignSelfProperty);
  56. public static readonly DependencyProperty FlexDirectionProperty = DependencyProperty.Register(
  57. nameof(FlexDirection), typeof(FlexDirection), typeof(FlexPanel),
  58. new FrameworkPropertyMetadata(default(FlexDirection), FrameworkPropertyMetadataOptions.AffectsMeasure));
  59. public FlexDirection FlexDirection
  60. {
  61. get => (FlexDirection) GetValue(FlexDirectionProperty);
  62. set => SetValue(FlexDirectionProperty, value);
  63. }
  64. public static readonly DependencyProperty FlexWrapProperty = DependencyProperty.Register(
  65. nameof(FlexWrap), typeof(FlexWrap), typeof(FlexPanel),
  66. new FrameworkPropertyMetadata(default(FlexWrap), FrameworkPropertyMetadataOptions.AffectsMeasure));
  67. public FlexWrap FlexWrap
  68. {
  69. get => (FlexWrap) GetValue(FlexWrapProperty);
  70. set => SetValue(FlexWrapProperty, value);
  71. }
  72. public static readonly DependencyProperty JustifyContentProperty = DependencyProperty.Register(
  73. nameof(JustifyContent), typeof(FlexContentJustify), typeof(FlexPanel),
  74. new FrameworkPropertyMetadata(default(FlexContentJustify),
  75. FrameworkPropertyMetadataOptions.AffectsMeasure));
  76. public FlexContentJustify JustifyContent
  77. {
  78. get => (FlexContentJustify) GetValue(JustifyContentProperty);
  79. set => SetValue(JustifyContentProperty, value);
  80. }
  81. public static readonly DependencyProperty AlignItemsProperty = DependencyProperty.Register(
  82. nameof(AlignItems), typeof(FlexItemsAlignment), typeof(FlexPanel),
  83. new FrameworkPropertyMetadata(default(FlexItemsAlignment),
  84. FrameworkPropertyMetadataOptions.AffectsMeasure));
  85. public FlexItemsAlignment AlignItems
  86. {
  87. get => (FlexItemsAlignment) GetValue(AlignItemsProperty);
  88. set => SetValue(AlignItemsProperty, value);
  89. }
  90. public static readonly DependencyProperty AlignContentProperty = DependencyProperty.Register(
  91. nameof(AlignContent), typeof(FlexContentAlignment), typeof(FlexPanel),
  92. new FrameworkPropertyMetadata(default(FlexContentAlignment),
  93. FrameworkPropertyMetadataOptions.AffectsMeasure));
  94. public FlexContentAlignment AlignContent
  95. {
  96. get => (FlexContentAlignment) GetValue(AlignContentProperty);
  97. set => SetValue(AlignContentProperty, value);
  98. }
  99. #endregion
  100. private static void OnItemPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  101. {
  102. if (d is UIElement element)
  103. {
  104. if (VisualTreeHelper.GetParent(element) is FlexPanel p)
  105. {
  106. p.InvalidateMeasure();
  107. }
  108. }
  109. }
  110. protected override Size MeasureOverride(Size constraint)
  111. {
  112. var flexDirection = FlexDirection;
  113. var flexWrap = FlexWrap;
  114. var curLineSize = new UVSize(flexDirection);
  115. var panelSize = new UVSize(flexDirection);
  116. _uvConstraint = new UVSize(flexDirection, constraint);
  117. var childConstraint = new Size(constraint.Width, constraint.Height);
  118. _lineCount = 1;
  119. var children = InternalChildren;
  120. _orderList.Clear();
  121. for (var i = 0; i < children.Count; i++)
  122. {
  123. var child = children[i];
  124. if (child == null) continue;
  125. _orderList.Add(new FlexItemInfo(i, GetOrder(child)));
  126. }
  127. _orderList.Sort();
  128. for (var i = 0; i < children.Count; i++)
  129. {
  130. var child = children[_orderList[i].Index];
  131. if (child == null) continue;
  132. var flexBasis = GetFlexBasis(child);
  133. if (!flexBasis.IsNaN())
  134. {
  135. child.SetCurrentValue(WidthProperty, flexBasis);
  136. }
  137. child.Measure(childConstraint);
  138. var sz = new UVSize(flexDirection, child.DesiredSize);
  139. if (flexWrap == FlexWrap.NoWrap) //continue to accumulate a line
  140. {
  141. curLineSize.U += sz.U;
  142. curLineSize.V = Math.Max(sz.V, curLineSize.V);
  143. }
  144. else
  145. {
  146. if (MathHelper.GreaterThan(curLineSize.U + sz.U, _uvConstraint.U)) //need to switch to another line
  147. {
  148. panelSize.U = Math.Max(curLineSize.U, panelSize.U);
  149. panelSize.V += curLineSize.V;
  150. curLineSize = sz;
  151. _lineCount++;
  152. if (MathHelper.GreaterThan(sz.U, _uvConstraint.U)) //the element is wider then the constrint - give it a separate line
  153. {
  154. panelSize.U = Math.Max(sz.U, panelSize.U);
  155. panelSize.V += sz.V;
  156. curLineSize = new UVSize(flexDirection);
  157. _lineCount++;
  158. }
  159. }
  160. else //continue to accumulate a line
  161. {
  162. curLineSize.U += sz.U;
  163. curLineSize.V = Math.Max(sz.V, curLineSize.V);
  164. }
  165. }
  166. }
  167. //the last line size, if any should be added
  168. panelSize.U = Math.Max(curLineSize.U, panelSize.U);
  169. panelSize.V += curLineSize.V;
  170. //go from UV space to W/H space
  171. return new Size(panelSize.Width, panelSize.Height);
  172. }
  173. protected override Size ArrangeOverride(Size arrangeSize)
  174. {
  175. var flexDirection = FlexDirection;
  176. var flexWrap = FlexWrap;
  177. var alignContent = AlignContent;
  178. var uvFinalSize = new UVSize(flexDirection, arrangeSize);
  179. if (MathHelper.IsZero(uvFinalSize.U) || MathHelper.IsZero(uvFinalSize.V)) return arrangeSize;
  180. // init status
  181. var children = InternalChildren;
  182. var lineIndex = 0;
  183. var curLineSizeArr = new UVSize[_lineCount];
  184. curLineSizeArr[0] = new UVSize(flexDirection);
  185. var lastInLineArr = new int[_lineCount];
  186. for (var i = 0; i < _lineCount; i++)
  187. {
  188. lastInLineArr[i] = int.MaxValue;
  189. }
  190. // calculate line max U space
  191. for (var i = 0; i < children.Count; i++)
  192. {
  193. var child = children[_orderList[i].Index];
  194. if (child == null) continue;
  195. var sz = new UVSize(flexDirection, child.DesiredSize);
  196. if (flexWrap == FlexWrap.NoWrap)
  197. {
  198. curLineSizeArr[lineIndex].U += sz.U;
  199. curLineSizeArr[lineIndex].V = Math.Max(sz.V, curLineSizeArr[lineIndex].V);
  200. }
  201. else
  202. {
  203. if (MathHelper.GreaterThan(curLineSizeArr[lineIndex].U + sz.U, uvFinalSize.U)) //need to switch to another line
  204. {
  205. lastInLineArr[lineIndex] = i;
  206. lineIndex++;
  207. curLineSizeArr[lineIndex] = sz;
  208. if (MathHelper.GreaterThan(sz.U, uvFinalSize.U)) //the element is wider then the constraint - give it a separate line
  209. {
  210. //switch to next line which only contain one element
  211. lastInLineArr[lineIndex] = i;
  212. lineIndex++;
  213. curLineSizeArr[lineIndex] = new UVSize(flexDirection);
  214. }
  215. }
  216. else //continue to accumulate a line
  217. {
  218. curLineSizeArr[lineIndex].U += sz.U;
  219. curLineSizeArr[lineIndex].V = Math.Max(sz.V, curLineSizeArr[lineIndex].V);
  220. }
  221. }
  222. }
  223. // init status
  224. var scaleU = Math.Min(_uvConstraint.U / uvFinalSize.U, 1);
  225. var firstInLine = 0;
  226. var wrapReverseAdd = 0;
  227. var wrapReverseFlag = flexWrap == FlexWrap.WrapReverse ? -1 : 1;
  228. var accumulatedFlag = flexWrap == FlexWrap.WrapReverse ? 1 : 0;
  229. var itemsU = .0;
  230. var accumulatedV = .0;
  231. var freeV = uvFinalSize.V;
  232. foreach (var flexSize in curLineSizeArr)
  233. {
  234. freeV -= flexSize.V;
  235. }
  236. var freeItemV = freeV;
  237. // calculate status
  238. var lineFreeVArr = new double[_lineCount];
  239. switch (alignContent)
  240. {
  241. case FlexContentAlignment.Stretch:
  242. if (_lineCount > 1)
  243. {
  244. freeItemV = freeV / _lineCount;
  245. for (var i = 0; i < _lineCount; i++)
  246. {
  247. lineFreeVArr[i] = freeItemV;
  248. }
  249. accumulatedV = flexWrap == FlexWrap.WrapReverse ? uvFinalSize.V - curLineSizeArr[0].V - lineFreeVArr[0] : 0;
  250. }
  251. break;
  252. case FlexContentAlignment.FlexStart:
  253. wrapReverseAdd = flexWrap == FlexWrap.WrapReverse ? 0 : 1;
  254. if (_lineCount > 1)
  255. {
  256. accumulatedV = flexWrap == FlexWrap.WrapReverse ? uvFinalSize.V - curLineSizeArr[0].V : 0;
  257. }
  258. else
  259. {
  260. wrapReverseAdd = 0;
  261. }
  262. break;
  263. case FlexContentAlignment.FlexEnd:
  264. wrapReverseAdd = flexWrap == FlexWrap.WrapReverse ? 1 : 0;
  265. if (_lineCount > 1)
  266. {
  267. accumulatedV = flexWrap == FlexWrap.WrapReverse ? uvFinalSize.V - curLineSizeArr[0].V - freeV : freeV;
  268. }
  269. else
  270. {
  271. wrapReverseAdd = 0;
  272. }
  273. break;
  274. case FlexContentAlignment.Center:
  275. if (_lineCount > 1)
  276. {
  277. accumulatedV = flexWrap == FlexWrap.WrapReverse ? uvFinalSize.V - curLineSizeArr[0].V - freeV * 0.5 : freeV * 0.5;
  278. }
  279. break;
  280. case FlexContentAlignment.SpaceBetween:
  281. if (_lineCount > 1)
  282. {
  283. freeItemV = freeV / (_lineCount - 1);
  284. for (var i = 0; i < _lineCount - 1; i++)
  285. {
  286. lineFreeVArr[i] = freeItemV;
  287. }
  288. accumulatedV = flexWrap == FlexWrap.WrapReverse ? uvFinalSize.V - curLineSizeArr[0].V : 0;
  289. }
  290. break;
  291. case FlexContentAlignment.SpaceAround:
  292. if (_lineCount > 1)
  293. {
  294. freeItemV = freeV / _lineCount * 0.5;
  295. for (var i = 0; i < _lineCount - 1; i++)
  296. {
  297. lineFreeVArr[i] = freeItemV * 2;
  298. }
  299. accumulatedV = flexWrap == FlexWrap.WrapReverse ? uvFinalSize.V - curLineSizeArr[0].V - freeItemV : freeItemV;
  300. }
  301. break;
  302. }
  303. // clear status
  304. lineIndex = 0;
  305. // arrange line
  306. for (var i = 0; i < children.Count; i++)
  307. {
  308. var child = children[_orderList[i].Index];
  309. if (child == null) continue;
  310. var sz = new UVSize(flexDirection, child.DesiredSize);
  311. if (flexWrap != FlexWrap.NoWrap)
  312. {
  313. if (i >= lastInLineArr[lineIndex]) //need to switch to another line
  314. {
  315. ArrangeLine(new FlexLineInfo
  316. {
  317. ItemsU = itemsU,
  318. OffsetV = accumulatedV + freeItemV * wrapReverseAdd,
  319. LineV = curLineSizeArr[lineIndex].V,
  320. LineFreeV = freeItemV,
  321. LineU = uvFinalSize.U,
  322. ItemStartIndex = firstInLine,
  323. ItemEndIndex = i,
  324. ScaleU = scaleU
  325. });
  326. accumulatedV += (lineFreeVArr[lineIndex] + curLineSizeArr[lineIndex + accumulatedFlag].V) * wrapReverseFlag;
  327. lineIndex++;
  328. itemsU = 0;
  329. if (i >= lastInLineArr[lineIndex]) //the element is wider then the constraint - give it a separate line
  330. {
  331. //switch to next line which only contain one element
  332. ArrangeLine(new FlexLineInfo
  333. {
  334. ItemsU = itemsU,
  335. OffsetV = accumulatedV + freeItemV * wrapReverseAdd,
  336. LineV = curLineSizeArr[lineIndex].V,
  337. LineFreeV = freeItemV,
  338. LineU = uvFinalSize.U,
  339. ItemStartIndex = i,
  340. ItemEndIndex = ++i,
  341. ScaleU = scaleU
  342. });
  343. accumulatedV += (lineFreeVArr[lineIndex] + curLineSizeArr[lineIndex + accumulatedFlag].V) * wrapReverseFlag;
  344. lineIndex++;
  345. itemsU = 0;
  346. }
  347. firstInLine = i;
  348. }
  349. }
  350. itemsU += sz.U;
  351. }
  352. // arrange the last line, if any
  353. if (firstInLine < children.Count)
  354. {
  355. ArrangeLine(new FlexLineInfo
  356. {
  357. ItemsU = itemsU,
  358. OffsetV = accumulatedV + freeItemV * wrapReverseAdd,
  359. LineV = curLineSizeArr[lineIndex].V,
  360. LineFreeV = freeItemV,
  361. LineU = uvFinalSize.U,
  362. ItemStartIndex = firstInLine,
  363. ItemEndIndex = children.Count,
  364. ScaleU = scaleU
  365. });
  366. }
  367. return arrangeSize;
  368. }
  369. private void ArrangeLine(FlexLineInfo lineInfo)
  370. {
  371. var flexDirection = FlexDirection;
  372. var flexWrap = FlexWrap;
  373. var justifyContent = JustifyContent;
  374. var alignItems = AlignItems;
  375. var isHorizontal = flexDirection == FlexDirection.Row || flexDirection == FlexDirection.RowReverse;
  376. var isReverse = flexDirection == FlexDirection.RowReverse || flexDirection == FlexDirection.ColumnReverse;
  377. var itemCount = lineInfo.ItemEndIndex - lineInfo.ItemStartIndex;
  378. var children = InternalChildren;
  379. var lineFreeU = lineInfo.LineU - lineInfo.ItemsU;
  380. var constraintFreeU = _uvConstraint.U - lineInfo.ItemsU;
  381. // calculate initial u
  382. var u = .0;
  383. if (isReverse)
  384. {
  385. u = justifyContent switch
  386. {
  387. FlexContentJustify.FlexStart => lineInfo.LineU,
  388. FlexContentJustify.SpaceBetween => lineInfo.LineU,
  389. FlexContentJustify.SpaceAround => lineInfo.LineU,
  390. FlexContentJustify.FlexEnd => lineInfo.ItemsU,
  391. FlexContentJustify.Center => (lineInfo.LineU + lineInfo.ItemsU) * 0.5,
  392. _ => u
  393. };
  394. }
  395. else
  396. {
  397. u = justifyContent switch
  398. {
  399. FlexContentJustify.FlexEnd => lineFreeU,
  400. FlexContentJustify.Center => lineFreeU * 0.5,
  401. _ => u
  402. };
  403. }
  404. u *= lineInfo.ScaleU;
  405. // apply FlexGrow
  406. var flexGrowUArr = new double[itemCount];
  407. if (constraintFreeU > 0)
  408. {
  409. var ignoreFlexGrow = true;
  410. var flexGrowSum = .0;
  411. for (var i = 0; i < itemCount; i++)
  412. {
  413. var flexGrow = GetFlexGrow(children[_orderList[i].Index]);
  414. ignoreFlexGrow &= MathHelper.IsVerySmall(flexGrow);
  415. flexGrowUArr[i] = flexGrow;
  416. flexGrowSum += flexGrow;
  417. }
  418. if (!ignoreFlexGrow)
  419. {
  420. var flexGrowItem = .0;
  421. if (flexGrowSum > 0)
  422. {
  423. flexGrowItem = constraintFreeU / flexGrowSum;
  424. lineInfo.ScaleU = 1;
  425. lineFreeU = 0; //line free U was used up
  426. }
  427. for (var i = 0; i < itemCount; i++)
  428. {
  429. flexGrowUArr[i] *= flexGrowItem;
  430. }
  431. }
  432. else
  433. {
  434. flexGrowUArr = new double[itemCount];
  435. }
  436. }
  437. // apply FlexShrink
  438. var flexShrinkUArr = new double[itemCount];
  439. if (constraintFreeU < 0)
  440. {
  441. var ignoreFlexShrink = true;
  442. var flexShrinkSum = .0;
  443. for (var i = 0; i < itemCount; i++)
  444. {
  445. var flexShrink = GetFlexShrink(children[_orderList[i].Index]);
  446. ignoreFlexShrink &= MathHelper.IsVerySmall(flexShrink - 1);
  447. flexShrinkUArr[i] = flexShrink;
  448. flexShrinkSum += flexShrink;
  449. }
  450. if (!ignoreFlexShrink)
  451. {
  452. var flexShrinkItem = .0;
  453. if (flexShrinkSum > 0)
  454. {
  455. flexShrinkItem = constraintFreeU / flexShrinkSum;
  456. lineInfo.ScaleU = 1;
  457. lineFreeU = 0; //line free U was used up
  458. }
  459. for (var i = 0; i < itemCount; i++)
  460. {
  461. flexShrinkUArr[i] *= flexShrinkItem;
  462. }
  463. }
  464. else
  465. {
  466. flexShrinkUArr = new double[itemCount];
  467. }
  468. }
  469. // calculate offset u
  470. var offsetUArr = new double[itemCount];
  471. if (lineFreeU > 0)
  472. {
  473. if (justifyContent == FlexContentJustify.SpaceBetween)
  474. {
  475. var freeItemU = lineFreeU / (itemCount - 1);
  476. for (var i = 1; i < itemCount; i++)
  477. {
  478. offsetUArr[i] = freeItemU;
  479. }
  480. }
  481. else if (justifyContent == FlexContentJustify.SpaceAround)
  482. {
  483. var freeItemU = lineFreeU / itemCount * 0.5;
  484. offsetUArr[0] = freeItemU;
  485. for (var i = 1; i < itemCount; i++)
  486. {
  487. offsetUArr[i] = freeItemU * 2;
  488. }
  489. }
  490. }
  491. // arrange item
  492. for (int i = lineInfo.ItemStartIndex, j = 0; i < lineInfo.ItemEndIndex; i++, j++)
  493. {
  494. var child = children[_orderList[i].Index];
  495. if (child == null) continue;
  496. var childSize = new UVSize(flexDirection, isHorizontal
  497. ? new Size(child.DesiredSize.Width * lineInfo.ScaleU, child.DesiredSize.Height)
  498. : new Size(child.DesiredSize.Width, child.DesiredSize.Height * lineInfo.ScaleU));
  499. childSize.U += flexGrowUArr[j] + flexShrinkUArr[j];
  500. if (isReverse)
  501. {
  502. u -= childSize.U;
  503. u -= offsetUArr[j];
  504. }
  505. else
  506. {
  507. u += offsetUArr[j];
  508. }
  509. var v = lineInfo.OffsetV;
  510. var alignSelf = GetAlignSelf(child);
  511. var alignment = alignSelf == FlexItemAlignment.Auto ? alignItems : (FlexItemsAlignment) alignSelf;
  512. switch (alignment)
  513. {
  514. case FlexItemsAlignment.Stretch:
  515. if (_lineCount == 1 && flexWrap == FlexWrap.NoWrap)
  516. {
  517. childSize.V = lineInfo.LineV + lineInfo.LineFreeV;
  518. }
  519. else
  520. {
  521. childSize.V = lineInfo.LineV;
  522. }
  523. break;
  524. case FlexItemsAlignment.FlexEnd:
  525. v += lineInfo.LineV - childSize.V;
  526. break;
  527. case FlexItemsAlignment.Center:
  528. v += (lineInfo.LineV - childSize.V) * 0.5;
  529. break;
  530. }
  531. child.Arrange(isHorizontal ? new Rect(u, v, childSize.U, childSize.V) : new Rect(v, u, childSize.V, childSize.U));
  532. if (!isReverse)
  533. {
  534. u += childSize.U;
  535. }
  536. }
  537. }
  538. private readonly struct FlexItemInfo : IComparable<FlexItemInfo>
  539. {
  540. public FlexItemInfo(int index, int order)
  541. {
  542. Index = index;
  543. Order = order;
  544. }
  545. private int Order { get; }
  546. public int Index { get; }
  547. public int CompareTo(FlexItemInfo other)
  548. {
  549. var orderCompare = Order.CompareTo(other.Order);
  550. if (orderCompare != 0) return orderCompare;
  551. return Index.CompareTo(other.Index);
  552. }
  553. }
  554. private struct FlexLineInfo
  555. {
  556. public double ItemsU { get; set; }
  557. public double OffsetV { get; set; }
  558. public double LineU { get; set; }
  559. public double LineV { get; set; }
  560. public double LineFreeV { get; set; }
  561. public int ItemStartIndex { get; set; }
  562. public int ItemEndIndex { get; set; }
  563. public double ScaleU { get; set; }
  564. }
  565. private struct UVSize
  566. {
  567. public UVSize(FlexDirection direction, Size size)
  568. {
  569. U = V = 0d;
  570. FlexDirection = direction;
  571. Width = size.Width;
  572. Height = size.Height;
  573. }
  574. public UVSize(FlexDirection direction)
  575. {
  576. U = V = 0d;
  577. FlexDirection = direction;
  578. }
  579. public double U { get; set; }
  580. public double V { get; set; }
  581. private FlexDirection FlexDirection { get; }
  582. public double Width
  583. {
  584. get => FlexDirection == FlexDirection.Row || FlexDirection == FlexDirection.RowReverse ? U : V;
  585. private set
  586. {
  587. if (FlexDirection == FlexDirection.Row || FlexDirection == FlexDirection.RowReverse)
  588. {
  589. U = value;
  590. }
  591. else
  592. {
  593. V = value;
  594. }
  595. }
  596. }
  597. public double Height
  598. {
  599. get => FlexDirection == FlexDirection.Row || FlexDirection == FlexDirection.RowReverse ? V : U;
  600. private set
  601. {
  602. if (FlexDirection == FlexDirection.Row || FlexDirection == FlexDirection.RowReverse)
  603. {
  604. V = value;
  605. }
  606. else
  607. {
  608. U = value;
  609. }
  610. }
  611. }
  612. }
  613. }