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)