Skip to content

Commit

Permalink
Stop stats timer if JL can't connect to specified WebSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Jan 21, 2024
1 parent b59dbaf commit 7bcf408
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions JL.Core/CoreConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static class CoreConfig
public static bool ForceSyncAnki { get; set; } = false;
public static bool AllowDuplicateCards { get; set; } = false;
public static int LookupRate { get; set; } = 0;
public static bool CaptureTextFromClipboard { get; set; } = true;
public static bool CaptureTextFromWebSocket { get; set; } = false;
public static Uri WebSocketUri { get; set; } = new("ws://127.0.0.1:6677");
public static int AudioVolume { get; set; } = 100;
Expand Down
14 changes: 14 additions & 0 deletions JL.Core/Network/WebSocketUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Net.WebSockets;
using System.Text;
using JL.Core.Statistics;
using JL.Core.Utilities;

namespace JL.Core.Network;
Expand Down Expand Up @@ -70,15 +71,28 @@ private static void ListenWebSocket(CancellationToken cancellationToken)
}
catch (WebSocketException webSocketException)
{
if (!CoreConfig.CaptureTextFromClipboard)
{
StatsUtils.StatsStopWatch.Stop();
StatsUtils.StopStatsTimer();
}

Utils.Logger.Warning(webSocketException, "WebSocket server is closed unexpectedly");
Utils.Frontend.Alert(AlertLevel.Error, "WebSocket server is closed");

break;
}
}
}

catch (WebSocketException webSocketException)
{
if (!CoreConfig.CaptureTextFromClipboard)
{
StatsUtils.StatsStopWatch.Stop();
StatsUtils.StopStatsTimer();
}

Utils.Logger.Warning(webSocketException, "Couldn't connect to the WebSocket server, probably because it is not running");
Utils.Frontend.Alert(AlertLevel.Error, "Couldn't connect to the WebSocket server, probably because it is not running");
}
Expand Down
11 changes: 5 additions & 6 deletions JL.Windows/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ internal static class ConfigManager
public static bool TextBoxRemoveNewlines { get; private set; } = false;
public static bool TextBoxIsReadOnly { get; set; } = true;
private static bool TextBoxApplyDropShadowEffect { get; set; } = true;
public static bool CaptureTextFromClipboard { get; set; } = true;
public static bool OnlyCaptureTextWithJapaneseChars { get; private set; } = true;
public static bool DisableLookupsForNonJapaneseCharsInMainWindow { get; private set; } = false;
public static bool MainWindowFocusOnHover { get; private set; } = false;
Expand Down Expand Up @@ -223,8 +222,8 @@ public static void ApplyPreferences()

MainWindow mainWindow = MainWindow.Instance;

CaptureTextFromClipboard = GetValueFromConfig(config, CaptureTextFromClipboard, nameof(CaptureTextFromClipboard), bool.TryParse);
if (CaptureTextFromClipboard)
CoreConfig.CaptureTextFromClipboard = GetValueFromConfig(config, CoreConfig.CaptureTextFromClipboard, nameof(CoreConfig.CaptureTextFromClipboard), bool.TryParse);
if (CoreConfig.CaptureTextFromClipboard)
{
WinApi.SubscribeToClipboardChanged(mainWindow.WindowHandle);
}
Expand All @@ -234,7 +233,7 @@ public static void ApplyPreferences()
}

