Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Aug 30, 2024
1 parent 3a7d90d commit 1d48b56
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 86 deletions.
4 changes: 2 additions & 2 deletions JL.Windows/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ public static void ApplyPreferences()
MaxNumResultsNotInMiningMode = ConfigDBManager.GetValueFromConfig(connection, MaxNumResultsNotInMiningMode, nameof(MaxNumResultsNotInMiningMode), int.TryParse);

AutoHidePopupIfMouseIsNotOverItDelayInMilliseconds = ConfigDBManager.GetNumberWithDecimalPointFromConfig(connection, AutoHidePopupIfMouseIsNotOverItDelayInMilliseconds, nameof(AutoHidePopupIfMouseIsNotOverItDelayInMilliseconds), double.TryParse);
PopupWindow.PopupAutoHideTimer.Enabled = false;
PopupWindow.PopupAutoHideTimer.Interval = AutoHidePopupIfMouseIsNotOverItDelayInMilliseconds;
PopupWindowUtils.PopupAutoHideTimer.Enabled = false;
PopupWindowUtils.PopupAutoHideTimer.Interval = AutoHidePopupIfMouseIsNotOverItDelayInMilliseconds;

PopupXOffset = ConfigDBManager.GetNumberWithDecimalPointFromConfig(connection, PopupXOffset, nameof(PopupXOffset), double.TryParse);
WindowsUtils.DpiAwareXOffset = PopupXOffset * WindowsUtils.Dpi.DpiScaleX;
Expand Down
36 changes: 1 addition & 35 deletions JL.Windows/GUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -217,44 +216,11 @@ private bool CopyText(string text)
BringToFront();
}, DispatcherPriority.Send);

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

return true;
}

private static void HandlePostCopy(string text, string? subsequentText)
{
bool newText = subsequentText is null;
if (newText)
{
BacklogUtils.AddToBacklog(text);
}
else
{
BacklogUtils.ReplaceLastBacklogText(text);
}

if (ConfigManager.TextToSpeechOnTextChange
&& SpeechSynthesisUtils.InstalledVoiceWithHighestPriority is not null)
{
_ = SpeechSynthesisUtils.TextToSpeech(SpeechSynthesisUtils.InstalledVoiceWithHighestPriority, text).ConfigureAwait(false);
}

string strippedText = ConfigManager.StripPunctuationBeforeCalculatingCharacterCount
? JapaneseUtils.RemovePunctuation(subsequentText ?? text)
: subsequentText ?? text;

if (strippedText.Length > 0)
{
Stats.IncrementStat(StatType.Characters, new StringInfo(strippedText).LengthInTextElements);

if (newText)
{
Stats.IncrementStat(StatType.Lines);
}
}
}

