Skip to content

Commit

Permalink
feat: Add a homepage function icon for optional display #16
Browse files Browse the repository at this point in the history
  • Loading branch information
ZGGSONG committed Jan 20, 2024
1 parent c2d5efb commit 5c37003
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 38 deletions.
35 changes: 35 additions & 0 deletions STranslate.Model/ConfigModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,41 @@ public partial class ConfigModel : ObservableObject
/// </summary>
public bool IsKeepTopmostAfterMousehook { get; set; } = false;

/// <summary>
/// 是否显示设置图标
/// </summary>
public bool IsShowPreference { get; set; } = true;

/// <summary>
/// 是否显示切换主题图标
/// </summary>
public bool IsShowSwitchTheme { get; set; } = true;

/// <summary>
/// 是否显示打开鼠标划词图标
/// </summary>
public bool IsShowMousehook { get; set; } = false;

/// <summary>
/// 是否显示截图翻译图标
/// </summary>
public bool IsShowScreenshot { get; set; } = false;

/// <summary>
/// 是否显示OCR图标
/// </summary>
public bool IsShowOCR { get; set; } = false;

/// <summary>
/// 是否显示静默OCR图标
/// </summary>
public bool IsShowSilentOCR { get; set; } = false;

/// <summary>
/// 是否显示识别二维码图标
/// </summary>
public bool IsShowQRCode { get; set; } = false;

/// <summary>
/// 服务
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions STranslate/Helper/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using STranslate.Log;
using STranslate.Model;
using STranslate.Util;
using STranslate.ViewModels;
using STranslate.ViewModels.Preference;
using STranslate.ViewModels.Preference.Services;
using System;
Expand Down Expand Up @@ -62,6 +63,9 @@ public void InitialOperate()

//初始化代理设置
ProxyUtil.UpdateDynamicProxy(CurrentConfig?.IsDisableSystemProxy ?? false);

//初始化首页图标
Singleton<MainViewModel>.Instance.UpdateMainViewIcons();
}

/// <summary>
Expand Down Expand Up @@ -161,6 +165,14 @@ public bool WriteConfig(CommonViewModel model)
CurrentConfig.DoubleTapTrayFunc = model.DoubleTapTrayFunc;
CurrentConfig.CustomFont = model.CustomFont;
CurrentConfig.IsKeepTopmostAfterMousehook = model.IsKeepTopmostAfterMousehook;
CurrentConfig.IsShowPreference = model.IsShowPreference;
CurrentConfig.IsShowSwitchTheme = model.IsShowSwitchTheme;
CurrentConfig.IsShowMousehook = model.IsShowMousehook;
CurrentConfig.IsShowScreenshot = model.IsShowScreenshot;
CurrentConfig.IsShowOCR = model.IsShowOCR;
CurrentConfig.IsShowSilentOCR = model.IsShowSilentOCR;
CurrentConfig.IsShowQRCode = model.IsShowQRCode;
Singleton<MainViewModel>.Instance.UpdateMainViewIcons();
WriteConfig(CurrentConfig);
isSuccess = true;
}
Expand Down Expand Up @@ -259,6 +271,13 @@ private ConfigModel InitialConfig()
DoubleTapTrayFunc = DoubleTapFuncEnum.InputFunc,
CustomFont = ConstStr.DEFAULTFONTNAME,
IsKeepTopmostAfterMousehook = false,
IsShowPreference = false,
IsShowSwitchTheme = false,
IsShowMousehook = false,
IsShowScreenshot = false,
IsShowOCR = false,
IsShowSilentOCR = false,
IsShowQRCode = false,
SourceLanguage = LanguageEnum.AUTO.GetDescription(),
TargetLanguage = LanguageEnum.AUTO.GetDescription(),
Services =
Expand Down
37 changes: 37 additions & 0 deletions STranslate/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,42 @@ private void SwitchTheme()
{
Singleton<CommonViewModel>.Instance.IsBright = !Singleton<CommonViewModel>.Instance.IsBright;
}

