using ComPDFKitViewer;
using ComPDFKitViewer.AnnotEvent;
using ComPDFKitViewer.PdfViewer;
using PDF_Master.CustomControl;
using PDF_Master.EventAggregators;
using PDF_Master.Helper;
using PDF_Master.Model;
using PDF_Master.ViewModels;
using PDF_Master.ViewModels.BOTA;
using PDF_Master.ViewModels.PropertyPanel.ViewModular;
using PDF_Master.ViewModels.Tools;
using PDF_Master.Views.BOTA;
using PDF_Master.Views.Dialog;
using PDF_Master.Views.PageEdit;
using PDF_Master.Views.PropertyPanel.ViewModular;
using PDF_Master.Views.Tools;
using Prism.Events;
using Prism.Regions;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace PDF_Master.Views
{
///
/// Interaction logic for ViewContent
///
public partial class ViewContent : UserControl
{
private ViewContentViewModel viewModel = null;
private string unicode = "";
///
/// 是否是展开工具栏
///
private bool expandToolRow = true;
private bool DelayIsEnable = true;
public ViewContent()
{
InitializeComponent();
unicode = App.mainWindowViewModel.SelectedItem.Unicode;
viewModel = (ViewContentViewModel)this.DataContext;
ScrViewTab.Margin = new Thickness(CalculateStackPanelWidth(Leftpanel)+16, 0, CalculateStackPanelWidth(Rightpanel)+16,0);
viewModel.PropertyChanged += ViewModel_PropertyChanged;
}
///
/// 计算StackPane的宽度
///
///
///
public double CalculateStackPanelWidth(StackPanel stackPanel)
{
double totalWidth = 0;
foreach (UIElement child in stackPanel.Children)
{
child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
totalWidth += child.DesiredSize.Width;
}
return totalWidth;
}
private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "OpenBOTA")
{
if (!viewModel.OpenBOTA)
{
//动态改变BOTA的最小宽度只能在这里设置,VM里绑定会有异常
BOTACloumn.MinWidth = 48;
viewModel.BOTAWidth = new GridLength(48);
BOTASplitter.Visibility = Visibility.Collapsed;
}
else
{
BOTACloumn.MinWidth = viewModel.BOTAMiniWidth;
BOTASplitter.Visibility = Visibility.Visible;
}
}
}
public ViewContent(IEventAggregator eventAggregator) : this()
{
eventAggregator.GetEvent().Subscribe(ChangeSplitModeUI, e => e.Unicode == unicode);
eventAggregator.GetEvent().Subscribe(ChangeReadModeUI, e => e.Unicode == unicode);
eventAggregator.GetEvent().Subscribe(ChangeReadModePropertyOpenUI, e => e.Unicode == unicode);
}
private void ChangeReadModePropertyOpenUI(ReadModePropertyOpenArgs obj)
{
viewModel.ReadModeIsShowProperty = true;
Grid.SetColumnSpan(GridViewer, 3);
Grid.SetColumn(GridViewer, 0);
Grid.SetColumn(Scroller, 4);
}
private void ChangeReadModeUI(ReadModeArgs obj)
{
if (obj.IsReadMode)
{
ReadModeContent.Visibility = Visibility.Visible;
RectangleReadMode.Visibility = Visibility.Collapsed;
// 是否悬停显示阅读模式翻页控件,防止在其他区域遮挡部分功能
RectangleReadMode.IsHitTestVisible = true;
//因为ispropertyOpen 属性改成了单向控制 因此这里的语句不再适用
//this.TbtnProperty.IsChecked = false;
Grid.SetRowSpan(DocumentView, 4);
Grid.SetRow(DocumentView, 0);
Grid.SetColumn(GridViewer, 0);
Grid.SetColumnSpan(GridViewer, 4);
//var annotArgs = new SelectToolArgs();
//viewModel.PDFViewer.SetToolParam(annotArgs);
#region to do
//Grid.SetRowSpan(RectangleBota, 4);
//Grid.SetRow(RectangleBota, 0);
//RectangleBota.Visibility = Visibility.Visible;
#endregion to do
}
else
{
ReadModeContent.Visibility = Visibility.Collapsed;
RectangleReadMode.Visibility = Visibility.Collapsed;
RectangleReadMode.IsHitTestVisible = false;
//this.TbtnProperty.IsChecked=true;
Grid.SetRowSpan(DocumentView, 1);
Grid.SetRow(DocumentView, 2);
Grid.SetColumn(GridViewer, 2);
Grid.SetColumnSpan(GridViewer, 1);
//var annotArgs = new SelectToolArgs();
//viewModel.PDFViewer.SetToolParam(annotArgs);
#region to do
//Grid.SetRowSpan(RectangleBota, 1);
//Grid.SetRow(RectangleBota, 2);
//RectangleBota.Visibility = Visibility.Visible;
//Panel.SetZIndex(BOTAContent, 0);
//Panel.SetZIndex(BOTASplitter, 0);
#endregion to do
}
}
private void RectangleBota_MouseEnter(object sender, MouseEventArgs e)
{
if (viewModel.mainViewModel.IsBookMode)
{
RectangleBota.Visibility = Visibility.Collapsed;
BOTAContent.Visibility = Visibility.Visible;
Panel.SetZIndex(BOTAContent, 2);
Panel.SetZIndex(BOTASplitter, 2);
ReadModeContent.Visibility = Visibility.Visible;
RectangleReadMode.Visibility = Visibility.Collapsed;
//因为ispropertyOpen 属性改成了单向控制 因此这里的语句不再适用
//this.TbtnProperty.IsChecked = false;
Grid.SetRowSpan(DocumentView, 4);
Grid.SetRow(DocumentView, 0);
Grid.SetColumnSpan(GridViewer, 1);
Grid.SetColumn(GridViewer, 2);
Grid.SetColumnSpan(BOTAContent, 1);
Grid.SetColumn(BOTAContent, 0);
Grid.SetColumnSpan(BOTASplitter, 1);
Grid.SetColumn(BOTASplitter, 1);
Grid.SetRowSpan(RectangleBota, 4);
Grid.SetRow(RectangleBota, 0);
}
else
{
}
}
private void GridViewer_MouseEnter(object sender, MouseEventArgs e)
{
if (viewModel.mainViewModel.IsBookMode)
{
ReadModeContent.Visibility = Visibility.Visible;
RectangleReadMode.Visibility = Visibility.Collapsed;
//因为ispropertyOpen 属性改成了单向控制 因此这里的语句不再适用
//this.TbtnProperty.IsChecked = false;
Grid.SetRowSpan(DocumentView, 4);
Grid.SetRow(DocumentView, 0);
Grid.SetRowSpan(RectangleBota, 4);
Grid.SetRow(RectangleBota, 0);
Panel.SetZIndex(BOTAContent, 0);
Panel.SetZIndex(BOTASplitter, 0);
RectangleBota.Visibility = Visibility.Visible;
if (viewModel.ReadModeIsShowProperty)
{
Grid.SetColumnSpan(GridViewer, 3);
Grid.SetColumn(GridViewer, 0);
Grid.SetColumn(Scroller, 4);
}
else
{
Grid.SetColumnSpan(GridViewer, 4);
Grid.SetColumn(GridViewer, 0);
}
}
}
///
/// 鼠标移动到控件范围时显示控件
///
///
///
private void RectangleReadMode_MouseEnter(object sender, MouseEventArgs e)
{
//Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(delegate
//{
if (viewModel.mainViewModel.IsBookMode)
{
DelayIsEnable = false;
this.ReadModeContent.Visibility = Visibility.Visible;
this.RectangleReadMode.Visibility = Visibility.Collapsed;
}
//}));
}
///
/// 鼠标移开页面控件,三秒后隐藏该控件
///
///
///
private async void ReadModeContent_MouseLeave(object sender, MouseEventArgs e)
{
DelayIsEnable = true;
await System.Threading.Tasks.Task.Delay(3000);
//Thread.Sleep(3000);
if (DelayIsEnable)
{
this.ReadModeContent.Visibility = Visibility.Collapsed;
this.RectangleReadMode.Visibility = Visibility.Visible;
}
}
///
/// 根据Vm事件通知处理分屏布局的事件
///
///
private void ChangeSplitModeUI(SplitEventArgs e)
{
switch (e.Mode)
{
case EventAggregators.SplitMode.Horizontal:
//水平分屏 上下
HorizontalSplitter.Visibility = Visibility.Visible;
VerticalSplitter.Visibility = Visibility.Collapsed;
RowRight.Height = new GridLength(1, GridUnitType.Star);
ColumnBottom.Width = new GridLength(0);
Grid.SetColumn(SplitPDFViewer, 0);
Grid.SetRow(SplitPDFViewer, 2);
break;
case EventAggregators.SplitMode.Vertical:
HorizontalSplitter.Visibility = Visibility.Collapsed;
VerticalSplitter.Visibility = Visibility.Visible;
RowRight.Height = new GridLength(0);
ColumnBottom.Width = new GridLength(1, GridUnitType.Star);
Grid.SetColumn(SplitPDFViewer, 2);
Grid.SetRow(SplitPDFViewer, 0);
break;
case EventAggregators.SplitMode.Single:
default:
//退出分屏模式
HorizontalSplitter.Visibility = Visibility.Collapsed;
VerticalSplitter.Visibility = Visibility.Collapsed;
RowRight.Height = new GridLength(0, GridUnitType.Star);
ColumnBottom.Width = new GridLength(0, GridUnitType.Star);
break;
}
GridViewer.UpdateLayout();
}
///
/// 退出阅读模式
///
///
///
private void UserControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (Key.Escape == e.Key)
{
if (viewModel != null)
{
if (viewModel.PDFViewer.MouseMode != MouseModes.PanTool)
{
if (this.ContentToolsBar.Content is AnnotToolContent annotTool)
{
if (annotTool.BtnSelecttool.IsChecked == true)
{
AnnotToolContentViewModel model = (AnnotToolContentViewModel)annotTool.DataContext;
if (model != null)
{
if (model.SnapshotEditMenuViewModel.SnapToolArgs != null)
{
model.SnapshotEditMenuViewModel.SnapToolArgs.ReSnapshot();
}
}
}
}
if (viewModel.mainViewModel.IsBookMode == true)
{
viewModel.Off_ReadModel();
}
}
else
{
if (viewModel.mainViewModel.IsBookMode == true)
{
viewModel.Off_ReadModel();
}
}
}
}
}
private void BtnFile_Initialized(object sender, EventArgs e)
{
BtnFile.ContextMenu = null;
}
private void BtnFile_Click(object sender, RoutedEventArgs e)
{
FileMenu.PlacementTarget = BtnFile;
FileMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
FileMenu.IsOpen = true;
}
private void BtnMore_Initialized(object sender, EventArgs e)
{
BtnMore.ContextMenu = null;
}
private void BtnMore_Click(object sender, RoutedEventArgs e)
{
MenuMore.PlacementTarget = BtnMore;
MenuMore.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
MenuMore.IsOpen = true;
}
private void ContentControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
}
private void MenuRecent_Loaded(object sender, RoutedEventArgs e)
{
int count = 0;
MenuRecent.Items.Clear();
foreach (var file in Properties.Settings.Default.RecentOpenFiles)
{
MenuItem item = new MenuItem();
item.Height = 32;
item.Header = file.FileName;
item.Tag = file.FilePath;
item.ToolTip = file.FilePath;
item.Click += RecentItem_Click;
MenuRecent.Items.Insert(MenuRecent.Items.Count, item);
count++;
if (count >= 20)
break;
}
if (Properties.Settings.Default.RecentOpenFiles.Count > 0)
{
MenuRecent.Items.Add(Menu_Separator);
MenuRecent.Items.Add(MenuDeleteAll);
MenuDeleteAll.IsEnabled = true;
MenuDeleteAll.Opacity = 1;
}
else
{
MenuRecent.Items.Add(MenuDeleteAll);
MenuDeleteAll.IsEnabled = false;
MenuDeleteAll.Opacity = 0.5;
}
}
private void RecentItem_Click(object sender, RoutedEventArgs e)
{
var item = sender as MenuItem;
string[] filePath = new string[1];
filePath[0] = item.Tag.ToString();
if (App.OpenedFileList.Contains(filePath[0]))
{
(this.DataContext as ViewContentViewModel).mainViewModel.mainWindowViewModel.SelectItem(filePath[0]);
}
else
{
(this.DataContext as ViewContentViewModel).mainViewModel.mainWindowViewModel.AddTabItem(filePath[0]);
}
}
private void MenuDeleteAll_Click(object sender, RoutedEventArgs e)
{
SettingHelper.RemoveAllRecentOpenFiles();
}
private void UserControl_DragEnter(object sender, DragEventArgs e)
{
DragDropHelper.DragEnter(this, e);
}
private void UserControl_DragOver(object sender, DragEventArgs e)
{
DragDropHelper.DragOver(this, e);
}
private void UserControl_DragLeave(object sender, DragEventArgs e)
{
DragDropHelper.DragLeave();
}
private void UserControl_Drop(object sender, DragEventArgs e)
{
try
{
DragDropHelper.Drop(this, e);
//页面编辑模式下,拖动文件到页面编辑的Listbox时,不运行打开文件的逻辑
//拖动到页面编辑的Listbox上方空白处时,打开文件
if (viewModel.isInPageEdit)
{
var pos2 = e.GetPosition(this);
var result2 = VisualTreeHelper.HitTest(this, pos2);
if (result2 != null)
{
var pageEdite = CommonHelper.FindVisualParent(result2.VisualHit);
var pageEdite1 = CommonHelper.FindVisualChild(result2.VisualHit);
}
// 暂时 用遍历 的方法,找PageEditContent,缺点=》有点耗时,非最优方案,后续有较好的方案可补充修改
viewModel.FindPageEditContent(out PageEditContent pageEditContent);
if (pageEditContent != null)
{
var pos1 = e.GetPosition(pageEditContent);
var result1 = VisualTreeHelper.HitTest(pageEditContent, pos1);
if (result1 == null)
{
OpenDocument(e);
}
else
{
pos1 = e.GetPosition(pageEditContent.GridHeader);
result1 = VisualTreeHelper.HitTest(pageEditContent.GridHeader, pos1);
if (result1 != null)
{
OpenDocument(e);
}
}
}
}
else
{
bool isTabItemThumbnail = viewModel.IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemThumbnail");
//BOTA缩略图和页面编辑拖动中时,不响应拖拽打开文件
//因为目前页面编辑支持拖拽到桌面生成文件,也会是FileDrop类型,所以通过鼠标坐标点判断是否在缩略图区域
if (isTabItemThumbnail)
{
var pos = e.GetPosition(BOTAContent);
var result = VisualTreeHelper.HitTest(BOTAContent, pos);
if (result == null)
{
//BOTA缩略图和页面编辑拖动中时,不响应拖拽打开文件
//因为目前页面编辑支持拖拽到桌面生成文件,也会是FileDrop类型,暂时只能通过DragDropEffects来区分app内的拖拽和外部文件拖入(外部文件拖入是多个操作类型)
//if (file != null && file.Length > 0 && flag == false && e.AllowedEffects != DragDropEffects.Copy)
//{
// if (!App.OpenedFileList.Contains(file[0]))
// {
// App.mainWindowViewModel.AddTab.Execute();
// }
// (App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).homeContentViewModel.AddFileFromDrag(file.ToList());
//}
OpenDocument(e);
}
}
else
{
OpenDocument(e);
}
}
//var pos = e.GetPosition(this);
//var result = VisualTreeHelper.HitTest(this, pos);
//var listbox = CommonHelper.FindVisualParent(result.VisualHit);
//if (listbox != null)
//{
// return;
//}
////BOTA缩略图和页面编辑拖动中时,不响应拖拽打开文件
////因为目前页面编辑支持拖拽到桌面生成文件,也会是FileDrop类型,暂时只能通过DragDropEffects来区分app内的拖拽和外部文件拖入(外部文件拖入是多个操作类型)
//OpenDocument(e);
}
catch { }
}
private void OpenDocument(DragEventArgs e)
{
string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
if (file != null && file.Length > 0 && e.AllowedEffects != DragDropEffects.Copy)
{
if (!App.OpenedFileList.Contains(file[0]))
{
App.mainWindowViewModel.AddTab.Execute();
}
(App.mainWindowViewModel.SelectedItem.DataContext as MainContentViewModel).homeContentViewModel.AddFileFromDrag(file.ToList());
}
}
private void CloseLeftCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
viewModel.OpenBOTA = false;
}
private void CloseRightCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
viewModel.ClosePropertyCommand.Execute();
}
private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
viewModel.CloseAutoSave();
}
private void ToolTabControl_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var visual = VisualTreeHelper.HitTest(ToolTabControl, e.GetPosition(ToolTabControl));
if (visual == null)
{
return;
}
var item = CommonHelper.FindVisualParent(visual.VisualHit);
if (item == null)
{
return;
}
//默认展开子工具栏
//单击工具栏工具展开子工具栏;再次单击工具栏工具则收起子工具栏
if (item.IsSelected && item.Name != "TabItemPageEdit")
{
expandToolRow = !expandToolRow;
ToolRow.Height = expandToolRow ? new GridLength(40) : new GridLength(0);
//取消选中
item.IsSelected = false;
//修复第一次打开文档后,第一次点击收起工具栏异常问题
e.Handled = true;
}
else
{
if (ToolRow.Height == new GridLength(0))
{
//点击其他菜单栏时,之前为折叠状态时,自动展开
expandToolRow = !expandToolRow;
ToolRow.Height = new GridLength(40);
}
//取消选中
item.IsSelected = true;
}
}
private void AddTabCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
viewModel.mainViewModel.mainWindowViewModel.AddTab.Execute();
}
private void CloseTabCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
viewModel.mainViewModel.CloseTab.Execute(null);
}
private void UserControl_MouseDown(object sender, MouseButtonEventArgs e)
{
//鼠标侧键无法在Xaml里绑定 因此写在后台代码里
if (e.ChangedButton == MouseButton.XButton2)
{
viewModel.bottom?.NextViewCommnad.Execute();
}
else if (e.ChangedButton == MouseButton.XButton1)
{
viewModel.bottom?.PreViewCommand.Execute();
}
}
private void ButLeft_Click(object sender, RoutedEventArgs e)
{
ScrViewTab.LineLeft();
}
private void ButRight_Click(object sender, RoutedEventArgs e)
{
ScrViewTab.LineRight();
}
private void ScrViewTab_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
if (e.ViewportWidth < 384)
{
ButLeft.Visibility = Visibility.Visible;
ButRight.Visibility = Visibility.Visible;
}
else
{
ButLeft.Visibility = Visibility.Collapsed;
ButRight.Visibility = Visibility.Collapsed;
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
//自动聚焦,修复快捷键无响应的问题
this.Focusable = true;
this.Focus();
}
private void ToolTabControl_PreviewKeyDown(object sender, KeyEventArgs e)
{
//禁用控件的左右按键,避免和左右翻页快捷键冲突
if (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Tab)
{
e.Handled = true;
}
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
//检查更新
if (App.SparkleUpdateDetector != null)
App.SparkleUpdateDetector?.CheckForUpdatesAtUserRequest();
}
private void BOTASplitter_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if(e.GetPosition(this).X