Browse Source

合并- 补充文件加载进度条,支持取消打开文件夹

ZhouJieSheng 1 year ago
parent
commit
9f5b901145

+ 212 - 139
PDF Office/ViewModels/Dialog/ToolsDialogs/MergeDialogViewModel.cs

@@ -119,6 +119,8 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
         public DelegateCommand ClearCommand { get; set; }
         public DelegateCommand<object> SetPageSizeTypeCommand { get; set; }
 
+        public DelegateCommand CancelAddFileCommand { get; set; }
+
         public IDialogService dialogs;
 
         private PageSizeType pageSizeType = PageSizeType.kDefault;
@@ -134,6 +136,20 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
             }
         }
 
+        private Visibility processVisible = Visibility.Collapsed;
+        /// <summary>
+        /// 是否显示进度条
+        /// </summary>
+        public Visibility ProcessVisible
+        {
+            get { return processVisible; }
+            set
+            {
+                SetProperty(ref processVisible, value);
+            }
+        }
+
+
         private string inputHeight;
 
         public string InputHeight
@@ -145,6 +161,41 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
             }
         }
 
+        private double maxValue;
+        /// <summary>
+        /// 最大文件数 用于显示进度条
+        /// </summary>
+        public double MaxValue
+        {
+            get { return maxValue; }
+            set
+            {
+                SetProperty(ref maxValue, value);
+            }
+        }
+
+        private double currentvalue;
+
+        /// <summary>
+        /// 当前进度值
+        /// </summary>
+        public double CurrentValue
+        {
+            get { return currentvalue; }
+            set
+            {
+                SetProperty(ref currentvalue, value);
+            }
+        }
+
+
+
+        /// <summary>
+        /// 是否取消添加文件
+        /// </summary>
+
+        private bool CancelAddFiles = false;
+
 
         public MergeDialogViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
         {
@@ -157,11 +208,18 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
             MergeCommand = new DelegateCommand(Merge);
             ClearCommand = new DelegateCommand(Clear);
             SetPageSizeTypeCommand = new DelegateCommand<object>(SetPageSizeType);
-        }
+            CancelAddFileCommand = new DelegateCommand(CancelAddFileOpention);
+        }
+
+
         #endregion
-
+
         #region 私有方法