CoreConfig.CaptureTextFromWebSocket = GetValueFromConfig(config, CoreConfig.CaptureTextFromWebSocket, nameof(CoreConfig.CaptureTextFromWebSocket), bool.TryParse);
if (!CoreConfig.CaptureTextFromWebSocket && !CaptureTextFromClipboard)
if (!CoreConfig.CaptureTextFromWebSocket && !CoreConfig.CaptureTextFromClipboard)
{
StatsUtils.StatsStopWatch.Stop();
StatsUtils.StopStatsTimer();
Expand Down Expand Up @@ -855,7 +854,7 @@ public static void LoadPreferences(PreferencesWindow preferenceWindow)
preferenceWindow.TextBoxTrimWhiteSpaceCharactersCheckBox.IsChecked = TextBoxTrimWhiteSpaceCharacters;
preferenceWindow.TextBoxRemoveNewlinesCheckBox.IsChecked = TextBoxRemoveNewlines;
preferenceWindow.TextBoxApplyDropShadowEffectCheckBox.IsChecked = TextBoxApplyDropShadowEffect;
preferenceWindow.CaptureTextFromClipboardCheckBox.IsChecked = CaptureTextFromClipboard;
preferenceWindow.CaptureTextFromClipboardCheckBox.IsChecked = CoreConfig.CaptureTextFromClipboard;
preferenceWindow.CaptureTextFromWebSocketCheckBox.IsChecked = CoreConfig.CaptureTextFromWebSocket;
preferenceWindow.OnlyCaptureTextWithJapaneseCharsCheckBox.IsChecked = OnlyCaptureTextWithJapaneseChars;
preferenceWindow.DisableLookupsForNonJapaneseCharsInMainWindowCheckBox.IsChecked = DisableLookupsForNonJapaneseCharsInMainWindow;
Expand Down Expand Up @@ -1059,7 +1058,7 @@ public static async Task SavePreferences(PreferencesWindow preferenceWindow)
preferenceWindow.TextBoxApplyDropShadowEffectCheckBox.IsChecked.ToString();


settings[nameof(CaptureTextFromClipboard)].Value =
settings[nameof(CoreConfig.CaptureTextFromClipboard)].Value =
preferenceWindow.CaptureTextFromClipboardCheckBox.IsChecked.ToString();
settings[nameof(CoreConfig.CaptureTextFromWebSocket)].Value =
preferenceWindow.CaptureTextFromWebSocketCheckBox.IsChecked.ToString();
Expand Down
21 changes: 14 additions & 7 deletions JL.Windows/GUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected override async void OnSourceInitialized(EventArgs e)
await StatsUtils.DeserializeLifetimeStats().ConfigureAwait(true);
await StatsUtils.DeserializeProfileLifetimeStats().ConfigureAwait(true);

if (ConfigManager.CaptureTextFromClipboard)
if (CoreConfig.CaptureTextFromClipboard)
{
s_clipboardSequenceNo = WinApi.GetClipboardSequenceNo();
CopyFromClipboard();
Expand Down Expand Up @@ -675,8 +675,8 @@ public async Task HandleHotKey(KeyGesture keyGesture, KeyEventArgs? e)
{
handled = true;

ConfigManager.CaptureTextFromClipboard = !ConfigManager.CaptureTextFromClipboard;
if (ConfigManager.CaptureTextFromClipboard)
CoreConfig.CaptureTextFromClipboard = !CoreConfig.CaptureTextFromClipboard;
if (CoreConfig.CaptureTextFromClipboard)
{
WinApi.SubscribeToClipboardChanged(WindowHandle);
}
Expand All @@ -685,12 +685,12 @@ public async Task HandleHotKey(KeyGesture keyGesture, KeyEventArgs? e)
WinApi.UnsubscribeFromClipboardChanged(WindowHandle);
}

if (!CoreConfig.CaptureTextFromWebSocket && !ConfigManager.CaptureTextFromClipboard)
if (!CoreConfig.CaptureTextFromWebSocket && !CoreConfig.CaptureTextFromClipboard)
{
StatsUtils.StatsStopWatch.Stop();
StatsUtils.StopStatsTimer();
}
else
else if (!ConfigManager.StopIncreasingTimeStatWhenMinimized || WindowState is not WindowState.Minimized)
{
StatsUtils.StatsStopWatch.Start();
StatsUtils.StartStatsTimer();
Expand All @@ -704,12 +704,12 @@ public async Task HandleHotKey(KeyGesture keyGesture, KeyEventArgs? e)
CoreConfig.CaptureTextFromWebSocket = !CoreConfig.CaptureTextFromWebSocket;
WebSocketUtils.HandleWebSocket();

if (!CoreConfig.CaptureTextFromWebSocket && !ConfigManager.CaptureTextFromClipboard)
if (!CoreConfig.CaptureTextFromWebSocket && !CoreConfig.CaptureTextFromClipboard)
{
StatsUtils.StatsStopWatch.Stop();
StatsUtils.StopStatsTimer();
}
else
else if (!ConfigManager.StopIncreasingTimeStatWhenMinimized || WindowState is not WindowState.Minimized)
{
StatsUtils.StatsStopWatch.Start();
StatsUtils.StartStatsTimer();
Expand All @@ -721,6 +721,13 @@ public async Task HandleHotKey(KeyGesture keyGesture, KeyEventArgs? e)
handled = true;

CoreConfig.CaptureTextFromWebSocket = true;

if (!StatsUtils.StatsStopWatch.IsRunning)
{
StatsUtils.StatsStopWatch.Start();
StatsUtils.StartStatsTimer();
}

WebSocketUtils.HandleWebSocket();
}

Expand Down

0 comments on commit 7bcf408

Please sign in to comment.