浏览代码

Misc code refractoring

Ruben 5 年之前
父节点
当前提交
910609394f

+ 4 - 0
.editorconfig

@@ -0,0 +1,4 @@
+[*.cs]
+
+# CA1031: Do not catch general exception types
+dotnet_diagnostic.CA1031.severity = none

+ 6 - 1
PicView.sln

@@ -3,7 +3,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio Version 16
 VisualStudioVersion = 16.0.29209.62
 MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PicView", "PicView\PicView.csproj", "{9B5425DC-4329-4A7A-A0C5-F93161F77245}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PicView", "PicView\PicView.csproj", "{9B5425DC-4329-4A7A-A0C5-F93161F77245}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1C3F8A75-6B0A-4652-859B-B78F49707C2B}"
+	ProjectSection(SolutionItems) = preProject
+		.editorconfig = .editorconfig
+	EndProjectSection
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution

+ 1 - 5
PicView/Copy-paste/Copy-paste.cs

@@ -107,11 +107,7 @@ namespace PicView
                         // If from same folder
                         if (!string.IsNullOrWhiteSpace(Pics[FolderIndex]) && Path.GetDirectoryName(x) == Path.GetDirectoryName(Pics[FolderIndex]))
                         {
-                            if (!Preloader.Contains(x))
-                            {
-                                PreloadCount = 4;
-                                Preloader.Add(x);
-                            }
+
 
                             Pic(Pics.IndexOf(x));
                         }

+ 13 - 17
PicView/File Logic/Open_Save.cs

@@ -20,7 +20,6 @@ namespace PicView
         {
             if (!File.Exists(Pics[FolderIndex]) || mainWindow.img.Source == null)
             {
-                ShowTooltipMessage("Error, File does not exist, or something went wrong...");
                 return;
             }
             try
@@ -95,7 +94,7 @@ namespace PicView
         /// </summary>
         internal static void SaveFiles()
         {
-            IsDialogOpen = true;
+            if (string.IsNullOrEmpty(Pics[FolderIndex])) return;
 
             var Savedlg = new SaveFileDialog()
             {
@@ -104,26 +103,23 @@ namespace PicView
                 FileName = Path.GetFileName(Pics[FolderIndex])
             };
 
-            if (!string.IsNullOrEmpty(Pics[FolderIndex]))
+            if (!Savedlg.ShowDialog().Value) return;
+
+            IsDialogOpen = true;
+
+            if (!TrySaveImage(Rotateint, Flipped, Pics[FolderIndex], Savedlg.FileName))
             {
-                if (Savedlg.ShowDialog().Value)
-                {
-                    if (!TrySaveImage(Rotateint, Flipped, Pics[FolderIndex], Savedlg.FileName))
-                    {
-                        ShowTooltipMessage("Error, File didn't get saved");
-                    }
-                }
-                else
-                {
-                    return;
-                }
+                ShowTooltipMessage("Saving file failed");
+            }
 
+            else if (Savedlg.FileName == Pics[FolderIndex])
+            {
                 //Refresh the list of pictures.
                 Reload();
-
-                Close_UserControls();
-                IsDialogOpen = false;
             }
+
+            Close_UserControls();
+            IsDialogOpen = false;
         }
     }
 }

+ 4 - 0
PicView/GlobalSuppressions.cs

