RecentFilesView.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. using ComPDFKitViewer.PdfViewer;
  2. using Microsoft.Win32;
  3. using PDF_Office.CustomControl;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Properties;
  6. using PDFSettings.Settings;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Input;
  16. using System.Windows.Interop;
  17. using System.Windows.Navigation;
  18. using winform = System.Windows.Forms;
  19. namespace PDF_Office.Views.HomePanel.RecentFiles
  20. {
  21. public partial class RecentFilesView : UserControl
  22. {
  23. private ObservableCollection<OpenFileInfo> RecentFilesGroup = new ObservableCollection<OpenFileInfo>();
  24. public RecentFilesView()
  25. {
  26. InitializeComponent();
  27. }
  28. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  29. {
  30. RecentFilesGroup = new ObservableCollection<OpenFileInfo>(Settings.Default.RecentOpenFiles);
  31. RecentFilesList.ItemsSource = RecentFilesGroup;
  32. GridRecentFilesList.ItemsSource = RecentFilesGroup;
  33. RecentFilesList.SelectedItem = null;
  34. ShowListViewContentUI();
  35. }
  36. private void SetLangText()
  37. {
  38. }
  39. private void InitContextMenu()
  40. {
  41. }
  42. private void InitUI()
  43. {
  44. }
  45. #region UI
  46. #region 列表和网格模式
  47. private void ListModeBtn_Click(object sender, RoutedEventArgs e)
  48. {
  49. SwitchFilesMode(sender);
  50. if (BtnListMode.IsChecked == false)
  51. BtnListMode.IsChecked = true;
  52. }
  53. private void GridModeBtn_Click(object sender, RoutedEventArgs e)
  54. {
  55. SwitchFilesMode(sender);
  56. if (BtnGridMode.IsChecked == false)
  57. BtnGridMode.IsChecked = true;
  58. }
  59. private void SwitchFilesMode(object sender)
  60. {
  61. try
  62. {
  63. var btn = sender as CustomIconToggleBtn;
  64. switch (btn.Tag.ToString())
  65. {
  66. case "ListMode":
  67. BtnGridMode.IsChecked = false;
  68. GridRecentFilesList.Visibility = Visibility.Collapsed;
  69. RecentFilesList.Visibility = Visibility.Visible;
  70. RecentFilesList.SelectedItems.Clear();
  71. if (GridRecentFilesList.SelectedItems.Count > 0)
  72. {
  73. for (int i = 0; i < GridRecentFilesList.SelectedItems.Count; i++)
  74. {
  75. RecentFilesList.SelectedItems.Add(GridRecentFilesList.SelectedItems[i]);
  76. }
  77. }
  78. break;
  79. case "GridMode":
  80. BtnListMode.IsChecked=false;
  81. GridRecentFilesList.Visibility = Visibility.Visible;
  82. RecentFilesList.Visibility = Visibility.Collapsed;
  83. GridRecentFilesList.SelectedItems.Clear();
  84. if (RecentFilesList.SelectedItems.Count > 0)
  85. {
  86. for (int i = 0; i < RecentFilesList.SelectedItems.Count; i++)
  87. {
  88. GridRecentFilesList.SelectedItems.Add(RecentFilesList.SelectedItems[i]);
  89. }
  90. }
  91. break;
  92. }
  93. }
  94. catch { }
  95. }
  96. private void ShowListViewContentUI()
  97. {
  98. if (RecentFilesList.Items.Count == 0)
  99. {
  100. BlankContentPanel.Visibility = Visibility.Visible;
  101. RecentFilesList.Visibility = Visibility.Collapsed;
  102. PnlRightBtns.Visibility = Visibility.Collapsed;
  103. }
  104. else
  105. {
  106. RecentFilesList.Visibility = Visibility.Visible;
  107. PnlRightBtns.Visibility = Visibility.Visible;
  108. BlankContentPanel.Visibility = Visibility.Collapsed;
  109. }
  110. }
  111. #endregion
  112. private void btnDelete_Click(object sender, RoutedEventArgs e)
  113. {
  114. string msg = "";
  115. int SelectedItemsType = 0;
  116. if (RecentFilesList.SelectedItems.Count == RecentFilesList.Items.Count || RecentFilesList.SelectedItems.Count == 0)
  117. {
  118. if (RecentFilesList.SelectedItems.Count == 1 && RecentFilesList.Items.Count == 1)
  119. msg = "ClearFile";
  120. else
  121. msg = "AllClearFiles";
  122. SelectedItemsType = 0;
  123. }
  124. else if (RecentFilesList.SelectedItems.Count == 1)
  125. {
  126. msg = "ClearFile";
  127. SelectedItemsType = 1;
  128. }
  129. else
  130. {
  131. msg = "ClearSelectedFiles";
  132. SelectedItemsType = 2;
  133. }
  134. winform.DialogResult result = MessageBoxEx.Show(msg, "", winform.MessageBoxButtons.OKCancel, winform.MessageBoxIcon.Question);
  135. if (result == winform.DialogResult.OK)
  136. {
  137. RemoveRecentFilesFrom(SelectedItemsType);
  138. }
  139. }
  140. private void RecentFilesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  141. {
  142. }
  143. private void GridRecentFilesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  144. {
  145. }
  146. private void ContextMenu_Opened(object sender, RoutedEventArgs e)
  147. {
  148. }
  149. /// <summary>
  150. /// 未被选中的项目,或只有一个被选中的项
  151. /// </summary>
  152. private void showMoreContexMenu(ContextMenu contexMenu)
  153. {
  154. foreach (var item in contexMenu.Items)
  155. {
  156. var menuitem = item as MenuItem;
  157. menuitem.Visibility = Visibility.Visible;
  158. }
  159. }
  160. private void ListViewItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  161. {
  162. Trace.WriteLine("Preview MouseRightButtonDown");
  163. e.Handled = true;
  164. }
  165. #endregion
  166. #region Events
  167. /// <summary>
  168. /// 删除最近文件的操作
  169. /// </summary>
  170. /// <param name="selectedItemsType">0:全部文件;1:一个文件;2:多个文件</param>
  171. private void RemoveRecentFilesFrom(int selectedItemsType)
  172. {
  173. if (selectedItemsType == 0)
  174. {
  175. SettingHelper.RemoveAllRecentOpenFiles();
  176. RecentFilesGroup.Clear();
  177. }
  178. else if (selectedItemsType == 1)
  179. {
  180. var file = RecentFilesList.SelectedItems[0] as OpenFileInfo;
  181. SettingHelper.RemoveRecentOpenFile(file.FilePath);
  182. RecentFilesGroup.Remove(file);
  183. }
  184. else
  185. {
  186. List<OpenFileInfo> OpenFileInfos = new List<OpenFileInfo>();
  187. foreach (var item in RecentFilesList.SelectedItems)
  188. {
  189. var openFileInfo = item as OpenFileInfo;
  190. OpenFileInfos.Add(openFileInfo);
  191. }
  192. foreach (var item in OpenFileInfos)
  193. {
  194. SettingHelper.RemoveRecentOpenFile(item.FilePath);
  195. RecentFilesGroup.Remove(item);
  196. }
  197. OpenFileInfos.Clear();
  198. }
  199. ShowListViewContentUI();
  200. }
  201. private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
  202. {
  203. DeleteFileItem(sender);
  204. }
  205. private void DeleteItemHandler_Click(object sender, RoutedEventArgs e)
  206. {
  207. DeleteFileItem(sender);
  208. }
  209. private bool IsSelectedFile(object sender)
  210. {
  211. return true;
  212. }
  213. private void DeleteFileItem(object sender)
  214. {
  215. if (RecentFilesList.SelectedItems.Count > 1)
  216. {
  217. var isSelected = IsSelectedFile(sender);
  218. if (!isSelected)
  219. {
  220. ShowListViewContentUI();
  221. return;
  222. }
  223. string msg = "";
  224. if (RecentFilesList.SelectedItems.Count == RecentFilesList.Items.Count)
  225. msg = "RecentFiles_AllClearMsg";
  226. else
  227. msg = "RecentFiles_SelectedClearMsg";
  228. if (MessageBoxEx.Show(msg, "", winform.MessageBoxButtons.OKCancel, winform.MessageBoxIcon.Question) == winform.DialogResult.OK)
  229. RemoveRecentFilesFrom(2);
  230. else
  231. return;
  232. }
  233. else
  234. {
  235. if (MessageBoxEx.Show("RecentFiles_ClearMsg", "", winform.MessageBoxButtons.OKCancel, winform.MessageBoxIcon.Question) != winform.DialogResult.OK)
  236. return;
  237. OpenFileInfo file = null;
  238. if ((sender as MenuItem) != null)
  239. {
  240. var menuItem = sender as MenuItem;
  241. file = menuItem.DataContext as OpenFileInfo;
  242. if (file != null)
  243. {
  244. SettingHelper.RemoveRecentOpenFile(file.FilePath);
  245. RecentFilesGroup.Remove(file);
  246. }
  247. }
  248. else if ((sender as Button) != null)
  249. {
  250. var btn = sender as Button;
  251. file = btn.DataContext as OpenFileInfo;
  252. if (file != null)
  253. {
  254. SettingHelper.RemoveRecentOpenFile(file.FilePath);
  255. RecentFilesGroup.Remove(file);
  256. }
  257. }
  258. }
  259. ShowListViewContentUI();
  260. }
  261. private void RecentFilesList_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
  262. {
  263. var item = sender as ListViewItem;
  264. if (item != null && (item.DataContext as OpenFileInfo) != null)
  265. {
  266. var fileInfo = item.DataContext as OpenFileInfo;
  267. if (File.Exists(fileInfo.FilePath))
  268. {
  269. MainWindow parentWindow = Window.GetWindow(this) as MainWindow;
  270. string[] filePath = new string[1];
  271. filePath[0] = fileInfo.FilePath;
  272. parentWindow.LoadPdfViewer(filePath);
  273. }
  274. else
  275. {
  276. //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_TheFileNotExistWarning"));
  277. SettingHelper.RemoveRecentOpenFile(fileInfo.FilePath);
  278. RecentFilesGroup.Remove(fileInfo);
  279. ShowListViewContentUI();
  280. }
  281. }
  282. }
  283. private async void OpenFiles_Click(object sender, RoutedEventArgs e)
  284. {
  285. var dlg = new OpenFileDialog();
  286. dlg.Multiselect = true;
  287. dlg.Filter = Properties.Resources.OpenDialogFilter;
  288. if (dlg.ShowDialog() == true)
  289. {
  290. await Task.Delay(3);
  291. MainWindow parentWindow = Window.GetWindow(this) as MainWindow;
  292. parentWindow.LoadPdfViewer(dlg.FileNames);
  293. }
  294. }
  295. #endregion
  296. private void PrintFile(CPDFViewer pdfViewer)
  297. {
  298. }
  299. private void PrintMenuItem_Click(object sender, RoutedEventArgs e)
  300. {
  301. }
  302. private void FileInfoMenuItem_Click(object sender, RoutedEventArgs e)
  303. {
  304. }
  305. private void ShareMenuItem_Click(object sender, RoutedEventArgs e)
  306. {
  307. }
  308. private void FilePathMenuItem_Click(object sender, RoutedEventArgs e)
  309. {
  310. }
  311. }
  312. }