MainWindowViewModel.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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;
  26. using PDF_Master.ViewModels.Dialog.ServiceDialog;
  27. using System.Diagnostics;
  28. using PDFReader_WPF.Helper;
  29. namespace PDF_Master.ViewModels
  30. {
  31. public class MainWindowViewModel : BindableBase
  32. {
  33. private MainContent selectedItem;
  34. #region 文案
  35. private string T_rename;
  36. public string T_Rename
  37. {
  38. get { return T_rename; }
  39. set
  40. {
  41. SetProperty(ref T_rename, value);
  42. }
  43. }
  44. private string _Msgtologin = "";
  45. public string Msgtologin
  46. {
  47. get { return _Msgtologin; }
  48. set
  49. {
  50. SetProperty(ref _Msgtologin, value);
  51. }
  52. }
  53. private void InitString()
  54. {
  55. Msgtologin = App.ServiceLoader.GetString("Msgtologin");
  56. }
  57. #endregion
  58. public MainContent SelectedItem
  59. {
  60. get { return selectedItem; }
  61. set
  62. {
  63. SetProperty(ref selectedItem, value);
  64. if (SelectedItem != null)
  65. {
  66. var pdfviewer = (SelectedItem.DataContext as MainContentViewModel).PDFViewer;
  67. if (pdfviewer != null)
  68. {
  69. CurrentPDFViewer = pdfviewer;
  70. }
  71. else
  72. {
  73. CurrentPDFViewer = null;
  74. }
  75. }
  76. }
  77. }
  78. private string progresstitle;
  79. /// <summary>
  80. /// 进度控件标题
  81. /// </summary>
  82. public string ProgressTitle
  83. {
  84. get { return progresstitle; }
  85. set
  86. {
  87. SetProperty(ref progresstitle, value);
  88. }
  89. }
  90. private double processvalue = 0;
  91. /// <summary>
  92. /// 进度条当前值
  93. /// </summary>
  94. public double Value
  95. {
  96. get { return processvalue; }
  97. set
  98. {
  99. SetProperty(ref processvalue, value);
  100. }
  101. }
  102. private double maxValue = 100;
  103. /// <summary>
  104. /// 进度条最大值
  105. /// </summary>
  106. public double MaxValue
  107. {
  108. get { return maxValue; }
  109. set
  110. {
  111. SetProperty(ref maxValue, value);
  112. }
  113. }
  114. private string _Useremailchar = "#";
  115. public string Useremailchar
  116. {
  117. get { return _Useremailchar; }
  118. set
  119. {
  120. SetProperty(ref _Useremailchar, value);
  121. }
  122. }
  123. /// <summary>
  124. /// UpgradeVis按钮只在订阅期中显示
  125. /// </summary>
  126. private Visibility _UpgradeVis = Visibility.Visible;
  127. public Visibility UpgradeVis
  128. {
  129. get { return _UpgradeVis; }
  130. set
  131. {
  132. SetProperty(ref _UpgradeVis, value);
  133. }
  134. }
  135. /// <summary>
  136. ///提示登录高级功能
  137. /// </summary>
  138. private Visibility _OphVis = Visibility.Collapsed;
  139. public Visibility OphVis
  140. {
  141. get { return _OphVis; }
  142. set
  143. {
  144. SetProperty(ref _OphVis, value);
  145. }
  146. }
  147. private Visibility _RegisterVis = Visibility.Collapsed;
  148. public Visibility RegisterVis
  149. {
  150. get { return _RegisterVis; }
  151. set
  152. {
  153. SetProperty(ref _RegisterVis, value);
  154. }
  155. }
  156. private Visibility _LoginVis = Visibility.Collapsed;
  157. public Visibility LoginVis
  158. {
  159. get { return _LoginVis; }
  160. set
  161. {
  162. SetProperty(ref _LoginVis, value);
  163. }
  164. }
  165. private Visibility _UserVis = Visibility.Collapsed;
  166. public Visibility UserVis
  167. {
  168. get { return _UserVis; }
  169. set
  170. {
  171. SetProperty(ref _UserVis, value);
  172. }
  173. }
  174. private Visibility isProcessVisible = Visibility.Collapsed;
  175. /// <summary>
  176. /// OCR进度条是否显示
  177. /// </summary>
  178. public Visibility IsProcessVisible
  179. {
  180. get { return isProcessVisible; }
  181. set
  182. {
  183. SetProperty(ref isProcessVisible, value);
  184. if (value == Visibility.Visible)
  185. {
  186. //还原关闭按钮的显示状态,修复:自动保存后,转档功能的关闭按钮不显示
  187. ProcessCloseBtnVisible = Visibility.Visible;
  188. }
  189. }
  190. }
  191. private Visibility processCloseBtnVisible = Visibility.Visible;
  192. /// <summary>
  193. /// 进度条控件的关闭按钮是否可见
  194. /// </summary>
  195. public Visibility ProcessCloseBtnVisible
  196. {
  197. get { return processCloseBtnVisible; }
  198. set
  199. {
  200. SetProperty(ref processCloseBtnVisible, value);
  201. }
  202. }
  203. /// <summary>
  204. /// 进度条关闭的事件
  205. /// </summary>
  206. public Action ProcessCloseAction;
  207. private bool isCloseAllEnable = false;
  208. public bool IsCloseAllEnable
  209. {
  210. get { return isCloseAllEnable; }
  211. set
  212. {
  213. SetProperty(ref isCloseAllEnable, value);
  214. }
  215. }
  216. public DelegateCommand CloseAllTabCommand { get; set; }
  217. private CPDFViewer pdfViewer;
  218. /// <summary>
  219. /// 当前页签的PdfViewer对象
  220. /// </summary>
  221. public CPDFViewer CurrentPDFViewer
  222. {
  223. get { return pdfViewer; }
  224. set { pdfViewer = value; }
  225. }
  226. public DelegateCommand AddTab { get; set; }
  227. /// <summary>
  228. /// 关闭OCR进度条
  229. /// </summary>
  230. public DelegateCommand CloseOCRCommand { get; set; }
  231. public DelegateCommand OpenRegisterCommand { get; set; }
  232. public DelegateCommand<string> OpenLoginCommand { get; set; }
  233. public DelegateCommand OpenUserCommand { get; set; }
  234. public DelegateCommand LoadCommand { get; set; }
  235. public DelegateCommand UpgradelinkCommand { get; set; }
  236. public IRegionManager region;
  237. public IEventAggregator eventer;
  238. public IDialogService dialogs;
  239. private IContainerProvider containerProvider;
  240. public IInterTabClient InterTabClient { get; }
  241. public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider container, IDialogService dialogService)
  242. {
  243. if (App.mainWindowViewModel == null)
  244. {
  245. //加载第一个窗体时 先赋值静态对象
  246. App.mainWindowViewModel = this;
  247. }
  248. dialogs = dialogService;
  249. region = regionManager;
  250. eventer = eventAggregator;
  251. containerProvider = container;
  252. InterTabClient = new InterTabClient(container);
  253. AddTab = new DelegateCommand(AddTabItem);
  254. CloseOCRCommand = new DelegateCommand(closeocr);
  255. OpenRegisterCommand = new DelegateCommand(OpenRegister);
  256. OpenLoginCommand = new DelegateCommand<string>(OpenLogin);
  257. OpenUserCommand = new DelegateCommand(OpenUser);
  258. UpgradelinkCommand= new DelegateCommand(Upgradelink);
  259. CloseAllTabCommand = new DelegateCommand(() =>
  260. {
  261. if (closeAllTabItem())
  262. {
  263. App.Current.MainWindow.Close();
  264. }
  265. });
  266. //第一次打开时需要自动加载Home页
  267. if (App.IsFirstOpen)
  268. {
  269. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  270. {
  271. //重新打开窗口关闭前 未关闭的页签
  272. if (Settings.Default.AppProperties.NeedToOpenFileList != null && Settings.Default.AppProperties.NeedToOpenFileList.Count > 0)
  273. {
  274. List<string> files = Settings.Default.AppProperties.NeedToOpenFileList;
  275. foreach (var file in files)
  276. {
  277. this.AddTabItem(file);
  278. }
  279. Settings.Default.AppProperties.NeedToOpenFileList.Clear();
  280. Settings.Default.Save();
  281. return;
  282. }
  283. else
  284. {
  285. //如果有拖拽文件到图标或者设为默认阅读器的情况,双击文件打开软件时,不加载首页
  286. if ((App.Current.MainWindow as MainWindow) != null && (App.Current.MainWindow as MainWindow).TabablzControl.Items.Count <= 0)
  287. {
  288. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  289. }
  290. }
  291. try
  292. {
  293. //避免注册弹窗和新手引导弹窗一起弹出
  294. //if (App.IsShowRegist && !Settings.Default.IsNewUser)
  295. //{
  296. // OpenRegister();
  297. //}
  298. //新用户弹出引导弹窗
  299. if (Settings.Default.IsNewUser)
  300. {
  301. if (ShowGuidDialog())
  302. {
  303. //未点击下次再看时,下次启动窗体不再显示
  304. Settings.Default.IsNewUser = false;
  305. Settings.Default.Save();
  306. }
  307. }
  308. }
  309. catch { }
  310. }
  311. ));
  312. //因为远程广告模块没有时间测试正式环境,暂时取消
  313. //ADServiceHelper.GetAD("PDFMasterWindows");
  314. }
  315. InitString();
  316. }
  317. private void Upgradelink()
  318. {
  319. DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Tbr, "Tbr_Btn", "Btn_Tbr_Upgrade");
  320. Process.Start(new ProcessStartInfo(ServiceHelper.WebHost + "/windows/store/master?email=" + Settings.Default.UserDate.Email));
  321. }
  322. //显示新手引导弹窗
  323. private bool ShowGuidDialog()
  324. {
  325. bool result = true;
  326. dialogs.ShowDialog(DialogNames.GuidDialog, null, r =>
  327. {
  328. if (r.Result == ButtonResult.Ignore)
  329. {
  330. result = false;
  331. }
  332. });
  333. return result;
  334. }
  335. //打开订阅成功弹窗
  336. public void OpenSubSubscriptionOK()
  337. {
  338. dialogs.ShowDialog(DialogNames.SubscriptionOKDialog);
  339. //优化部分设备上 进入阅读页后关闭注册窗体时 主窗体置底的问题
  340. App.Current.MainWindow.Activate();
  341. }
  342. //打开注册弹窗
  343. public void OpenRegister()
  344. {
  345. dialogs.ShowDialog(DialogNames.RegisterDialog);
  346. //优化部分设备上 进入阅读页后关闭注册窗体时 主窗体置底的问题
  347. App.Current.MainWindow.Activate();
  348. }
  349. //打开登录弹窗
  350. public void OpenLogin(String Loginoff=null)
  351. {
  352. DialogParameters value = new DialogParameters();
  353. value.Add(ParameterNames.Loginoff, Loginoff);
  354. App.LoginOpen = true;
  355. dialogs.ShowDialog(DialogNames.LoginDialog, value, dialogResult =>
  356. {
  357. });
  358. }
  359. /// <summary>
  360. /// 注册成功弹窗
  361. /// </summary>
  362. public void OpenRegisterOK()
  363. {
  364. dialogs.ShowDialog(DialogNames.RegisterOKRegion);
  365. }
  366. /// <summary>
  367. /// 登录超出设备数限制,重新登录提示弹窗
  368. /// </summary>
  369. public void OpenLoginoff()
  370. {
  371. dialogs.ShowDialog(DialogNames.LoginoffDialog);
  372. }
  373. //打开用户弹窗
  374. public void OpenUser()
  375. { //判断本地有没有token,没有显示登录,有再判断登录状态
  376. if (Settings.Default.AppProperties.LoginToken != "")
  377. {
  378. string code = ServiceHelper.GetUser();
  379. if (code == "330" || code == "304")
  380. {
  381. UserVis = Visibility.Collapsed;
  382. LoginVis = Visibility.Visible;
  383. RegisterVis = Visibility.Collapsed;
  384. OphVis = Visibility.Visible;
  385. if (Settings.Default.UserDate.IsLoginoff == false)
  386. {
  387. App.mainWindowViewModel.OpenLoginoff();
  388. Settings.Default.UserDate.IsLoginoff = true;
  389. Settings.Default.Save();
  390. }
  391. }
  392. else
  393. {
  394. Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
  395. App.mainWindowViewModel.UserVis = Visibility.Visible;
  396. App.UserOpen = true;
  397. dialogs.ShowDialog(DialogNames.UserDialog);
  398. }
  399. }
  400. else
  401. {
  402. LoginVis = Visibility.Visible;
  403. RegisterVis = Visibility.Collapsed;
  404. OphVis = Visibility.Visible;
  405. OpenLogin();
  406. }
  407. }
  408. /// <summary>
  409. /// 关闭OCR
  410. /// </summary>
  411. private void closeocr()
  412. {
  413. if (ProcessCloseAction != null)
  414. {
  415. ProcessCloseAction.Invoke();
  416. }
  417. }
  418. /// <summary>
  419. /// 单击+号时添加页签
  420. /// </summary>
  421. private void AddTabItem()
  422. {
  423. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  424. CheckViewsCount();
  425. }
  426. /// <summary>
  427. /// 选择多文档打开时
  428. /// </summary>
  429. /// <param name="filePath"></param>
  430. public void AddTabItem(string filePath)
  431. {
  432. NavigationParameters parameters = new NavigationParameters
  433. {
  434. { ParameterNames.FilePath, filePath }
  435. };
  436. region.RequestNavigate(RegionNames.MainRegion, "MainContent", parameters);
  437. CheckViewsCount();
  438. }
  439. /// <summary>
  440. /// 关闭所有页签
  441. /// </summary>
  442. public bool closeAllTabItem()
  443. {
  444. //下一次重新打开未关闭的文档
  445. if (Settings.Default.AppProperties.Description.OpenUnClosedFileWhenOpen)
  446. {
  447. Settings.Default.AppProperties.NeedToOpenFileList = App.OpenedFileList;
  448. Settings.Default.Save();
  449. }
  450. while (region.Regions[RegionNames.MainRegion].Views.Count() > 0)
  451. {
  452. if (CurrentPDFViewer != null && CurrentPDFViewer.UndoManager.CanSave)
  453. {
  454. ContentResult result = ShowSaveDialog((SelectedItem.DataContext as MainContentViewModel).viewContentViewModel);
  455. if (result == ContentResult.Cancel)
  456. {
  457. //如果是设置了,保存前不显示弹窗提示的功能,则点击取消时也可以关闭
  458. //否则,会变成:用户必须要点击保存按钮才能关闭窗体,不合理
  459. if (Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
  460. {
  461. return true;
  462. }
  463. else
  464. {
  465. //普通模式下,点击了取消按钮,则不进行后续的关闭等操作
  466. return false;
  467. }
  468. }
  469. }
  470. if (selectedItem != null)
  471. {
  472. region.Regions[RegionNames.MainRegion].Remove(selectedItem);
  473. }
  474. //多窗体情况下,单个窗体的所有页签均已关闭时,返回true,允许关闭窗体
  475. if (selectedItem == null)
  476. {
  477. return true;
  478. }
  479. }
  480. return true;
  481. }
  482. /// <summary>
  483. /// 设置value值小于MaxValue值时 自动显示
  484. /// Value值 大于等于MaxValue值时,自动隐藏
  485. /// </summary>
  486. public async void SetProcessValue(string title, double value, double maxvalue, bool showClose = true)
  487. {
  488. ProgressTitle = title;
  489. if (value < 0)
  490. {
  491. //异常处理
  492. this.Value = 0;
  493. }
  494. if (maxvalue < 0)
  495. {
  496. //异常处理
  497. this.MaxValue = 0;
  498. }
  499. if (value < maxvalue)
  500. {
  501. this.Value = value;
  502. this.MaxValue = maxvalue;
  503. if (IsProcessVisible != Visibility.Visible)
  504. {
  505. IsProcessVisible = Visibility.Visible;
  506. }
  507. }
  508. else
  509. {
  510. //进度完成后自动隐藏
  511. this.Value = value;
  512. this.MaxValue = maxvalue;
  513. //为了能看到进度条实际到100的效果
  514. await Task.Delay(150);
  515. IsProcessVisible = Visibility.Collapsed;
  516. }
  517. ProcessCloseBtnVisible = showClose ? Visibility.Visible : Visibility.Collapsed;
  518. }
  519. public void CloseTabItem(object item)
  520. {
  521. //获取x号所在的maincontentviewmodel对象
  522. var maincontentViewModel = (item as MainContent).DataContext as MainContentViewModel;
  523. //如果已经有打开文档,先切换到目标页签
  524. if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.Document != null)
  525. {
  526. SelectItem(maincontentViewModel.PDFViewer.Document.FilePath);
  527. }
  528. //可以保存时,弹窗询问提示弹窗
  529. if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.UndoManager.CanSave)
  530. {
  531. ContentResult result = ShowSaveDialog(maincontentViewModel.viewContentViewModel);
  532. //此处要加上第二个判断,如果去除第二个判断,则新建文件后,点击关闭按钮,不选择保存路径时,无法关闭页签
  533. if (result == ContentResult.Cancel && !Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
  534. {
  535. //非自动保存且取消了另存为时
  536. return;
  537. }
  538. }
  539. //这里不能加Else 需要分开写If 否则会出现无法关闭页签的情况
  540. //不要保存 可以直接关闭页签时(有多个页签时)
  541. if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
  542. {
  543. region.Regions[RegionNames.MainRegion].Remove(item);
  544. CheckViewsCount();
  545. //关闭子窗体的最后一个页签时,子窗体关闭(不显示首页),参考福昕逻辑
  546. if ((App.Current.MainWindow as MainWindow).TabablzControl.Items.Count == 0)
  547. {
  548. App.Current.MainWindow.Close();
  549. }
  550. }
  551. else
  552. {
  553. //只剩一个页签的情况
  554. if (maincontentViewModel.PDFViewer != null)
  555. {
  556. // 如果是关闭文档的页签 则回到首页
  557. region.Regions[RegionNames.MainRegion].Remove(item);
  558. region.RequestNavigate(RegionNames.MainRegion, "MainContent");
  559. }
  560. else
  561. {
  562. // 如果是关闭首页则关闭软件
  563. region.Regions[RegionNames.MainRegion].Remove(item);
  564. App.Current.MainWindow.Close();
  565. }
  566. }
  567. }
  568. /// <summary>
  569. /// 检查页签个数 更新close allcommand 命令状态
  570. /// </summary>
  571. private void CheckViewsCount()
  572. {
  573. if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
  574. {
  575. IsCloseAllEnable = true;
  576. }
  577. else
  578. {
  579. IsCloseAllEnable = false;
  580. }
  581. }
  582. public void SelectItem(string filepath)
  583. {
  584. var item = region.Regions[RegionNames.MainRegion].Views.Where(t => ((t as MainContent).DataContext as MainContentViewModel).FilePath == filepath).FirstOrDefault() as MainContent;
  585. if (item == null)
  586. return;
  587. region.Regions[RegionNames.MainRegion].Activate(item);
  588. var behavior = region.Regions[RegionNames.MainRegion].Behaviors[DragablzWindowBehavior.BehaviorKey] as DragablzWindowBehavior;
  589. if (behavior != null)
  590. {
  591. behavior.ActivateView(item);
  592. }
  593. }
  594. /// <summary>
  595. /// 显示关闭询问弹窗
  596. /// </summary>
  597. /// <param name="pdfViewer"></param>
  598. /// <returns></returns>
  599. private ContentResult ShowSaveDialog(ViewContentViewModel viewContentViewModel)
  600. {
  601. ContentResult result = ContentResult.Ok;
  602. if (!Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
  603. {
  604. //偏好设置里未设置直接保存时,需要弹窗提示
  605. AlertsMessage alertsMessage = new AlertsMessage();
  606. alertsMessage.ShowDialog("", string.Format(App.MainPageLoader.GetString("Main_ModifiedFileWarning"), System.IO.Path.GetFileName(CurrentPDFViewer.Document.FilePath)), App.ServiceLoader.GetString("Text_cancel"), App.ServiceLoader.GetString("Text_no"), App.ServiceLoader.GetString("Text_ok"));
  607. result = alertsMessage.result;
  608. }
  609. if (result == ContentResult.Ok)
  610. {
  611. var isSave = viewContentViewModel.saveFile();
  612. if (isSave == false)
  613. result = ContentResult.Cancel;
  614. }
  615. return result;
  616. }
  617. }
  618. }