Skip to content

Commit

Permalink
Add "Reposition window to fixed right edge position on text change" a…
Browse files Browse the repository at this point in the history
…nd "Fixed right edge position" options

Add "Reposition window to fixed bottom position on text change" and "Fixed bottom position" options
Allow resetting settings without restarting JL
  • Loading branch information
rampaa committed Nov 17, 2024
1 parent 4218ed3 commit 7168db4
Show file tree
Hide file tree
Showing 27 changed files with 1,093 additions and 840 deletions.
41 changes: 26 additions & 15 deletions JL.Core/Config/CoreConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,34 @@

namespace JL.Core.Config;

public static class CoreConfigManager
public sealed class CoreConfigManager
{
public static Uri AnkiConnectUri { get; set; } = new("http://127.0.0.1:8765");
public static bool KanjiMode { get; set; } // = false;
public static bool AnkiIntegration { get; set; } // = false;
public static bool ForceSyncAnki { get; private set; } // = false;
public static bool AllowDuplicateCards { get; private set; } // = false;
public static double LookupRate { get; private set; } // = 0;
public static bool CaptureTextFromClipboard { get; set; } = true;
public static bool CaptureTextFromWebSocket { get; set; } // = false;
public static bool AutoReconnectToWebSocket { get; private set; } // = false;
public static bool TextBoxTrimWhiteSpaceCharacters { get; private set; } = true;
public static bool TextBoxRemoveNewlines { get; private set; } // = false;
public static Uri WebSocketUri { get; private set; } = new("ws://127.0.0.1:6677");
public static bool CheckForJLUpdatesOnStartUp { get; private set; } = true;
public static CoreConfigManager Instance { get; private set; } = new();

public static void ApplyPreferences(SqliteConnection connection)
public Uri AnkiConnectUri { get; set; } = new("http://127.0.0.1:8765");
public bool KanjiMode { get; set; } // = false;
public bool AnkiIntegration { get; set; } // = false;
public bool ForceSyncAnki { get; private set; } // = false;
public bool AllowDuplicateCards { get; private set; } // = false;
public double LookupRate { get; private set; } // = 0;
public bool CaptureTextFromClipboard { get; set; } = true;
public bool CaptureTextFromWebSocket { get; set; } // = false;
public bool AutoReconnectToWebSocket { get; private set; } // = false;
public bool TextBoxTrimWhiteSpaceCharacters { get; private set; } = true;
public bool TextBoxRemoveNewlines { get; private set; } // = false;
public Uri WebSocketUri { get; private set; } = new("ws://127.0.0.1:6677");
public bool CheckForJLUpdatesOnStartUp { get; private set; } = true;

private CoreConfigManager()
{
}

public static void CreateNewCoreConfigManager()
{
Instance = new CoreConfigManager();
}

public void ApplyPreferences(SqliteConnection connection)
{
Utils.s_loggingLevelSwitch.MinimumLevel = ConfigDBManager.GetValueFromConfig(connection, LogEventLevel.Error, "MinimumLogLevel", Enum.TryParse);

Expand Down
7 changes: 4 additions & 3 deletions JL.Core/Lookup/LookupUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public static class LookupUtils

public static List<LookupResult>? LookupText(string text)
{
CoreConfigManager coreConfigManager = CoreConfigManager.Instance;
DateTime preciseTimeNow = new(Stopwatch.GetTimestamp());
if ((preciseTimeNow - s_lastLookupTime).TotalMilliseconds < CoreConfigManager.LookupRate)
if ((preciseTimeNow - s_lastLookupTime).TotalMilliseconds < coreConfigManager.LookupRate)
{
return null;
}
Expand All @@ -55,7 +56,7 @@ public static class LookupUtils
.ToList();

kanji = text.EnumerateRunes().First().ToString();
if (CoreConfigManager.KanjiMode)
if (coreConfigManager.KanjiMode)
{
_ = Parallel.ForEach(DictUtils.Dicts.Values.ToList(), dict =>
{
Expand Down Expand Up @@ -120,7 +121,7 @@ public static class LookupUtils
}
}

else if (CoreConfigManager.KanjiMode)
else if (coreConfigManager.KanjiMode)
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Mining/Anki/AnkiConnect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static async Task Sync()
Utils.Logger.Information("Sending: {Payload}", await payload.ReadAsStringAsync().ConfigureAwait(false));

using HttpResponseMessage postResponse = await Networking.Client
.PostAsync(CoreConfigManager.AnkiConnectUri, payload).ConfigureAwait(false);
.PostAsync(CoreConfigManager.Instance.AnkiConnectUri, payload).ConfigureAwait(false);

if (!postResponse.IsSuccessStatusCode)
{
Expand Down
9 changes: 5 additions & 4 deletions JL.Core/Mining/MiningUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ public static async Task MineToFile(LookupResult lookupResult, string currentTex

public static async Task Mine(LookupResult lookupResult, string currentText, string? formattedDefinitions, string? selectedDefinitions, int currentCharPosition)
{
if (!CoreConfigManager.AnkiIntegration)
CoreConfigManager coreConfigManager = CoreConfigManager.Instance;
if (!coreConfigManager.AnkiIntegration)
{
Utils.Frontend.Alert(AlertLevel.Error, "Please setup mining first in the preferences");
return;
Expand Down Expand Up @@ -486,7 +487,7 @@ public static async Task Mine(LookupResult lookupResult, string currentText, str
// Audio/Picture/Video shouldn't be set here
// Otherwise AnkiConnect will place them under the "collection.media" folder even when it's a duplicate note
Note note = new(ankiConfig.DeckName, ankiConfig.ModelName, fields, ankiConfig.Tags, null, null, null, null);
if (!CoreConfigManager.AllowDuplicateCards)
if (!coreConfigManager.AllowDuplicateCards)
{
bool? canAddNote = await AnkiUtils.CanAddNote(note).ConfigureAwait(false);
if (canAddNote is null)
Expand Down Expand Up @@ -563,7 +564,7 @@ public static async Task Mine(LookupResult lookupResult, string currentText, str
note.Options = new Dictionary<string, object>(1, StringComparer.Ordinal)
{
{
"allowDuplicate", CoreConfigManager.AllowDuplicateCards
"allowDuplicate", coreConfigManager.AllowDuplicateCards
}
};

Expand All @@ -587,7 +588,7 @@ public static async Task Mine(LookupResult lookupResult, string currentText, str
Utils.Logger.Information("Mined {PrimarySpelling}", lookupResult.PrimarySpelling);
}

if (CoreConfigManager.ForceSyncAnki)
if (coreConfigManager.ForceSyncAnki)
{
await AnkiConnect.Sync().ConfigureAwait(false);
}
Expand Down
2 changes: 1 addition & 1 deletion JL.Core/Network/Networking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private static async void CheckForUpdates(object? sender, ElapsedEventArgs e)
{
await DictUpdater.AutoUpdateBuiltInDicts().ConfigureAwait(false);

if (CoreConfigManager.CheckForJLUpdatesOnStartUp)
if (CoreConfigManager.Instance.CheckForJLUpdatesOnStartUp)
{
await CheckForJLUpdates(true).ConfigureAwait(false);
}
Expand Down
35 changes: 16 additions & 19 deletions JL.Core/Network/WebSocketUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,42 @@ public static class WebSocketUtils

public static void HandleWebSocket()
{
if (!CoreConfigManager.CaptureTextFromWebSocket)
s_webSocketCancellationTokenSource?.Cancel();
s_webSocketCancellationTokenSource?.Dispose();
s_webSocketCancellationTokenSource = null;

if (!CoreConfigManager.Instance.CaptureTextFromWebSocket)
{
s_webSocketTask = null;
}
else if (s_webSocketTask is null)
{
s_webSocketCancellationTokenSource?.Dispose();
s_webSocketCancellationTokenSource = new CancellationTokenSource();
ListenWebSocket(s_webSocketCancellationTokenSource.Token);
}
else
{
s_webSocketCancellationTokenSource!.Cancel();
s_webSocketCancellationTokenSource.Dispose();
s_webSocketCancellationTokenSource = new CancellationTokenSource();
ListenWebSocket(s_webSocketCancellationTokenSource.Token);
}
}

private static void ListenWebSocket(CancellationToken cancellationToken)
{
CoreConfigManager coreConfigManager = CoreConfigManager.Instance;
s_webSocketTask = Task.Run(async () =>
{
do
{
try
{
using ClientWebSocket webSocketClient = new();
await webSocketClient.ConnectAsync(CoreConfigManager.WebSocketUri, CancellationToken.None).ConfigureAwait(false);
await webSocketClient.ConnectAsync(coreConfigManager.WebSocketUri, CancellationToken.None).ConfigureAwait(false);

// 256-4096
Memory<byte> buffer = new byte[1024 * 4];

while (CoreConfigManager.CaptureTextFromWebSocket && !cancellationToken.IsCancellationRequested && webSocketClient.State is WebSocketState.Open)
while (coreConfigManager.CaptureTextFromWebSocket && !cancellationToken.IsCancellationRequested && webSocketClient.State is WebSocketState.Open)
{
try
{
ValueWebSocketReceiveResult result = await webSocketClient.ReceiveAsync(buffer, CancellationToken.None).ConfigureAwait(false);
if (!CoreConfigManager.CaptureTextFromWebSocket || cancellationToken.IsCancellationRequested)
if (!coreConfigManager.CaptureTextFromWebSocket || cancellationToken.IsCancellationRequested)
{
return;
}
Expand All @@ -79,16 +76,16 @@ private static void ListenWebSocket(CancellationToken cancellationToken)
}
catch (WebSocketException webSocketException)
{
if (!CoreConfigManager.AutoReconnectToWebSocket)
if (!coreConfigManager.AutoReconnectToWebSocket)
{
if (!CoreConfigManager.CaptureTextFromClipboard)
if (!coreConfigManager.CaptureTextFromClipboard)
{
StatsUtils.StatsStopWatch.Stop();
StatsUtils.StopStatsTimer();
}
}

if (CoreConfigManager.CaptureTextFromWebSocket && !cancellationToken.IsCancellationRequested)
if (coreConfigManager.CaptureTextFromWebSocket && !cancellationToken.IsCancellationRequested)
{
Utils.Logger.Warning(webSocketException, "WebSocket server is closed unexpectedly");
Utils.Frontend.Alert(AlertLevel.Error, "WebSocket server is closed");
Expand All @@ -101,15 +98,15 @@ private static void ListenWebSocket(CancellationToken cancellationToken)

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

if (CoreConfigManager.CaptureTextFromWebSocket && !cancellationToken.IsCancellationRequested)
if (coreConfigManager.CaptureTextFromWebSocket && !cancellationToken.IsCancellationRequested)
{
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 All @@ -121,7 +118,7 @@ private static void ListenWebSocket(CancellationToken cancellationToken)
}
}
}
while (CoreConfigManager.AutoReconnectToWebSocket && CoreConfigManager.CaptureTextFromWebSocket && !cancellationToken.IsCancellationRequested);
while (coreConfigManager.AutoReconnectToWebSocket && coreConfigManager.CaptureTextFromWebSocket && !cancellationToken.IsCancellationRequested);
}, cancellationToken);
}
}
5 changes: 3 additions & 2 deletions JL.Core/Utilities/TextUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ public static string SanitizeText(string text)
text = RemoveInvalidUnicodeSequences(text, firstInvalidUnicodeCharIndex);
}

if (CoreConfigManager.TextBoxTrimWhiteSpaceCharacters)
CoreConfigManager coreConfigManager = CoreConfigManager.Instance;
if (coreConfigManager.TextBoxTrimWhiteSpaceCharacters)
{
text = text.Trim();
}

if (CoreConfigManager.TextBoxRemoveNewlines)
if (coreConfigManager.TextBoxRemoveNewlines)
{
text = text.ReplaceLineEndings("");
}
Expand Down
Loading

0 comments on commit 7168db4

Please sign in to comment.