123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672 |
- 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
- {
- /// <summary>
- /// Interaction logic for ViewContent
- /// </summary>
- public partial class ViewContent : UserControl
- {
- private ViewContentViewModel viewModel = null;
- private string unicode = "";
- /// <summary>
- /// 是否是展开工具栏
- /// </summary>
- 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;
- }
- /// <summary>
- /// 计算StackPane的宽度
- /// </summary>
- /// <param name="stackPanel"></param>
- /// <returns></returns>
- 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<SplitEvent>().Subscribe(ChangeSplitModeUI, e => e.Unicode == unicode);
- eventAggregator.GetEvent<ReadModeEvent>().Subscribe(ChangeReadModeUI, e => e.Unicode == unicode);
- eventAggregator.GetEvent<ReadModePropertyOpenEvent>().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);
- }
- }
- }
- /// <summary>
- /// 鼠标移动到控件范围时显示控件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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;
- }
- //}));
- }
- /// <summary>
- /// 鼠标移开页面控件,三秒后隐藏该控件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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;
- }
- }
- /// <summary>
- /// 根据Vm事件通知处理分屏布局的事件
- /// </summary>
- /// <param name="e"></param>
- 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();
- }
- /// <summary>
- /// 退出阅读模式
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- 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<PageEditContent>(result2.VisualHit);
- var pageEdite1 = CommonHelper.FindVisualChild<PageEditContent>(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<ListBox>(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<TabItem>(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<viewModel.BOTAMiniWidth)
- {
- //需要拦截控件本身关于拖拽事件后的操作,才能响应关闭BOTA的逻辑
- e.Handled = true;
- viewModel.OpenBOTA = false;
- }
- }
- }
- }
|