Skip to content

Commit

Permalink
Used a Task to do delete all and replace all
Browse files Browse the repository at this point in the history
Fixes #463
  • Loading branch information
TheJoeFin committed Aug 25, 2024
1 parent 8494fc2 commit 1c8d68a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
8 changes: 7 additions & 1 deletion Text-Grab/Controls/FindAndReplaceWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
Padding="8,2"
Icon="{StaticResource TextGrabIcon}" />

<Grid Grid.Row="1">
<Grid x:Name="MainContentGrid" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
Expand Down Expand Up @@ -266,5 +266,11 @@
</ListView.ItemTemplate>
</ui:ListView>
</Border>

<ui:ProgressRing
x:Name="LoadingSpinner"
Grid.Row="3"
IsIndeterminate="True"
Visibility="Collapsed" />
</Grid>
</ui:FluentWindow>
76 changes: 55 additions & 21 deletions Text-Grab/Controls/FindAndReplaceWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1c8d68a

Please sign in to comment.