123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722 |
- using Microsoft.Win32;
- using PDF_Master.CustomControl.SystemControl;
- using PDF_Master.Model;
- using PDF_Master.EventAggregators;
- using PDF_Master.Views;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- using ComPDFKitViewer.PdfViewer;
- using Dragablz;
- using System.Windows;
- using Prism.Ioc;
- using PDF_Master.CustomControl;
- using Prism.Services.Dialogs;
- using PDF_Master.Properties;
- using PDF_Master.Helper;
- using PDFSettings;
- using PDF_Master.ViewModels.Dialog.ServiceDialog;
- using System.Diagnostics;
- using PDFReader_WPF.Helper;
- namespace PDF_Master.ViewModels
- {
- public class MainWindowViewModel : BindableBase
- {
- private MainContent selectedItem;
- #region 文案
- private string T_rename;
- public string T_Rename
- {
- get { return T_rename; }
- set
- {
- SetProperty(ref T_rename, value);
- }
- }
- private string _Msgtologin = "";
- public string Msgtologin
- {
- get { return _Msgtologin; }
- set
- {
- SetProperty(ref _Msgtologin, value);
- }
- }
- private void InitString()
- {
- Msgtologin = App.ServiceLoader.GetString("Msgtologin");
- }
- #endregion
- public MainContent SelectedItem
- {
- get { return selectedItem; }
- set
- {
- SetProperty(ref selectedItem, value);
- if (SelectedItem != null)
- {
- var pdfviewer = (SelectedItem.DataContext as MainContentViewModel).PDFViewer;
- if (pdfviewer != null)
- {
- CurrentPDFViewer = pdfviewer;
- }
- else
- {
- CurrentPDFViewer = null;
- }
- }
- }
- }
- private string progresstitle;
- /// <summary>
- /// 进度控件标题
- /// </summary>
- public string ProgressTitle
- {
- get { return progresstitle; }
- set
- {
- SetProperty(ref progresstitle, value);
- }
- }
- private double processvalue = 0;
- /// <summary>
- /// 进度条当前值
- /// </summary>
- public double Value
- {
- get { return processvalue; }
- set
- {
- SetProperty(ref processvalue, value);
- }
- }
- private double maxValue = 100;
- /// <summary>
- /// 进度条最大值
- /// </summary>
- public double MaxValue
- {
- get { return maxValue; }
- set
- {
- SetProperty(ref maxValue, value);
- }
- }
- private string _Useremailchar = "#";
- public string Useremailchar
- {
- get { return _Useremailchar; }
- set
- {
- SetProperty(ref _Useremailchar, value);
- }
- }
- /// <summary>
- /// UpgradeVis按钮只在订阅期中显示
- /// </summary>
- private Visibility _UpgradeVis = Visibility.Visible;
- public Visibility UpgradeVis
- {
- get { return _UpgradeVis; }
- set
- {
- SetProperty(ref _UpgradeVis, value);
- }
- }
- /// <summary>
- ///提示登录高级功能
- /// </summary>
- private Visibility _OphVis = Visibility.Collapsed;
- public Visibility OphVis
- {
- get { return _OphVis; }
- set
- {
- SetProperty(ref _OphVis, value);
- }
- }
- private Visibility _RegisterVis = Visibility.Collapsed;
- public Visibility RegisterVis
- {
- get { return _RegisterVis; }
- set
- {
- SetProperty(ref _RegisterVis, value);
- }
- }
- private Visibility _LoginVis = Visibility.Collapsed;
- public Visibility LoginVis
- {
- get { return _LoginVis; }
- set
- {
- SetProperty(ref _LoginVis, value);
- }
- }
- private Visibility _UserVis = Visibility.Collapsed;
- public Visibility UserVis
- {
- get { return _UserVis; }
- set
- {
- SetProperty(ref _UserVis, value);
- }
- }
- private Visibility isProcessVisible = Visibility.Collapsed;
- /// <summary>
- /// OCR进度条是否显示
- /// </summary>
- public Visibility IsProcessVisible
- {
- get { return isProcessVisible; }
- set
- {
- SetProperty(ref isProcessVisible, value);
- if (value == Visibility.Visible)
- {
- //还原关闭按钮的显示状态,修复:自动保存后,转档功能的关闭按钮不显示
- ProcessCloseBtnVisible = Visibility.Visible;
- }
- }
- }
- private Visibility processCloseBtnVisible = Visibility.Visible;
- /// <summary>
- /// 进度条控件的关闭按钮是否可见
- /// </summary>
- public Visibility ProcessCloseBtnVisible
- {
- get { return processCloseBtnVisible; }
- set
- {
- SetProperty(ref processCloseBtnVisible, value);
- }
- }
- /// <summary>
- /// 进度条关闭的事件
- /// </summary>
- public Action ProcessCloseAction;
- private bool isCloseAllEnable = false;
- public bool IsCloseAllEnable
- {
- get { return isCloseAllEnable; }
- set
- {
- SetProperty(ref isCloseAllEnable, value);
- }
- }
- public DelegateCommand CloseAllTabCommand { get; set; }
- private CPDFViewer pdfViewer;
- /// <summary>
- /// 当前页签的PdfViewer对象
- /// </summary>
- public CPDFViewer CurrentPDFViewer
- {
- get { return pdfViewer; }
- set { pdfViewer = value; }
- }
- public DelegateCommand AddTab { get; set; }
- /// <summary>
- /// 关闭OCR进度条
- /// </summary>
- public DelegateCommand CloseOCRCommand { get; set; }
- public DelegateCommand OpenRegisterCommand { get; set; }
- public DelegateCommand<string> OpenLoginCommand { get; set; }
- public DelegateCommand OpenUserCommand { get; set; }
- public DelegateCommand LoadCommand { get; set; }
- public DelegateCommand UpgradelinkCommand { get; set; }
-
- public IRegionManager region;
- public IEventAggregator eventer;
- public IDialogService dialogs;
- private IContainerProvider containerProvider;
- public IInterTabClient InterTabClient { get; }
- public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IContainerProvider container, IDialogService dialogService)
- {
- if (App.mainWindowViewModel == null)
- {
- //加载第一个窗体时 先赋值静态对象
- App.mainWindowViewModel = this;
- }
- dialogs = dialogService;
- region = regionManager;
- eventer = eventAggregator;
- containerProvider = container;
- InterTabClient = new InterTabClient(container);
- AddTab = new DelegateCommand(AddTabItem);
- CloseOCRCommand = new DelegateCommand(closeocr);
- OpenRegisterCommand = new DelegateCommand(OpenRegister);
- OpenLoginCommand = new DelegateCommand<string>(OpenLogin);
- OpenUserCommand = new DelegateCommand(OpenUser);
- UpgradelinkCommand= new DelegateCommand(Upgradelink);
- CloseAllTabCommand = new DelegateCommand(() =>
- {
- if (closeAllTabItem())
- {
- App.Current.MainWindow.Close();
- }
- });
- //第一次打开时需要自动加载Home页
- if (App.IsFirstOpen)
- {
- System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
- {
- //重新打开窗口关闭前 未关闭的页签
- if (Settings.Default.AppProperties.NeedToOpenFileList != null && Settings.Default.AppProperties.NeedToOpenFileList.Count > 0)
- {
- List<string> files = Settings.Default.AppProperties.NeedToOpenFileList;
- foreach (var file in files)
- {
- this.AddTabItem(file);
- }
- Settings.Default.AppProperties.NeedToOpenFileList.Clear();
- Settings.Default.Save();
- return;
- }
- else
- {
- //如果有拖拽文件到图标或者设为默认阅读器的情况,双击文件打开软件时,不加载首页
- if ((App.Current.MainWindow as MainWindow) != null && (App.Current.MainWindow as MainWindow).TabablzControl.Items.Count <= 0)
- {
- region.RequestNavigate(RegionNames.MainRegion, "MainContent");
- }
- }
- try
- {
- //避免注册弹窗和新手引导弹窗一起弹出
- //if (App.IsShowRegist && !Settings.Default.IsNewUser)
- //{
- // OpenRegister();
- //}
- //新用户弹出引导弹窗
- if (Settings.Default.IsNewUser)
- {
- if (ShowGuidDialog())
- {
- //未点击下次再看时,下次启动窗体不再显示
- Settings.Default.IsNewUser = false;
- Settings.Default.Save();
- }
- }
- }
- catch { }
- }
- ));
- //因为远程广告模块没有时间测试正式环境,暂时取消
- //ADServiceHelper.GetAD("PDFMasterWindows");
- }
- InitString();
- }
-
- private void Upgradelink()
- {
- DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.Tbr, "Tbr_Btn", "Btn_Tbr_Upgrade");
- Process.Start(new ProcessStartInfo(ServiceHelper.WebHost + "/windows/store/master?email=" + Settings.Default.UserDate.Email));
- }
- //显示新手引导弹窗
- private bool ShowGuidDialog()
- {
- bool result = true;
- dialogs.ShowDialog(DialogNames.GuidDialog, null, r =>
- {
- if (r.Result == ButtonResult.Ignore)
- {
- result = false;
- }
- });
- return result;
- }
- //打开订阅成功弹窗
- public void OpenSubSubscriptionOK()
- {
- dialogs.ShowDialog(DialogNames.SubscriptionOKDialog);
- //优化部分设备上 进入阅读页后关闭注册窗体时 主窗体置底的问题
- App.Current.MainWindow.Activate();
- }
- //打开注册弹窗
- public void OpenRegister()
- {
- dialogs.ShowDialog(DialogNames.RegisterDialog);
- //优化部分设备上 进入阅读页后关闭注册窗体时 主窗体置底的问题
- App.Current.MainWindow.Activate();
- }
- //打开登录弹窗
- public void OpenLogin(String Loginoff=null)
- {
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.Loginoff, Loginoff);
- App.LoginOpen = true;
- dialogs.ShowDialog(DialogNames.LoginDialog, value, dialogResult =>
- {
- });
-
- }
- /// <summary>
- /// 注册成功弹窗
- /// </summary>
- public void OpenRegisterOK()
- {
- dialogs.ShowDialog(DialogNames.RegisterOKRegion);
- }
- /// <summary>
- /// 登录超出设备数限制,重新登录提示弹窗
- /// </summary>
- public void OpenLoginoff()
- {
- dialogs.ShowDialog(DialogNames.LoginoffDialog);
- }
- //打开用户弹窗
- public void OpenUser()
- { //判断本地有没有token,没有显示登录,有再判断登录状态
- if (Settings.Default.AppProperties.LoginToken != "")
- {
- string code = ServiceHelper.GetUser();
- if (code == "330" || code == "304")
- {
- UserVis = Visibility.Collapsed;
- LoginVis = Visibility.Visible;
- RegisterVis = Visibility.Collapsed;
- OphVis = Visibility.Visible;
- if (Settings.Default.UserDate.IsLoginoff == false)
- {
- App.mainWindowViewModel.OpenLoginoff();
- Settings.Default.UserDate.IsLoginoff = true;
- Settings.Default.Save();
- }
- }
- else
- {
-
- Useremailchar = Settings.Default.UserDate.Email.Substring(0, 1);
- App.mainWindowViewModel.UserVis = Visibility.Visible;
- App.UserOpen = true;
- dialogs.ShowDialog(DialogNames.UserDialog);
- }
- }
- else
- {
- LoginVis = Visibility.Visible;
- RegisterVis = Visibility.Collapsed;
- OphVis = Visibility.Visible;
- OpenLogin();
- }
- }
- /// <summary>
- /// 关闭OCR
- /// </summary>
- private void closeocr()
- {
- if (ProcessCloseAction != null)
- {
- ProcessCloseAction.Invoke();
- }
- }
- /// <summary>
- /// 单击+号时添加页签
- /// </summary>
- private void AddTabItem()
- {
- region.RequestNavigate(RegionNames.MainRegion, "MainContent");
- CheckViewsCount();
- }
- /// <summary>
- /// 选择多文档打开时
- /// </summary>
- /// <param name="filePath"></param>
- public void AddTabItem(string filePath)
- {
- NavigationParameters parameters = new NavigationParameters
- {
- { ParameterNames.FilePath, filePath }
- };
- region.RequestNavigate(RegionNames.MainRegion, "MainContent", parameters);
- CheckViewsCount();
- }
- /// <summary>
- /// 关闭所有页签
- /// </summary>
- public bool closeAllTabItem()
- {
- //下一次重新打开未关闭的文档
- if (Settings.Default.AppProperties.Description.OpenUnClosedFileWhenOpen)
- {
- Settings.Default.AppProperties.NeedToOpenFileList = App.OpenedFileList;
- Settings.Default.Save();
- }
- while (region.Regions[RegionNames.MainRegion].Views.Count() > 0)
- {
- if (CurrentPDFViewer != null && CurrentPDFViewer.UndoManager.CanSave)
- {
- ContentResult result = ShowSaveDialog((SelectedItem.DataContext as MainContentViewModel).viewContentViewModel);
- if (result == ContentResult.Cancel)
- {
- //如果是设置了,保存前不显示弹窗提示的功能,则点击取消时也可以关闭
- //否则,会变成:用户必须要点击保存按钮才能关闭窗体,不合理
- if (Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
- {
- return true;
- }
- else
- {
- //普通模式下,点击了取消按钮,则不进行后续的关闭等操作
- return false;
- }
- }
- }
- if (selectedItem != null)
- {
- region.Regions[RegionNames.MainRegion].Remove(selectedItem);
- }
- //多窗体情况下,单个窗体的所有页签均已关闭时,返回true,允许关闭窗体
- if (selectedItem == null)
- {
- return true;
- }
- }
- return true;
- }
- /// <summary>
- /// 设置value值小于MaxValue值时 自动显示
- /// Value值 大于等于MaxValue值时,自动隐藏
- /// </summary>
- public async void SetProcessValue(string title, double value, double maxvalue, bool showClose = true)
- {
- ProgressTitle = title;
- if (value < 0)
- {
- //异常处理
- this.Value = 0;
- }
- if (maxvalue < 0)
- {
- //异常处理
- this.MaxValue = 0;
- }
- if (value < maxvalue)
- {
- this.Value = value;
- this.MaxValue = maxvalue;
- if (IsProcessVisible != Visibility.Visible)
- {
- IsProcessVisible = Visibility.Visible;
- }
- }
- else
- {
- //进度完成后自动隐藏
- this.Value = value;
- this.MaxValue = maxvalue;
- //为了能看到进度条实际到100的效果
- await Task.Delay(150);
- IsProcessVisible = Visibility.Collapsed;
- }
- ProcessCloseBtnVisible = showClose ? Visibility.Visible : Visibility.Collapsed;
- }
- public void CloseTabItem(object item)
- {
- //获取x号所在的maincontentviewmodel对象
- var maincontentViewModel = (item as MainContent).DataContext as MainContentViewModel;
- //如果已经有打开文档,先切换到目标页签
- if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.Document != null)
- {
- SelectItem(maincontentViewModel.PDFViewer.Document.FilePath);
- }
- //可以保存时,弹窗询问提示弹窗
- if (maincontentViewModel.PDFViewer != null && maincontentViewModel.PDFViewer.UndoManager.CanSave)
- {
- ContentResult result = ShowSaveDialog(maincontentViewModel.viewContentViewModel);
- //此处要加上第二个判断,如果去除第二个判断,则新建文件后,点击关闭按钮,不选择保存路径时,无法关闭页签
- if (result == ContentResult.Cancel && !Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
- {
- //非自动保存且取消了另存为时
- return;
- }
- }
- //这里不能加Else 需要分开写If 否则会出现无法关闭页签的情况
- //不要保存 可以直接关闭页签时(有多个页签时)
- if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
- {
- region.Regions[RegionNames.MainRegion].Remove(item);
- CheckViewsCount();
- //关闭子窗体的最后一个页签时,子窗体关闭(不显示首页),参考福昕逻辑
- if ((App.Current.MainWindow as MainWindow).TabablzControl.Items.Count == 0)
- {
- App.Current.MainWindow.Close();
- }
- }
- else
- {
- //只剩一个页签的情况
- if (maincontentViewModel.PDFViewer != null)
- {
- // 如果是关闭文档的页签 则回到首页
- region.Regions[RegionNames.MainRegion].Remove(item);
- region.RequestNavigate(RegionNames.MainRegion, "MainContent");
- }
- else
- {
- // 如果是关闭首页则关闭软件
- region.Regions[RegionNames.MainRegion].Remove(item);
- App.Current.MainWindow.Close();
- }
- }
- }
- /// <summary>
- /// 检查页签个数 更新close allcommand 命令状态
- /// </summary>
- private void CheckViewsCount()
- {
- if (region.Regions[RegionNames.MainRegion].Views.Count() > 1)
- {
- IsCloseAllEnable = true;
- }
- else
- {
- IsCloseAllEnable = false;
- }
- }
- public void SelectItem(string filepath)
- {
- var item = region.Regions[RegionNames.MainRegion].Views.Where(t => ((t as MainContent).DataContext as MainContentViewModel).FilePath == filepath).FirstOrDefault() as MainContent;
- if (item == null)
- return;
- region.Regions[RegionNames.MainRegion].Activate(item);
- var behavior = region.Regions[RegionNames.MainRegion].Behaviors[DragablzWindowBehavior.BehaviorKey] as DragablzWindowBehavior;
- if (behavior != null)
- {
- behavior.ActivateView(item);
- }
- }
- /// <summary>
- /// 显示关闭询问弹窗
- /// </summary>
- /// <param name="pdfViewer"></param>
- /// <returns></returns>
- private ContentResult ShowSaveDialog(ViewContentViewModel viewContentViewModel)
- {
- ContentResult result = ContentResult.Ok;
- if (!Settings.Default.AppProperties.Description.NotShowSaveWhenClose)
- {
- //偏好设置里未设置直接保存时,需要弹窗提示
- AlertsMessage alertsMessage = new AlertsMessage();
- 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"));
- result = alertsMessage.result;
- }
- if (result == ContentResult.Ok)
- {
- var isSave = viewContentViewModel.saveFile();
- if (isSave == false)
- result = ContentResult.Cancel;
- }
- return result;
- }
- }
- }
|