BOTAContentViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using Prism.Mvvm;
  2. using Prism.Regions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using ComPDFKitViewer.PdfViewer;
  9. using PDF_Master.Model;
  10. using Prism.Commands;
  11. using System.Windows.Controls;
  12. using DryIoc;
  13. using Prism.Services.Dialogs;
  14. using PDF_Master.Views.PageEdit;
  15. using PDF_Master.Properties;
  16. using PDF_Master.Helper;
  17. using PDFReader_WPF.Helper;
  18. using System.Drawing;
  19. using PDF_Master.Views.BOTA;
  20. using ImTools;
  21. namespace PDF_Master.ViewModels.BOTA
  22. {
  23. public class BOTAContentViewModel : BindableBase, INavigationAware
  24. {
  25. public CPDFViewer pdfViewer { get; set; }
  26. private IRegionManager regions { get; set; }
  27. private IDialogService dialogs { get; set; }
  28. public ViewContentViewModel viewContentViewModel { get; set; }
  29. private Dictionary<string, string> viewNameByTabItem;
  30. private string botaContentRegionName;
  31. public string BOTAContentRegionName
  32. {
  33. get { return botaContentRegionName; }
  34. set
  35. {
  36. SetProperty(ref botaContentRegionName, value);
  37. }
  38. }
  39. private int selectedIndex;
  40. public int SelectedIndex
  41. {
  42. get { return selectedIndex; }
  43. set
  44. {
  45. SetProperty(ref selectedIndex, value);
  46. }
  47. }
  48. private string tip_Thumbnail;
  49. public string Tip_Thumbnail
  50. {
  51. get { return tip_Thumbnail; }
  52. set
  53. {
  54. SetProperty(ref tip_Thumbnail, value);
  55. }
  56. }
  57. private string tip_OutLine;
  58. public string Tip_OutLine
  59. {
  60. get { return tip_OutLine; }
  61. set
  62. {
  63. SetProperty(ref tip_OutLine, value);
  64. }
  65. }
  66. private string tip_BookMark;
  67. public string Tip_BookMark
  68. {
  69. get { return tip_BookMark; }
  70. set
  71. {
  72. SetProperty(ref tip_BookMark, value);
  73. }
  74. }
  75. private string tip_Search;
  76. public string Tip_Search
  77. {
  78. get { return tip_Search; }
  79. set
  80. {
  81. SetProperty(ref tip_Search, value);
  82. }
  83. }
  84. private string tip_Annote;
  85. public string Tip_Annote
  86. {
  87. get { return tip_Annote; }
  88. set
  89. {
  90. SetProperty(ref tip_Annote, value);
  91. }
  92. }
  93. public string CurrentBar = "";
  94. public DelegateCommand<object> TabControlSelectionChangedCommand { get; set; }
  95. public BOTAContentViewModel(IRegionManager regionManager, IDialogService dialogService)
  96. {
  97. regions = regionManager;
  98. dialogs = dialogService;
  99. BOTAContentRegionName = Guid.NewGuid().ToString();
  100. TabControlSelectionChangedCommand = new DelegateCommand<object>(TabControlSelectionChangedEvent);
  101. viewNameByTabItem = new Dictionary<string, string>();
  102. InitDictionartViewNameByTabItem(ref viewNameByTabItem);
  103. InitString();
  104. }
  105. private void TabControlSelectionChangedEvent(object e)
  106. {
  107. var args = e as SelectionChangedEventArgs;
  108. if (args != null && args.AddedItems.Count > 0)
  109. {
  110. if (!string.IsNullOrEmpty((args.AddedItems[0] as TabItem).Name))
  111. {
  112. TabControlDataTracking((args.AddedItems[0] as TabItem).Name);
  113. EnterSelectedBar((args.AddedItems[0] as TabItem).Name);
  114. if ((args.AddedItems[0] as TabItem).IsSelected)
  115. {
  116. viewContentViewModel.OpenBOTA = true;
  117. }
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// 侧边按钮埋点
  123. /// </summary>
  124. /// <param name="tag">按钮名字</param>
  125. private void TabControlDataTracking(string tag)
  126. {
  127. switch (tag)
  128. {
  129. case "TabItemOutLine":
  130. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.LeftSideBar, "LeftSideBar_Btn", "Btn_LeftSideBar_Outline");
  131. break;
  132. case "TabItemThumbnail":
  133. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.LeftSideBar, "LeftSideBar_Btn", "Btn_LeftSideBar_Thumbnail");
  134. break;
  135. case "TabItemBookMark":
  136. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.LeftSideBar, "LeftSideBar_Btn", "Btn_LeftSideBar_BookMark");
  137. break;
  138. case "TabItemAnnotation":
  139. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.LeftSideBar, "LeftSideBar_Btn", "Btn_LeftSideBar_Annotation");
  140. break;
  141. case "TabItemSearch":
  142. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.LeftSideBar, "LeftSideBar_Btn", "Btn_LeftSideBar_Search");
  143. break;
  144. }
  145. }
  146. private void InitString()
  147. {
  148. Tip_Annote = App.MainPageLoader.GetString("ViewTopToolbar_Annotation");
  149. Tip_OutLine = App.MainPageLoader.GetString("Outline_Title");
  150. Tip_BookMark = App.MainPageLoader.GetString("Bookmark_Title");
  151. Tip_Search = App.MainPageLoader.GetString("ViewRightMenu_Search");
  152. Tip_Thumbnail = App.MainPageLoader.GetString("Thumbnails_Title");
  153. }
  154. /// <summary>
  155. /// 初始化名称-视图字典
  156. /// </summary>
  157. private void InitDictionartViewNameByTabItem(ref Dictionary<string, string> viewNameByTabItem)
  158. {
  159. //绑定tabitem名字和对应的View控件名称
  160. viewNameByTabItem.Add("TabItemThumbnail", "PageEditContent");
  161. viewNameByTabItem.Add("TabItemOutLine", "OutLineControl");
  162. viewNameByTabItem.Add("TabItemBookMark", "BookmarkContent");
  163. viewNameByTabItem.Add("TabItemAnnotation", "AnnotationContent");
  164. viewNameByTabItem.Add("TabItemSearch", "SearchContent");
  165. viewNameByTabItem.Add("TabItemForm", "");
  166. viewNameByTabItem.Add("TabItemSign", "");
  167. }
  168. /// <summary>
  169. /// 导航至目标TabItem
  170. /// </summary>
  171. /// <param name="currentBar"></param>
  172. private void EnterSelectedBar(string currentBar)
  173. {
  174. //增加判空逻辑
  175. if (!string.IsNullOrEmpty(currentBar))
  176. {
  177. CurrentBar = currentBar;
  178. NavigationParameters param = new NavigationParameters();
  179. param.Add(ParameterNames.PDFViewer, pdfViewer);
  180. param.Add(ParameterNames.ViewContentViewModel, viewContentViewModel);
  181. if (currentBar == "TabItemThumbnail")
  182. {
  183. param.Add(ParameterNames.BOTAThumb, true);
  184. }
  185. if (viewNameByTabItem.ContainsKey(currentBar))
  186. {
  187. regions.RequestNavigate(BOTAContentRegionName, viewNameByTabItem[currentBar], param);
  188. var views = regions.Regions[BOTAContentRegionName].Views;
  189. var search = views.FindFirst(x => x is SearchContent);
  190. if (search is SearchContent searchContent)
  191. {
  192. viewContentViewModel.SearchTextFocus(searchContent);
  193. }
  194. }
  195. //增加判空逻辑
  196. if (pdfViewer != null && pdfViewer.Document != null)
  197. {
  198. var info = SettingHelper.GetFileInfo(pdfViewer.Document.FilePath);
  199. if (info != null)
  200. {
  201. info.BOTASelectedTab = currentBar;
  202. info.BOTASelectedInex = SelectedIndex;
  203. SettingHelper.SetFileInfo(info);
  204. }
  205. }
  206. }
  207. }
  208. /// <summary>
  209. /// 根据缓存判断,如果记录需要展开BOTA 则还原上一次的展开状态
  210. /// </summary>
  211. public void OpenBOTA()
  212. {
  213. //记录上一次BOTA展开情况
  214. if (Settings.Default.AppProperties.InitialVIew.RememberBOTA)
  215. {
  216. //如果是加密文档 则先不展开,解密后再展开
  217. if (pdfViewer.Document.IsLocked)
  218. {
  219. return;
  220. }
  221. var info = SettingHelper.GetFileInfo(pdfViewer.Document.FilePath);
  222. //避免新文档因打开文件没有selectedTab信息导致崩溃
  223. if (info != null && !string.IsNullOrEmpty(info.BOTASelectedTab))
  224. {
  225. SelectedIndex = info.BOTASelectedInex;
  226. EnterSelectedBar(info.BOTASelectedTab);
  227. viewContentViewModel.OpenBOTA = true;
  228. }
  229. }
  230. }
  231. #region Navigate
  232. public bool IsNavigationTarget(NavigationContext navigationContext)
  233. {
  234. return true;
  235. }
  236. public void OnNavigatedFrom(NavigationContext navigationContext)
  237. {
  238. }
  239. public void OnNavigatedTo(NavigationContext navigationContext)
  240. {
  241. var contentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
  242. if (contentViewModel != null)
  243. {
  244. viewContentViewModel = contentViewModel;
  245. contentViewModel.botaViewModel = this;
  246. }
  247. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  248. if (pdfview != null)
  249. {
  250. pdfViewer = pdfview;
  251. }
  252. //大纲和文档同时打开
  253. if (Settings.Default.AppProperties.InitialVIew.ShowOutLine && pdfViewer.Document.GetOutlineList().Count > 0)
  254. {
  255. SelectedIndex = 2;
  256. EnterSelectedBar("TabItemOutLine");
  257. viewContentViewModel.OpenBOTA = true;
  258. }
  259. //确认是否要展开BOTA
  260. OpenBOTA();
  261. }
  262. #endregion Navigate
  263. }
  264. }