Skip to content

Commit 49ee233

Browse files
committed
refactor: streamline context getter and improve Snackbar notifications
1 parent b5625c2 commit 49ee233

File tree

7 files changed

+61
-56
lines changed

7 files changed

+61
-56
lines changed

src/Translator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ public static async Task Log(string originalText, string translatedText,
295295
}
296296
catch (Exception ex)
297297
{
298-
SnackbarHost.Show("Error!", $"Logging history failed: {ex.Message}", "error", 2);
298+
SnackbarHost.Show("[ERROR] Logging history failed.", ex.Message, SnackbarType.Error,
299+
timeout: 2, closeButton: true);
299300
}
300301
}
301302

@@ -314,7 +315,8 @@ public static async Task LogOnly(string originalText,
314315
}
315316
catch (Exception ex)
316317
{
317-
SnackbarHost.Show("Error!", $"Logging history failed: {ex.Message}", "error", 2);
318+
SnackbarHost.Show("[ERROR] Logging history failed.", ex.Message, SnackbarType.Error,
319+
timeout: 2, closeButton: true);
318320
}
319321
}
320322

src/controls/SnackbarHost.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@ class SnackbarHost
77
public static Snackbar? mainSnackbar;
88
public static MainWindow? mainWindow = (MainWindow)App.Current.MainWindow;
99

10-
public static void Show(string title = "", string message = "", string type = "info",
11-
int timeout = 5, int width = 500, bool closeButton = true)
10+
public static void Show(string title = "", string message = "", SnackbarType type = SnackbarType.Info,
11+
int width = 500, int timeout = 1, bool closeButton = false)
1212
{
1313
ControlAppearance appearance;
1414
SymbolIcon icon;
1515
Snackbar? snackbar;
1616

1717
switch (type)
1818
{
19-
case "warning":
19+
case SnackbarType.Warning:
2020
appearance = ControlAppearance.Caution;
2121
icon = new SymbolIcon(SymbolRegular.Alert24);
2222
break;
23-
case "success":
24-
appearance = ControlAppearance.Success;
25-
icon = new SymbolIcon(SymbolRegular.CheckmarkCircle24);
26-
break;
27-
case "error":
23+
case SnackbarType.Error:
2824
appearance = ControlAppearance.Danger;
2925
icon = new SymbolIcon(SymbolRegular.DismissCircle24);
3026
break;
27+
case SnackbarType.Success:
28+
appearance = ControlAppearance.Success;
29+
icon = new SymbolIcon(SymbolRegular.CheckmarkCircle24);
30+
break;
3131
default:
3232
appearance = ControlAppearance.Secondary;
3333
icon = new SymbolIcon(SymbolRegular.Info24);
@@ -49,4 +49,12 @@ public static void Show(string title = "", string message = "", string type = "i
4949
snackbar.Show(true);
5050
}
5151
}
52+
53+
public enum SnackbarType
54+
{
55+
Warning,
56+
Error,
57+
Success,
58+
Info
59+
}
5260
}

src/models/Caption.cs

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ public class Caption : INotifyPropertyChanged
2323
public string TranslatedCaption { get; set; } = string.Empty;
2424

2525
public Queue<TranslationHistoryEntry> Contexts { get; } = new(MAX_CONTEXTS);
26-
27-
public IEnumerable<TranslationHistoryEntry> AwareContexts => Contexts.Reverse().Take(
28-
Math.Min(Translator.Setting.NumContexts, Contexts.Count)).Reverse();
29-
public string AwareContextsCaption => GetPreviousCaption(
30-
Math.Min(Translator.Setting.NumContexts, Contexts.Count));
3126

32-
public IEnumerable<TranslationHistoryEntry> DisplayLogCards => Contexts.Reverse().Take(
33-
Math.Min(Translator.Setting.DisplaySentences, Contexts.Count));
27+
public IEnumerable<TranslationHistoryEntry> AwareContexts => GetPreviousContexts(Translator.Setting.NumContexts);
28+
public string AwareContextsCaption => GetPreviousText(Translator.Setting.NumContexts, TextType.Caption);
29+
30+
public IEnumerable<TranslationHistoryEntry> DisplayLogCards =>
31+
GetPreviousContexts(Translator.Setting.DisplaySentences).Reverse();
3432