public void BringToFront()
{
if (ConfigManager.AlwaysOnTop
Expand Down
60 changes: 17 additions & 43 deletions JL.Windows/GUI/PopupWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using JL.Core.Utilities;
using JL.Windows.SpeechSynthesis;
using JL.Windows.Utilities;
using Timer = System.Timers.Timer;

namespace JL.Windows.GUI;

Expand Down Expand Up @@ -62,8 +61,6 @@ internal sealed partial class PopupWindow : Window

public bool MiningMode { get; private set; }

public static Timer PopupAutoHideTimer { get; } = new();

private ScrollViewer? _popupListViewScrollViewer;

private readonly ContextMenu _editableTextBoxContextMenu = new();
Expand Down Expand Up @@ -480,7 +477,7 @@ public void DisplayResults(bool generateAllResults)
{
_dictsWithResults.Clear();

PopupListView.Items.Filter = NoAllDictFilter;
PopupListView.Items.Filter = PopupWindowUtils.NoAllDictFilter;

_ = DictUtils.SingleDictTypeDicts.TryGetValue(DictType.PitchAccentYomichan, out Dict? pitchDict);
bool pitchDictIsActive = pitchDict?.Active ?? false;
Expand Down Expand Up @@ -1066,35 +1063,30 @@ private StackPanel PrepareResultStackPanel(LookupResult result, int index, int r
return stackPanel;
}

private static int GetIndexOfListViewItemFromStackPanel(StackPanel stackPanel)
{
return (int)((WrapPanel)stackPanel.Children[0]).Tag;
}

private int GetFirstVisibleListViewItemIndex()
{
StackPanel? firstVisibleStackPanel = PopupListView.Items.Cast<StackPanel>()
.FirstOrDefault(static stackPanel => stackPanel.Visibility is Visibility.Visible);

return firstVisibleStackPanel is not null
? GetIndexOfListViewItemFromStackPanel(firstVisibleStackPanel)
? PopupWindowUtils.GetIndexOfListViewItemFromStackPanel(firstVisibleStackPanel)
: 0;
}

private void ListViewItem_MouseEnter(object sender, MouseEventArgs e)
{
if (PopupContextMenu.IsVisible)
{
_listViewItemIndexAfterContextMenuIsClosed = GetIndexOfListViewItemFromStackPanel((StackPanel)sender);
_listViewItemIndexAfterContextMenuIsClosed = PopupWindowUtils.GetIndexOfListViewItemFromStackPanel((StackPanel)sender);
}
else
{
_listViewItemIndex = GetIndexOfListViewItemFromStackPanel((StackPanel)sender);
_listViewItemIndex = PopupWindowUtils.GetIndexOfListViewItemFromStackPanel((StackPanel)sender);
LastSelectedText = LastLookupResults[_listViewItemIndex].PrimarySpelling;
}
}

private static void Unselect(object sender, RoutedEventArgs e)
private void Unselect(object sender, RoutedEventArgs e)
{
WindowsUtils.Unselect((TextBox)sender);
}
Expand Down Expand Up @@ -1215,7 +1207,7 @@ private async void PrimarySpelling_PreviewMouseUp(object sender, MouseButtonEven
int listViewItemIndex = _listViewItemIndex;
TextBox? definitionsTextBox = GetDefinitionTextBox(listViewItemIndex);
string? formattedDefinitions = definitionsTextBox?.Text;
string? selectedDefinitions = GetSelectedDefinitions(definitionsTextBox);
string? selectedDefinitions = PopupWindowUtils.GetSelectedDefinitions(definitionsTextBox);

HidePopup();

Expand Down Expand Up @@ -1406,7 +1398,7 @@ public async Task HandleHotKey(KeyGesture keyGesture, KeyEventArgs? e)
ShowAddNameWindow();
}

PopupAutoHideTimer.Start();
PopupWindowUtils.PopupAutoHideTimer.Start();
}
}

Expand Down Expand Up @@ -1436,7 +1428,7 @@ public async Task HandleHotKey(KeyGesture keyGesture, KeyEventArgs? e)
ShowAddWordWindow();
}

PopupAutoHideTimer.Start();
PopupWindowUtils.PopupAutoHideTimer.Start();
}
}

Expand Down Expand Up @@ -1619,10 +1611,10 @@ public async Task HandleHotKey(KeyGesture keyGesture, KeyEventArgs? e)

