ColLayout.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.ComponentModel;
  3. using System.Globalization;
  4. using System.Text;
  5. using System.Windows.Markup;
  6. using HandyControl.Data;
  7. using HandyControl.Tools.Converter;
  8. namespace HandyControl.Tools.Extension;
  9. [TypeConverter(typeof(ColLayoutConverter))]
  10. public class ColLayout : MarkupExtension
  11. {
  12. public const int ColMaxCellCount = 24;
  13. public const int HalfColMaxCellCount = 12;
  14. public const int XsMaxWidth = 768;
  15. public const int SmMaxWidth = 992;
  16. public const int MdMaxWidth = 1200;
  17. public const int LgMaxWidth = 1920;
  18. public const int XlMaxWidth = 2560;
  19. public int Xs { get; set; } = 24;
  20. public int Sm { get; set; } = 12;
  21. public int Md { get; set; } = 8;
  22. public int Lg { get; set; } = 6;
  23. public int Xl { get; set; } = 4;
  24. public int Xxl { get; set; } = 2;
  25. public ColLayout()
  26. {
  27. }
  28. public ColLayout(int uniformWidth)
  29. {
  30. Xs = uniformWidth;
  31. Sm = uniformWidth;
  32. Md = uniformWidth;
  33. Lg = uniformWidth;
  34. Xl = uniformWidth;
  35. Xxl = uniformWidth;
  36. }
  37. public ColLayout(int xs, int sm, int md, int lg, int xl, int xxl)
  38. {
  39. Xs = xs;
  40. Sm = sm;
  41. Md = md;
  42. Lg = lg;
  43. Xl = xl;
  44. Xxl = xxl;
  45. }
  46. public override object ProvideValue(IServiceProvider serviceProvider)
  47. {
  48. return new ColLayout
  49. {
  50. Xs = Xs,
  51. Sm = Sm,
  52. Md = Md,
  53. Lg = Lg,
  54. Xl = Xl,
  55. Xxl = Xxl
  56. };
  57. }
  58. public static ColLayoutStatus GetLayoutStatus(double width)
  59. {
  60. if (width < MdMaxWidth)
  61. {
  62. if (width < SmMaxWidth)
  63. {
  64. if (width < XsMaxWidth)
  65. {
  66. return ColLayoutStatus.Xs;
  67. }
  68. return ColLayoutStatus.Sm;
  69. }
  70. return ColLayoutStatus.Md;
  71. }
  72. if (width < XlMaxWidth)
  73. {
  74. if (width < LgMaxWidth)
  75. {
  76. return ColLayoutStatus.Lg;
  77. }
  78. return ColLayoutStatus.Xl;
  79. }
  80. return ColLayoutStatus.Xxl;
  81. }
  82. public override string ToString()
  83. {
  84. var cultureInfo = CultureInfo.CurrentCulture;
  85. var listSeparator = TokenizerHelper.GetNumericListSeparator(cultureInfo);
  86. // Initial capacity [128] is an estimate based on a sum of:
  87. // 72 = 6x double (twelve digits is generous for the range of values likely)
  88. // 4 = 4x separator characters
  89. var sb = new StringBuilder(128);
  90. sb.Append(Xs.ToString(cultureInfo));
  91. sb.Append(listSeparator);
  92. sb.Append(Sm.ToString(cultureInfo));
  93. sb.Append(listSeparator);
  94. sb.Append(Md.ToString(cultureInfo));
  95. sb.Append(listSeparator);
  96. sb.Append(Lg.ToString(cultureInfo));
  97. sb.Append(listSeparator);
  98. sb.Append(Xl.ToString(cultureInfo));
  99. sb.Append(listSeparator);
  100. sb.Append(Xxl.ToString(cultureInfo));
  101. return sb.ToString();
  102. }
  103. }