Browse Source

视图-阅读模式-页面控件

OYXH\oyxh 2 years ago
parent
commit
fe62b43183

+ 185 - 1
PDF Office/ViewModels/PropertyPanel/ViewModular/ReadModeContentViewModel.cs

@@ -1,15 +1,175 @@
-using Prism.Mvvm;
+using ComPDFKitViewer.PdfViewer;
+using Dropbox.Api.Sharing;
+using PDF_Office.Helper;
+using PDF_Office.Model;
+using Prism.Commands;
+using Prism.Mvvm;
 using Prism.Regions;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
+using System.Windows.Controls;
+using System.Windows.Forms;
+using System.Windows.Input;
+using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrayNotify;
+using System.Windows.Media;
+using KeyEventArgs = System.Windows.Input.KeyEventArgs;
+using TextBox = System.Windows.Controls.TextBox;
+using static System.Windows.Forms.VisualStyles.VisualStyleElement;
 
 namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
 {
     public class ReadModeContentViewModel : BindableBase, INavigationAware
     {
+        private ViewContentViewModel viewContentViewModel;
+        private CPDFViewer PDFViewer;
+
+        private int currentPage;
+
+        public int CurrentPage
+        {
+            get { return currentPage; }
+            set
+            {
+                SetProperty(ref currentPage, value);
+            }
+        }
+
+        private int pageCount;
+
+        public int PageCount
+        {
+            get { return pageCount; }
+            set
+            {
+                SetProperty(ref pageCount, value);
+            }
+        }
+
+        public DelegateCommand<object> KeyDownCommand { get; set; }
+        public DelegateCommand<object> PreviewKeyDownCommand { get; set; }
+        public DelegateCommand<object> ZoomOutCommand { get; set; }
+        public DelegateCommand<object> ZoomInCommand { get; set; }
+        public DelegateCommand<object> PrePageCommand { get; set; }
+        public DelegateCommand<object> NextPageCommand { get; set; }
+
+        public ReadModeContentViewModel()
+        {
+            KeyDownCommand = new DelegateCommand<object>(KeyDownEvent);
+            PreviewKeyDownCommand = new DelegateCommand<object>(PreviewKeyDownEvent);
+            ZoomOutCommand = new DelegateCommand<object>(PageZoomOutEvent);
+            ZoomInCommand = new DelegateCommand<object>(PageZoomInEvent);
+
+            PrePageCommand = new DelegateCommand<object>(PrePageEvent);
+            NextPageCommand = new DelegateCommand<object>(NextPageEvent);
+        }
+
+        private void NextPageEvent(object obj)
+        {
+            if (PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Double || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.DoubleContinuous || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Book || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.BookContinuous)
+
+            {
+                PDFViewer.GoToPage(PDFViewer.CurrentIndex + 2);
+            }
+            else
+                PDFViewer.GoToPage(PDFViewer.CurrentIndex + 1);
+        }
+
+        private void PrePageEvent(object obj)
+        {
+            if (PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Double || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.DoubleContinuous || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.Book || PDFViewer.ModeView == ComPDFKitViewer.ViewMode.BookContinuous)
+            {
+                PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
+            }
+            else
+            {
+                PDFViewer.GoToPage(PDFViewer.CurrentIndex - 1);
+            }
+        }
+
+        private void PageZoomInEvent(object obj)
+        {
+            double zoom = PDFViewer.ZoomFactor * 100;
+            zoom = zoom + 25;
+            if (zoom != 0 && zoom <= 1000 && PDFViewer != null)
+            {
+                PDFViewer.Zoom(zoom / 100);
+            }
+        }
+
+        private void PageZoomOutEvent(object obj)
+        {
+            double zoom = PDFViewer.ZoomFactor * 100;
+            zoom = zoom - 25;
+            if (zoom != 0 && PDFViewer != null)
+            {
+                PDFViewer.Zoom(zoom / 100);
+            }
+        }
+
+        private void PreviewKeyDownEvent(object obj)
+        {
+            var args = obj as KeyEventArgs;
+            if (args != null)
+            {
+                //显示文本框输入内容
+                List<Key> NumberKeys = new List<Key>() { Key.D0,Key.D1,Key.D2,Key.D3,Key.D4,Key.D5,Key.D6,Key.D7,Key.D8,Key.D9,Key.NumPad0,Key.NumPad1,Key.NumPad2,Key.NumPad3,Key.NumPad4,Key.NumPad5,Key.NumPad6,Key.NumPad7,Key.NumPad8,Key.NumPad9,Key.Delete,Key.Back,Key.Enter,Key.Right,Key.Left};
+                if (!NumberKeys.Contains(args.Key))
+                {
+                    args.Handled = true;
+                }
+            }
+        }
+
+        private void KeyDownEvent(object obj)
+        {
+            if (obj is CompositeCommandParameter objs)
+            {
+                if (objs.EventArgs is System.Windows.Input.KeyEventArgs keyEventArgs)
+                {
+                    TextBlock textBlock = objs.Parameter as TextBlock;
+                    if (keyEventArgs.Key == Key.Enter)
+                    {
+                        if (keyEventArgs.OriginalSource is System.Windows.Controls.TextBox txtCurrentPageNum)
+                        {
+                            int pagenum = 0;
+                            string text = txtCurrentPageNum.Text.ToString();
+                            char[] chs = { ' ', '/', '\\' };//字符截取  拒止非法输入
+                            int i = text.LastIndexOfAny(chs);
+                            if (i > 0)
+                            {
+                                text = text.Substring(0, i - 1);
+                            }
+                            if (!int.TryParse(text, out pagenum))
+                            {
+                                CurrentPage = PDFViewer.Document.PageCount;
+                                txtCurrentPageNum.Text = PDFViewer.Document.PageCount.ToString();
+                                return;
+                            }
+                            if (pagenum < 1)
+                            {
+                                pagenum = 1;
+                            }
+                            else if (pagenum > PDFViewer.Document.PageCount)
+                            {
+                                pagenum = PDFViewer.Document.PageCount;
+                            }
+
+                            PDFViewer.GoToPage(pagenum - 1);
+                            CurrentPage = pagenum;
+                            txtCurrentPageNum.Text = pagenum.ToString();
+                            textBlock.Visibility = System.Windows.Visibility.Visible;
+                            txtCurrentPageNum.Background = new SolidColorBrush(Colors.Transparent);
+                            txtCurrentPageNum.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
+                        }
+                    }
+                }
+            }
+        }
+
         public bool IsNavigationTarget(NavigationContext navigationContext)
         {
             return true;
@@ -21,6 +181,30 @@ namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
 
         public void OnNavigatedTo(NavigationContext navigationContext)
         {
+            var viewContentViewModel = navigationContext.Parameters[ParameterNames.ViewContentViewModel] as ViewContentViewModel;
+            if (viewContentViewModel != null)
+            {
+                this.viewContentViewModel = viewContentViewModel;
+            }
+            var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
+            if (pdfview != null)
+            {
+                //获取页面设置等信息
+                this.PDFViewer = pdfview;
+                PageCount = PDFViewer.Document.PageCount;
+                CurrentPage = PDFViewer.CurrentIndex + 1;
+                this.PDFViewer.InfoChanged += PDFViewer_InfoChanged;
+            }
+        }
+
+        private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
+        {
+            if (e.Key == "SplieMode" || e.Key == "ViewMode")
+            {
+                return;
+            }
+            PageCount = PDFViewer.Document.PageCount;
+            CurrentPage = PDFViewer.CurrentIndex + 1;
         }
     }
 }

+ 48 - 17
PDF Office/Views/PropertyPanel/ViewModular/ReadModeContent.xaml

@@ -3,22 +3,28 @@
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:ViewModular="clr-namespace:PDF_Office.ViewModels.PropertyPanel.ViewModular"
+    xmlns:convert="clr-namespace:PDF_Office.DataConvert"
     xmlns:converter="clr-namespace:PDF_Office.DataConvert"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:helper="clr-namespace:PDF_Office.Helper"
+    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
     xmlns:local="clr-namespace:PDF_Office.Views.PropertyPanel.ViewModular"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:prism="http://prismlibrary.com/"
+    x:Name="ReadMode"
     Width="auto"
     d:DataContext="{d:DesignInstance Type=ViewModular:ReadModeContentViewModel}"
     d:DesignHeight="44"
     d:DesignWidth="220"
     Background="#CC000000"
     mc:Ignorable="d">
+
     <UserControl.Resources>
         <ResourceDictionary>
             <ResourceDictionary.MergedDictionaries>
                 <ResourceDictionary Source="pack://application:,,,/Styles/ButtonStyle.xaml" />
             </ResourceDictionary.MergedDictionaries>
+            <convert:ObjectConvert x:Key="ObjectConvert" />
         </ResourceDictionary>
     </UserControl.Resources>
     <StackPanel Name="ParentPanel" Orientation="Horizontal">
@@ -30,6 +36,7 @@
             Margin="1,0,2,0"
             Background="Transparent"
             BorderThickness="0"
+            Command="{Binding ZoomOutCommand}"
             Interval="200"
             Template="{StaticResource zoomout}" />
         <RepeatButton
@@ -39,11 +46,12 @@
             Margin="0,0,1,0"
             Background="Transparent"
             BorderThickness="0"
+            Command="{Binding ZoomInCommand}"
             Interval="200"
             Template="{StaticResource zoomin}" />
 
         <Border
-            Width="92"
+            Width="100"
             Height="32"
             BorderBrush="White"
             BorderThickness="0">
@@ -54,54 +62,77 @@
                 </Grid.ColumnDefinitions>
                 <Grid>
                     <TextBox
-                        x:Name="txtCurrentPageNum"
+                        x:Name="TxtCurrentPageNum"
                         Width="45"
                         Height="32"
-                        Padding="5,5.5,0,0"
+                        Padding="0,5.5,0,0"
                         Background="Transparent"
                         BorderThickness="0"
-                        CaretBrush="White"
+                        CaretBrush="Black"
                         FontFamily="Segoe UI"
                         FontSize="14"
-                        Foreground="White"
-                        Text="1"
-                        TextAlignment="Center" />
+                        Foreground="Black"
+                        InputMethod.IsInputMethodEnabled="False"
+                        LostFocus="TxtCurrentPageNum_LostFocus"
+                        LostKeyboardFocus="TxtCurrentPageNum_LostKeyboardFocus"
+                        Text="{Binding CurrentPage}"
+                        TextAlignment="Center">
+                        <i:Interaction.Triggers>
+                            <i:EventTrigger EventName="KeyDown">
+                                <helper:AdvancedInvokeCommandAction
+                                    Command="{Binding KeyDownCommand}"
+                                    CommandParameter="{Binding ElementName=TxbCurrentPageNum}"
+                                    PassEventArgsToCommand="True" />
+                            </i:EventTrigger>
+                            <i:EventTrigger EventName="PreviewKeyDown">
+                                <i:InvokeCommandAction Command="{Binding PreviewKeyDownCommand}" PassEventArgsToCommand="True" />
+                            </i:EventTrigger>
+                            <!--<i:EventTrigger EventName="LostKeyboardFocus">
+                            <helper:AdvancedInvokeCommandAction
+                            Command="{Binding LostKeyboardFocusCommand}"
+                            CommandParameter="{Binding ElementName=TxbCurrentPageNum}"
+                            PassEventArgsToCommand="True" />
+                            </i:EventTrigger>-->
+                        </i:Interaction.Triggers>
+                    </TextBox>
+
                     <TextBlock
-                        Name="TxbTitle"
+                        Name="TxbCurrentPageNum"
                         Width="45"
                         Height="32"
-                        Padding="5,5.5,0,0"
-                        Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=Background}"
+                        Padding="0,5.5,0,0"
+                        Background="Transparent"
                         FontFamily="Segoe UI"
                         FontSize="14"
                         Foreground="White"
