MainWindowViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. using Microsoft.Win32;
  2. using PDF_Master.CustomControl.SystemControl;
  3. using PDF_Master.Model;
  4. using PDF_Master.EventAggregators;
  5. using PDF_Master.Views;
  6. using Prism.Commands;
  7. using Prism.Events;
  8. using Prism.Mvvm;
  9. using Prism.Regions;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows.Controls;
  17. using ComPDFKitViewer.PdfViewer;
  18. using Dragablz;
  19. using System.Windows;
  20. using Prism.Ioc;
  21. using PDF_Master.CustomControl;
  22. using Prism.Services.Dialogs;
  23. using PDF_Master.Properties;
  24. using PDF_Master.Helper;
  25. using PDFSettings.Settings;
  26. namespace PDF_Master.ViewModels
  27. {
  28. public class MainWindowViewModel : BindableBase
  29. {
  30. private MainContent selectedItem;
  31. public MainContent SelectedItem
  32. {
  33. get { return selectedItem; }
  34. set
  35. {
  36. SetProperty(ref selectedItem, value);
  37. if (SelectedItem != null)
  38. {
  39. var pdfviewer = (SelectedItem.DataContext as MainContentViewModel).PDFViewer;
  40. if (pdfviewer != null)
  41. {
  42. CurrentPDFViewer = pdfviewer;
  43. }
  44. else
  45. {
  46. CurrentPDFViewer = null;
  47. }
  48. }
  49. }
  50. }
  51. private string progresstitle;
  52. /// <summary>
  53. /// 进度控件标题
  54. /// </summary>
  55. public string ProgressTitle
  56. {
  57. get { return progresstitle; }
  58. set
  59. {
  60. SetProperty(ref progresstitle, value);
  61. }
  62. }
  63. private double processvalue = 0;
  64. /// <summary>
  65. /// 进度条当前值
  66. /// </summary>
  67. public double Value
  68. {
  69. get { return processvalue; }
  70. set
  71. {
  72. SetProperty(ref processvalue, value);
  73. }
  74. }
  75. private double maxValue = 100;
  76. /// <summary>
  77. /// 进度条最大值
  78. /// </summary>
  79. public double MaxValue
  80. {
  81. get { return maxValue; }
  82. set
  83. {
  84. SetProperty(ref maxValue, value);
  85. }
  86. }
  87. /// <summary>
  88. ///提示登录高级功能
  89. /// </summary>
  90. private Visibility _OphVis = Visibility.Collapsed;
  91. public Visibility OphVis
  92. {
  93. get { return _OphVis; }
  94. set
  95. {
  96. SetProperty(ref _OphVis, value);
  97. }
  98. }
  99. private Visibility _RegisterVis = Visibility.Collapsed;
  100. public Visibility RegisterVis
  101. {
  102. get { return _RegisterVis; }
  103. set
  104. {
  105. SetProperty(ref _RegisterVis, value);
  106. }
  107. }
  108. private Visibility _LoginVis = Visibility.Collapsed;
  109. public Visibility LoginVis
  110. {
  111. get { return _LoginVis; }
  112. set
  113. {
  114. SetProperty(ref _LoginVis, value);
  115. }
  116. }
  117. private Visibility _UserVis = Visibility.Collapsed;
  118. public Visibility UserVis
  119. {
  120. get { return _UserVis; }
  121. set
  122. {
  123. SetProperty(ref _UserVis, value);
  124. }
  125. }
  126. private Visibility isProcessVisible = Visibility.Collapsed;
  127. /// <summary>
  128. /// OCR进度条是否显示
  129. /// </summary>
  130. public Visibility IsProcessVisible
  131. {
  132. get { return isProcessVisible; }
  133. set
  134. {
  135. SetProperty(ref isProcessVisible, value);
  136. }
  137. }
  138. /// <summary>
  139. /// 进度条关闭的事件
  140. /// </summary>
  141. public Action ProcessCloseAction;
  142. private bool isCloseAllEnable = false;
  143. public bool IsCloseAllEnable
  144. {
  145. get { return isCloseAllEnable; }
  146. set
  147. {
  148. SetProperty(ref isCloseAllEnable, value);
  149. }
  150. }
  151. public DelegateCommand CloseAllTabCommand { get; set; }
  152. private CPDFViewer pdfViewer;
  153. /// <summary>
  154. /// 当前页签的PdfViewer对象
  155. /// </summary>
  156. public CPDFViewer CurrentPDFViewer
  157. {
  158. get { return pdfViewer; }
  159. set { pdfViewer = value; }
  160. }
  161. public DelegateCommand AddTab { get; set; }
  162. /// <summary>
  163. /// 关闭OCR进度条
  164. /// </summary>
  165. public DelegateCommand CloseOCRCommand { get; set; }
  166. public DelegateCommand OpenRegisterCommand { get; set; }
  167. public DelegateCommand OpenLoginCommand { get; set; }
  168. public DelegateCommand OpenUserCommand { get; set; }
  169. public DelegateCommand LoadCommand { get; set; }
  170. public IRegionManager region;
  171. public IEventAggregator eventer;
  172. public IDialogService dialogs;
  173. private IContainerProvider containerProvider;
  174. public IInterTabClient InterTabClient { get; }
  175. public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider container, IDialogService dialogService)
  176. {
  177. if (App.mainWindowViewModel == null)
  178. {
  179. //加载第一个窗体时 先赋值静态对象
  180. App.mainWindowViewModel = this;
  181. }
  182. dialogs = dialogService;
  183. region = regionManager;
  184. eventer = eventAggregator;
  185. containerProvider = container;
  186. InterTabClient = new InterTabClient(container);
  187. AddTab = new DelegateCommand(AddTabItem);
  188. CloseOCRCommand = new DelegateCommand(closeocr);
  189. OpenRegisterCommand = new DelegateCommand(OpenRegister);
  190. OpenLoginCommand = new DelegateCommand(OpenLogin);
  191. OpenUserCommand = new DelegateCommand(OpenUser);
  192. CloseAllTabCommand = new DelegateCommand(() => { if (closeAllTabItem())
  193. {
  194. App.Current.MainWindow.Close();
  195. }
  196. });
  197. //第一次打开时需要自动加载Home页
  198. if (App.IsFirstOpen)
  199. {
  200. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  201. {
  202. if (Settings.Default.AppProperties.NeedToOpenFileList != null && Settings.Default.AppProperties.NeedToOpenFileList.Count > 0)
  203. {
  204. List<string> files = Settings.Default.AppProperties.NeedToOpenFileList;
  205. foreach (var file in files)
  206. {
  207. this.AddTabItem(file);
  208. }
  209. Settings.Default.AppProperties.NeedToOpenFileList.Clear();
  210. Settings.Default.Save();
  211. return;
  212. }
  213. else
  214. {
  215. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  216. }
  217. try
  218. {
  219. //避免注册弹窗和新手引导弹窗一起弹出
  220. if (App.IsShowRegist && !Settings.Default.IsNewUser)
  221. {
  222. OpenRegister();
  223. }
  224. //新用户弹出引导弹窗
  225. if (Settings.Default.IsNewUser)
  226. {
  227. if (ShowGuidDialog())
  228. {
  229. //未点击下次再看时,下次启动窗体不再显示
  230. Settings.Default.IsNewUser = false;
  231. Settings.Default.Save();
  232. }
  233. }
  234. }
  235. catch { }
  236. }
  237. ));
  238. App.IsFirstOpen = false;
  239. if (Settings.Default.AppProperties.LoginToken != "")
  240. {
  241. if(ServiceHelper.GetUser()=="false")
  242. {
  243. LoginVis = Visibility.Visible;
  244. OphVis = Visibility.Visible;
  245. }
  246. else
  247. {
  248. UserVis = Visibility.Visible;
  249. }
  250. }
  251. else
  252. {
  253. LoginVis = Visibility.Visible;
  254. OphVis = Visibility.Visible;
  255. }
  256. }
  257. else
  258. {
  259. if (ServiceHelper.GetUser() == "false")
  260. {
  261. LoginVis = Visibility.Visible;
  262. }
  263. else
  264. {
  265. UserVis = Visibility.Visible;
  266. }
  267. }
  268. }
  269. //显示新手引导弹窗
  270. private bool ShowGuidDialog()
  271. {
  272. bool result = true;
  273. dialogs.ShowDialog(DialogNames.GuidDialog, null, r =>
  274. {
  275. if(r.Result == ButtonResult.Ignore)
  276. {
  277. result = false;
  278. }
  279. });
  280. return result;
  281. }
  282. //打开注册弹窗
  283. public void OpenRegister()
  284. {
  285. dialogs.ShowDialog(DialogNames.RegisterDialog);
  286. }
  287. //打开登录弹窗
  288. public void OpenLogin()
  289. {
  290. dialogs.ShowDialog(DialogNames.LoginDialog);
  291. }
  292. //打开用户弹窗
  293. public void OpenUser()
  294. {
  295. dialogs.ShowDialog(DialogNames.UserDialog);
  296. }
  297. /// <summary>
  298. /// 关闭OCR
  299. /// </summary>
  300. private void closeocr()
  301. {
  302. if (ProcessCloseAction != null)
  303. {
  304. ProcessCloseAction.Invoke();
  305. }
  306. }
  307. /// <summary>
  308. /// 单击+号时添加页签
  309. /// </summary>
  310. private void AddTabItem()
  311. {
  312. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  313. CheckViewsCount();
  314. }
  315. /// <summary>
  316. /// 选择多文档打开时
  317. /// </summary>
  318. /// <param name="filePath"></param>
  319. public void AddTabItem(string filePath)
  320. {
  321. NavigationParameters parameters = new NavigationParameters
  322. {
  323. { ParameterNames.FilePath, filePath }
  324. };
  325. region.RequestNavigate(RegionNames.MainRegion, "MainContent", parameters);
  326. CheckViewsCount();
  327. }
  328. /// <summary>
  329. /// 关闭所有页签
  330. /// </summary>
  331. public bool closeAllTabItem()
  332. {
  333. //下一次重新打开未关闭的文档
  334. if (Settings.Default.AppProperties.Description.OpenUnClosedFileWhenOpen)
  335. {
  336. Settings.Default.AppProperties.NeedToOpenFileList = App.OpenedFileList;
  337. Settings.Default.Save();
  338. }
  339. while (region.Regions[RegionNames.MainRegion].Views.Count() > 0)
  340. {
  341. if (CurrentPDFViewer != null && CurrentPDFViewer.UndoManager.CanSave)
  342. {
  343. ContentResult result = ShowSaveDialog((SelectedItem.DataContext as MainContentViewModel).viewContentViewModel);
  344. if (result == ContentResult.Cancel)
  345. return false;
  346. App.OpenedFileList.Remove(CurrentPDFViewer.Document.FilePath);
  347. CurrentPDFViewer.CloseDocument();
  348. }
  349. if (selectedItem != null)
  350. {
  351. region.Regions[RegionNames.MainRegion].Remove(selectedItem);
  352. }
  353. //多窗体情况下,单个窗体的所有页签均已关闭时,返回true,允许关闭窗体
  354. if (selectedItem == null)
  355. {
  356. return true;
  357. }
  358. }
  359. return true;
  360. }
  361. public void CloseTabItem(object item)
  362. {
  363. //获取x号所在的maincontentviewmodel对象
  364. var maincontentViewModel = (item as MainContent).DataContext as MainContentViewModel;
  365. //如果已经有打开文档,先切换到目标页签
  366. if (maincontentViewModel.PDFViewer != null)
  367. {
  368. SelectItem(maincontentViewModel.PDFViewer.Document.FilePath);
  369. }
  370. if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.UndoManager.CanSave)
  371. {
  372. ContentResult result = ShowSaveDialog(maincontentViewModel.viewContentViewModel);
  373. if (result == ContentResult.Cancel)
  374. {
  375. return;
  376. }
  377. App.OpenedFileList.Remove(maincontentViewModel.PDFViewer.Document.FilePath);
  378. if (maincontentViewModel.PDFViewer.Document != null)
  379. {
  380. maincontentViewModel.PDFViewer.CloseDocument();
  381. }
  382. }
  383. if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
  384. {
  385. region.Regions[RegionNames.MainRegion].Remove(item);
  386. CheckViewsCount();
  387. }
  388. else
  389. {
  390. if (maincontentViewModel.PDFViewer != null)
  391. {
  392. //如果是打开文档的页签 则回到首页
  393. region.Regions[RegionNames.MainRegion].Remove(item);
  394. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  395. }
  396. else
  397. {
  398. //如果是关闭首页则关闭软件
  399. region.Regions[RegionNames.MainRegion].Remove(item);
  400. App.Current.MainWindow.Close();
  401. }
  402. }
  403. return;
  404. }
  405. /// <summary>
  406. /// 检查页签个数 更新close allcommand 命令状态
  407. /// </summary>
  408. private void CheckViewsCount()
  409. {
  410. if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
  411. {
  412. IsCloseAllEnable = true;
  413. }
  414. else
  415. {
  416. IsCloseAllEnable = false;
  417. }
  418. }
  419. public void SelectItem(string filepath)
  420. {
  421. var item = region.Regions[RegionNames.MainRegion].Views.Where(t => ((t as MainContent).DataContext as MainContentViewModel).FilePath == filepath).FirstOrDefault() as MainContent;
  422. if (item == null)
  423. return;
  424. region.Regions[RegionNames.MainRegion].Activate(item);
  425. var behavior = region.Regions[RegionNames.MainRegion].Behaviors[DragablzWindowBehavior.BehaviorKey] as DragablzWindowBehavior;
  426. if (behavior != null)
  427. {
  428. behavior.ActivateView(item);
  429. }
  430. }
  431. /// <summary>
  432. /// 显示关闭询问弹窗
  433. /// </summary>
  434. /// <param name="pdfViewer"></param>
  435. /// <returns></returns>
  436. private ContentResult ShowSaveDialog(ViewContentViewModel viewContentViewModel)
  437. {
  438. if(Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
  439. {
  440. //偏好设置里选择自动保存时
  441. return ContentResult.Ok;
  442. }
  443. AlertsMessage alertsMessage = new AlertsMessage();
  444. alertsMessage.ShowDialog("关闭提示", "当前文档有为保存的操作,是否需要保存?", "Cancel", "No", "Ok");
  445. var result = alertsMessage.result;
  446. if (result == ContentResult.Ok)
  447. {
  448. var isSave = viewContentViewModel.saveFile();
  449. if (isSave == false)
  450. result = ContentResult.Cancel;
  451. }
  452. return result;
  453. }
  454. }
  455. }