3533
public string DisplayOriginalCaption
3634
{
@@ -78,8 +76,9 @@ public string OverlayCurrentTranslation
7876
OnPropertyChanged("OverlayCurrentTranslation");
7977
}
8078
}
81-
public string OverlayPreviousTranslation => GetPreviousTranslation(
82-
Math.Min(Translator.Setting.DisplaySentences, Contexts.Count));
79+
80+
public string OverlayPreviousTranslation =>
81+
GetPreviousText(Translator.Setting.DisplaySentences, TextType.Translation);
8382

8483
private Caption()
8584
{
@@ -93,14 +92,16 @@ public static Caption GetInstance()
9392
return instance;
9493
}
9594

96-
public string GetPreviousCaption(int count)
95+
public string GetPreviousText(int count, TextType textType)
9796
{
9897
if (count <= 0)
9998
return string.Empty;
10099

101100
var prev = Contexts
102101
.Reverse().Take(count).Reverse()
103-
.Select(entry => entry.SourceText)
102+
.Select(entry => string.CompareOrdinal(entry.TranslatedText, "N/A") == 0 ||
103+
entry.TranslatedText.Contains("[ERROR]") || entry.TranslatedText.Contains("[WARNING]") ?
104+
"" : (textType == TextType.Caption ? entry.SourceText : entry.TranslatedText))
104105
.Aggregate((accu, cur) =>
105106
{
106107
if (!string.IsNullOrEmpty(accu))
@@ -114,46 +115,36 @@ public string GetPreviousCaption(int count)
114115
return accu + cur;
115116
});
116117

118+
if (textType == TextType.Translation)
119+
prev = RegexPatterns.NoticePrefix().Replace(prev, "");
117120
if (!string.IsNullOrEmpty(prev) && Array.IndexOf(TextUtil.PUNC_EOS, prev[^1]) == -1)
118121
prev += TextUtil.isCJChar(prev[^1]) ? "。" : ".";
119122
if (!string.IsNullOrEmpty(prev) && Encoding.UTF8.GetByteCount(prev[^1].ToString()) < 2)
120123
prev += " ";
121124
return prev;
122125
}
123126

124-
public string GetPreviousTranslation(int count)
127+
public IEnumerable<TranslationHistoryEntry> GetPreviousContexts(int count)
125128
{
126129
if (count <= 0)
127-
return string.Empty;
130+
return Enumerable.Empty<TranslationHistoryEntry>();
128131

129-
var prev = Contexts
132+
return Contexts
130133
.Reverse().Take(count).Reverse()
131-
.Select(entry => entry.TranslatedText.Contains("[ERROR]") || entry.TranslatedText.Contains("[WARNING]") ?
132-
"" : entry.TranslatedText)
133-
.Aggregate((accu, cur) =>
134-
{
135-
if (!string.IsNullOrEmpty(accu))
136-
{
137-
if (Array.IndexOf(TextUtil.PUNC_EOS, accu[^1]) == -1)
138-
accu += TextUtil.isCJChar(accu[^1]) ? "。" : ". ";
139-
else
140-
accu += TextUtil.isCJChar(accu[^1]) ? "" : " ";
141-
}
142-
cur = RegexPatterns.NoticePrefix().Replace(cur, "");
143-
return accu + cur;
144-
});
145-
146-
prev = RegexPatterns.NoticePrefix().Replace(prev, "");
147-
if (!string.IsNullOrEmpty(prev) && Array.IndexOf(TextUtil.PUNC_EOS, prev[^1]) == -1)
148-
prev += TextUtil.isCJChar(prev[^1]) ? "。" : ".";
149-
if (!string.IsNullOrEmpty(prev) && Encoding.UTF8.GetByteCount(prev[^1].ToString()) < 2)
150-
prev += " ";
151-
return prev;
134+
.Where(entry => string.CompareOrdinal(entry.TranslatedText, "N/A") != 0 &&
135+
!entry.TranslatedText.Contains("[ERROR]") &&
136+
!entry.TranslatedText.Contains("[WARNING]"));
152137
}
153138

