Skip to content

Commit

Permalink
Add "Allow partial text matches to be merged" option
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Oct 9, 2024
1 parent f0c7f2f commit 6017228
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
6 changes: 6 additions & 0 deletions JL.Windows/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ internal static class ConfigManager
private static bool TextBoxApplyDropShadowEffect { get; set; } = true;
private static bool HorizontallyCenterMainWindowText { get; set; } // = false;
public static bool MergeSequentialTextsWhenTheyMatch { get; private set; } // = false;
public static bool AllowPartialMatchingForTextMerge { get; private set; } = true; // = false;
public static double MaxDelayBetweenCopiesForMergingMatchingSequentialTextsInMilliseconds { get; private set; } = 5000;
public static bool TextBoxUseCustomLineHeight { get; private set; } // = false;
public static double TextBoxCustomLineHeight { get; private set; } = 75;
Expand Down Expand Up @@ -323,6 +324,7 @@ public static void ApplyPreferences()
HidePopupsOnTextChange = ConfigDBManager.GetValueFromConfig(connection, HidePopupsOnTextChange, nameof(HidePopupsOnTextChange), bool.TryParse);

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

HideAllTitleBarButtonsWhenMouseIsNotOverTitleBar = ConfigDBManager.GetValueFromConfig(connection, HideAllTitleBarButtonsWhenMouseIsNotOverTitleBar, nameof(HideAllTitleBarButtonsWhenMouseIsNotOverTitleBar), bool.TryParse);
MainWindow.Instance.ChangeVisibilityOfTitleBarButtons();
Expand Down Expand Up @@ -868,6 +870,7 @@ public static void LoadPreferenceWindow(PreferencesWindow preferenceWindow)
preferenceWindow.TextToSpeechOnTextChangeCheckBox.IsChecked = TextToSpeechOnTextChange;
preferenceWindow.HidePopupsOnTextChangeCheckBox.IsChecked = HidePopupsOnTextChange;
preferenceWindow.MergeSequentialTextsWhenTheyMatchCheckBox.IsChecked = MergeSequentialTextsWhenTheyMatch;
preferenceWindow.AllowPartialMatchingForTextMergeCheckBox.IsChecked = AllowPartialMatchingForTextMerge;
preferenceWindow.TextBoxUseCustomLineHeightCheckBox.IsChecked = TextBoxUseCustomLineHeight;
preferenceWindow.ToggleHideAllTitleBarButtonsWhenMouseIsNotOverTitleBarCheckBox.IsChecked = HideAllTitleBarButtonsWhenMouseIsNotOverTitleBar;
preferenceWindow.HorizontallyCenterMainWindowTextCheckBox.IsChecked = HorizontallyCenterMainWindowText;
Expand Down Expand Up @@ -1126,6 +1129,9 @@ public static async Task SavePreferences(PreferencesWindow preferenceWindow)
ConfigDBManager.UpdateSetting(connection, nameof(MergeSequentialTextsWhenTheyMatch),
preferenceWindow.MergeSequentialTextsWhenTheyMatchCheckBox.IsChecked.ToString()!);

ConfigDBManager.UpdateSetting(connection, nameof(AllowPartialMatchingForTextMerge),
preferenceWindow.AllowPartialMatchingForTextMergeCheckBox.IsChecked.ToString()!);

ConfigDBManager.UpdateSetting(connection, nameof(TextBoxUseCustomLineHeight),
preferenceWindow.TextBoxUseCustomLineHeightCheckBox.IsChecked.ToString()!);

Expand Down
48 changes: 40 additions & 8 deletions JL.Windows/GUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,41 +158,73 @@ private bool CopyText(string text)
return false;
}

string sanitizedText = TextUtils.SanitizeText(text);
if (sanitizedText.Length is 0)
string sanitizedNewText = TextUtils.SanitizeText(text);
if (sanitizedNewText.Length is 0)
{
return false;
}

bool mergeTexts = false;
string? subsequentText = null;
string? mergedText = null;