@@ -13,3 +13,7 @@ using System.Diagnostics.CodeAnalysis;
 [assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<Pending>", Scope = "member", Target = "~M:PicView.LoadFromWeb.PicWeb(System.String)")]
 [assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<Pending>", Scope = "member", Target = "~M:PicView.Open_Save.OpenWith(System.String)")]
 [assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<Pending>", Scope = "member", Target = "~M:PicView.FileFunctions.RenameFile(System.String,System.String)~System.Boolean")]
+[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<Pending>", Scope = "member", Target = "~M:PicView.ImageDecoder.TrySaveImage(System.Int32,System.Boolean,System.String,System.String)~System.Boolean")]
+[assembly: SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "<Pending>", Scope = "member", Target = "~M:PicView.NativeMethods.WndProc(System.IntPtr,System.Int32,System.IntPtr,System.IntPtr,System.Boolean@)~System.IntPtr")]
+[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<Pending>", Scope = "member", Target = "~M:PicView.Utilities.UpdateColor(System.Boolean)")]
+[assembly: SuppressMessage("Globalization", "CA1305:Specify IFormatProvider", Justification = "<Pending>", Scope = "member", Target = "~M:PicView.FileFunctions.MakeValidFileName(System.String)~System.String")]

+ 16 - 29
PicView/Image Logic/ImageDecoder.cs

@@ -167,42 +167,29 @@ namespace PicView
         /// <returns></returns>
         internal static bool TrySaveImage(int rotate, bool flipped, string path, string destination)
         {
-            if (File.Exists(path))
+            try
             {
-                try
+                using var SaveImage = new MagickImage();
+                // Set maximum quality
+                var mrs = new MagickReadSettings()
                 {
-                    using (var SaveImage = new MagickImage())
-                    {
-                        // Set maximum quality
-                        var mrs = new MagickReadSettings()
-                        {
-                            Density = new Density(300, 300),
-                        };
-                        SaveImage.Quality = 100;
-
-                        SaveImage.Read(path, mrs);
-
-                        // Apply transformation values
-                        if (flipped)
-                        {
-                            SaveImage.Flop();
-                        }
+                    Density = new Density(300, 300),
+                };
+                SaveImage.Quality = 100;
 
-                        SaveImage.Rotate(rotate);
+                SaveImage.Read(path, mrs);
 
-                        SaveImage.Write(destination);
-                    }
-                }
-                catch (Exception)
+                // Apply transformation values
+                if (flipped)
                 {
-                    return false;
+                    SaveImage.Flop();
                 }
-            }
-            else
-            {
-                return false;
-            }
 
+                SaveImage.Rotate(rotate);
+
+                SaveImage.Write(destination);
+            }
+            catch (Exception) { return false; }
             return true;
         }
 

+ 1 - 1
PicView/Image Logic/Resize_and_Zoom.cs

@@ -486,7 +486,7 @@ namespace PicView
                 if (AutoFit)
                 {
                     /// Calculate to screen size
-                    AspectRatio = Math.Min(monitorWidth / height, monitorHeight / width);
+                    AspectRatio = Math.Min(maxWidth / height, maxHeight / width);
                 }
                 else
                 {

+ 20 - 10
PicView/Navigation/Preloader.cs

@@ -285,6 +285,16 @@ namespace PicView.PreLoading
         }
 
 
+        internal static async void PreloaderFix(string file)
+        {
+            if (!Contains(file))
+            {
+                PreloadCount = 4;
+                await Add(file).ConfigureAwait(false);
+            }
+        }
+
+
 
         /// <summary>
         /// Starts decoding images into memory,
@@ -300,7 +310,7 @@ namespace PicView.PreLoading
                 + string.Concat(Reverse ? "backwards" : "forwards"));
 #endif
 
-            return Task.Run(() =>
+            return Task.Run(async() =>
             {
                 var toLoad = 8;
                 var extraToLoad = toLoad / 2;
@@ -320,7 +330,7 @@ namespace PicView.PreLoading
                                 break;
                             }
 
-                            Add(i);
+                            await Add(i).ConfigureAwait(false);
                         }
                         // Add second elements behind
                         for (int i = index - 1; i > (index - 1) - extraToLoad; i--)
@@ -330,7 +340,7 @@ namespace PicView.PreLoading
                                 break;
                             }
 
-                            Add(i);
+                            await Add(i).ConfigureAwait(false);
                         }
 
                         //Clean up behind
@@ -363,7 +373,7 @@ namespace PicView.PreLoading
                                 break;
                             }
 
-                            Add(i);
+                            await Add(i).ConfigureAwait(false);
                         }
                         // Add second elements
                         for (int i = index + 1; i <= (index + 1) + toLoad; i++)
@@ -373,7 +383,7 @@ namespace PicView.PreLoading
                                 break;
                             }
 
-                            Add(i);
+                            await Add(i).ConfigureAwait(false);
                         }
 
                         //Clean up infront
@@ -406,12 +416,12 @@ namespace PicView.PreLoading
                         // Add first elements
                         for (int i = index + 1; i < (index + 1) + toLoad; i++)
                         {
-                            Add(i % Pics.Count);
+                            await Add(i % Pics.Count).ConfigureAwait(false);
                         }
                         // Add second elements behind
                         for (int i = index - 1; i > (index - 1) - extraToLoad; i--)
                         {
-                            Add(i % Pics.Count);
+                            await Add(i % Pics.Count).ConfigureAwait(false);
                         }
 
                         //Clean up behind
@@ -435,17 +445,17 @@ namespace PicView.PreLoading
                             {
                                 for (int x = Pics.Count - 1; x >= Pics.Count - y; x--)
                                 {
-                                    Add(x);
+                                    await Add(x).ConfigureAwait(false);
                                 }
                                 break;
                             }
-                            Add(i);
+                            await Add(i).ConfigureAwait(false);
                         }
 
                         // Add second elements
                         for (int i = index + 1; i <= (index + 1) + toLoad; i++)
                         {
-                            Add(i % Pics.Count);
+                            await Add(i % Pics.Count).ConfigureAwait(false);
                         }
 
                         //Clean up infront

+ 3 - 0
PicView/PicVIew.csproj

@@ -179,6 +179,9 @@
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
   </ItemGroup>
+  <ItemGroup>
+    <None Include="..\.editorconfig" Link=".editorconfig" />
+  </ItemGroup>
   <PropertyGroup>
     <FileVersion>0.9.8.5</FileVersion>
     <SignAssembly>true</SignAssembly>

+ 1 - 1
PicView/UserControls/Misc/CustomTextBox.xaml.cs

@@ -14,6 +14,6 @@ namespace PicView.UserControls
 
         public string Text { get { return Bar.Text; } set { Bar.Text = value; } }
 
-        public bool IsFocused { get { return Bar.IsFocused; } }
+        public new bool IsFocused { get { return Bar.IsFocused; } }
     }
 }