/// <summary>
/// 更新主界面图标显示
/// </summary>
internal void UpdateMainViewIcons()
{
IsShowPreference = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowPreference ?? false;
IsShowSwitchTheme = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowSwitchTheme ?? false;
IsShowMousehook = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowMousehook ?? false;
IsShowScreenshot = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowScreenshot ?? false;
IsShowOCR = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowOCR ?? false;
IsShowSilentOCR = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowSilentOCR ?? false;
IsShowQRCode = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowQRCode ?? false;
}

#region 显示图标
[ObservableProperty]
private bool isShowPreference;

[ObservableProperty]
private bool isShowSwitchTheme;

[ObservableProperty]
private bool isShowMousehook;

[ObservableProperty]
private bool isShowScreenshot;

[ObservableProperty]
private bool isShowOCR;

[ObservableProperty]
private bool isShowSilentOCR;

[ObservableProperty]
private bool isShowQRCode;
#endregion 显示图标
}
}
37 changes: 22 additions & 15 deletions STranslate/ViewModels/NotifyIconViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,28 @@ internal void SilentOCRHandler()
view.BitmapCallback += (
bitmap =>
{
var bytes = BitmapUtil.ConvertBitmap2Bytes(bitmap);

var getText = Singleton<PaddleOCRHelper>.Instance.Execute(bytes).Trim();

//取词前移除换行
getText =
Singleton<ConfigHelper>.Instance.CurrentConfig?.IsRemoveLineBreakGettingWords ?? false && !string.IsNullOrEmpty(getText)
? StringUtil.RemoveLineBreaks(getText)
: getText;

//写入剪贴板
Clipboard.SetDataObject(getText, true);

var tmp = getText.Length >= 9 ? getText[..9] + "..." : getText;
OnShowBalloonTip?.Invoke($"OCR识别成功: {tmp}");
try
{
var bytes = BitmapUtil.ConvertBitmap2Bytes(bitmap);
var getText = Singleton<PaddleOCRHelper>.Instance.Execute(bytes).Trim();

//取词前移除换行
getText =
Singleton<ConfigHelper>.Instance.CurrentConfig?.IsRemoveLineBreakGettingWords ?? false && !string.IsNullOrEmpty(getText)
? StringUtil.RemoveLineBreaks(getText)
: getText;

//写入剪贴板
Clipboard.SetDataObject(getText, true);

var tmp = getText.Length >= 9 ? getText[..9] + "..." : getText;
OnShowBalloonTip?.Invoke($"OCR识别成功: {tmp}");
}
catch (Exception ex)
{
OnShowBalloonTip?.Invoke($"OCR识别失败: {ex.Message}");
LogService.Logger.Error("静默OCR失败", ex);
}
}
);
}
Expand Down
51 changes: 50 additions & 1 deletion STranslate/ViewModels/Preference/CommonViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ private void Reset()
DoubleTapTrayFunc = Singleton<ConfigHelper>.Instance.CurrentConfig?.DoubleTapTrayFunc ?? DoubleTapFuncEnum.InputFunc;
CustomFont = Singleton<ConfigHelper>.Instance.CurrentConfig?.CustomFont ?? ConstStr.DEFAULTFONTNAME;
IsKeepTopmostAfterMousehook = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsKeepTopmostAfterMousehook ?? false;

