From bdcd39b4eafa87c2a0ec6853f52a3aa4379b7fdb Mon Sep 17 00:00:00 2001 From: zggsong Date: Wed, 31 Jul 2024 14:07:06 +0800 Subject: [PATCH 01/12] Fix(FullscreenGrab): Fix the issue where TopPanel cannot be displayed immediately after switching between Freeze and Unfreeze --- Text-Grab/Utilities/WindowUtilities.cs | 26 ++++++++++++++++++++++++-- Text-Grab/Views/FullscreenGrab.xaml.cs | 4 ++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Text-Grab/Utilities/WindowUtilities.cs b/Text-Grab/Utilities/WindowUtilities.cs index 7437ffa8..307c2635 100644 --- a/Text-Grab/Utilities/WindowUtilities.cs +++ b/Text-Grab/Utilities/WindowUtilities.cs @@ -1,7 +1,9 @@ using Dapplo.Windows.User32; +using Fasetto.Word; using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -31,7 +33,7 @@ public static void SetWindowPosition(Window passedWindow) storedPositionString = AppUtilities.TextGrabSettings.EditTextWindowSizeAndPosition; if (passedWindow is GrabFrame) - storedPositionString = AppUtilities.TextGrabSettings.GrabFrameWindowSizeAndPosition; + storedPositionString = AppUtilities.TextGrabSettings.GrabFrameWindowSizeAndPosition; List storedPosition = new(storedPositionString.Split(',')); @@ -286,4 +288,24 @@ public static void ShouldShutDown() if (shouldShutDown) Application.Current.Shutdown(); } -} + + public static bool IsMouseInWindow(this Window window) + { + GetCursorPos(out POINT mousePosition); + + DpiScale dpi = System.Windows.Media.VisualTreeHelper.GetDpi(window); + Point absPosPoint = window.GetAbsolutePosition(); + Rect windowRect = new(absPosPoint.X, absPosPoint.Y, + window.ActualWidth * dpi.DpiScaleX, + window.ActualHeight * dpi.DpiScaleY); + return windowRect.Contains(new Point(mousePosition.X, mousePosition.Y)); + } + + #region DLLImport + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool GetCursorPos(out POINT lpPoint); + + #endregion +} \ No newline at end of file diff --git a/Text-Grab/Views/FullscreenGrab.xaml.cs b/Text-Grab/Views/FullscreenGrab.xaml.cs index ccd338ef..1b6b5d71 100644 --- a/Text-Grab/Views/FullscreenGrab.xaml.cs +++ b/Text-Grab/Views/FullscreenGrab.xaml.cs @@ -232,8 +232,8 @@ private async void FreezeUnfreeze(bool Activate) RegionClickCanvas.ContextMenu.IsOpen = false; await Task.Delay(150); SetImageToBackground(); - - if (IsMouseOver) + + if (this.IsMouseInWindow()) TopButtonsStackPanel.Visibility = Visibility.Visible; } else From 86fb9406b84ae1829ee1cebca2b28c2d8884cc34 Mon Sep 17 00:00:00 2001 From: zggsong Date: Wed, 31 Jul 2024 17:45:43 +0800 Subject: [PATCH 02/12] Fix: Fix issues with PanSelection for multi monitor secondary screens #482 and Fix the issue where multiple displays fail to retrieve screen information when clicking with the mouse --- Text-Grab/Utilities/WindowUtilities.cs | 15 +++++++++++++-- Text-Grab/Views/FullscreenGrab.xaml.cs | 23 +++++++++++++++-------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Text-Grab/Utilities/WindowUtilities.cs b/Text-Grab/Utilities/WindowUtilities.cs index 307c2635..2d2f3016 100644 --- a/Text-Grab/Utilities/WindowUtilities.cs +++ b/Text-Grab/Utilities/WindowUtilities.cs @@ -288,17 +288,28 @@ public static void ShouldShutDown() if (shouldShutDown) Application.Current.Shutdown(); } + + public static bool GetMousePosition(out Point mousePosition) + { + if (GetCursorPos(out POINT point)) + { + mousePosition = new Point(point.X, point.Y); + return true; + } + mousePosition = default; + return false; + } public static bool IsMouseInWindow(this Window window) { - GetCursorPos(out POINT mousePosition); + GetMousePosition(out Point mousePosition); DpiScale dpi = System.Windows.Media.VisualTreeHelper.GetDpi(window); Point absPosPoint = window.GetAbsolutePosition(); Rect windowRect = new(absPosPoint.X, absPosPoint.Y, window.ActualWidth * dpi.DpiScaleX, window.ActualHeight * dpi.DpiScaleY); - return windowRect.Contains(new Point(mousePosition.X, mousePosition.Y)); + return windowRect.Contains(mousePosition); } #region DLLImport diff --git a/Text-Grab/Views/FullscreenGrab.xaml.cs b/Text-Grab/Views/FullscreenGrab.xaml.cs index 1b6b5d71..cb744248 100644 --- a/Text-Grab/Views/FullscreenGrab.xaml.cs +++ b/Text-Grab/Views/FullscreenGrab.xaml.cs @@ -423,11 +423,19 @@ private void PanSelection(System.Windows.Point movingPoint) if (CurrentScreen is not null && dpiScale is not null) { - double currentScreenLeft = CurrentScreen.Bounds.Left; // Should always be 0 + double currentScreenLeft = 0; + double currentScreenTop = 0; double currentScreenRight = CurrentScreen.Bounds.Right / dpiScale.Value.DpiScaleX; - double currentScreenTop = CurrentScreen.Bounds.Top; // Should always be 0 double currentScreenBottom = CurrentScreen.Bounds.Bottom / dpiScale.Value.DpiScaleY; + // If it is a secondary screen, recalculate the coordinates + if (CurrentScreen.Bounds.Left != 0 || CurrentScreen.Bounds.Top != 0) + { + currentScreenRight = (CurrentScreen.Bounds.Right + CurrentScreen.Bounds.Width) / dpiScale.Value.DpiScaleX; + currentScreenBottom = (CurrentScreen.Bounds.Bottom + CurrentScreen.Bounds.Height) / dpiScale.Value.DpiScaleY; + } + + leftValue = Math.Clamp(leftValue, currentScreenLeft, (currentScreenRight - selectBorder.Width)); topValue = Math.Clamp(topValue, currentScreenTop, (currentScreenBottom - selectBorder.Height)); } @@ -509,13 +517,12 @@ private void RegionClickCanvas_MouseDown(object sender, MouseButtonEventArgs e) Canvas.SetLeft(selectBorder, clickedPoint.X); Canvas.SetTop(selectBorder, clickedPoint.Y); - DisplayInfo[] screens = DisplayInfo.AllDisplayInfos; - System.Windows.Point formsPoint = new((int)clickedPoint.X, (int)clickedPoint.Y); - foreach (DisplayInfo scr in screens) + WindowUtilities.GetMousePosition(out System.Windows.Point mousePoint); + foreach (DisplayInfo? screen in DisplayInfo.AllDisplayInfos) { - Rect bound = scr.ScaledBounds(); - if (bound.Contains(formsPoint)) - CurrentScreen = scr; + Rect bound = screen.ScaledBounds(); + if (bound.Contains(mousePoint)) + CurrentScreen = screen; } } From 1fc2e5b403cb1a2ae0589ce828cbd4778e696181 Mon Sep 17 00:00:00 2001 From: zggsong Date: Wed, 31 Jul 2024 19:56:38 +0800 Subject: [PATCH 03/12] perf: Update PanSelection coordinate calculation method --- Text-Grab/Views/FullscreenGrab.xaml.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Text-Grab/Views/FullscreenGrab.xaml.cs b/Text-Grab/Views/FullscreenGrab.xaml.cs index cb744248..af42de34 100644 --- a/Text-Grab/Views/FullscreenGrab.xaml.cs +++ b/Text-Grab/Views/FullscreenGrab.xaml.cs @@ -425,16 +425,8 @@ private void PanSelection(System.Windows.Point movingPoint) { double currentScreenLeft = 0; double currentScreenTop = 0; - double currentScreenRight = CurrentScreen.Bounds.Right / dpiScale.Value.DpiScaleX; - double currentScreenBottom = CurrentScreen.Bounds.Bottom / dpiScale.Value.DpiScaleY; - - // If it is a secondary screen, recalculate the coordinates - if (CurrentScreen.Bounds.Left != 0 || CurrentScreen.Bounds.Top != 0) - { - currentScreenRight = (CurrentScreen.Bounds.Right + CurrentScreen.Bounds.Width) / dpiScale.Value.DpiScaleX; - currentScreenBottom = (CurrentScreen.Bounds.Bottom + CurrentScreen.Bounds.Height) / dpiScale.Value.DpiScaleY; - } - + double currentScreenRight = CurrentScreen.Bounds.Width / dpiScale.Value.DpiScaleX; + double currentScreenBottom = CurrentScreen.Bounds.Height / dpiScale.Value.DpiScaleY; leftValue = Math.Clamp(leftValue, currentScreenLeft, (currentScreenRight - selectBorder.Width)); topValue = Math.Clamp(topValue, currentScreenTop, (currentScreenBottom - selectBorder.Height)); From 3fb17529b6e5718ffcf71265f6e455cbec5d2621 Mon Sep 17 00:00:00 2001 From: zggsong Date: Wed, 31 Jul 2024 19:58:15 +0800 Subject: [PATCH 04/12] fFix: Hold down the shift selection to throw an exception at the beginning of the fix --- Text-Grab/Views/FullscreenGrab.xaml.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Text-Grab/Views/FullscreenGrab.xaml.cs b/Text-Grab/Views/FullscreenGrab.xaml.cs index af42de34..f74c61bc 100644 --- a/Text-Grab/Views/FullscreenGrab.xaml.cs +++ b/Text-Grab/Views/FullscreenGrab.xaml.cs @@ -495,8 +495,8 @@ private void RegionClickCanvas_MouseDown(object sender, MouseButtonEventArgs e) RegionClickCanvas.CaptureMouse(); CursorClipper.ClipCursor(this); clickedPoint = e.GetPosition(this); - selectBorder.Height = 1; - selectBorder.Width = 1; + selectBorder.Height = 2; + selectBorder.Width = 2; dpiScale = VisualTreeHelper.GetDpi(this); From a2906079425a7d99a29f026b70b71da85fba3de8 Mon Sep 17 00:00:00 2001 From: Joseph Finney Date: Sat, 3 Aug 2024 11:36:54 -0500 Subject: [PATCH 05/12] Update packages --- Tests/Tests.csproj | 4 ++-- Text-Grab/Text-Grab.csproj | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 959ba6bb..7fd55112 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -10,8 +10,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/Text-Grab/Text-Grab.csproj b/Text-Grab/Text-Grab.csproj index 3c45dfe3..bf28e4ec 100644 --- a/Text-Grab/Text-Grab.csproj +++ b/Text-Grab/Text-Grab.csproj @@ -1,4 +1,4 @@ - + WinExe @@ -70,9 +70,9 @@ - - - + + + From 7fdc86c4f108746ee623b80890f7a744675cfda1 Mon Sep 17 00:00:00 2001 From: zggsong Date: Mon, 5 Aug 2024 13:33:56 +0800 Subject: [PATCH 06/12] perf: Use selectborder to determine if it is a small click --- Text-Grab/Views/FullscreenGrab.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Text-Grab/Views/FullscreenGrab.xaml.cs b/Text-Grab/Views/FullscreenGrab.xaml.cs index f74c61bc..84557286 100644 --- a/Text-Grab/Views/FullscreenGrab.xaml.cs +++ b/Text-Grab/Views/FullscreenGrab.xaml.cs @@ -596,7 +596,7 @@ private async void RegionClickCanvas_MouseUp(object sender, MouseButtonEventArgs if (LanguagesComboBox.SelectedItem is TessLang tessLang) tessTag = tessLang.LanguageTag; - bool isSmallClick = (regionScaled.Width < 3 || regionScaled.Height < 3); + bool isSmallClick = (selectBorder.Width < 3 || selectBorder.Height < 3); bool isSingleLine = SingleLineMenuItem is not null && SingleLineMenuItem.IsChecked; bool isTable = TableMenuItem is not null && TableMenuItem.IsChecked; From f545abc72fa4a0ee3a2c4e2078e302425809561a Mon Sep 17 00:00:00 2001 From: Joseph Finney Date: Mon, 19 Aug 2024 19:21:35 -0500 Subject: [PATCH 07/12] update package --- Text-Grab/Text-Grab.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Text-Grab/Text-Grab.csproj b/Text-Grab/Text-Grab.csproj index bf28e4ec..2507b5e1 100644 --- a/Text-Grab/Text-Grab.csproj +++ b/Text-Grab/Text-Grab.csproj @@ -70,7 +70,7 @@ - + From d4f8051e77a3a11c7b39a75b622888ffc0e251c6 Mon Sep 17 00:00:00 2001 From: Joseph Finney Date: Sun, 25 Aug 2024 14:22:28 -0500 Subject: [PATCH 08/12] fix jump when clicking on word border --- Text-Grab/Controls/ZoomBorder.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Text-Grab/Controls/ZoomBorder.cs b/Text-Grab/Controls/ZoomBorder.cs index 952c6f20..a3a30fa2 100644 --- a/Text-Grab/Controls/ZoomBorder.cs +++ b/Text-Grab/Controls/ZoomBorder.cs @@ -138,6 +138,9 @@ void Child_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) private void Child_MouseMove(object sender, MouseEventArgs e) { + if (e.OriginalSource is TextBox) + return; + if (child is null || GetScaleTransform(child) is not ScaleTransform st || st.ScaleX == 1.0 From 2f67b2a0536b9224656808045a1b47e2adc7d615 Mon Sep 17 00:00:00 2001 From: Joseph Finney Date: Sun, 25 Aug 2024 15:13:49 -0500 Subject: [PATCH 09/12] update packages --- Tests/Tests.csproj | 4 ++-- Text-Grab/Text-Grab.csproj | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 7fd55112..792d45d7 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -9,9 +9,9 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/Text-Grab/Text-Grab.csproj b/Text-Grab/Text-Grab.csproj index 2507b5e1..0af29a12 100644 --- a/Text-Grab/Text-Grab.csproj +++ b/Text-Grab/Text-Grab.csproj @@ -68,11 +68,11 @@ - + - - - + + + From 8494fc2177caaf1f019e9d9d37d3a4dcfcaf5461 Mon Sep 17 00:00:00 2001 From: Joseph Finney Date: Sun, 25 Aug 2024 16:20:53 -0500 Subject: [PATCH 10/12] Update common letter number confusions style code --- Tests/StringMethodTests.cs | 12 +-- .../Extensions/StringBuilderExtensions.cs | 2 +- Text-Grab/Utilities/StringMethods.cs | 92 ++++++++++--------- 3 files changed, 54 insertions(+), 52 deletions(-) diff --git a/Tests/StringMethodTests.cs b/Tests/StringMethodTests.cs index afbed776..3ce59f6d 100644 --- a/Tests/StringMethodTests.cs +++ b/Tests/StringMethodTests.cs @@ -81,9 +81,9 @@ public void ReturnWordAtCursorWithNewLines(int cursorPosition, string expectedWo [Theory] [InlineData("", "")] - [InlineData("Hello, world! 0123456789", "Hello, world! ol23h5678g")] + [InlineData("Hello, world! 0123456789", "Hello, world! olz3hSb7Bg")] [InlineData("Foo 4r b4r", "Foo hr bhr")] - [InlineData("B4zz 9zzl3", "Bhzz gzzl3")] + [InlineData("B4zz5 9zzl3", "BhzzS gzzl3")] [InlineData("abcdefghijklmnop", "abcdefghijklmnop")] public void TryFixToLetters_ReplacesDigitsWithLetters_AsExpected(string input, string expected) { @@ -106,10 +106,10 @@ public void TryFixNumOrLetters(string input, string expected) [Theory] [InlineData("", "")] - [InlineData("Hello, world! 0123456789", "He110, w0r1d! 0123456789")] - [InlineData("Foo 4r b4r", "F00 4r b4r")] - [InlineData("B4zz 9zzl3", "B4zz 9zz13")] - [InlineData("abcdefghijklmnop", "ab0def9h1jk1mn0p")] + [InlineData("Hello, world! 0123456789", "4e110, w0r1d! 0123456789")] + [InlineData("Foo 4r b4r", "F00 4r 64r")] + [InlineData("B4zzS 9zzl3", "84225 92213")] + [InlineData("abcdefghijklmnopqrs", "a60def941jk1mn0pqr5")] public void TryFixToLetters_ReplacesLettersWithDigits_AsExpected(string input, string expected) { // Act diff --git a/Text-Grab/Extensions/StringBuilderExtensions.cs b/Text-Grab/Extensions/StringBuilderExtensions.cs index 2c8fc94b..d5cbd513 100644 --- a/Text-Grab/Extensions/StringBuilderExtensions.cs +++ b/Text-Grab/Extensions/StringBuilderExtensions.cs @@ -25,7 +25,7 @@ public static void RemoveTrailingNewlines(this StringBuilder text) public static void ReplaceGreekOrCyrillicWithLatin(this StringBuilder stringBuilder) { - stringBuilder.CharDictionaryReplace(StringMethods.greekCyrillicLatinMap); + stringBuilder.CharDictionaryReplace(StringMethods.GreekCyrillicLatinMap); } public static void TryFixToLetters(this StringBuilder stringBuilder) diff --git a/Text-Grab/Utilities/StringMethods.cs b/Text-Grab/Utilities/StringMethods.cs index 1806eb9f..d53ed248 100644 --- a/Text-Grab/Utilities/StringMethods.cs +++ b/Text-Grab/Utilities/StringMethods.cs @@ -8,13 +8,15 @@ namespace Text_Grab.Utilities; public static class StringMethods { - public static readonly List specialCharList = new() - { '\\', ' ', '.', ',', '$', '^', '{', '[', '(', '|', ')', '*', '+', '?', '=' }; + public static readonly List specialCharList = [ + '\\', ' ', '.', ',', '$', '^', '{', '[', '(', '|', ')', + '*', '+', '?', '=' ]; - public static readonly List ReservedChars = new() - { ' ', '"', '*', '/', ':', '<', '>', '?', '\\', '|', '+', ',', '.', ';', '=', '[', ']', '!', '@' }; + public static readonly List ReservedChars = [ + ' ', '"', '*', '/', ':', '<', '>', '?', '\\', '|', '+', + ',', '.', ';', '=', '[', ']', '!', '@' ]; - public static readonly Dictionary greekCyrillicLatinMap = new() + public static readonly Dictionary GreekCyrillicLatinMap = new() { // Similar Looking Greek characters {'Γ', 'r'}, {'Δ', 'A'}, {'Θ', 'O'}, {'Λ', 'A'}, {'Ξ', 'E'}, @@ -43,41 +45,42 @@ public static class StringMethods {'ø', 'e'}, }; - public static Dictionary NumbersToLetters = new() + public static readonly Dictionary NumbersToLetters = new() { - {'0', 'o'}, {'4', 'h'}, {'9', 'g'}, {'1', 'l'} + {'0', 'o'}, {'4', 'h'}, {'9', 'g'}, {'1', 'l'}, {'8', 'B'}, + {'5', 'S'}, {'6', 'b'}, {'2', 'z' } }; - public static Dictionary LettersToNumbers = new() + public static readonly Dictionary LettersToNumbers = new() { {'o', '0'}, {'O', '0'}, {'Q', '0'}, {'c', '0'}, {'C', '0'}, - {'i', '1'}, {'I', '1'}, {'l', '1'}, {'g', '9'} + {'i', '1'}, {'I', '1'}, {'l', '1'}, {'g', '9'}, {'G', '9'}, + {'h', '4'}, {'H', '4'}, {'s', '5'}, {'S', '5'}, {'B', '8'}, + {'b', '6'}, {'z', '2'}, {'Z', '2'} }; public static string ReplaceWithDictionary(this string str, Dictionary dict) { - var sb = new StringBuilder(); + StringBuilder sb = new(); foreach (char c in str) - { - sb.Append(dict.ContainsKey(c) ? dict[c] : c); - } + sb.Append(dict.TryGetValue(c, out char value) ? value : c); return sb.ToString(); } public static string ReplaceGreekOrCyrillicWithLatin(this string str) { - return str.ReplaceWithDictionary(greekCyrillicLatinMap); + return str.ReplaceWithDictionary(GreekCyrillicLatinMap); } - public static IEnumerable AllIndexesOf(this string str, string searchstring) + public static IEnumerable AllIndexesOf(this string str, string searchString) { - int minIndex = str.IndexOf(searchstring); + int minIndex = str.IndexOf(searchString); while (minIndex != -1) { yield return minIndex; - minIndex = str.IndexOf(searchstring, minIndex + searchstring.Length); + minIndex = str.IndexOf(searchString, minIndex + searchString.Length); } } @@ -102,7 +105,7 @@ public static (int, int) CursorWordBoundaries(this string input, int cursorPosit // Check if the cursor is at a space if (char.IsWhiteSpace(input[cursorPosition])) - cursorPosition = findNearestLetterIndex(input, cursorPosition); + cursorPosition = FindNearestLetterIndex(input, cursorPosition); // Find the start and end of the word by moving the cursor // backwards and forwards until we find a non-letter character. @@ -132,7 +135,7 @@ public static string GetWordAtCursorPosition(this string input, int cursorPositi return input.Substring(start, length); } - private static int findNearestLetterIndex(string input, int cursorPosition) + private static int FindNearestLetterIndex(string input, int cursorPosition) { Math.Clamp(cursorPosition, 0, input.Length - 1); @@ -242,14 +245,14 @@ public static string TryFixNumberLetterErrors(this string stringToFix) public static string TryFixEveryWordLetterNumberErrors(this string stringToFix) { string[] listOfWords = stringToFix.Split(' '); - List fixedWords = new(); + List fixedWords = []; foreach (string word in listOfWords) { string newWord = word.TryFixNumberLetterErrors(); fixedWords.Add(newWord); } - string joinedString = string.Join(' ', fixedWords.ToArray()); + string joinedString = string.Join(' ', [.. fixedWords]); joinedString = joinedString.Replace("\t ", "\t"); joinedString = joinedString.Replace("\r ", "\r"); joinedString = joinedString.Replace("\n ", "\n"); @@ -276,7 +279,7 @@ public static string MakeStringSingleLine(this string textToEdit) if (workingString[0] == ' ') workingString.Remove(0, 1); - if (workingString[workingString.Length - 1] == ' ') + if (workingString[^1] == ' ') workingString.Remove(workingString.Length - 1, 1); return workingString.ToString(); @@ -344,8 +347,8 @@ public enum CharType { Letter, Number, Space, Special, Other }; public class CharRun { public CharType TypeOfChar { get; set; } - public Char Character { get; set; } - public int numberOfRun { get; set; } + public char Character { get; set; } + public int NumberOfRun { get; set; } } public static string ReplaceReservedCharacters(this string stringToClean) @@ -353,7 +356,7 @@ public static string ReplaceReservedCharacters(this string stringToClean) StringBuilder sb = new(); sb.Append(stringToClean); - foreach (Char reservedChar in ReservedChars) + foreach (char reservedChar in ReservedChars) sb.Replace(reservedChar, '-'); return Regex.Replace(sb.ToString(), @"-+", "-"); @@ -382,13 +385,13 @@ public static string ExtractSimplePattern(this string stringToExtract) foreach (char c in stringToExtract) { CharType thisCharType = CharType.Other; - if (Char.IsWhiteSpace(c)) + if (char.IsWhiteSpace(c)) thisCharType = CharType.Space; else if (specialCharList.Contains(c)) thisCharType = CharType.Special; - else if (Char.IsLetter(c)) + else if (char.IsLetter(c)) thisCharType = CharType.Letter; - else if (Char.IsNumber(c)) + else if (char.IsNumber(c)) thisCharType = CharType.Number; if (thisCharType == charRunList.LastOrDefault()?.TypeOfChar) @@ -396,11 +399,11 @@ public static string ExtractSimplePattern(this string stringToExtract) if (thisCharType == CharType.Other) { if (c == charRunList.Last().Character) - charRunList.Last().numberOfRun++; + charRunList.Last().NumberOfRun++; } else { - charRunList.Last().numberOfRun++; + charRunList.Last().NumberOfRun++; } } else @@ -408,7 +411,7 @@ public static string ExtractSimplePattern(this string stringToExtract) CharRun newRun = new() { Character = c, - numberOfRun = 1, + NumberOfRun = 1, TypeOfChar = thisCharType }; charRunList.Add(newRun); @@ -416,7 +419,6 @@ public static string ExtractSimplePattern(this string stringToExtract) } StringBuilder sb = new(); - // sb.Append("("); foreach (CharRun ct in charRunList) { @@ -441,9 +443,9 @@ public static string ExtractSimplePattern(this string stringToExtract) break; } - if (ct.numberOfRun > 1) + if (ct.NumberOfRun > 1) { - sb.Append('{').Append(ct.numberOfRun).Append('}'); + sb.Append('{').Append(ct.NumberOfRun).Append('}'); } } @@ -500,7 +502,7 @@ private static string ShortenRegexPattern(this string pattern) sb.Clear(); } - possibleShortenedPatterns = possibleShortenedPatterns.OrderBy(p => p.Length).ToList(); + possibleShortenedPatterns = [.. possibleShortenedPatterns.OrderBy(p => p.Length)]; return possibleShortenedPatterns.First(); } @@ -574,14 +576,14 @@ public static string UnstackGroups(this string stringGroupedToUnstack, int numbe public static string RemoveDuplicateLines(this string stringToDeduplicate) { - string[] splitString = stringToDeduplicate.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.TrimEntries); - List uniqueLines = new(); + string[] splitString = stringToDeduplicate.Split(new string[] { Environment.NewLine }, StringSplitOptions.TrimEntries); + List uniqueLines = []; foreach (string originalLine in splitString) if (!uniqueLines.Contains(originalLine)) uniqueLines.Add(originalLine); - return string.Join(Environment.NewLine, uniqueLines.ToArray()); + return string.Join(Environment.NewLine, [.. uniqueLines]); } public static string RemoveAllInstancesOf(this string stringToBeEdited, string stringToRemove) @@ -592,7 +594,7 @@ public static string RemoveAllInstancesOf(this string stringToBeEdited, string s public static string RemoveFromEachLine(this string stringToEdit, int numberOfChars, SpotInLine spotInLine) { - string[] splitString = stringToEdit.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None); + string[] splitString = stringToEdit.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); StringBuilder sb = new(); foreach (string line in splitString) @@ -607,10 +609,10 @@ public static string RemoveFromEachLine(this string stringToEdit, int numberOfCh switch (spotInLine) { case SpotInLine.Beginning: - sb.AppendLine(line.Substring(numberOfChars)); + sb.AppendLine(line[numberOfChars..]); break; case SpotInLine.End: - sb.AppendLine(line.Substring(0, lineLength - numberOfChars)); + sb.AppendLine(line[..(lineLength - numberOfChars)]); break; default: break; @@ -661,7 +663,7 @@ public static string LimitCharactersPerLine(this string stringToEdit, int charac } if (spotInLine== SpotInLine.Beginning) - returnStringBuilder.AppendLine(line.Substring(0, lineLimit)); + returnStringBuilder.AppendLine(line[..lineLimit]); else returnStringBuilder.AppendLine(line.Substring(line.Length - (lineLimit), lineLimit)); } @@ -689,7 +691,7 @@ public static string GetCharactersToLeftOfNewLine(ref string mainString, int ind int newLineIndex = GetNewLineIndexToLeft(ref mainString, index); if (newLineIndex < 1) - return mainString.Substring(0, index); + return mainString[..index]; newLineIndex++; @@ -709,13 +711,13 @@ public static string GetCharactersToRightOfNewLine(ref string mainString, int in { int newLineIndex = GetNewLineIndexToRight(ref mainString, index); if (newLineIndex < 1) - return mainString.Substring(index); + return mainString[index..]; if (newLineIndex - index > numberOfCharacters) return string.Concat(mainString.AsSpan(index, numberOfCharacters), "..."); if (newLineIndex == mainString.Length) - return mainString.Substring(index); + return mainString[index..]; return string.Concat(mainString.AsSpan(index, newLineIndex - index), "..."); } From 1c8d68abcf5a6272b5d49ad9f301b26faa5ef9f8 Mon Sep 17 00:00:00 2001 From: Joseph Finney Date: Sun, 25 Aug 2024 17:47:35 -0500 Subject: [PATCH 11/12] Used a Task to do delete all and replace all Fixes #463 --- Text-Grab/Controls/FindAndReplaceWindow.xaml | 8 +- .../Controls/FindAndReplaceWindow.xaml.cs | 76 ++++++++++++++----- 2 files changed, 62 insertions(+), 22 deletions(-) diff --git a/Text-Grab/Controls/FindAndReplaceWindow.xaml b/Text-Grab/Controls/FindAndReplaceWindow.xaml index 1be850d6..51664835 100644 --- a/Text-Grab/Controls/FindAndReplaceWindow.xaml +++ b/Text-Grab/Controls/FindAndReplaceWindow.xaml @@ -60,7 +60,7 @@ Padding="8,2" Icon="{StaticResource TextGrabIcon}" /> - + @@ -266,5 +266,11 @@ + + diff --git a/Text-Grab/Controls/FindAndReplaceWindow.xaml.cs b/Text-Grab/Controls/FindAndReplaceWindow.xaml.cs index db23ad3d..afa63844 100644 --- a/Text-Grab/Controls/FindAndReplaceWindow.xaml.cs +++ b/Text-Grab/Controls/FindAndReplaceWindow.xaml.cs @@ -1,8 +1,10 @@ using System; +using System.Collections; 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; using System.Windows.Input; @@ -190,28 +192,36 @@ private void DeleteAll_CanExecute(object sender, CanExecuteRoutedEventArgs e) e.CanExecute = false; } - private void DeleteAll_Executed(object sender, ExecutedRoutedEventArgs e) + private async void DeleteAll_Executed(object sender, ExecutedRoutedEventArgs e) { if (Matches is null || Matches.Count < 1 || textEditWindow is null) return; - var selection = ResultsListView.SelectedItems; - if (selection.Count < 2) - selection = ResultsListView.Items; + SetWindowToLoading(); + + IList selection = ResultsListView.SelectedItems; + StringBuilder stringBuilderOfText = new(textEditWindow.PassedTextControl.Text); - for (int j = selection.Count - 1; j >= 0; j--) + await Task.Run(() => { - if (selection[j] is not FindResult selectedResult) - continue; + if (selection.Count < 2) + selection = ResultsListView.Items; - textEditWindow.PassedTextControl.Select(selectedResult.Index, selectedResult.Length); - textEditWindow.PassedTextControl.SelectedText = string.Empty; - } + for (int j = selection.Count - 1; j >= 0; j--) + { + if (selection[j] is not FindResult selectedResult) + continue; + + stringBuilderOfText.Remove(selectedResult.Index, selectedResult.Length); + } + }); + + textEditWindow.PassedTextControl.Text = stringBuilderOfText.ToString(); - textEditWindow.PassedTextControl.Select(0, 0); SearchForText(); + ResetWindowLoading(); } private void EditTextBoxChanged(object sender, TextChangedEventArgs e) @@ -314,27 +324,51 @@ private void Replace_Executed(object sender, ExecutedRoutedEventArgs e) SearchForText(); } - private void ReplaceAll_Executed(object sender, ExecutedRoutedEventArgs e) + private async void ReplaceAll_Executed(object sender, ExecutedRoutedEventArgs e) { if (Matches is null || Matches.Count < 1 || textEditWindow is null) return; - var selection = ResultsListView.SelectedItems; - if (selection.Count < 2) - selection = ResultsListView.Items; + SetWindowToLoading(); + + StringBuilder stringBuilder = new(textEditWindow.PassedTextControl.Text); - for (int j = selection.Count - 1; j >= 0; j--) + IList selection = ResultsListView.SelectedItems; + string newText = ReplaceTextBox.Text; + + await Task.Run(() => { - if (selection[j] is not FindResult selectedResult) - continue; + if (selection.Count < 2) + selection = ResultsListView.Items; - textEditWindow.PassedTextControl.Select(selectedResult.Index, selectedResult.Length); - textEditWindow.PassedTextControl.SelectedText = ReplaceTextBox.Text; - } + for (int j = selection.Count - 1; j >= 0; j--) + { + if (selection[j] is not FindResult selectedResult) + continue; + + stringBuilder.Remove(selectedResult.Index, selectedResult.Length); + stringBuilder.Insert(selectedResult.Index, newText); + } + }); + + textEditWindow.PassedTextControl.Text = stringBuilder.ToString(); SearchForText(); + ResetWindowLoading(); + } + + private void ResetWindowLoading() + { + MainContentGrid.IsEnabled = true; + LoadingSpinner.Visibility = Visibility.Collapsed; + } + + private void SetWindowToLoading() + { + MainContentGrid.IsEnabled = false; + LoadingSpinner.Visibility = Visibility.Visible; } private void ResultsListView_SelectionChanged(object sender, SelectionChangedEventArgs e) From 5a52eff111155962db72be9909d1fb6aaf09ff08 Mon Sep 17 00:00:00 2001 From: Joseph Finney Date: Sun, 25 Aug 2024 18:20:43 -0500 Subject: [PATCH 12/12] version to 4.5.1 --- Text-Grab-Package/Package.appxmanifest | 2 +- Text-Grab/Pages/GeneralSettings.xaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Text-Grab-Package/Package.appxmanifest b/Text-Grab-Package/Package.appxmanifest index 830c216a..1955af12 100644 --- a/Text-Grab-Package/Package.appxmanifest +++ b/Text-Grab-Package/Package.appxmanifest @@ -11,7 +11,7 @@ + Version="4.5.1.0" /> Text Grab diff --git a/Text-Grab/Pages/GeneralSettings.xaml b/Text-Grab/Pages/GeneralSettings.xaml index 8cc38b3c..17d06dbd 100644 --- a/Text-Grab/Pages/GeneralSettings.xaml +++ b/Text-Grab/Pages/GeneralSettings.xaml @@ -27,7 +27,7 @@ x:Name="VersionTextblock" VerticalAlignment="Center" Style="{StaticResource TextBodyNormal}" - Text="Version 4.5" /> + Text="Version 4.5.1" />