AnnotationReplyListControl.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace ComPDFKit.Controls.PDFControlUI
  17. {
  18. /// <summary>
  19. /// Interaction logic for AnnotationReplyListControl.xaml
  20. /// </summary>
  21. public partial class AnnotationReplyListControl : UserControl
  22. {
  23. public ObservableCollection<CPDFAnnotationListUI.ReplyData> ReplyListSource
  24. {
  25. get => (ObservableCollection<CPDFAnnotationListUI.ReplyData>)GetValue(ReplyListProperty);
  26. set => SetValue(ReplyListProperty, value);
  27. }
  28. /// <summary>
  29. /// The source of the reply list.
  30. /// </summary>
  31. public static readonly DependencyProperty ReplyListProperty = DependencyProperty.Register(
  32. nameof(ReplyListSource),
  33. typeof(ObservableCollection<CPDFAnnotationListUI.ReplyData>),
  34. typeof(AnnotationReplyListControl),
  35. new PropertyMetadata(null, OnReplyListSourceChanged));
  36. private static void OnReplyListSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  37. {
  38. var newReplyListSource = e.NewValue as ObservableCollection<CPDFAnnotationListUI.ReplyData>;
  39. if (d is AnnotationReplyListControl control)
  40. {
  41. control.ReplyList.ItemsSource = newReplyListSource;
  42. }
  43. }
  44. public bool IsShowInput
  45. {
  46. get => (bool)GetValue(IsShowInputProperty);
  47. set => SetValue(IsShowInputProperty, value);
  48. }
  49. /// <summary>
  50. /// The visibility of the reply input.
  51. /// </summary>
  52. public static readonly DependencyProperty IsShowInputProperty = DependencyProperty.Register(
  53. nameof(IsShowInput),
  54. typeof(bool),
  55. typeof(AnnotationReplyListControl),
  56. new PropertyMetadata(false, OnIsShowInputChanged));
  57. public static event EventHandler ReplyListChanged;
  58. public static event EventHandler<CPDFAnnotationListUI.ReplyData> ReplyDeleted;
  59. private static void OnIsShowInputChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  60. {
  61. if (d is AnnotationReplyListControl control)
  62. {
  63. control.SetReplyInputVisibility((bool)e.NewValue);
  64. }
  65. }
  66. public bool IsShowList
  67. {
  68. get => (bool)GetValue(IsShowListProperty);
  69. set => SetValue(IsShowListProperty, value);
  70. }
  71. /// <summary>
  72. /// The visibility of the reply list.
  73. /// </summary>
  74. public static readonly DependencyProperty IsShowListProperty = DependencyProperty.Register(
  75. nameof(IsShowList),
  76. typeof(bool),
  77. typeof(AnnotationReplyListControl),
  78. new PropertyMetadata(false, OnIsShowListChanged));
  79. public void SetReplyInputVisibility(bool isVisible)
  80. {
  81. if (isVisible)
  82. {
  83. ReplyGrid.RowDefinitions[0].Height = new GridLength(1, GridUnitType.Auto);
  84. ReplyGrid.RowDefinitions[1].Height = new GridLength(1, GridUnitType.Auto);
  85. }
  86. else
  87. {
  88. ReplyGrid.RowDefinitions[0].Height = new GridLength(0);
  89. ReplyGrid.RowDefinitions[1].Height = new GridLength(0);
  90. }
  91. }
  92. private static void OnIsShowListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  93. {
  94. if (d is AnnotationReplyListControl control)
  95. {
  96. control.SetReplyListVisibility((bool)e.NewValue);
  97. }
  98. }
  99. public void SetReplyListVisibility(bool isVisible)
  100. {
  101. ReplyGrid.RowDefinitions[2].Height = isVisible ? new GridLength(1, GridUnitType.Auto) : new GridLength(0);
  102. }
  103. public static void InvokeReplyListChanged()
  104. {
  105. ReplyListChanged?.Invoke(null, EventArgs.Empty);
  106. }
  107. public static void InvokeReplyDeleted(CPDFAnnotationListUI.ReplyData replyData)
  108. {
  109. ReplyDeleted?.Invoke(null, replyData);
  110. }
  111. public AnnotationReplyListControl()
  112. {
  113. InitializeComponent();
  114. }
  115. private void ReplyButton_Click(object sender, RoutedEventArgs e)
  116. {
  117. if (sender is Button button)
  118. {
  119. if (button.DataContext is AnnotationBindData annotBindData)
  120. {
  121. if (!string.IsNullOrEmpty(InputTxb.Text))
  122. {
  123. var replyAnnot = annotBindData.BindProperty.Annotation.CreateReplyAnnotation();
  124. replyAnnot.SetContent(InputTxb.Text);
  125. replyAnnot.SetAuthor(Data.CPDFAnnotationData.Author);
  126. annotBindData.BindProperty.ReplyList.Add(new CPDFAnnotationListUI.ReplyData
  127. {
  128. ReplyAnnotation = replyAnnot
  129. });
  130. InputTxb.Text = string.Empty;
  131. InvokeReplyListChanged();
  132. }
  133. }
  134. }
  135. }
  136. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  137. {
  138. IsShowInput = false;
  139. }
  140. private void ContentBox_LostFocus(object sender, RoutedEventArgs e)
  141. {
  142. if (sender is TextBox textBox)
  143. {
  144. textBox.Visibility = Visibility.Collapsed;
  145. if (textBox.DataContext is CPDFAnnotationListUI.ReplyData replyData)
  146. {
  147. replyData.ReplyAnnotation.SetContent(textBox.Text);
  148. }
  149. }
  150. }
  151. }
  152. public class ShowContentBoxCommand : ICommand
  153. {
  154. public event EventHandler CanExecuteChanged;
  155. public bool CanExecute(object parameter)
  156. {
  157. return true;
  158. }
  159. public void Execute(object parameter)
  160. {
  161. if (parameter is TextBox textBox)
  162. {
  163. textBox.Visibility = Visibility.Visible;
  164. textBox.Focus();
  165. textBox.SelectAll();
  166. }
  167. }
  168. }
  169. public class DeleteReplyCommand : ICommand
  170. {
  171. public event EventHandler CanExecuteChanged;
  172. public bool CanExecute(object parameter)
  173. {
  174. return true;
  175. }
  176. public void Execute(object parameter)
  177. {
  178. if (parameter is CPDFAnnotationListUI.ReplyData replyData)
  179. {
  180. replyData.ReplyAnnotation.RemoveAnnot();
  181. AnnotationReplyListControl.InvokeReplyListChanged();
  182. AnnotationReplyListControl.InvokeReplyDeleted(replyData);
  183. }
  184. }
  185. }
  186. }