RecentFilesContent.xaml.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using ComPDFKitViewer.PdfViewer;
  2. using Microsoft.Win32;
  3. using PDF_Master.CustomControl;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Properties;
  6. using PDF_Master.ViewModels.HomePanel.RecentFiles;
  7. using PDFSettings;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Input;
  17. using System.Windows.Interop;
  18. using System.Windows.Navigation;
  19. using winform = System.Windows.Forms;
  20. namespace PDF_Master.Views.HomePanel.RecentFiles
  21. {
  22. public partial class RecentFilesContent : UserControl
  23. {
  24. private RecentFilesContentViewModel ViewModel => DataContext as RecentFilesContentViewModel;
  25. public RecentFilesContent()
  26. {
  27. InitializeComponent();
  28. }
  29. #region 初始化
  30. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  31. {
  32. InitVariables();
  33. InitEvents();
  34. }
  35. private void InitVariables()
  36. {
  37. if (RecentFilesList.SelectedItems != null)
  38. RecentFilesList.SelectedItems.Clear();
  39. if (GridRecentFilesList.SelectedItems != null)
  40. GridRecentFilesList.SelectedItems.Clear();
  41. }
  42. private void InitEvents()
  43. {
  44. ViewModel.RecentFilesSelectionHandler -= ViewModel_RecentFilesSelection;
  45. ViewModel.RecentFilesSelectionHandler += ViewModel_RecentFilesSelection;
  46. }
  47. #endregion
  48. #region UI操作
  49. private void ViewModel_RecentFilesSelection(object sender, bool e)
  50. {
  51. //if (e)
  52. //{
  53. // RecentFilesList.SelectedItems.Clear();
  54. // if (GridRecentFilesList.SelectedItems.Count > 0)
  55. // {
  56. // for (int i = 0; i < GridRecentFilesList.SelectedItems.Count; i++)
  57. // {
  58. // RecentFilesList.SelectedItems.Add(GridRecentFilesList.SelectedItems[i]);
  59. // }
  60. // }
  61. //}
  62. //else
  63. //{
  64. // GridRecentFilesList.SelectedItems.Clear();
  65. // if (RecentFilesList.SelectedItems.Count > 0)
  66. // {
  67. // for (int i = 0; i < RecentFilesList.SelectedItems.Count; i++)
  68. // {
  69. // GridRecentFilesList.SelectedItems.Add(RecentFilesList.SelectedItems[i]);
  70. // }
  71. // }
  72. //}
  73. }
  74. private void ContextMenu_Opened(object sender, RoutedEventArgs e)
  75. {
  76. try
  77. {
  78. ListView listView;
  79. if (BtnListMode.IsChecked == true)
  80. listView = RecentFilesList;
  81. else
  82. listView = GridRecentFilesList;
  83. var contexMenu = sender as ContextMenu;
  84. if (contexMenu != null)
  85. {
  86. foreach (var item in contexMenu.Items)
  87. {
  88. var menuitem = item as MenuItem;
  89. menuitem.Visibility = Visibility.Visible;
  90. }
  91. }
  92. //临时绑定文案
  93. (contexMenu.Items[0] as MenuItem).Header = ViewModel.T_MenuShow;
  94. (contexMenu.Items[1] as MenuItem).Header = ViewModel.T_MenuOpenFile;
  95. (contexMenu.Items[2] as MenuItem).Header = ViewModel.T_MenuRemove;
  96. if (listView.SelectedItems.Count >= 2)
  97. {
  98. foreach (var item in contexMenu.Items)
  99. {
  100. var menuitem = item as MenuItem;
  101. if (menuitem.Name == "FilePathMenuItem")
  102. menuitem.Visibility = Visibility.Collapsed;
  103. if(menuitem.Name == "MenuOpenFile")
  104. {
  105. menuitem.Visibility = Visibility.Visible;
  106. }
  107. }
  108. }
  109. else
  110. {
  111. foreach (var item in contexMenu.Items)
  112. {
  113. var menuitem = item as MenuItem;
  114. if (menuitem.Name == "MenuOpenFile")
  115. {
  116. menuitem.Visibility = Visibility.Collapsed;
  117. }
  118. }
  119. }
  120. }
  121. catch { }
  122. }
  123. /// <summary>
  124. /// 禁用触发鼠标右键,列表项被选中
  125. /// </summary>
  126. private void ListViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  127. {
  128. Trace.WriteLine("Preview MouseRightButtonDown");
  129. e.Handled = true;
  130. }
  131. #endregion
  132. #region Events
  133. /// <summary>
  134. /// 右键菜单:删除文件记录
  135. /// </summary>
  136. private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
  137. {
  138. ListView listView;
  139. if (BtnListMode.IsChecked == true)
  140. listView = RecentFilesList;
  141. else
  142. listView = GridRecentFilesList;
  143. if (listView.SelectedItems == null || listView.SelectedItems.Count == 0)
  144. {
  145. var item = (sender as MenuItem).DataContext as OpenFileInfo;
  146. if (item != null)
  147. {
  148. ViewModel?.RemoveFileItemCommand.Execute(item);
  149. }
  150. }
  151. else
  152. {
  153. ViewModel?.RemoveFilesFromContainerCommand.Execute(listView.SelectedItems);
  154. }
  155. }
  156. /// <summary>
  157. /// 右键菜单:打开文件路径
  158. /// </summary>
  159. /// <param name="sender"></param>
  160. /// <param name="e"></param>
  161. private void FilePathMenuItem_Click(object sender, RoutedEventArgs e)
  162. {
  163. if ((sender as MenuItem) != null)
  164. {
  165. var menuItem = sender as MenuItem;
  166. var file = menuItem.DataContext as OpenFileInfo;
  167. ViewModel?.ExplorerFileCommand.Execute(file);
  168. }
  169. }
  170. /// <summary>
  171. /// 双击打开最近列表
  172. /// </summary>
  173. private void RecentFilesList_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  174. {
  175. var item = sender as ListViewItem;
  176. if (item != null && (item.DataContext as OpenFileInfo) != null)
  177. {
  178. var fileInfo = item.DataContext as OpenFileInfo;
  179. if(fileInfo != null)
  180. {
  181. ViewModel?.OpenRecentFilesCommand.Execute(fileInfo);
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// 文件列表项:打开文件
  187. /// </summary>
  188. private void DocItemListViewControl_OpenFileItemHandler(object sender, RoutedEventArgs e)
  189. {
  190. var item = (sender as MenuItem).DataContext as OpenFileInfo;
  191. if (item != null)
  192. {
  193. ViewModel?.OpenRecentFilesCommand.Execute(item);
  194. }
  195. }
  196. /// <summary>
  197. /// 文件列表项:删除文件记录
  198. /// </summary>
  199. private void DocItemListViewControl_DeleteItemHandler(object sender, RoutedEventArgs e)
  200. {
  201. var item = (sender as MenuItem).DataContext as OpenFileInfo;
  202. if (item != null)
  203. {
  204. ViewModel?.RemoveFileItemCommand.Execute(item);
  205. }
  206. }
  207. #endregion
  208. private void UserControl_KeyDown(object sender, KeyEventArgs e)
  209. {
  210. if (e.Key == Key.Escape)
  211. {
  212. //Windows:最近文件列表补充点击空白处取消选中;移除Esc取消选中逻辑
  213. //点击ESC取消选中
  214. //if (GridRecentFilesList.Visibility == Visibility.Visible)
  215. //{
  216. // GridRecentFilesList.SelectedIndex = -1;
  217. //}
  218. //else
  219. //{
  220. // RecentFilesList.SelectedIndex = -1;
  221. //}
  222. }
  223. }
  224. private void MenuOpenFile_Click(object sender, RoutedEventArgs e)
  225. {
  226. ListView listView;
  227. if (BtnListMode.IsChecked == true)
  228. listView = RecentFilesList;
  229. else
  230. listView = GridRecentFilesList;
  231. if (listView.SelectedItems.Count >=2)
  232. {
  233. List<string> files = new List<string>();
  234. foreach (OpenFileInfo item in listView.SelectedItems)
  235. {
  236. var fileInfo = item;
  237. if (!File.Exists(fileInfo.FilePath))
  238. {
  239. SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
  240. }
  241. else
  242. {
  243. files.Add(fileInfo.FilePath);
  244. }
  245. }
  246. ViewModel?.LoadPdfViewer(files.ToArray());
  247. }
  248. }
  249. private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
  250. {
  251. //已经加载后,切换页签时,确认是否要刷新数据源
  252. if(IsLoaded&&(bool)e.NewValue)
  253. {
  254. ViewModel.UpdateItemSoucre();
  255. }
  256. }
  257. }
  258. }