154139
public void OnPropertyChanged([CallerMemberName] string propName = "")
155140
{
156141
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
157142
}
158143
}
144+
145+
public enum TextType
146+
{
147+
Caption,
148+
Translation
149+
}
159150
}

src/pages/CaptionPage.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ private async void TextBlock_MouseLeftButtonDown(object sender, RoutedEventArgs
4343
try
4444
{
4545
Clipboard.SetText(textBlock.Text);
46-
SnackbarHost.Show("Copied", textBlock.Text, "info", 1, 100, false);
46+
SnackbarHost.Show("Copied.", textBlock.Text, SnackbarType.Info, 100);
4747
}
4848
catch
4949
{
50-
SnackbarHost.Show(title: "Copy Failed", type: "error");
50+
SnackbarHost.Show("Copy Failed.", string.Empty, SnackbarType.Error, 100);
5151
}
5252
await Task.Delay(500);
5353
}

src/pages/HistoryPage.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ private async void Export_click(object sender, RoutedEventArgs e)
127127
try
128128
{
129129
await SQLiteHistoryLogger.ExportToCSV(saveFileDialog.FileName);
130-
SnackbarHost.Show("Saved Success", $"File saved to: {saveFileDialog.FileName}");
130+
SnackbarHost.Show("Saved Success.", $"File saved to: {saveFileDialog.FileName}", SnackbarType.Success);
131131
}
132132
catch (Exception ex)
133133
{
134-
SnackbarHost.Show("Save Failed", $"File saved faild:{ex.Message}", "error");
134+
SnackbarHost.Show("Save Failed.", $"File saved faild:{ex.Message}", SnackbarType.Error);
135135
}
136136
}
137137
}

src/windows/MainWindow.xaml.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private void LogOnlyButton_Click(object sender, RoutedEventArgs e)
119119
symbolIcon.Filled = true;
120120
}
121121

122-
Translator.Caption.Contexts.Clear();
122+
Translator.ClearContexts();
123123
}
124124

125125
private void CaptionLogButton_Click(object sender, RoutedEventArgs e)
@@ -180,7 +180,8 @@ private async Task CheckForUpdates()
180180
}
181181
catch (Exception ex)
182182
{
183-
SnackbarHost.Show("[ERROR] Update Check Failed.", ex.Message, "error");
183+
SnackbarHost.Show("[ERROR] Update Check Failed.", ex.Message, SnackbarType.Error,
184+
timeout: 2, closeButton: true);
184185

185186
return;
186187
}
@@ -215,7 +216,8 @@ private async Task CheckForUpdates()
215216
}
216217
catch (Exception ex)
217218
{
218-
SnackbarHost.Show("[ERROR] Open Browser Failed.", ex.Message, "error");
219+
SnackbarHost.Show("[ERROR] Open Browser Failed.", ex.Message, SnackbarType.Error,
220+
timeout: 2, closeButton: true);
219221
}
220222
}
221223
else

src/windows/OverlayWindow.xaml.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,14 @@ public void ApplyBackgroundOpacity()
357357
(byte)Translator.Setting.OverlayWindow.Opacity, color.R, color.G, color.B));
358358
}
359359

360-
private void UpdateTranslationColor(SolidColorBrush brush, double brightRatio = 0.4)
360+
private void UpdateTranslationColor(SolidColorBrush brush)
361361
{
362362
var color = brush.Color;
363-
byte r = (byte)Math.Min(color.R + (255 - color.R) * brightRatio, 255);
364-
byte g = (byte)Math.Min(color.G + (255 - color.G) * brightRatio, 255);
365-
byte b = (byte)Math.Min(color.B + (255 - color.B) * brightRatio, 255);
363+
364+
double target = 0.299 * color.R + 0.587 * color.G + 0.114 * color.B > 127 ? 0 : 255;
365+
byte r = (byte)Math.Clamp(color.R + (target - color.R) * 0.3, 0, 255);
366+
byte g = (byte)Math.Clamp(color.G + (target - color.G) * 0.4, 0, 255);
367+
byte b = (byte)Math.Clamp(color.B + (target - color.B) * 0.3, 0, 255);
366368

367369
NoticePrefixRun.Foreground = brush;
368370
PreviousTranslationRun.Foreground = brush;

0 commit comments

Comments
 (0)