ViewContent.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Master.EventAggregators;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Model;
  6. using PDF_Master.ViewModels;
  7. using PDF_Master.ViewModels.PropertyPanel.ViewModular;
  8. using PDF_Master.ViewModels.Tools;
  9. using PDF_Master.Views.Dialog;
  10. using PDF_Master.Views.PropertyPanel.ViewModular;
  11. using PDF_Master.Views.Tools;
  12. using Prism.Events;
  13. using Prism.Regions;
  14. using System;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Input;
  21. using System.Windows.Media;
  22. using System.Windows.Threading;
  23. namespace PDF_Master.Views
  24. {
  25. /// <summary>
  26. /// Interaction logic for ViewContent
  27. /// </summary>
  28. public partial class ViewContent : UserControl
  29. {
  30. private ViewContentViewModel viewModel = null;
  31. private string unicode = "";
  32. public ViewContent()
  33. {
  34. InitializeComponent();
  35. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  36. viewModel = (ViewContentViewModel)this.DataContext;
  37. viewModel.PropertyChanged += ViewModel_PropertyChanged;
  38. }
  39. private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  40. {
  41. if (e.PropertyName == "OpenBOTA")
  42. {
  43. if (!viewModel.OpenBOTA)
  44. {
  45. //动态改变BOTA的最小宽度只能在这里设置,VM里绑定会有异常
  46. BOTACloumn.MinWidth = 48;
  47. viewModel.BOTAWidth = new GridLength(48);
  48. BOTASplitter.Visibility = Visibility.Collapsed;
  49. }
  50. else
  51. {
  52. BOTACloumn.MinWidth = 260;
  53. BOTASplitter.Visibility = Visibility.Visible;
  54. }
  55. }
  56. }
  57. public ViewContent(IEventAggregator eventAggregator) : this()
  58. {
  59. eventAggregator.GetEvent<SplitEvent>().Subscribe(ChangeSplitModeUI, e => e.Unicode == unicode);
  60. eventAggregator.GetEvent<ReadModeEvent>().Subscribe(ChangeReadModeUI, e => e.Unicode == unicode);
  61. eventAggregator.GetEvent<ReadModePropertyOpenEvent>().Subscribe(ChangeReadModePropertyOpenUI, e => e.Unicode == unicode);
  62. }
  63. private void ChangeReadModePropertyOpenUI(ReadModePropertyOpenArgs obj)
  64. {
  65. Grid.SetColumnSpan(GridViewer, 3);
  66. Grid.SetColumn(GridViewer, 0);
  67. Grid.SetColumn(Scroller, 4);
  68. }
  69. private void ChangeReadModeUI(ReadModeArgs obj)
  70. {
  71. if (obj.IsReadMode)
  72. {
  73. ReadModeContent.Visibility = Visibility.Visible;
  74. RectangleReadMode.Visibility = Visibility.Collapsed;
  75. this.TbtnProperty.IsChecked = false;
  76. Grid.SetRowSpan(DocumentView, 4);
  77. Grid.SetRow(DocumentView, 0);
  78. Grid.SetColumnSpan(GridViewer, 4);
  79. Grid.SetColumn(GridViewer, 0);
  80. }
  81. else
  82. {
  83. ReadModeContent.Visibility = Visibility.Collapsed;
  84. RectangleReadMode.Visibility = Visibility.Collapsed;
  85. //this.TbtnProperty.IsChecked=true;
  86. Grid.SetRowSpan(DocumentView, 1);
  87. Grid.SetRow(DocumentView, 2);
  88. Grid.SetColumnSpan(GridViewer, 1);
  89. Grid.SetColumn(GridViewer, 2);
  90. }
  91. }
  92. /// <summary>
  93. /// 鼠标移动到控件范围时显示控件
  94. /// </summary>
  95. /// <param name="sender"></param>
  96. /// <param name="e"></param>
  97. private void RectangleReadMode_MouseEnter(object sender, MouseEventArgs e)
  98. {
  99. //Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(delegate
  100. //{
  101. if (viewModel.mainViewModel.IsBookMode)
  102. {
  103. this.ReadModeContent.Visibility = Visibility.Visible;
  104. this.RectangleReadMode.Visibility = Visibility.Collapsed;
  105. }
  106. //}));
  107. }
  108. /// <summary>
  109. /// 鼠标移开页面控件,三秒后隐藏该控件
  110. /// </summary>
  111. /// <param name="sender"></param>
  112. /// <param name="e"></param>
  113. private async void ReadModeContent_MouseLeave(object sender, MouseEventArgs e)
  114. {
  115. await System.Threading.Tasks.Task.Delay(3000);
  116. //Thread.Sleep(3000);
  117. this.ReadModeContent.Visibility = Visibility.Collapsed;
  118. this.RectangleReadMode.Visibility = Visibility.Visible;
  119. }
  120. /// <summary>
  121. /// 根据Vm事件通知处理分屏布局的事件
  122. /// </summary>
  123. /// <param name="e"></param>
  124. private void ChangeSplitModeUI(SplitEventArgs e)
  125. {
  126. switch (e.Mode)
  127. {
  128. case EventAggregators.SplitMode.Horizontal:
  129. //水平分屏 上下
  130. HorizontalSplitter.Visibility = Visibility.Visible;
  131. VerticalSplitter.Visibility = Visibility.Collapsed;
  132. RowRight.Height = new GridLength(1, GridUnitType.Star);
  133. ColumnBottom.Width = new GridLength(0);
  134. Grid.SetColumn(SplitPDFViewer, 0);
  135. Grid.SetRow(SplitPDFViewer, 2);
  136. break;
  137. case EventAggregators.SplitMode.Vertical:
  138. HorizontalSplitter.Visibility = Visibility.Collapsed;
  139. VerticalSplitter.Visibility = Visibility.Visible;
  140. RowRight.Height = new GridLength(0);
  141. ColumnBottom.Width = new GridLength(1, GridUnitType.Star);
  142. Grid.SetColumn(SplitPDFViewer, 2);
  143. Grid.SetRow(SplitPDFViewer, 0);
  144. break;
  145. case EventAggregators.SplitMode.Single:
  146. default:
  147. //退出分屏模式
  148. HorizontalSplitter.Visibility = Visibility.Collapsed;
  149. VerticalSplitter.Visibility = Visibility.Collapsed;
  150. RowRight.Height = new GridLength(0, GridUnitType.Star);
  151. ColumnBottom.Width = new GridLength(0, GridUnitType.Star);
  152. break;
  153. }
  154. GridViewer.UpdateLayout();
  155. }
  156. /// <summary>
  157. /// 退出阅读模式
  158. /// </summary>
  159. /// <param name="sender"></param>
  160. /// <param name="e"></param>
  161. private void UserControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  162. {
  163. if (Key.Escape == e.Key)
  164. {
  165. if (viewModel != null)
  166. {
  167. if (viewModel.PDFViewer.MouseMode != MouseModes.PanTool)
  168. {
  169. if (this.ContentToolsBar.Content is AnnotToolContent annotTool)
  170. {
  171. if (annotTool.BtnSelecttool.IsChecked == true)
  172. {
  173. AnnotToolContentViewModel model = (AnnotToolContentViewModel)annotTool.DataContext;
  174. if (model != null)
  175. {
  176. if (model.SnapshotEditMenuViewModel.SnapToolArgs != null)
  177. {
  178. model.SnapshotEditMenuViewModel.SnapToolArgs.ReSnapshot();
  179. }
  180. }
  181. }
  182. }
  183. if (viewModel.mainViewModel.IsBookMode == true)
  184. {
  185. viewModel.Off_ReadModel();
  186. }
  187. }
  188. else
  189. {
  190. if (viewModel.mainViewModel.IsBookMode == true)
  191. {
  192. viewModel.Off_ReadModel();
  193. }
  194. }
  195. }
  196. }
  197. }
  198. private void BtnFile_Initialized(object sender, EventArgs e)
  199. {
  200. BtnFile.ContextMenu = null;
  201. }
  202. private void BtnFile_Click(object sender, RoutedEventArgs e)
  203. {
  204. FileMenu.PlacementTarget = BtnFile;
  205. FileMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
  206. FileMenu.IsOpen = true;
  207. }
  208. private void ContentControl_SizeChanged(object sender, SizeChangedEventArgs e)
  209. {
  210. }
  211. private void MenuRecent_Loaded(object sender, RoutedEventArgs e)
  212. {
  213. int count = 0;
  214. MenuRecent.Items.Clear();
  215. foreach (var file in Properties.Settings.Default.RecentOpenFiles)
  216. {
  217. MenuItem item = new MenuItem();
  218. item.Height = 32;
  219. item.Header = file.FileName;
  220. item.Tag = file.FilePath;
  221. item.ToolTip = file.FilePath;
  222. item.Click += RecentItem_Click;
  223. MenuRecent.Items.Insert(MenuRecent.Items.Count, item);
  224. count++;
  225. if (count >= 20)
  226. break;
  227. }
  228. if (Properties.Settings.Default.RecentOpenFiles.Count > 0)
  229. {
  230. MenuRecent.Items.Add(Menu_Separator);
  231. MenuRecent.Items.Add(MenuDeleteAll);
  232. MenuDeleteAll.IsEnabled = true;
  233. MenuDeleteAll.Opacity = 1;
  234. }
  235. else
  236. {
  237. MenuRecent.Items.Add(MenuDeleteAll);
  238. MenuDeleteAll.IsEnabled = false;
  239. MenuDeleteAll.Opacity = 0.5;
  240. }
  241. }
  242. private void RecentItem_Click(object sender, RoutedEventArgs e)
  243. {
  244. var item = sender as MenuItem;
  245. string[] filePath = new string[1];
  246. filePath[0] = item.Tag.ToString();
  247. if (App.OpenedFileList.Contains(filePath[0]))
  248. {
  249. (this.DataContext as ViewContentViewModel).mainViewModel.mainWindowViewModel.SelectItem(filePath[0]);
  250. }
  251. else
  252. {
  253. (this.DataContext as ViewContentViewModel).mainViewModel.mainWindowViewModel.AddTabItem(filePath[0]);
  254. }
  255. }
  256. private void MenuDeleteAll_Click(object sender, RoutedEventArgs e)
  257. {
  258. SettingHelper.RemoveAllRecentOpenFiles();
  259. }
  260. private void UserControl_DragEnter(object sender, DragEventArgs e)
  261. {
  262. DragDropHelper.DragEnter(this, e);
  263. }
  264. private void UserControl_DragOver(object sender, DragEventArgs e)
  265. {
  266. DragDropHelper.DragOver(this, e);
  267. }
  268. private void UserControl_DragLeave(object sender, DragEventArgs e)
  269. {
  270. DragDropHelper.DragLeave();
  271. }
  272. private void UserControl_Drop(object sender, DragEventArgs e)
  273. {
  274. try
  275. {
  276. DragDropHelper.Drop(this, e);
  277. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  278. //页面编辑模式下 不响应拖拽打开文件
  279. if (file != null && file.Length > 0 && !(this.DataContext as ViewContentViewModel).isInPageEdit)
  280. {
  281. App.mainWindowViewModel.AddTab.Execute();
  282. (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).homeContentViewModel.AddFileFromDrag(file.ToList());
  283. }
  284. }
  285. catch { }
  286. }
  287. private void CloseLeftCommand_Executed(object sender, ExecutedRoutedEventArgs e)
  288. {
  289. BOTASplitter.Visibility = Visibility.Collapsed;
  290. }
  291. private void CloseRightCommand_Executed(object sender, ExecutedRoutedEventArgs e)
  292. {
  293. viewModel.ClosePropertyCommand.Execute();
  294. }
  295. }
  296. }