From 92528efaf58e0ed1704fd9e8b4655103801ea410 Mon Sep 17 00:00:00 2001
From: BenjaminS <97973081+benjisho@users.noreply.github.com>
Date: Tue, 30 May 2023 17:05:21 +0300
Subject: [PATCH 1/5] Added error handling to Initialize method in Win10Plugin
This pull request adds more robust error handling to the Initialize method in the Win10Plugin class. The changes include:
1. Added a try-catch block to the Initialize method to catch and log any exceptions that occur during initialization.
2. Enhanced the logging of exceptions to include not only the error message but also the entire error content, including the stack trace. This change makes debugging easier by providing more context when an error occurs.
---
src/Greenshot.Plugin.Win10/Win10Plugin.cs | 39 ++++++++++++++---------
1 file changed, 24 insertions(+), 15 deletions(-)
diff --git a/src/Greenshot.Plugin.Win10/Win10Plugin.cs b/src/Greenshot.Plugin.Win10/Win10Plugin.cs
index 6b15ff3c5..ab0447faf 100644
--- a/src/Greenshot.Plugin.Win10/Win10Plugin.cs
+++ b/src/Greenshot.Plugin.Win10/Win10Plugin.cs
@@ -62,23 +62,32 @@ namespace Greenshot.Plugin.Win10
/// true if plugin is initialized, false if not (doesn't show)
public bool Initialize()
{
- // Here we check if the build version of Windows is actually what we support
- if (!WindowsVersion.IsWindows10BuildOrLater(17763))
+ try
{
- Log.WarnFormat("No support for Windows build {0}", WindowsVersion.BuildVersion);
+ // Here we check if the build version of Windows is actually what we support
+ if (!WindowsVersion.IsWindows10BuildOrLater(17763))
+ {
+ Log.WarnFormat("No support for Windows build {0}", WindowsVersion.BuildVersion);
+ return false;
+ }
+
+ SimpleServiceProvider.Current.AddService(ToastNotificationService.Create());
+ // Set this as IOcrProvider
+ SimpleServiceProvider.Current.AddService(new Win10OcrProvider());
+ // Add the processor
+ SimpleServiceProvider.Current.AddService(new Win10OcrProcessor());
+
+ // Add the destinations
+ SimpleServiceProvider.Current.AddService(new Win10OcrDestination());
+ SimpleServiceProvider.Current.AddService(new Win10ShareDestination());
+
+ return true;
+ }
+ catch (Exception e)
+ {
+ Log.Error("Failed to initialize Win10Plugin. Error: " + e.ToString(), e);
return false;
}
-
- SimpleServiceProvider.Current.AddService(ToastNotificationService.Create());
- // Set this as IOcrProvider
- SimpleServiceProvider.Current.AddService(new Win10OcrProvider());
- // Add the processor
- SimpleServiceProvider.Current.AddService(new Win10OcrProcessor());
-
- // Add the destinations
- SimpleServiceProvider.Current.AddService(new Win10OcrDestination());
- SimpleServiceProvider.Current.AddService(new Win10ShareDestination());
- return true;
}
public void Shutdown()
@@ -86,4 +95,4 @@ namespace Greenshot.Plugin.Win10
// Nothing to shutdown
}
}
-}
\ No newline at end of file
+}
From e9b32c0f0e29522d2ab6d5ac088ac0fa56200895 Mon Sep 17 00:00:00 2001
From: BenjaminS <97973081+benjisho@users.noreply.github.com>
Date: Wed, 31 May 2023 03:09:52 +0300
Subject: [PATCH 2/5] Updated documentation style in Win10Configuration.cs
This commit updates the comment style in the Win10Configuration.cs file to use XML comments, enhancing readability and providing better IntelliSense support in IDEs.
The changes include:
Class and property comments have been updated to use XML documentation syntax, providing a more detailed and consistent documentation style.
Here's an example of the changes:
```
///
/// Gets or sets a value indicating whether OCR should be run automatically on every capture.
///
```
This change is purely cosmetic and does not affect the functionality of the code.
---
src/Greenshot.Plugin.Win10/Win10Configuration.cs | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/src/Greenshot.Plugin.Win10/Win10Configuration.cs b/src/Greenshot.Plugin.Win10/Win10Configuration.cs
index 841232bf2..44624932f 100644
--- a/src/Greenshot.Plugin.Win10/Win10Configuration.cs
+++ b/src/Greenshot.Plugin.Win10/Win10Configuration.cs
@@ -29,7 +29,10 @@ namespace Greenshot.Plugin.Win10
[IniSection("Win10", Description = "Greenshot Win10 Plugin configuration")]
public class Win10Configuration : IniSection
{
+ ///
+ /// Gets or sets a value indicating whether OCR should be run automatically on every capture.
+ ///
[IniProperty("AlwaysRunOCROnCapture", Description = "Determines if OCR is run automatically on every capture", DefaultValue = "False")]
public bool AlwaysRunOCROnCapture { get; set; }
}
-}
\ No newline at end of file
+}
From 56235bf33677b49becc9f8680d41b5b02d75bcc6 Mon Sep 17 00:00:00 2001
From: BenjaminS <97973081+benjisho@users.noreply.github.com>
Date: Wed, 31 May 2023 03:19:03 +0300
Subject: [PATCH 3/5] Add XML documentation comments to
ToastNotificationService.cs
This commit adds more XML documentation comments to the ToastNotificationService.cs file, improving the clarity and readability of the code. This change is purely cosmetic and does not affect the functionality of the code.
---
.../ToastNotificationService.cs | 20 +++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/src/Greenshot.Plugin.Win10/ToastNotificationService.cs b/src/Greenshot.Plugin.Win10/ToastNotificationService.cs
index b9120fee7..57f630734 100644
--- a/src/Greenshot.Plugin.Win10/ToastNotificationService.cs
+++ b/src/Greenshot.Plugin.Win10/ToastNotificationService.cs
@@ -42,7 +42,11 @@ namespace Greenshot.Plugin.Win10
private static readonly CoreConfiguration CoreConfiguration = IniConfig.GetIniSection();
private readonly string _imageFilePath;
-
+
+ ///
+ /// Initializes a new instance of the ToastNotificationService class.
+ /// If the program was started by a toast notification, logs this information.
+ ///
public ToastNotificationService()
{
if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
@@ -80,12 +84,12 @@ namespace Greenshot.Plugin.Win10
}
///
- /// This creates the actual toast
+ /// Creates and displays the actual toast notification with the specified message and parameters.
///
- /// string
- /// TimeSpan until the toast timeouts
- /// Action called when clicked
- /// Action called when the toast is closed
+ /// The string message to display in the toast notification.
+ /// TimeSpan until the toast timeouts or should stay on screen, or null for the default duration.
+ /// Action called when clicked, or null for no action.
+ /// Action called when the toast is closed, or null for no action.
private void ShowMessage(string message, TimeSpan? timeout = default, Action onClickAction = null, Action onClosedAction = null)
{
// Do not inform the user if this is disabled
@@ -200,7 +204,7 @@ namespace Greenshot.Plugin.Win10
}
///
- /// Factory method, helping with checking if the notification service is even available
+ /// Factory method, helping with checking if the notification service is even available - Creates a new instance of the ToastNotificationService class, if supported on the current system.
///
/// ToastNotificationService
public static ToastNotificationService Create()
@@ -215,4 +219,4 @@ namespace Greenshot.Plugin.Win10
return null;
}
}
-}
\ No newline at end of file
+}
From ef9443b016ed3a85361ef98bfa9298443b81b485 Mon Sep 17 00:00:00 2001
From: BenjaminS <97973081+benjisho@users.noreply.github.com>
Date: Wed, 31 May 2023 03:45:56 +0300
Subject: [PATCH 4/5] Added comments to Greenshot.Plugin.Win10.csproj
Added comments to describe each section of the project file. This helps other developers understand why certain files are included or why certain package references are needed.
```
```
---
src/Greenshot.Plugin.Win10/Greenshot.Plugin.Win10.csproj | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/src/Greenshot.Plugin.Win10/Greenshot.Plugin.Win10.csproj b/src/Greenshot.Plugin.Win10/Greenshot.Plugin.Win10.csproj
index c1c1729e0..b2e20258c 100644
--- a/src/Greenshot.Plugin.Win10/Greenshot.Plugin.Win10.csproj
+++ b/src/Greenshot.Plugin.Win10/Greenshot.Plugin.Win10.csproj
@@ -1,13 +1,18 @@
+
+
PreserveNewest
+
+
+
From 9a609e9ef3b4c001aa0d3b5cf04671d49b61f1ec Mon Sep 17 00:00:00 2001
From: BenjaminS <97973081+benjisho@users.noreply.github.com>
Date: Wed, 31 May 2023 04:49:32 +0300
Subject: [PATCH 5/5] Added Additional Comments for Code Readability
Win10OcrProvider.cs
This commit introduces more inline comments to the Win10OcrProvider class. These comments aim to improve the understanding of the class functions and the flow of the OCR process. Key areas clarified include:
1. Logging of available OCR languages.
2. Handling of image sizing before OCR process.
3. The OCR process itself and the creation of the OCR result.
---
.../Win10OcrProvider.cs | 30 +++++++++++++++++--
1 file changed, 27 insertions(+), 3 deletions(-)
diff --git a/src/Greenshot.Plugin.Win10/Win10OcrProvider.cs b/src/Greenshot.Plugin.Win10/Win10OcrProvider.cs
index e33909581..f8269f545 100644
--- a/src/Greenshot.Plugin.Win10/Win10OcrProvider.cs
+++ b/src/Greenshot.Plugin.Win10/Win10OcrProvider.cs
@@ -41,7 +41,10 @@ namespace Greenshot.Plugin.Win10
///
public class Win10OcrProvider : IOcrProvider
{
+ // Log for debugging and information
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(Win10OcrProvider));
+
+ // Minimum width and height for the OCR to work correctly
private const int MinWidth = 130;
private const int MinHeight = 130;
@@ -50,7 +53,10 @@ namespace Greenshot.Plugin.Win10
///
public Win10OcrProvider()
{
+ // Get available languages from the OCR engine and log them
var languages = OcrEngine.AvailableRecognizerLanguages;
+
+ // Log all available languages for OCR.
foreach (var language in languages)
{
Log.DebugFormat("Found language {0} {1}", language.NativeName, language.LanguageTag);
@@ -64,19 +70,26 @@ namespace Greenshot.Plugin.Win10
/// OcrResult sync
public async Task DoOcrAsync(ISurface surface)
{
+ // Will contain the result of the OCR process
OcrInformation result;
+
+ // Using a memory stream to handle the image
using (var imageStream = new MemoryStream())
{
// We only want the background
+ // Output settings for the image before OCR process
var outputSettings = new SurfaceOutputSettings(OutputFormat.png, 0, true)
{
ReduceColors = true,
SaveBackgroundOnly = true
};
- // Force Grayscale output
+ // Force Grayscale output to the image
outputSettings.Effects.Add(new GrayscaleEffect());
+
+ // If the surface is smaller than the minimum dimensions, resize it
if (surface.Image.Width < MinWidth || surface.Image.Height < MinHeight)
{
+ // Calculate dimensions to add
int addedWidth = MinWidth - surface.Image.Width;
if (addedWidth < 0)
{
@@ -95,13 +108,18 @@ namespace Greenshot.Plugin.Win10
{
addedHeight /= 2;
}
+ // Add a resize effect to the image
IEffect effect = new ResizeCanvasEffect(addedWidth, addedWidth, addedHeight, addedHeight);
outputSettings.Effects.Add(effect);
}
+ // Save the surface to the stream and reset position
ImageIO.SaveToStream(surface, imageStream, outputSettings);
imageStream.Position = 0;
+
+ // Create a random access stream from the memory stream
var randomAccessStream = imageStream.AsRandomAccessStream();
-
+
+ // Perform OCR on the stream
result = await DoOcrAsync(randomAccessStream);
}
@@ -158,8 +176,10 @@ namespace Greenshot.Plugin.Win10
{
var result = new OcrInformation();
+ // Iterate over all lines in the OCR result
foreach (var ocrLine in ocrResult.Lines)
{
+ // Create a new line and add it to the result
var line = new Line(ocrLine.Words.Count)
{
Text = ocrLine.Text
@@ -167,12 +187,16 @@ namespace Greenshot.Plugin.Win10
result.Lines.Add(line);
+ // Loop through each word in the line and process it.
for (var index = 0; index < ocrLine.Words.Count; index++)
{
var ocrWord = ocrLine.Words[index];
+
+ // Create the bounding rectangle for the word
var location = new NativeRect((int) ocrWord.BoundingRect.X, (int) ocrWord.BoundingRect.Y,
(int) ocrWord.BoundingRect.Width, (int) ocrWord.BoundingRect.Height);
+ // Add the word to the line
var word = line.Words[index];
word.Text = ocrWord.Text;
word.Bounds = location;
@@ -182,4 +206,4 @@ namespace Greenshot.Plugin.Win10
return result;
}
}
-}
\ No newline at end of file
+}