if (MiningMode && PopupListView.SelectedItem is not null)
{
int index = GetIndexOfListViewItemFromStackPanel((StackPanel)PopupListView.SelectedItem);
int index = PopupWindowUtils.GetIndexOfListViewItemFromStackPanel((StackPanel)PopupListView.SelectedItem);
TextBox? definitionsTextBox = GetDefinitionTextBox(index);
string? formattedDefinitions = definitionsTextBox?.Text;
string? selectedDefinitions = GetSelectedDefinitions(definitionsTextBox);
string? selectedDefinitions = PopupWindowUtils.GetSelectedDefinitions(definitionsTextBox);

HidePopup();

Expand Down Expand Up @@ -1733,7 +1725,7 @@ private void OnMouseEnter(object sender, MouseEventArgs e)
{
if (!ChildPopupWindow?.IsVisible ?? true)
{
PopupAutoHideTimer.Stop();
PopupWindowUtils.PopupAutoHideTimer.Stop();
}

return;
Expand Down Expand Up @@ -1799,13 +1791,13 @@ private void OnMouseLeave(object sender, MouseEventArgs e)
|| AddWordWindow.IsItVisible()
|| AddNameWindow.IsItVisible())
{
PopupAutoHideTimer.Stop();
PopupWindowUtils.PopupAutoHideTimer.Stop();
}

else if (!ChildPopupWindow?.IsVisible ?? true)
{
PopupAutoHideTimer.Stop();
PopupAutoHideTimer.Start();
PopupWindowUtils.PopupAutoHideTimer.Stop();
PopupWindowUtils.PopupAutoHideTimer.Start();
}
}
}
Expand Down Expand Up @@ -1866,7 +1858,7 @@ private void ClickDictTypeButton(Button button)
bool isAllButton = button == _buttonAll;
if (isAllButton)
{
PopupListView.Items.Filter = NoAllDictFilter;
PopupListView.Items.Filter = PopupWindowUtils.NoAllDictFilter;
}

else
Expand All @@ -1890,17 +1882,6 @@ private bool DictFilter(object item)
return (Dict)items.Tag == _filteredDict;
}

private static bool NoAllDictFilter(object item)
{
if (CoreConfigManager.KanjiMode)
{
return true;
}

Dict dict = (Dict)((StackPanel)item).Tag;
return !dict.Options.NoAll.Value;
}

private void PopupContextMenu_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
bool contextMenuBecameInvisible = !(bool)e.NewValue;
Expand All @@ -1913,7 +1894,7 @@ private void PopupContextMenu_IsVisibleChanged(object sender, DependencyProperty
&& !AddWordWindow.IsItVisible()
&& !AddNameWindow.IsItVisible())
{
PopupAutoHideTimer.Start();
PopupWindowUtils.PopupAutoHideTimer.Start();
}
}
}
Expand Down Expand Up @@ -1991,7 +1972,7 @@ public void HidePopup()
_firstVisibleListViewItemIndex = 0;
_lastInteractedTextBox = null;

PopupAutoHideTimer.Stop();
PopupWindowUtils.PopupAutoHideTimer.Stop();

UpdateLayout();
Hide();
Expand Down Expand Up @@ -2045,13 +2026,6 @@ private void PopupListView_MouseLeave(object sender, MouseEventArgs e)
return ((StackPanel)((StackPanel)PopupListView.Items[listViewIndex]!).Children[1]).GetChildByName<TextBox>(nameof(LookupResult.FormattedDefinitions));
}

private static string? GetSelectedDefinitions(TextBox? definitionsTextBox)
{
return definitionsTextBox?.SelectionLength > 0
? definitionsTextBox.SelectedText
: null;
}

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
HidePopup();
Expand Down
2 changes: 1 addition & 1 deletion JL.Windows/Utilities/MagpieUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static double GetMagpieWindowTopEdgePosition(nint windowHandle)

//public static double GetMagpieWindowBottomEdgePosition(nint windowHandle)
//{
// return WinApi.GetProp(windowHandle, "Magpie.DestBottom") / WindowsUtils.Dpi.DpiScaleY;
// return WinApi.GetProp(windowHandle, "Magpie.DestBottom");
//}

public static bool IsMagpieReallyScaling()
Expand Down
35 changes: 30 additions & 5 deletions JL.Windows/Utilities/PopupWindowUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
using System.Windows.Media;
using System.Windows.Shapes;
using JL.Core.Audio;
using JL.Core.Config;
using JL.Core.Dicts;
using JL.Core.Dicts.PitchAccent;
using JL.Core.Utilities;
using JL.Windows.GUI;
using JL.Windows.GUI.UserControls;
using NAudio.Wave;
using Timer = System.Timers.Timer;

