CPdfThumbnailUI.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. namespace ComPDFKit.Controls.PDFControlUI
  8. {
  9. public partial class CPDFThumbnailUI : UserControl
  10. {
  11. /// <summary>
  12. /// Click to select the event in the thumbnail list
  13. /// </summary>
  14. public event EventHandler<int> SelectionChanged;
  15. /// <summary>
  16. /// Scroll state change event
  17. /// </summary>
  18. public event EventHandler<ScrollChangedEventArgs> ViewChanged;
  19. private bool lockGoToPage;
  20. /// <summary>
  21. /// A list of thumbnail results
  22. /// </summary>
  23. private List<ThumbnailItem> thumbResultList=new List<ThumbnailItem>();
  24. public CPDFThumbnailUI()
  25. {
  26. InitializeComponent();
  27. }
  28. /// <summary>
  29. /// Select Result Change event
  30. /// </summary>
  31. private void ThumbListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  32. {
  33. if (!lockGoToPage)
  34. {
  35. SelectionChanged?.Invoke(this, ThumbListBox.SelectedIndex);
  36. }
  37. }
  38. /// <summary>
  39. /// Set the thumbnail list
  40. /// </summary>
  41. /// <param name="thumbList"></param>
  42. public void SetThumbResult(List<ThumbnailItem> thumbList)
  43. {
  44. thumbResultList?.Clear();
  45. ThumbListBox.ItemsSource = null;
  46. if (thumbList == null || thumbList.Count == 0)
  47. {
  48. return;
  49. }
  50. thumbResultList.AddRange(thumbList);
  51. ThumbListBox.ItemsSource = thumbResultList;
  52. ThumbListBox.UpdateLayout();
  53. }
  54. /// <summary>
  55. /// Content scrolling events
  56. /// </summary>
  57. private void ThumbListBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
  58. {
  59. ViewChanged?.Invoke(this, e);
  60. }
  61. /// <summary>
  62. /// Determines whether the specified object is visible
  63. /// </summary>
  64. public bool IsItemVisible(ThumbnailItem checkItem)
  65. {
  66. if (ThumbListBox == null || thumbResultList == null || thumbResultList.Count == 0)
  67. {
  68. return false;
  69. }
  70. try
  71. {
  72. int index = thumbResultList.IndexOf(checkItem);
  73. if (index != -1)
  74. {
  75. ListBoxItem itemContainer = (ListBoxItem)ThumbListBox.ItemContainerGenerator.ContainerFromIndex(index);
  76. if (itemContainer==null || itemContainer.IsVisible == false || itemContainer.Visibility != Visibility.Visible)
  77. {
  78. return false;
  79. }
  80. GeneralTransform transform = itemContainer.TransformToAncestor(ThumbListBox);
  81. Rect visualRect= transform.TransformBounds(new Rect(
  82. 0,
  83. 0,
  84. itemContainer.ActualWidth,
  85. itemContainer.ActualHeight
  86. ));
  87. Rect containerRect=new Rect(
  88. 0,
  89. 0,
  90. ThumbListBox.ActualWidth,
  91. ThumbListBox.ActualHeight);
  92. containerRect.Intersect(visualRect);
  93. if (containerRect.Width>1 && containerRect.Height>1)
  94. {
  95. return true;
  96. }
  97. }
  98. }
  99. catch(Exception ex)
  100. {
  101. }
  102. return false;
  103. }
  104. /// <summary>
  105. /// Select an object
  106. /// </summary>
  107. public void SelectItem(int checkIndex)
  108. {
  109. if(ThumbListBox!=null && thumbResultList != null && thumbResultList.Count>checkIndex && checkIndex>=0)
  110. {
  111. ThumbnailItem thumbItem= thumbResultList[checkIndex];
  112. if(IsItemVisible(thumbItem)==false)
  113. {
  114. ThumbListBox.ScrollIntoView(thumbItem);
  115. }
  116. lockGoToPage = false;
  117. ThumbListBox.SelectedIndex = checkIndex;
  118. }
  119. }
  120. /// <summary>
  121. /// Select an object
  122. /// </summary>
  123. public void SelectItemWithoutGoTo(int checkIndex)
  124. {
  125. if (ThumbListBox != null && thumbResultList != null && thumbResultList.Count > checkIndex && checkIndex >= 0)
  126. {
  127. ThumbnailItem thumbItem = thumbResultList[checkIndex];
  128. if (IsItemVisible(thumbItem) == false)
  129. {
  130. ThumbListBox.ScrollIntoView(thumbItem);
  131. }
  132. lockGoToPage = true;
  133. ThumbListBox.SelectedIndex = checkIndex;
  134. lockGoToPage = false;
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// Thumbnail object
  140. /// </summary>
  141. public class ThumbnailItem
  142. {
  143. /// <summary>
  144. /// Image width
  145. /// </summary>
  146. public int ImageWidth { get; set; }
  147. /// <summary>
  148. /// Image height
  149. /// </summary>
  150. public int ImageHeight { get; set; }
  151. /// <summary>
  152. ///Thumbnail width
  153. /// </summary>
  154. public int ThumbnailWidth { get; set; }
  155. /// <summary>
  156. /// Thumbnail height
  157. /// </summary>
  158. public int ThumbnailHeight { get; set; }
  159. /// <summary>
  160. /// Display page numbers
  161. /// </summary>
  162. public string ShowPageText
  163. {
  164. get
  165. {
  166. if (PageIndex >= 0)
  167. {
  168. return (PageIndex + 1).ToString();
  169. }
  170. return string.Empty;
  171. }
  172. }
  173. /// <summary>
  174. /// Page index (starts with 0)
  175. /// </summary>
  176. public int PageIndex { get; set; } = -1;
  177. /// <summary>
  178. /// Thumbnails are like content
  179. /// </summary>
  180. public Image ImageData { get; set; } = new Image();
  181. }
  182. }