-                        Text="1"
+                        PreviewMouseLeftButtonDown="TxbCurrentPageNum_PreviewMouseLeftButtonDown"
+                        Text="{Binding CurrentPage}"
                         TextAlignment="Center"
                         Visibility="Visible" />
                 </Grid>
 
                 <TextBlock
-                    x:Name="txtPageCount"
+                    x:Name="TxtPageCount"
                     Grid.Column="1"
-                    Width="45"
                     Height="32"
+                    MinWidth="50"
                     Padding="0,5.5,0,0"
                     FontFamily="Segoe UI"
                     FontSize="14"
                     Foreground="#999999"
-                    Text="/5"
+                    Text="{Binding PageCount, StringFormat=/    {0}}"
                     TextAlignment="Left" />
             </Grid>
         </Border>
         <Button
             Name="btnPrePage"
-            Margin="1,0,2,0"
+            Margin="25,0,2,0"
             Background="Transparent"
-            Template="{StaticResource prepage}" />
+            Template="{StaticResource prepage}" Command="{Binding PrePageCommand}" />
         <Button
             x:Name="btnNextPage"
             Margin="2,0,1,0"
             Background="Transparent"
-            Template="{StaticResource nextpage}" />
+            Template="{StaticResource nextpage}" Command="{Binding NextPageCommand}" />
     </StackPanel>
 </UserControl>