-
+        private void CancelAddFileOpention()
+        {
+            CancelAddFiles = true;
+        }
+
         private void UpDataMergeObjectIndex()
         {
             for (int i = 0; i < MergeObjectlist.Count; i++)
@@ -201,6 +259,7 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
         public void ButtonAddFiles(object data)
         {
             int index = Convert.ToInt32(data);
+            CancelAddFiles = false;
             switch (index)
             {
                 case 0:
@@ -483,157 +542,171 @@ namespace PDF_Master.ViewModels.Dialog.ToolsDialogs
             UpDataMergeObjectIndex();
         }
 
-        public void AddFiles(string[] FilePath)
+        public async void AddFiles(string[] FilePath)
         {
             if (FilePath == null)
             {
                 return;
             }
             bool showDialog = false;
-            for (int i = 0; i < FilePath.Length; i++)
-            {
-                MergeObject mergeObject = new MergeObject();
-                mergeObject.FilePath = FilePath[i];
-
-                //通过路径判断文件是否已添加
-                bool IsExists = false;
-                for (int j = 0; j < MergeObjectlist.Count; j++)
-                {
-                    if (MergeObjectlist[j].FilePath == mergeObject.FilePath)
-                    {
-                        IsExists = true;
-                    }
-                }
-                if (IsExists)
-                {
-                    continue;
-                }
-                string FileType = Path.GetExtension(mergeObject.FilePath).Trim().ToLower();
-                if (string.IsNullOrEmpty(FileType))
-                {
-                    showDialog = true;
-                    //获取不到文件类型
-                    continue;
-                }
-                if (FileType != ".pdf")
-                {
-                    string imagetype = "*" + FileType;
-                    string[] x = Properties.Resources.imageex.ToLower().Split(';');
-                    List<string> list = x.ToList();
-                    int imageindex = list.IndexOf(imagetype);
-                    if (imageindex < 0)
-                    {
-                        showDialog = true;
-                        //图片格式不支持
-                        continue;
-                    };
-
-                    mergeObject.DocName = Path.GetFileName(mergeObject.FilePath);
-                    mergeObject.DocPageCount = 1 + " " + App.MainPageLoader.GetString("Merge_ItemPages");
-                    mergeObject.SDKPageCount = 1;
-                    mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
-                    try
-                    {
-                        mergeObject.DocThumbnail = new BitmapImage(new Uri(mergeObject.FilePath));
-                    }
-                    catch (Exception)
-                    {
-                        showDialog = true;
-                        //解码错误
-                        continue;
-                    }
-                }
-                else
-                {
-                    CPDFDocument doc = CPDFDocument.InitWithFilePath(mergeObject.FilePath);
-                    if (doc == null)
-                    {
-                        showDialog = true;
-                        //PDF打开失败
-                        continue;
-                    }
-
-                    ///Fix:
-                    ///情况分为:
-                    ///开启路径是当前路径:
-                    ///已解锁或本身就无密码->直接加入
-                    ///未解锁-> release
-                    ///
-                    /// 不是当前路径
-                    ///解锁或取消->
-                    ///
-
-                    if (CurrentFilePath == doc.FilePath)
-                    {
-
-                        if ((!(!doc.IsLocked && (SecurityHelper.CheckHaveAllPermissions(doc)))) &&
-                            (!(!string.IsNullOrEmpty(currentLoadedPassword) &&
-                            doc.UnlockWithPassword(currentLoadedPassword) &&
-                            (doc.CheckOwnerPassword(currentLoadedPassword) || SecurityHelper.CheckHaveAllPermissions(doc)))))
-                        {
-                            doc.Release();
-                            continue;
-                        }
-                        //添加的第一个文档有权限密码时,保存密码,避免合并时因没有密码导致合并失败
-                        //表现为在首页打开一个带权限的文档进入合并,点击合并按钮没有反应
-                        if (!string.IsNullOrEmpty(currentLoadedPassword))
-                        {
-                            mergeObject.Password = currentLoadedPassword;
-                        }
-                    }
-                    else
-                    {
-                        VerifyPasswordResult condition = SecurityHelper.VerifyPasswordByPasswordKind(doc, EnumPasswordKind.StatusPermissionsPassword, dialogs);
-                        if (condition.IsDiscryptied)
-                        {
-                            if (condition.Password != null)
-                            {
-                                mergeObject.Password = condition.Password;
-                                if (doc.UnlockWithPassword(condition.Password) && doc.CheckOwnerPassword(condition.Password))
-                                {
-
-                                }
-                            }
-                        }
-                        else
-                        {
-                            doc.Release();
-                            continue;
-                        }
+            ProcessVisible = Visibility.Visible;
+            MaxValue = FilePath.Length;
+            await System.Threading.Tasks.Task.Run((() =>
+            {
+                for (int i = 0; i < FilePath.Length; i++)
+                {
+                    //如果中断了文件添加,则取消循环
+                    if (CancelAddFiles)
+                    {
+                        break;
                     }
+                    MergeObject mergeObject = new MergeObject();
+                    mergeObject.FilePath = FilePath[i];
 
-                    mergeObject.DocName = doc.FileName;
-                   
-                    if (doc.PageCount > 1)
+                    //通过路径判断文件是否已添加
+                    bool IsExists = false;
+                    for (int j = 0; j < MergeObjectlist.Count; j++)
                     {
-                        mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPages");
-                        mergeObject.IsEvenPageIsEnabled = true;
+                        if (MergeObjectlist[j].FilePath == mergeObject.FilePath)
+                        {
+                            IsExists = true;
+                        }
                     }
-                    else
+                    if (IsExists)
                     {
-                        mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPage");
-                        mergeObject.IsEvenPageIsEnabled = false;
+                        continue;
                     }
-                    mergeObject.SDKPageCount = doc.PageCount;
-                    mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
+                    string FileType = Path.GetExtension(mergeObject.FilePath).Trim().ToLower();
+                    if (string.IsNullOrEmpty(FileType))
+                    {
+                        showDialog = true;
+                        //获取不到文件类型
+                        continue;
+                    }
+                    if (FileType != ".pdf")
+                    {
+                        string imagetype = "*" + FileType;
+                        string[] x = Properties.Resources.imageex.ToLower().Split(';');
+                        List<string> list = x.ToList();
+                        int imageindex = list.IndexOf(imagetype);
+                        if (imageindex < 0)
+                        {
+                            showDialog = true;
+                            //图片格式不支持
+                            continue;
+                        };
 
-                    //获取第一页缩略图
-                    CPDFPage page = doc.PageAtIndex(0);
-                    Size size = doc.GetPageSize(0);
+                        mergeObject.DocName = Path.GetFileName(mergeObject.FilePath);
+                        mergeObject.DocPageCount = 1 + " " + App.MainPageLoader.GetString("Merge_ItemPages");
+                        mergeObject.SDKPageCount = 1;
+                        mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
+                        try
+                        {
+                            mergeObject.DocThumbnail = new BitmapImage(new Uri(mergeObject.FilePath));
+                        }
+                        catch (Exception)
+                        {
+                            showDialog = true;
+                            //解码错误
+                            continue;
+                        }
+                    }
+                    else
+                    {
+                        CPDFDocument doc = CPDFDocument.InitWithFilePath(mergeObject.FilePath);
+                        if (doc == null)
+                        {
+                            showDialog = true;
+                            //PDF打开失败
+                            continue;
+                        }
 
-                    byte[] bmpData = new byte[(int)(size.Width * size.Height * 4)];
-                    WriteableBitmap WirteBitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Bgra32, null);
-                    page.RenderPageBitmap(0, 0, (int)size.Width, (int)size.Height, 0xFFFFFFFF, bmpData, 1);
-                    WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)size.Width, (int)size.Height), bmpData, WirteBitmap.BackBufferStride, 0);
-                    WirteBitmap.Freeze();
-                    mergeObject.DocThumbnail = WirteBitmap;
+                        ///Fix:
+                        ///情况分为:
+                        ///开启路径是当前路径:
+                        ///已解锁或本身就无密码->直接加入
+                        ///未解锁-> release
+                        ///
+                        /// 不是当前路径
+                        ///解锁或取消->
+                        ///
 
-                    doc.Release();
-                }
-                MergeObjectlist.Add(mergeObject);
-                UpDataMergeObjectIndex();
-            }
-
+                        if (CurrentFilePath == doc.FilePath)
+                        {
+
+                            if ((!(!doc.IsLocked && (SecurityHelper.CheckHaveAllPermissions(doc)))) &&
+                                (!(!string.IsNullOrEmpty(currentLoadedPassword) &&
+                                doc.UnlockWithPassword(currentLoadedPassword) &&
+                                (doc.CheckOwnerPassword(currentLoadedPassword) || SecurityHelper.CheckHaveAllPermissions(doc)))))
+                            {
+                                doc.Release();
+                                continue;
+                            }
+                            //添加的第一个文档有权限密码时,保存密码,避免合并时因没有密码导致合并失败
+                            //表现为在首页打开一个带权限的文档进入合并,点击合并按钮没有反应
+                            if (!string.IsNullOrEmpty(currentLoadedPassword))
+                            {
+                                mergeObject.Password = currentLoadedPassword;
+                            }
+                        }
+                        else
+                        {
+                            VerifyPasswordResult condition = SecurityHelper.VerifyPasswordByPasswordKind(doc, EnumPasswordKind.StatusPermissionsPassword, dialogs);
+                            if (condition.IsDiscryptied)
+                            {
+                                if (condition.Password != null)
+                                {
+                                    mergeObject.Password = condition.Password;
+                                    if (doc.UnlockWithPassword(condition.Password) && doc.CheckOwnerPassword(condition.Password))
+                                    {
+
+                                    }
+                                }
+                            }
+                            else
+                            {
+                                doc.Release();
+                                continue;
+                            }
+                        }
+
+                        mergeObject.DocName = doc.FileName;
+
+                        if (doc.PageCount > 1)
+                        {
+                            mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPages");
+                            mergeObject.IsEvenPageIsEnabled = true;
+                        }
+                        else
+                        {
+                            mergeObject.DocPageCount = doc.PageCount.ToString() + " " + App.MainPageLoader.GetString("Merge_ItemPage");
+                            mergeObject.IsEvenPageIsEnabled = false;
+                        }
+                        mergeObject.SDKPageCount = doc.PageCount;
+                        mergeObject.DocSize = CommonHelper.GetFileSize(mergeObject.FilePath);
+
+                        //获取第一页缩略图
+                        CPDFPage page = doc.PageAtIndex(0);
+                        Size size = doc.GetPageSize(0);
+
+                        byte[] bmpData = new byte[(int)(size.Width * size.Height * 4)];
+                        WriteableBitmap WirteBitmap = new WriteableBitmap((int)size.Width, (int)size.Height, 96, 96, PixelFormats.Bgra32, null);
+                        page.RenderPageBitmap(0, 0, (int)size.Width, (int)size.Height, 0xFFFFFFFF, bmpData, 1);
+                        WirteBitmap.WritePixels(new Int32Rect(0, 0, (int)size.Width, (int)size.Height), bmpData, WirteBitmap.BackBufferStride, 0);
+                        WirteBitmap.Freeze();
+                        mergeObject.DocThumbnail = WirteBitmap;
+
+                        doc.Release();
+                    }
+                    App.Current.Dispatcher.Invoke(() =>
+                    {
+                        MergeObjectlist.Add(mergeObject);
+                    });
+                    CurrentValue = i;
+                    UpDataMergeObjectIndex();
+                }
+            }));
+            ProcessVisible = Visibility.Collapsed;
             if (showDialog)
             {
                 AlertsMessage alertsMessage = new AlertsMessage();

+ 44 - 0
PDF Office/Views/Dialog/ToolsDialogs/MergeDialog.xaml

@@ -603,5 +603,49 @@
                     MouseOverBackground="{StaticResource color.item-state.hov.bg}" />
             </Border>
         </StackPanel>
+        <Border
+            Grid.RowSpan="7"
+            Grid.ColumnSpan="5"
+            Visibility="{Binding ProcessVisible}">
+            <Border.Background>
+                <SolidColorBrush Opacity="0.3" Color="#CED0D4" />
+            </Border.Background>
+            <Border
+                x:Name="BdProcess"
+                Grid.RowSpan="5"
+                Grid.ColumnSpan="5"
+                Width="226"
+                Height="58"
+                Background="{StaticResource color.sys.layout.dark.bg}"
+                BorderThickness="0"
+                CornerRadius="{StaticResource border-radius.8}"
+                Effect="{StaticResource shadow.neutral.m}">
+                <Grid Margin="16,13" Background="Transparent">
+                    <StackPanel Orientation="Horizontal">
+                        <TextBlock Foreground="{StaticResource color.sys.text.anti.norm}" Text="Adding..." />
+                    </StackPanel>
+                    <Button
+                        Width="12"
+                        Height="12"
+                        Padding="0,0,1,1"
+                        HorizontalAlignment="Right"
+                        VerticalAlignment="Top"
+                        Background="{StaticResource color.sys.layout.dark.bg}"
+                        BorderThickness="0"
+                        Command="{Binding CancelAddFileCommand}"
+                        Style="{StaticResource btn.sec}">
+                        <Path Data="M6.00006 7.06072L9.46973 10.5304L10.5304 9.46973L7.06072 6.00006L10.5304 2.53039L9.46973 1.46973L6.00006 4.9394L2.53039 1.46973L1.46973 2.53039L4.9394 6.00006L1.46973 9.46973L2.53039 10.5304L6.00006 7.06072Z" Fill="#CED0D4" />
+                    </Button>
+                    <ProgressBar
+                        Height="4"
+                        Margin="0,0,0,4"
+                        VerticalAlignment="Bottom"
+                        BorderThickness="0"
+                        Foreground="{StaticResource color.slider.track-filled.norm}"
+                        Maximum="{Binding MaxValue}"
+                        Value="{Binding CurrentValue}" />
+                </Grid>
+            </Border>
+        </Border>
     </Grid>
 </UserControl>