using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace ComPDFKit.Controls.PDFControlUI { public partial class CPDFThumbnailUI : UserControl { /// /// Click to select the event in the thumbnail list /// public event EventHandler SelectionChanged; /// /// Scroll state change event /// public event EventHandler ViewChanged; private bool lockGoToPage; /// /// A list of thumbnail results /// private List thumbResultList=new List(); public CPDFThumbnailUI() { InitializeComponent(); } /// /// Select Result Change event /// private void ThumbListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (!lockGoToPage) { SelectionChanged?.Invoke(this, ThumbListBox.SelectedIndex); } } /// /// Set the thumbnail list /// /// public void SetThumbResult(List thumbList) { thumbResultList?.Clear(); ThumbListBox.ItemsSource = null; if (thumbList == null || thumbList.Count == 0) { return; } thumbResultList.AddRange(thumbList); ThumbListBox.ItemsSource = thumbResultList; ThumbListBox.UpdateLayout(); } /// /// Content scrolling events /// private void ThumbListBox_ScrollChanged(object sender, ScrollChangedEventArgs e) { ViewChanged?.Invoke(this, e); } /// /// Determines whether the specified object is visible /// public bool IsItemVisible(ThumbnailItem checkItem) { if (ThumbListBox == null || thumbResultList == null || thumbResultList.Count == 0) { return false; } try { int index = thumbResultList.IndexOf(checkItem); if (index != -1) { ListBoxItem itemContainer = (ListBoxItem)ThumbListBox.ItemContainerGenerator.ContainerFromIndex(index); if (itemContainer==null || itemContainer.IsVisible == false || itemContainer.Visibility != Visibility.Visible) { return false; } GeneralTransform transform = itemContainer.TransformToAncestor(ThumbListBox); Rect visualRect= transform.TransformBounds(new Rect( 0, 0, itemContainer.ActualWidth, itemContainer.ActualHeight )); Rect containerRect=new Rect( 0, 0, ThumbListBox.ActualWidth, ThumbListBox.ActualHeight); containerRect.Intersect(visualRect); if (containerRect.Width>1 && containerRect.Height>1) { return true; } } } catch(Exception ex) { } return false; } /// /// Select an object /// public void SelectItem(int checkIndex) { if(ThumbListBox!=null && thumbResultList != null && thumbResultList.Count>checkIndex && checkIndex>=0) { ThumbnailItem thumbItem= thumbResultList[checkIndex]; if(IsItemVisible(thumbItem)==false) { ThumbListBox.ScrollIntoView(thumbItem); } lockGoToPage = false; ThumbListBox.SelectedIndex = checkIndex; } } /// /// Select an object /// public void SelectItemWithoutGoTo(int checkIndex) { if (ThumbListBox != null && thumbResultList != null && thumbResultList.Count > checkIndex && checkIndex >= 0) { ThumbnailItem thumbItem = thumbResultList[checkIndex]; if (IsItemVisible(thumbItem) == false) { ThumbListBox.ScrollIntoView(thumbItem); } lockGoToPage = true; ThumbListBox.SelectedIndex = checkIndex; lockGoToPage = false; } } } /// /// Thumbnail object /// public class ThumbnailItem { /// /// Image width /// public int ImageWidth { get; set; } /// /// Image height /// public int ImageHeight { get; set; } /// ///Thumbnail width /// public int ThumbnailWidth { get; set; } /// /// Thumbnail height /// public int ThumbnailHeight { get; set; } /// /// Display page numbers /// public string ShowPageText { get { if (PageIndex >= 0) { return (PageIndex + 1).ToString(); } return string.Empty; } } /// /// Page index (starts with 0) /// public int PageIndex { get; set; } = -1; /// /// Thumbnails are like content /// public Image ImageData { get; set; } = new Image(); } }