ViewContent.xaml.cs 10 KB

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