PagedListView.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. #if UNITY_2019_1_OR_NEWER
  4. using UnityEngine.UIElements;
  5. #else
  6. using UnityEngine.Experimental.UIElements;
  7. using UnityEngine.Experimental.UIElements.StyleEnums;
  8. #endif
  9. namespace UnityEditor.Collaboration
  10. {
  11. internal interface IPagerData
  12. {
  13. int curPage { get; }
  14. int totalPages { get; }
  15. PageChangeAction OnPageChanged { get; }
  16. }
  17. internal class PagerElement : VisualElement
  18. {
  19. IPagerData m_Data;
  20. readonly Label m_PageText;
  21. readonly Button m_DownButton;
  22. readonly Button m_UpButton;
  23. public PagerElement(IPagerData dataSource)
  24. {
  25. m_Data = dataSource;
  26. this.style.flexDirection = FlexDirection.Row;
  27. this.style.alignSelf = Align.Center;
  28. Add(m_DownButton = new Button(OnPageDownClicked) {text = "\u25c5 Newer"});
  29. m_DownButton.AddToClassList("PagerDown");
  30. m_PageText = new Label();
  31. m_PageText.AddToClassList("PagerLabel");
  32. Add(m_PageText);
  33. Add(m_UpButton = new Button(OnPageUpClicked) {text = "Older \u25bb"});
  34. m_UpButton.AddToClassList("PagerUp");
  35. UpdateControls();
  36. }
  37. void OnPageDownClicked()
  38. {
  39. CollabAnalytics.SendUserAction(CollabAnalytics.historyCategoryString, "NewerPage");
  40. m_Data.OnPageChanged(m_Data.curPage - 1);
  41. }
  42. void OnPageUpClicked()
  43. {
  44. CollabAnalytics.SendUserAction(CollabAnalytics.historyCategoryString, "OlderPage");
  45. m_Data.OnPageChanged(m_Data.curPage + 1);
  46. }
  47. public void Refresh()
  48. {
  49. UpdateControls();
  50. }
  51. void UpdateControls()
  52. {
  53. var curPage = m_Data.curPage;
  54. var totalPages = m_Data.totalPages;
  55. m_PageText.text = (curPage + 1) + " / " + totalPages;
  56. m_DownButton.SetEnabled(curPage > 0);
  57. m_UpButton.SetEnabled(curPage < totalPages - 1);
  58. }
  59. }
  60. internal enum PagerLocation
  61. {
  62. Top,
  63. Bottom,
  64. }
  65. internal class PagedListView : VisualElement, IPagerData
  66. {
  67. public const int DefaultItemsPerPage = 10;
  68. readonly VisualElement m_ItemContainer;
  69. readonly PagerElement m_PagerTop, m_PagerBottom;
  70. int m_PageSize = DefaultItemsPerPage;
  71. IEnumerable<VisualElement> m_Items;
  72. int m_TotalItems;
  73. int m_CurPage;
  74. public int pageSize
  75. {
  76. set { m_PageSize = value; }
  77. }
  78. public IEnumerable<VisualElement> items
  79. {
  80. set
  81. {
  82. m_Items = value;
  83. LayoutItems();
  84. }
  85. }
  86. public int totalItems
  87. {
  88. set
  89. {
  90. if (m_TotalItems == value)
  91. return;
  92. m_TotalItems = value;
  93. UpdatePager();
  94. }
  95. }
  96. public PageChangeAction OnPageChanged { get; set; }
  97. public PagedListView()
  98. {
  99. m_PagerTop = new PagerElement(this);
  100. m_ItemContainer = new VisualElement()
  101. {
  102. name = "PagerItems",
  103. };
  104. Add(m_ItemContainer);
  105. m_Items = new List<VisualElement>();
  106. m_PagerBottom = new PagerElement(this);
  107. }
  108. void LayoutItems()
  109. {
  110. m_ItemContainer.Clear();
  111. foreach (var item in m_Items)
  112. {
  113. m_ItemContainer.Add(item);
  114. }
  115. }
  116. void UpdatePager()
  117. {
  118. if (m_PagerTop.parent != this && totalPages > 1 && curPage > 0)
  119. Insert(0, m_PagerTop);
  120. if (m_PagerTop.parent == this && (totalPages <= 1 || curPage == 0))
  121. Remove(m_PagerTop);
  122. if (m_PagerBottom.parent != this && totalPages > 1)
  123. Add(m_PagerBottom);
  124. if (m_PagerBottom.parent == this && totalPages <= 1)
  125. Remove(m_PagerBottom);
  126. m_PagerTop.Refresh();
  127. m_PagerBottom.Refresh();
  128. }
  129. int pageCount
  130. {
  131. get
  132. {
  133. var pages = m_TotalItems / m_PageSize;
  134. if (m_TotalItems % m_PageSize > 0)
  135. pages++;
  136. return pages;
  137. }
  138. }
  139. public int curPage
  140. {
  141. get { return m_CurPage; }
  142. set
  143. {
  144. m_CurPage = value;
  145. UpdatePager();
  146. }
  147. }
  148. public int totalPages
  149. {
  150. get
  151. {
  152. var extraPage = 0;
  153. if (m_TotalItems % m_PageSize > 0)
  154. extraPage = 1;
  155. return m_TotalItems / m_PageSize + extraPage;
  156. }
  157. }
  158. }
  159. }