MainWindowViewModel.cs 17 KB

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