+ 37 - 2
PDF Office/Views/PropertyPanel/ViewModular/ReadModeContent.xaml.cs

@@ -1,7 +1,9 @@
-using System;
+using DryIoc;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
@@ -24,5 +26,38 @@ namespace PDF_Office.Views.PropertyPanel.ViewModular
         {
             InitializeComponent();
         }
+
+        private void TxbCurrentPageNum_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
+        {
+            if (e.ClickCount >= 2)
+            {
+                TxbCurrentPageNum.Visibility = Visibility.Collapsed;
+                TxtCurrentPageNum.Dispatcher.BeginInvoke(new Action(() =>
+                {
+                    TxtCurrentPageNum.Background = new SolidColorBrush(Colors.White);
+                    TxtCurrentPageNum.Focus();
+                    TxtCurrentPageNum.SelectAll();
+                }));
+            }
+        }
+
+        private void TxtCurrentPageNum_MouseLeave(object sender, RoutedEventArgs e)
+        {
+            TxbCurrentPageNum.Visibility = Visibility.Visible;
+
+            TxtCurrentPageNum.Background = new SolidColorBrush(Colors.Transparent);
+        }
+
+        private void TxtCurrentPageNum_LostFocus(object sender, RoutedEventArgs e)
+        {
+            TxbCurrentPageNum.Visibility = Visibility.Visible;
+            TxtCurrentPageNum.Background = new SolidColorBrush(Colors.Transparent);
+        }
+
+        private void TxtCurrentPageNum_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
+        {
+            TxbCurrentPageNum.Visibility = Visibility.Visible;
+            TxtCurrentPageNum.Background = new SolidColorBrush(Colors.Transparent);
+        }
     }
