using ComPDFKitViewer.PdfViewer;
using PDF_Master.Model;
using PDF_Master.Properties;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace PDF_Master.ViewModels.PropertyPanel.ViewModular
{
public class ReadViewContentViewModel : BindableBase, INavigationAware
{
#region 文案
private string T_ESCreadMode;
public string T_ESCReadMode
{
get { return T_ESCreadMode; }
set
{
SetProperty(ref T_ESCreadMode, value);
}
}
private void InitString()
{
T_ESCReadMode = App.MainPageLoader.GetString("GlobalTip_ESCReadMode");
}
#endregion
public string ReadRegionName { get; set; }
public string ReadModeRegionName { get; set; }
public ViewContentViewModel ViewContentViewModel { get; set; }
public CPDFViewer PDFViewer { get; set; }
private Visibility pressEscTip = Visibility.Visible;
public Visibility PressEscTip
{
get { return pressEscTip; }
set
{
SetProperty(ref pressEscTip, value);
}
}
private bool canSave;
///
/// 是否可以保存
///
public bool CanSave
{
get { return canSave; }
set
{
SetProperty(ref canSave, value);
}
}
private bool canUndo;
///
/// 是否可以进行Undo
///
public bool CanUndo
{
get { return canUndo; }
set
{
SetProperty(ref canUndo, value);
}
}
private bool canRedo;
///
/// 是否可以进行Redo
///
public bool CanRedo
{
get { return canRedo; }
set
{
SetProperty(ref canRedo, value);
}
}
private Visibility readModeContentVisibility = Visibility.Collapsed;
public Visibility ReadModeContentVisibility
{
get { return readModeContentVisibility; }
set
{
SetProperty(ref readModeContentVisibility, value);
}
}
private Visibility rectangleReadModeVisibility = Visibility.Collapsed;
public Visibility RectangleReadModeVisibility
{
get { return rectangleReadModeVisibility; }
set
{
SetProperty(ref rectangleReadModeVisibility, value);
}
}
public IRegionManager region;
public IDialogService dialogs;
///
/// 鼠标滚轮缩放的缩放值
///
private double[] zoomLevel = { 1.00f, 10, 25, 50, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
public DelegateCommand Load { get; set; }
public ReadViewContentViewModel(IRegionManager regionManager, IDialogService dialogService)
{
region = regionManager;
dialogs = dialogService;
ReadRegionName = RegionNames.ReadRegionName;
ReadModeRegionName = RegionNames.ReadModePageRegionName;
ReadModeContentVisibility = Visibility.Visible;
RectangleReadModeVisibility = Visibility.Collapsed;
InitString();
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public async void OnNavigatedTo(NavigationContext navigationContext)
{
var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
pdfview.SetBackgroundBrush(new System.Windows.Media.SolidColorBrush(Settings.Default.AppProperties.InitialVIew.BackGroundInFulWindow));
ViewContentViewModel viewContentView = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
if (pdfview != null)
{
PDFViewer = pdfview;
PressEscTip = Visibility.Visible;
LoadFile();
await System.Threading.Tasks.Task.Delay(3000);
PressEscTip = Visibility.Collapsed;
}
if (viewContentView != null)
{
ViewContentViewModel = viewContentView;
}
}
///
/// 将PDFViwer添加到Region
///
private void LoadFile()
{
PDFViewer.MouseWheelZoomHandler += PdfViewer_MouseWheelZoomHandler;
PDFViewer.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
CanSave = PDFViewer.UndoManager.CanSave;
CanUndo = PDFViewer.UndoManager.CanUndo;
CanRedo = PDFViewer.UndoManager.CanRedo;
if (region.Regions.ContainsRegionWithName(ReadRegionName))
{
if (region.Regions[ReadRegionName].Views.Contains(PDFViewer))
{
var contentRegion = region.Regions[ReadRegionName];
contentRegion.Remove(PDFViewer);
}
region.AddToRegion(ReadRegionName, PDFViewer);
}
System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
{
NavigationParameters parameters = new NavigationParameters();
parameters.Add(ParameterNames.PDFViewer, PDFViewer);
parameters.Add(ParameterNames.ViewContentViewModel, this);
region.RequestNavigate(ReadModeRegionName, "ReadModeContent", parameters);
}
));
}
public void PdfViewer_MouseWheelZoomHandler(object sender, bool e)
{
double newZoom = CheckZoomLevel(PDFViewer.ZoomFactor + (e ? 0.01 : -0.01), e);
PDFViewer.Zoom(newZoom);
}
private double CheckZoomLevel(double zoom, bool IsGrowth)
{
double standardZoom = 100;
if (zoom <= 0.01)
{
return 0.01;
}
if (zoom >= 10)
{
return 10;
}
zoom *= 100;
for (int i = 0; i < zoomLevel.Length - 1; i++)
{
if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
{
standardZoom = zoomLevel[i + 1];
break;
}
if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
{
standardZoom = zoomLevel[i];
break;
}
}
return standardZoom / 100;
}
private void UndoManager_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
//不处于页面编辑模式下时,根据PDFVIewer的undo redo状态来更新按钮状态
//页面编辑模式下,按钮状态根据页面编辑的undo redo来显示
if (e.PropertyName == "CanUndo")
{
CanUndo = PDFViewer.UndoManager.CanUndo;
}
if (e.PropertyName == "CanRedo")
{
CanRedo = PDFViewer.UndoManager.CanRedo;
}
if (e.PropertyName == "CanSave")
{
CanSave = PDFViewer.UndoManager.CanSave;
}
}
}
}