Skip to content

Commit 7a6fe4a

Browse files
SakiRinnA-nony-mous
authored andcommitted
Merge pull request #269 from jrkasprzyk/feat/caption-font-size
Add adjustable caption font sizes on main window (Ctrl+Scroll)
2 parents 794f442 + a05a4d5 commit 7a6fe4a

3 files changed

Lines changed: 59 additions & 29 deletions

File tree

src/models/WindowState.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class MainWindowState : INotifyPropertyChanged
1111
private bool topmost = true;
1212
private bool captionLogEnabled = false;
1313
private bool latencyShow = false;
14+
private int originalFontSize = 15;
15+
private int translatedFontSize = 18;
1416

1517
public bool Topmost
1618
{
@@ -39,6 +41,24 @@ public bool LatencyShow
3941
OnPropertyChanged("LatencyShow");
4042
}
4143
}
44+
public int OriginalFontSize
45+
{
46+
get => originalFontSize;
47+
set
48+
{
49+
originalFontSize = value;
50+
OnPropertyChanged("OriginalFontSize");
51+
}
52+
}
53+
public int TranslatedFontSize
54+
{
55+
get => translatedFontSize;
56+
set
57+
{
58+
translatedFontSize = value;
59+
OnPropertyChanged("TranslatedFontSize");
60+
}
61+
}
4262

4363
public void OnPropertyChanged([CallerMemberName] string propName = "")
4464
{

src/pages/CaptionPage.xaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
<ui:Card
2727
Grid.Row="0"
2828
Padding="8"
29-
VerticalAlignment="Stretch">
29+
VerticalAlignment="Stretch"
30+
PreviewMouseWheel="OriginalCard_PreviewMouseWheel">
3031
<TextBlock
3132
x:Name="OriginalCaption"
3233
Background="Transparent"
@@ -35,15 +36,16 @@
3536
MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"
3637
Style="{StaticResource CaptionBlockStyle}"
3738
Text="{Binding DisplayOriginalCaption, UpdateSourceTrigger=PropertyChanged}"
38-
ToolTip="Click To Copy" />
39+
ToolTip="Click to Copy, Ctrl+Scroll to Resize Font" />
3940
</ui:Card>
4041

4142
<ui:Card
4243
Grid.Row="1"
4344
MinHeight="43"
4445
Margin="0,3,0,0"
4546
Padding="8"
46-
VerticalAlignment="Stretch">
47+
VerticalAlignment="Stretch"
48+
PreviewMouseWheel="TranslatedCard_PreviewMouseWheel">
4749
<TextBlock
4850
x:Name="TranslatedCaption"
4951
Background="Transparent"
@@ -52,7 +54,7 @@
5254
MouseLeftButtonDown="TextBlock_MouseLeftButtonDown"
5355
Style="{StaticResource CaptionBlockStyle}"
5456
Text="{Binding DisplayTranslatedCaption, UpdateSourceTrigger=PropertyChanged}"
55-
ToolTip="Click To Copy" />
57+
ToolTip="Click to Copy, Ctrl+Scroll to Resize Font" />
5658
</ui:Card>
5759

5860
<ui:Card

src/pages/CaptionPage.xaml.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System.ComponentModel;
2-
using System.Text;
3-
using System.Windows;
1+
using System.Windows;
42
using System.Windows.Controls;
5-
using System.Windows.Threading;
3+
using System.Windows.Input;
64

7-
using LiveCaptionsTranslator.utils;
5+
using LiveCaptionsTranslator.Utils;
86

97
namespace LiveCaptionsTranslator
108
{
@@ -25,15 +23,14 @@ public CaptionPage()
2523
{
2624
AutoHeight();
2725
(App.Current.MainWindow as MainWindow).CaptionLogButton.Visibility = Visibility.Visible;
28-
Translator.Caption.PropertyChanged += TranslatedChanged;
2926
};
3027
Unloaded += (s, e) =>
3128
{
3229
(App.Current.MainWindow as MainWindow).CaptionLogButton.Visibility = Visibility.Collapsed;
33-
Translator.Caption.PropertyChanged -= TranslatedChanged;
3430
};
3531

3632
CollapseTranslatedCaption(Translator.Setting.MainWindow.CaptionLogEnabled);
33+
ApplyFontSizes();
3734
}
3835

3936
private async void TextBlock_MouseLeftButtonDown(object sender, RoutedEventArgs e)
@@ -53,25 +50,36 @@ private async void TextBlock_MouseLeftButtonDown(object sender, RoutedEventArgs
5350
}
5451
}
5552

56-
private void TranslatedChanged(object sender, PropertyChangedEventArgs e)
53+
private void ApplyFontSizes()
5754
{
58-
if (e.PropertyName == nameof(Translator.Caption.DisplayTranslatedCaption))
59-
{
60-
if (Encoding.UTF8.GetByteCount(Translator.Caption.DisplayTranslatedCaption) >= TextUtil.LONG_THRESHOLD)
61-
{
62-
Dispatcher.BeginInvoke(new Action(() =>
63-
{
64-
this.TranslatedCaption.FontSize = 15;
65-
}), DispatcherPriority.Background);
66-
}
67-
else
68-
{
69-
Dispatcher.BeginInvoke(new Action(() =>
70-
{
71-
this.TranslatedCaption.FontSize = 18;
72-
}), DispatcherPriority.Background);
73-
}
74-
}
55+
OriginalCaption.FontSize = Translator.Setting.MainWindow.OriginalFontSize;
56+
TranslatedCaption.FontSize = Translator.Setting.MainWindow.TranslatedFontSize;
57+
}
58+
59+
private void OriginalCard_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
60+
{
61+
if (Keyboard.Modifiers != ModifierKeys.Control)
62+
return;
63+
Translator.Setting.MainWindow.OriginalFontSize =
64+
AdjustFontSize(Translator.Setting.MainWindow.OriginalFontSize, e.Delta);
65+
ApplyFontSizes();
66+
e.Handled = true;
67+
}
68+
69+
private void TranslatedCard_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
70+
{
71+
if (Keyboard.Modifiers != ModifierKeys.Control)
72+
return;
73+
Translator.Setting.MainWindow.TranslatedFontSize =
74+
AdjustFontSize(Translator.Setting.MainWindow.TranslatedFontSize, e.Delta);
75+
ApplyFontSizes();
76+
e.Handled = true;
77+
}
78+
79+
private static int AdjustFontSize(int current, int wheelDelta)
80+
{
81+
int next = current + (wheelDelta > 0 ? StyleConsts.DELTA_FONT_SIZE : -StyleConsts.DELTA_FONT_SIZE);
82+
return Math.Clamp(next, StyleConsts.MIN_FONT_SIZE, StyleConsts.MAX_FONT_SIZE);
7583
}
7684

7785
public void CollapseTranslatedCaption(bool isCollapsed)

0 commit comments

Comments
 (0)