IsShowPreference = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowPreference ?? false;
IsShowSwitchTheme = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowSwitchTheme ?? false;
IsShowMousehook = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowMousehook ?? false;
IsShowScreenshot = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowScreenshot ?? false;
IsShowOCR = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowOCR ?? false;
IsShowSilentOCR = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowSilentOCR ?? false;
IsShowQRCode = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowQRCode ?? false;
ToastHelper.Show("重置配置", WindowType.Preference);
if (IsStartup)
{
Expand Down Expand Up @@ -217,5 +223,48 @@ public string CustomFont

[ObservableProperty]
private bool isKeepTopmostAfterMousehook = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsKeepTopmostAfterMousehook ?? false;


/// <summary>
/// 是否显示设置图标
/// </summary>
[ObservableProperty]
private bool isShowPreference = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowPreference ?? false;

/// <summary>
/// 是否显示切换主题图标
/// </summary>
[ObservableProperty]
private bool isShowSwitchTheme = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowSwitchTheme ?? false;

/// <summary>
/// 是否显示打开鼠标划词图标
/// </summary>
[ObservableProperty]
private bool isShowMousehook = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowMousehook ?? false;

/// <summary>
/// 是否显示截图翻译图标
/// </summary>
[ObservableProperty]
private bool isShowScreenshot = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowScreenshot ?? false;

/// <summary>
/// 是否显示OCR图标
/// </summary>
[ObservableProperty]
private bool isShowOCR = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowOCR ?? false;

/// <summary>
/// 是否显示静默OCR图标
/// </summary>
[ObservableProperty]
private bool isShowSilentOCR = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowSilentOCR ?? false;

/// <summary>
/// 是否显示识别二维码图标
/// </summary>
[ObservableProperty]
private bool isShowQRCode = Singleton<ConfigHelper>.Instance.CurrentConfig?.IsShowQRCode ?? false;
}
}
72 changes: 50 additions & 22 deletions STranslate/Views/HeaderView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,60 @@
<Button x:Name="TopmostBtn"
Margin="10,5,0,0"
HorizontalAlignment="Left"
ToolTip="置顶"
Command="{Binding StickyCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
Content="{Binding TopMostContent}"
FontSize="22"
FontSize="21"
Style="{DynamicResource ButtonStickyIconStyle}"
Tag="{Binding IsTopMost}" />
<Button Margin="0,0,100,0"
HorizontalAlignment="Right"
Command="{Binding MouseHookCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
Content="&#xe650;"
FontSize="21"
FontWeight="Black"
Style="{DynamicResource ButtonCrosswordsIconStyle}"
Tag="{Binding IsEnableMosehook}" />
<Button Margin="0,0,60,0"
HorizontalAlignment="Right"
Content="&#xe994;"
FontSize="21"
Command="{Binding SwitchThemeCommand}"
Style="{DynamicResource ButtonIconStyle}" />
<Button Margin="0,0,20,0"
HorizontalAlignment="Right"
Command="{Binding NotifyIconVM.OpenPreferenceCommand}"
Content="&#xe604;"
FontSize="21"
Style="{DynamicResource ButtonIconStyle}" />
<StackPanel Margin="0,0,10,0" Orientation="Horizontal" FlowDirection="RightToLeft">
<Button HorizontalAlignment="Right"
ToolTip="偏好设置"
Visibility="{Binding IsShowPreference, Converter={StaticResource BooleanToVisibilityConverter}}"
Command="{Binding NotifyIconVM.OpenPreferenceCommand}"
Content="&#xe656;"
Style="{DynamicResource ButtonIconStyle}" />
<Button HorizontalAlignment="Right"
ToolTip="切换主题"
Visibility="{Binding IsShowSwitchTheme, Converter={StaticResource BooleanToVisibilityConverter}}"
Content="&#xe994;"
Command="{Binding SwitchThemeCommand}"
Style="{DynamicResource ButtonIconStyle}" />
<Button HorizontalAlignment="Right"
ToolTip="监听鼠标划词"
Visibility="{Binding IsShowMousehook, Converter={StaticResource BooleanToVisibilityConverter}}"
Command="{Binding MouseHookCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
Content="&#xeb94;"
Height="30"
Width="30"
Style="{DynamicResource ButtonCrosswordsIconStyle}"
Tag="{Binding IsEnableMosehook}" />
<Button HorizontalAlignment="Right"
ToolTip="截图翻译"
Visibility="{Binding IsShowScreenshot, Converter={StaticResource BooleanToVisibilityConverter}}"
Content="&#xe679;"
Command="{Binding NotifyIconVM.ScreenShotTranslateCommand}"
Style="{DynamicResource ButtonIconStyle}" />
<Button HorizontalAlignment="Right"
ToolTip="OCR"
Visibility="{Binding IsShowOCR, Converter={StaticResource BooleanToVisibilityConverter}}"
Content="&#xe695;"
Command="{Binding NotifyIconVM.OCRCommand}"
Style="{DynamicResource ButtonIconStyle}" />
<Button HorizontalAlignment="Right"
ToolTip="静默OCR"
Visibility="{Binding IsShowSilentOCR, Converter={StaticResource BooleanToVisibilityConverter}}"
Content="&#xe861;"
Command="{Binding NotifyIconVM.SilentOCRCommand}"
Style="{DynamicResource ButtonIconStyle}" />
<Button HorizontalAlignment="Right"
ToolTip="识别二维码"
Visibility="{Binding IsShowQRCode, Converter={StaticResource BooleanToVisibilityConverter}}"
Content="&#xe642;"
Command="{Binding NotifyIconVM.QRCodeCommand}"
Style="{DynamicResource ButtonIconStyle}" />
</StackPanel>
</Grid>
</UserControl>
47 changes: 47 additions & 0 deletions STranslate/Views/Preference/CommonPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,53 @@
</DockPanel>
</StackPanel>
</Border>