-}
+}

+ 2 - 2
PDF Office/Views/PropertyPanel/ViewModular/ViewModularContent.xaml.cs

@@ -41,10 +41,10 @@ namespace PDF_Office.Views.PropertyPanel.ViewModular
                 {
                     if (main.ContentMain.Content is ViewContent viewContent)
                     {
-                        viewContent.MenuEnterReadMode_Click(sender, e);
+                        viewContent.RbtnReadMode_Click(sender, e);
 
                     }
-                    
+
                 }
             }
         }

+ 17 - 0
PDF Office/Views/ViewContent.xaml

@@ -3,6 +3,7 @@
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:Viewer="clr-namespace:ComPDFKitViewer.PdfViewer;assembly=ComPDFKit.Viewer"
+    xmlns:convert="clr-namespace:PDF_Office.DataConvert"
     xmlns:cus="clr-namespace:PDF_Office.CustomControl"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
@@ -31,8 +32,10 @@
                 <Setter Property="Width" Value="20" />
                 <Setter Property="Height" Value="20" />
             </Style>
+            <convert:BoolToVisible x:Key="BoolToVisibleConvert" />
         </ResourceDictionary>
     </UserControl.Resources>
+
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Name="HeadRow" Height="40" />
@@ -224,6 +227,20 @@
                 HorizontalAlignment="Center"
                 VerticalAlignment="Bottom"
                 prism:RegionManager.RegionName="{Binding ReadModeRegionName}"
+                MouseLeave="ReadModeContent_MouseLeave"
+                Visibility="Collapsed" />
+
+            <Rectangle
+                x:Name="RectangleReadMode"
+                Grid.Column="0"
+                Grid.ColumnSpan="5"
+                Width="{Binding ActualWidth, ElementName=ReadModeContent, Mode=OneWay}"
+                Height="44"
+                Margin="0,0,0,5"
+                HorizontalAlignment="Center"
+                VerticalAlignment="Bottom"
+                Fill="Transparent"
+                MouseEnter="RectangleReadMode_MouseEnter"
                 Visibility="Collapsed" />
         </Grid>
 