Dispatcher.Invoke(() =>
{
if (ConfigManager.MergeSequentialTextsWhenTheyMatch)
{
DateTime preciseTimeNow = new(Stopwatch.GetTimestamp());

string currentText = MainTextBox.Text;
string previousText = MainTextBox.Text;

mergeTexts = (ConfigManager.MaxDelayBetweenCopiesForMergingMatchingSequentialTextsInMilliseconds is 0
|| (preciseTimeNow - s_lastTextCopyTime).TotalMilliseconds < ConfigManager.MaxDelayBetweenCopiesForMergingMatchingSequentialTextsInMilliseconds)
&& sanitizedText.StartsWith(currentText, StringComparison.Ordinal);
|| (preciseTimeNow - s_lastTextCopyTime).TotalMilliseconds <= ConfigManager.MaxDelayBetweenCopiesForMergingMatchingSequentialTextsInMilliseconds)
&& previousText.Length > 0;

s_lastTextCopyTime = preciseTimeNow;

if (mergeTexts)
{
subsequentText = sanitizedText[currentText.Length..];
if (!ConfigManager.AllowPartialMatchingForTextMerge)
{
if (sanitizedNewText.StartsWith(previousText, StringComparison.Ordinal))
{
subsequentText = sanitizedNewText[previousText.Length..];
mergedText = sanitizedNewText;
}
}
else
{
int startIndex = Math.Max(previousText.Length - sanitizedNewText.Length, 0);
for (int i = startIndex; i < previousText.Length; i++)
{
if (sanitizedNewText.StartsWith(previousText[i..], StringComparison.Ordinal))
{
subsequentText = sanitizedNewText[(previousText.Length - i)..];
if (subsequentText.Length is 0 && sanitizedNewText != previousText)
{
subsequentText = null;
}
else
{
mergedText = previousText + subsequentText;
}

break;
}
}
}
}
}

mergeTexts = mergeTexts && subsequentText is not null;
if (mergeTexts)
{
MainTextBox.AppendText(subsequentText);
}
else
{
MainTextBox.Text = sanitizedText;
MainTextBox.Text = sanitizedNewText;
mergedText = null;
}

MainTextBox.Foreground = ConfigManager.MainWindowTextColor;
Expand All @@ -214,7 +246,7 @@ private bool CopyText(string text)
BringToFront();
}, DispatcherPriority.Send);

WindowsUtils.HandlePostCopy(sanitizedText, subsequentText);
WindowsUtils.HandlePostCopy(sanitizedNewText, subsequentText, mergedText);

return true;
}
Expand Down
12 changes: 12 additions & 0 deletions JL.Windows/GUI/PreferencesWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,18 @@
<CheckBox x:Name="MergeSequentialTextsWhenTheyMatchCheckBox" HorizontalAlignment="Right" />
</DockPanel>

<DockPanel>
<TextBlock HorizontalAlignment="Left" Text="Allow partial text matches to be merged"
Style="{StaticResource TextBlockDefault}"
TextWrapping="Wrap" VerticalAlignment="Center"
Cursor="Help"
ToolTip="This option only takes effect if the &quot;Merge sequential texts when they match&quot; option is enabled.
&#10;Enabling this option allows XYZ and ZTVR to be merged into XYZTVR, and XYZ and YZTVR will also be merged into XYZTVR.
&#10;When this option is disabled, texts will only be merged if the new text found in Clipboard/WebSocket starts with the current text.
&#10;For example, when this option is disabled, XYZ and YZTVR won't be merged, but XYZ and XYZTVR will be merged into XYZTVR."/>
<CheckBox x:Name="AllowPartialMatchingForTextMergeCheckBox" HorizontalAlignment="Right" />
</DockPanel>

<DockPanel>
<TextBlock HorizontalAlignment="Left" Text="Max delay between copies to merge sequential texts when they match (in milliseconds)"
Style="{StaticResource TextBlockDefault}"
Expand Down
6 changes: 3 additions & 3 deletions JL.Windows/Utilities/WindowsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,9 @@ public static void CopyTextToClipboard(string? text)
}
}

public static void HandlePostCopy(string text, string? subsequentText)
public static void HandlePostCopy(string text, string? subsequentText, string? mergedText)
{
bool newText = subsequentText is null;
bool newText = mergedText is null;
if (ConfigManager.EnableBacklog)
{
if (newText)
Expand All @@ -655,7 +655,7 @@ public static void HandlePostCopy(string text, string? subsequentText)
}
else
{
BacklogUtils.ReplaceLastBacklogText(text);
BacklogUtils.ReplaceLastBacklogText(mergedText!);
}
}

Expand Down

0 comments on commit 6017228

Please sign in to comment.