<!-- // 首页图标设置 // -->
<Border Margin="8" Background="{DynamicResource BorderContentBackground}" CornerRadius="5">
<StackPanel Orientation="Vertical">
<DockPanel Margin="20,10">
<TextBlock Text="显示设置图标" />
<TextBlock Style="{DynamicResource InfoTextBlock}" ToolTip="主界面显示设置图标" />
<ToggleButton Height="26" HorizontalAlignment="Right" IsChecked="{Binding IsShowPreference}" />
</DockPanel>

<DockPanel Margin="20,10">
<TextBlock Text="显示主题图标" />
<TextBlock Style="{DynamicResource InfoTextBlock}" ToolTip="主界面显示主题图标" />
<ToggleButton Height="26" HorizontalAlignment="Right" IsChecked="{Binding IsShowSwitchTheme}" />
</DockPanel>

<DockPanel Margin="20,10">
<TextBlock Text="显示鼠标划词图标" />
<TextBlock Style="{DynamicResource InfoTextBlock}" ToolTip="主界面显示鼠标划词图标" />
<ToggleButton Height="26" HorizontalAlignment="Right" IsChecked="{Binding IsShowMousehook}" />
</DockPanel>

<DockPanel Margin="20,10">
<TextBlock Text="显示截图翻译图标" />
<TextBlock Style="{DynamicResource InfoTextBlock}" ToolTip="主界面显示截图翻译图标" />
<ToggleButton Height="26" HorizontalAlignment="Right" IsChecked="{Binding IsShowScreenshot}" />
</DockPanel>

<DockPanel Margin="20,10">
<TextBlock Text="显示OCR图标" />
<TextBlock Style="{DynamicResource InfoTextBlock}" ToolTip="主界面显示OCR图标" />
<ToggleButton Height="26" HorizontalAlignment="Right" IsChecked="{Binding IsShowOCR}" />
</DockPanel>

<DockPanel Margin="20,10">
<TextBlock Text="显示静默OCR图标" />
<TextBlock Style="{DynamicResource InfoTextBlock}" ToolTip="主界面显示静默OCR图标" />
<ToggleButton Height="26" HorizontalAlignment="Right" IsChecked="{Binding IsShowSilentOCR}" />
</DockPanel>

<DockPanel Margin="20,10">
<TextBlock Text="显示二维码图标" />
<TextBlock Style="{DynamicResource InfoTextBlock}" ToolTip="主界面显示识别二维码图标" />
<ToggleButton Height="26" HorizontalAlignment="Right" IsChecked="{Binding IsShowQRCode}" />
</DockPanel>
</StackPanel>
</Border>

<!-- // 内容调整设置 // -->
<Border Margin="8" Background="{DynamicResource BorderContentBackground}" CornerRadius="5">
Expand Down

0 comments on commit 5c37003

Please sign in to comment.