+ 64 - 19
PDF Office/Views/ViewContent.xaml.cs

@@ -5,6 +5,7 @@ using System;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Input;
+using System.Windows.Media;
 
 namespace PDF_Office.Views
 {
@@ -21,41 +22,59 @@ namespace PDF_Office.Views
             viewModel = (ViewContentViewModel)this.DataContext;
         }
 
-        public void MenuEnterReadMode_Click(object sender, RoutedEventArgs e)
+        /// <summary>
+        /// 阅读模式
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        public void RbtnReadMode_Click(object sender, RoutedEventArgs e)
         {
             if (viewModel != null)
             {
                 App.IsBookMode = true;
-                SetReadMode(Visibility.Collapsed, false, 0, 4, 0, 4);
+                SetReadMode(true);
             }
         }
 
-        private void SetReadMode(Visibility visibility, bool isPropertyOpen, int viewColumn, int viewColumnSpan, int docViewColumn, int docViewColumnSpan)
+        /// <summary>
+        /// 设置阅读模式下的控件
+        /// </summary>
+        /// <param name="isBookMode"></param>
+        private void SetReadMode(bool isBookMode)
         {
-            viewModel.IsReadMode = visibility;
-            viewModel.IsPropertyOpen = isPropertyOpen;
-            viewModel.ToolContentVisible = visibility;
-            viewModel.ToolsBarContentVisible = visibility;
-            if (visibility != Visibility.Visible)
+            if (isBookMode)
             {
+                viewModel.IsReadMode = Visibility.Collapsed;
+                viewModel.IsPropertyOpen = false;
+                viewModel.ToolContentVisible = Visibility.Collapsed;
+                viewModel.ToolsBarContentVisible = Visibility.Collapsed;
                 this.ReadModeContent.Visibility = Visibility.Visible;
-                
+                this.RectangleReadMode.Visibility = Visibility.Collapsed;
+                Grid.SetColumn(this.PDFViewerContent, 0);
+                Grid.SetColumnSpan(this.PDFViewerContent, 4);
+                Grid.SetRow(this.DocumentView, 0);
+                Grid.SetRowSpan(this.DocumentView, 4);
             }
             else
             {
+                viewModel.IsReadMode = Visibility.Visible;
+                viewModel.IsPropertyOpen = true;
+                viewModel.ToolContentVisible = Visibility.Visible;
+                viewModel.ToolsBarContentVisible = Visibility.Visible;
                 this.ReadModeContent.Visibility = Visibility.Collapsed;
+                this.RectangleReadMode.Visibility = Visibility.Visible;
+                Grid.SetColumn(this.PDFViewerContent, 2);
+                Grid.SetColumnSpan(this.PDFViewerContent, 1);
+                Grid.SetRow(this.DocumentView, 2);
+                Grid.SetRowSpan(this.DocumentView, 1);
             }
-
-            Grid.SetColumn(this.PDFViewerContent, viewColumn);
-            Grid.SetColumnSpan(this.PDFViewerContent, viewColumnSpan);
-            Grid.SetRow(this.DocumentView, docViewColumn);
-            Grid.SetRowSpan(this.DocumentView, docViewColumnSpan);
-        }
-
-        private void PropPanel_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
-        {
         }
 
+        /// <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)
@@ -63,9 +82,35 @@ namespace PDF_Office.Views
                 if (viewModel != null)
                 {
                     App.IsBookMode = false;
-                    SetReadMode(Visibility.Visible, true, 2, 1, 2, 1);
+                    SetReadMode(false);
                 }
             }
         }
+
+        /// <summary>
+        /// 鼠标移开页面控件,三秒后隐藏该控件
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private async void ReadModeContent_MouseLeave(object sender, RoutedEventArgs e)
+        {
+            await System.Threading.Tasks.Task.Delay(3000);
+            this.ReadModeContent.Visibility = Visibility.Collapsed;
+            this.RectangleReadMode.Visibility = Visibility.Visible;
+        }
+
+        /// <summary>
+        /// 鼠标移动到控件范围时显示控件
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void RectangleReadMode_MouseEnter(object sender, MouseEventArgs e)
+        {
+            if (App.IsBookMode)
+            {
+                this.ReadModeContent.Visibility = Visibility.Visible;
+                this.RectangleReadMode.Visibility = Visibility.Collapsed;
+            }
+        }
     }
 }