namespace JL.Windows.Utilities;

internal static class PopupWindowUtils
{
private static string? s_primarySpellingOfLastPlayedAudio;

private static string? s_readingOfLastPlayedAudio;
private static DoubleCollection StrokeDashArray { get; set; } = [1, 1];
public static readonly Timer PopupAutoHideTimer = new();

public static TextBlock CreateTextBlock(string name, string text, Brush foregroundBrush, double fontSize, ContextMenu contextMenu, VerticalAlignment verticalAlignment, Thickness margin)
{
Expand Down Expand Up @@ -177,10 +179,10 @@ public static Grid CreatePitchAccentGrid(string primarySpelling, string[]? alter

public static void SetPopupAutoHideTimer()
{
PopupWindow.PopupAutoHideTimer.Interval = ConfigManager.AutoHidePopupIfMouseIsNotOverItDelayInMilliseconds;
PopupWindow.PopupAutoHideTimer.Elapsed += PopupAutoHideTimerEvent;
PopupWindow.PopupAutoHideTimer.AutoReset = false;
PopupWindow.PopupAutoHideTimer.Enabled = true;
PopupAutoHideTimer.Interval = ConfigManager.AutoHidePopupIfMouseIsNotOverItDelayInMilliseconds;
PopupAutoHideTimer.Elapsed += PopupAutoHideTimerEvent;
PopupAutoHideTimer.AutoReset = false;
PopupAutoHideTimer.Enabled = true;
}

private static void PopupAutoHideTimerEvent(object? sender, ElapsedEventArgs e)
Expand Down Expand Up @@ -259,4 +261,27 @@ public static void SetStrokeDashArray(bool showPitchAccentWithDottedLines)
{
StrokeDashArray = showPitchAccentWithDottedLines ? [1, 1] : [1, 0];
}

public static int GetIndexOfListViewItemFromStackPanel(StackPanel stackPanel)
{
return (int)((WrapPanel)stackPanel.Children[0]).Tag;
}

public static bool NoAllDictFilter(object item)
{
if (CoreConfigManager.KanjiMode)
{
return true;
}

Dict dict = (Dict)((StackPanel)item).Tag;
return !dict.Options.NoAll.Value;
}

public static string? GetSelectedDefinitions(TextBox? definitionsTextBox)
{
return definitionsTextBox?.SelectionLength > 0
? definitionsTextBox.SelectedText
: null;
}
}
33 changes: 33 additions & 0 deletions JL.Windows/Utilities/WindowsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,39 @@ public static void CopyTextToClipboard(string? text)
} while (retry);
}

public static void HandlePostCopy(string text, string? subsequentText)
{
bool newText = subsequentText is null;
if (newText)
{
BacklogUtils.AddToBacklog(text);
}
else
{
BacklogUtils.ReplaceLastBacklogText(text);
}

if (ConfigManager.TextToSpeechOnTextChange
&& SpeechSynthesisUtils.InstalledVoiceWithHighestPriority is not null)
{
_ = SpeechSynthesisUtils.TextToSpeech(SpeechSynthesisUtils.InstalledVoiceWithHighestPriority, text).ConfigureAwait(false);
}

string strippedText = ConfigManager.StripPunctuationBeforeCalculatingCharacterCount
? JapaneseUtils.RemovePunctuation(subsequentText ?? text)
: subsequentText ?? text;

if (strippedText.Length > 0)
{
Stats.IncrementStat(StatType.Characters, new StringInfo(strippedText).LengthInTextElements);

if (newText)
{
Stats.IncrementStat(StatType.Lines);
}
}
}

public static void UpdateMainWindowVisibility()
{
if (!MainWindow.Instance.FirstPopupWindow.IsVisible)
Expand Down

0 comments on commit 1d48b56

Please sign in to comment.