SearchContent.xaml.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using PDF_Master.CustomControl;
  2. using PDF_Master.EventAggregators;
  3. using PDF_Master.Model.BOTA;
  4. using PDF_Master.ViewModels.BOTA;
  5. using Prism.Events;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.ComponentModel;
  10. using System.Linq;
  11. using System.Runtime.CompilerServices;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace PDF_Master.Views.BOTA
  24. {
  25. /// <summary>
  26. /// SearchContent.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class SearchContent : UserControl
  29. {
  30. private bool Updata = false;
  31. private string unicode = "";
  32. public SearchContent()
  33. {
  34. InitializeComponent();
  35. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  36. }
  37. public SearchContent(IEventAggregator eventAggregator) : this()
  38. {
  39. //Windows: BOTA列表,移除点击阅读页取消选中逻辑
  40. //eventAggregator.GetEvent<CleanSelectAllEvent>().Subscribe(CleanSelectAll, e => e.Unicode == unicode);
  41. }
  42. private void CleanSelectAll(CleanSelectAllArgs obj)
  43. {
  44. SearchResultList.SelectedIndex = -1;
  45. (DataContext as SearchContentViewModel).CleaSelect();
  46. if (Updata)
  47. {
  48. if (string.IsNullOrEmpty(SearchText.Text))
  49. {
  50. return;
  51. }
  52. (DataContext as SearchContentViewModel).SearchText(SearchText.Text);
  53. HistorySearchText.Remove(SearchText.Text);
  54. if (HistorySearchText.Count >= MaxHistoryCount)
  55. {
  56. HistorySearchText.RemoveAt(HistorySearchText.Count - 1);
  57. }
  58. HistorySearchText.Insert(0, SearchText.Text);
  59. }
  60. }
  61. private List<string> HistorySearchText = new List<string>();
  62. private const int MaxHistoryCount = 10;
  63. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  64. {
  65. if (e.Key == Key.Enter)
  66. {
  67. TextBox text = e.Source as TextBox;
  68. if (text == null)
  69. {
  70. return;
  71. }
  72. if (string.IsNullOrEmpty(text.Text))
  73. {
  74. return;
  75. }
  76. (DataContext as SearchContentViewModel).SearchText(text.Text);
  77. HistorySearchText.Remove(text.Text);
  78. if (HistorySearchText.Count >= MaxHistoryCount)
  79. {
  80. HistorySearchText.RemoveAt(HistorySearchText.Count - 1);
  81. }
  82. HistorySearchText.Insert(0, text.Text);
  83. }
  84. }
  85. /// <summary>
  86. /// 控制收起与展开
  87. /// </summary>
  88. private void BtnExptend_Click(object sender, RoutedEventArgs e)
  89. {
  90. var btn = sender as PathButton;
  91. if (btn == null) return;
  92. btn.IsChecked = !btn.IsChecked;
  93. var item = (sender as FrameworkElement).DataContext as CollectionViewGroup;
  94. if (item != null)
  95. {
  96. foreach (object item2 in item.Items)
  97. {
  98. SearchItem searchItem = item2 as SearchItem;
  99. if (searchItem.TextProperty.ItemVisibility == Visibility.Visible)
  100. {
  101. searchItem.TextProperty.ItemVisibility = Visibility.Collapsed;
  102. }
  103. else
  104. {
  105. searchItem.TextProperty.ItemVisibility = Visibility.Visible;
  106. }
  107. }
  108. }
  109. }
  110. private void Create_Click(object sender, RoutedEventArgs e)
  111. {
  112. MenuItem menuItem = sender as MenuItem;
  113. if (menuItem == null)
  114. {
  115. return;
  116. }
  117. foreach (object item in SearchResultList.SelectedItems)
  118. {
  119. (DataContext as SearchContentViewModel).CreateAnnotate(item, menuItem.Tag.ToString());
  120. }
  121. }
  122. private void TextBoxEx_Initialized(object sender, EventArgs e)
  123. {
  124. var btn = sender as TextBoxEx;
  125. if (btn != null)
  126. {
  127. btn.ContextMenu = null;
  128. }
  129. }
  130. private void PathButton_Click(object sender, RoutedEventArgs e)
  131. {
  132. ContextSearchText.PlacementTarget = SearchText;
  133. ContextSearchText.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
  134. ContextSearchText.IsOpen = true;
  135. UpDataItem();
  136. }
  137. private void UpDataItem()
  138. {
  139. foreach (var item in ContextSearchText.Items)
  140. {
  141. if (item is MenuItem)
  142. {
  143. (item as MenuItem).Click -= SearchContent_Click;
  144. }
  145. }
  146. ContextSearchText.Items.Clear();
  147. ContextSearchText.Items.Add(IgnoreItem);
  148. if (HistorySearchText.Count > 0)
  149. {
  150. ContextSearchText.Items.Add(IgnoreSeparator);
  151. }
  152. foreach (var item in HistorySearchText)
  153. {
  154. MenuItem Searchitem = new MenuItem();
  155. Searchitem.Height = 32;
  156. Searchitem.Header = item;
  157. Searchitem.Padding = new Thickness(0, 7, 0, 7);
  158. Searchitem.Click += SearchContent_Click;
  159. ContextSearchText.Items.Add(Searchitem);
  160. }
  161. if (HistorySearchText.Count > 0)
  162. {
  163. ContextSearchText.Items.Add(ClearSeparator);
  164. ContextSearchText.Items.Add(ClearItem);
  165. }
  166. }
  167. private void SearchContent_Click(object sender, RoutedEventArgs e)
  168. {
  169. MenuItem item = sender as MenuItem;
  170. if (item != null)
  171. {
  172. SearchText.Text = item.Header.ToString();
  173. (DataContext as SearchContentViewModel).SearchText(SearchText.Text);
  174. }
  175. }
  176. private void ClearItem_Click(object sender, RoutedEventArgs e)
  177. {
  178. HistorySearchText.Clear();
  179. UpDataItem();
  180. }
  181. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  182. {
  183. SearchText.Focus();
  184. }
  185. private void UserControl_KeyDown(object sender, KeyEventArgs e)
  186. {
  187. if (e.Key == Key.Escape)
  188. {
  189. //Windows:BOTA列表,移除Esc取消选中逻辑
  190. //SearchResultList.SelectedIndex = -1;
  191. //e.Handled = true;
  192. }
  193. }
  194. private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
  195. {
  196. SearchContent searchContent = sender as SearchContent;
  197. if (searchContent != null && searchContent.ActualWidth > 209 && searchContent.ActualWidth < 216)
  198. {
  199. SearchText.Focus();
  200. }
  201. }
  202. private void SearchResultList_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  203. {
  204. if (e.LeftButton == MouseButtonState.Pressed)
  205. {
  206. var pos = e.GetPosition(SearchResultList);
  207. var result = VisualTreeHelper.HitTest(SearchResultList, pos);
  208. if (result != null)
  209. {
  210. //获取当前鼠标指针下的容器
  211. var listBoxItem = Helper.CommonHelper.FindVisualParent<ListViewItem>(result.VisualHit);
  212. if (listBoxItem == null)
  213. {
  214. if (SearchResultList.SelectedItem != null)
  215. {
  216. SearchResultList.SelectedIndex = -1;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. private void SearchText_LostFocus(object sender, RoutedEventArgs e)
  223. {
  224. Updata = false;
  225. }
  226. private void SearchText_GotFocus(object sender, RoutedEventArgs e)
  227. {
  228. Updata = true;
  229. }
  230. }
  231. }