Skip to content

Commit

Permalink
Add "Don't capture identical text from Clipboard/WebSocket" option
Browse files Browse the repository at this point in the history
Closes #108
  • Loading branch information
rampaa committed Dec 7, 2024
1 parent 1994275 commit be5430b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
6 changes: 6 additions & 0 deletions JL.Windows/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ internal sealed class ConfigManager
public double MainWindowMinDynamicHeight { get; set; } = 50;
private bool TextBoxApplyDropShadowEffect { get; set; } = true;
private bool HorizontallyCenterMainWindowText { get; set; } // = false;
public bool DiscardIdenticalText { get; set; } // = false;
public bool MergeSequentialTextsWhenTheyMatch { get; private set; } // = false;
public bool AllowPartialMatchingForTextMerge { get; private set; } // = false;
public double MaxDelayBetweenCopiesForMergingMatchingSequentialTextsInMilliseconds { get; private set; } = 5000;
Expand Down Expand Up @@ -364,6 +365,7 @@ public void ApplyPreferences()

HidePopupsOnTextChange = ConfigDBManager.GetValueFromConfig(connection, HidePopupsOnTextChange, nameof(HidePopupsOnTextChange), bool.TryParse);

DiscardIdenticalText = ConfigDBManager.GetValueFromConfig(connection, DiscardIdenticalText, nameof(DiscardIdenticalText), bool.TryParse);
MergeSequentialTextsWhenTheyMatch = ConfigDBManager.GetValueFromConfig(connection, MergeSequentialTextsWhenTheyMatch, nameof(MergeSequentialTextsWhenTheyMatch), bool.TryParse);
AllowPartialMatchingForTextMerge = ConfigDBManager.GetValueFromConfig(connection, AllowPartialMatchingForTextMerge, nameof(AllowPartialMatchingForTextMerge), bool.TryParse);

Expand Down Expand Up @@ -903,6 +905,7 @@ public void LoadPreferenceWindow(PreferencesWindow preferenceWindow)
preferenceWindow.AutoSaveBacklogBeforeClosingCheckBox.IsChecked = AutoSaveBacklogBeforeClosing;
preferenceWindow.TextToSpeechOnTextChangeCheckBox.IsChecked = TextToSpeechOnTextChange;
preferenceWindow.HidePopupsOnTextChangeCheckBox.IsChecked = HidePopupsOnTextChange;
preferenceWindow.DiscardIdenticalTextCheckBox.IsChecked = DiscardIdenticalText;
preferenceWindow.MergeSequentialTextsWhenTheyMatchCheckBox.IsChecked = MergeSequentialTextsWhenTheyMatch;
preferenceWindow.AllowPartialMatchingForTextMergeCheckBox.IsChecked = AllowPartialMatchingForTextMerge;
preferenceWindow.TextBoxUseCustomLineHeightCheckBox.IsChecked = TextBoxUseCustomLineHeight;
Expand Down Expand Up @@ -1169,6 +1172,9 @@ public async Task SavePreferences(PreferencesWindow preferenceWindow)
ConfigDBManager.UpdateSetting(connection, nameof(HidePopupsOnTextChange),
preferenceWindow.HidePopupsOnTextChangeCheckBox.IsChecked.ToString()!);

ConfigDBManager.UpdateSetting(connection, nameof(DiscardIdenticalText),
preferenceWindow.DiscardIdenticalTextCheckBox.IsChecked.ToString()!);

ConfigDBManager.UpdateSetting(connection, nameof(MergeSequentialTextsWhenTheyMatch),
preferenceWindow.MergeSequentialTextsWhenTheyMatchCheckBox.IsChecked.ToString()!);

Expand Down
31 changes: 24 additions & 7 deletions JL.Windows/GUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,22 @@ private bool CopyText(string text)
string? subsequentText = null;
string? mergedText = null;

Dispatcher.Invoke(() =>
bool result = Dispatcher.Invoke(() =>
{
if (configManager.MergeSequentialTextsWhenTheyMatch)
string previousText = MainTextBox.Text;
if (configManager.DiscardIdenticalText && sanitizedNewText == previousText)
{
DateTime preciseTimeNow = new(Stopwatch.GetTimestamp());
if (configManager.MergeSequentialTextsWhenTheyMatch)
{
s_lastTextCopyTime = new(Stopwatch.GetTimestamp());
}

string previousText = MainTextBox.Text;
return false;
}

if (configManager.MergeSequentialTextsWhenTheyMatch)
{
DateTime preciseTimeNow = new(Stopwatch.GetTimestamp());
mergeTexts = (configManager.MaxDelayBetweenCopiesForMergingMatchingSequentialTextsInMilliseconds is 0
|| (preciseTimeNow - s_lastTextCopyTime).TotalMilliseconds <= configManager.MaxDelayBetweenCopiesForMergingMatchingSequentialTextsInMilliseconds)
&& previousText.Length > 0;
Expand All @@ -179,6 +187,11 @@ private bool CopyText(string text)

if (mergeTexts)
{
if (!configManager.DiscardIdenticalText && previousText == sanitizedNewText)
{
return false;
}

if (!configManager.AllowPartialMatchingForTextMerge)
{
if (sanitizedNewText.StartsWith(previousText, StringComparison.Ordinal))
Expand Down Expand Up @@ -234,13 +247,17 @@ private bool CopyText(string text)
}

UpdatePosition();

BringToFront();

return true;
}, DispatcherPriority.Send);

WindowsUtils.HandlePostCopy(sanitizedNewText, subsequentText, mergedText);
if (result)
{
WindowsUtils.HandlePostCopy(sanitizedNewText, subsequentText, mergedText);
}

return true;
return result;
}

public void BringToFront()
Expand Down
9 changes: 9 additions & 0 deletions JL.Windows/GUI/PreferencesWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,15 @@
<CheckBox x:Name="OnlyCaptureTextWithJapaneseCharsCheckBox" HorizontalAlignment="Right" />
</DockPanel>

<DockPanel>
<TextBlock HorizontalAlignment="Left" Text="Don't capture identical text from Clipboard/WebSocket"
Style="{StaticResource TextBlockDefault}"
TextWrapping="Wrap" VerticalAlignment="Center"
Cursor="Help"
ToolTip="When this option is enabled, new text found in the Clipboard/WebSocket that is identical to the current text in the main window will not be copied." />
<CheckBox x:Name="DiscardIdenticalTextCheckBox" HorizontalAlignment="Right" />
</DockPanel>

<DockPanel>
<TextBlock HorizontalAlignment="Left" Text="Merge sequential texts when they match"
Style="{StaticResource TextBlockDefault}"
Expand Down

0 comments on commit be5430b

Please sign in to comment.