perf: update setting

dev_settings^2
DESKTOP-3BO4HSG\ksat 2 years ago
parent 655335d49f
commit dcd47be44a

@ -9,6 +9,7 @@ namespace STranslate.Helper
public static String Get() public static String Get()
{ {
SendCtrlC(); SendCtrlC();
System.Diagnostics.Debug.Print(STranslate.ViewModel.SettingsVM.Instance.WordPickupInterval.ToString());
Thread.Sleep((int)STranslate.ViewModel.SettingsVM.Instance.WordPickupInterval); Thread.Sleep((int)STranslate.ViewModel.SettingsVM.Instance.WordPickupInterval);
return NativeMethodHelper.GetText(); return NativeMethodHelper.GetText();
} }

@ -17,6 +17,7 @@ namespace STranslate.Model
{ {
return new ConfigModel return new ConfigModel
{ {
WordPickupInterval = 200,
IsBright = true, IsBright = true,
SourceLanguage = LanguageEnum.AUTO.GetDescription(), SourceLanguage = LanguageEnum.AUTO.GetDescription(),
TargetLanguage = LanguageEnum.AUTO.GetDescription(), TargetLanguage = LanguageEnum.AUTO.GetDescription(),
@ -42,6 +43,11 @@ namespace STranslate.Model
}; };
} }
/// <summary>
/// 是否亮色模式
/// </summary>
[JsonProperty("wordPickupInterval")]
public double WordPickupInterval { get; set; }
/// <summary> /// <summary>
/// 是否亮色模式 /// 是否亮色模式
/// </summary> /// </summary>

@ -104,7 +104,6 @@
<DependentUpon>Resources.resx</DependentUpon> <DependentUpon>Resources.resx</DependentUpon>
</Compile> </Compile>
<Compile Include="Util\Util.cs" /> <Compile Include="Util\Util.cs" />
<Compile Include="ViewModel\BaseMainVM.cs" />
<Compile Include="ViewModel\MainVM.cs" /> <Compile Include="ViewModel\MainVM.cs" />
<Compile Include="ViewModel\ScreenShotVM.cs" /> <Compile Include="ViewModel\ScreenShotVM.cs" />
<Compile Include="ViewModel\SettingsVM.cs" /> <Compile Include="ViewModel\SettingsVM.cs" />

@ -61,15 +61,13 @@ namespace STranslate.View
private MainVM vm = MainVM.Instance; private MainVM vm = MainVM.Instance;
public readonly NotifyIcon NotifyIcon = new NotifyIcon(); public readonly NotifyIcon NotifyIcon = new NotifyIcon();
#region Initial TrayIcon #region Initial TrayIcon
private void InitialTray() private void InitialTray()
{ {
var app = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly()?.Location); var app = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly()?.Location);
NotifyIcon.Text = $@"{app} {vm.Version}"; NotifyIcon.Text = $@"{app} {SettingsVM.Instance.Version}";
var stream = Application var stream = Application
.GetResourceStream(new Uri("Images/translate.ico", UriKind.Relative))?.Stream; .GetResourceStream(new Uri("Images/translate.ico", UriKind.Relative))?.Stream;
if (stream != null) if (stream != null)
@ -93,17 +91,9 @@ namespace STranslate.View
var openMainWinBtn = new MenuItem("显示主界面\tAlt+G"); var openMainWinBtn = new MenuItem("显示主界面\tAlt+G");
openMainWinBtn.Click += OpenMainWin_Click; openMainWinBtn.Click += OpenMainWin_Click;
var checkUpdateBtn = new MenuItem("检查更新");
checkUpdateBtn.Click += CheckUpdateBTN_Click;
var autoStartBtn = new MenuItem("开机自启");
autoStartBtn.Click += AutoStart_Click;
var preferenceBtn = new MenuItem("首选项"); var preferenceBtn = new MenuItem("首选项");
preferenceBtn.Click += Preference_Click; preferenceBtn.Click += Preference_Click;
autoStartBtn.Checked = StartupHelper.IsStartup();
var exitBtn = new MenuItem("退出"); var exitBtn = new MenuItem("退出");
exitBtn.Click += Exit_Click; exitBtn.Click += Exit_Click;
@ -112,8 +102,6 @@ namespace STranslate.View
screenshotTranslateMenuItemBtn, screenshotTranslateMenuItemBtn,
crossWordTranslateMenuItemBtn, crossWordTranslateMenuItemBtn,
openMainWinBtn, openMainWinBtn,
checkUpdateBtn,
autoStartBtn,
preferenceBtn, preferenceBtn,
exitBtn, exitBtn,
}; };
@ -127,51 +115,22 @@ namespace STranslate.View
/// <param name="e"></param> /// <param name="e"></param>
private void Preference_Click(object sender, EventArgs e) private void Preference_Click(object sender, EventArgs e)
{ {
var setting = new SettingsWindow(); SettingsWindow window = null;
setting.Show(); foreach (Window item in Application.Current.Windows)
setting.Activate();
}
/// <summary>
/// 检查更新 by https://github.com/Planshit/Tai
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CheckUpdateBTN_Click(object sender, EventArgs e)
{
try
{ {
var updaterExePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, if (item is SettingsWindow)
"Updater.exe");
var updaterCacheExePath = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"Updater",
"Updater.exe");
var updateDirPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Updater");
if (!Directory.Exists(updateDirPath))
{
Directory.CreateDirectory(updateDirPath);
}
if (!File.Exists(updaterExePath))
{ {
MessageBox.Show("升级程序似乎已被删除,请手动前往发布页查看新版本"); window = (SettingsWindow)item;
return; window.WindowState = WindowState.Normal;
window.Activate();
break;
} }
File.Copy(updaterExePath, updaterCacheExePath, true);
File.Copy(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Newtonsoft.Json.dll"), Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
"Updater",
"Newtonsoft.Json.dll"), true);
ProcessHelper.Run(updaterCacheExePath, new string[] { vm.Version });
} }
catch (Exception ex) if (window == null)
{ {
window = new SettingsWindow();
MessageBox.Show($"无法正确启动检查更新程序\n{ex.Message}"); window.Show();
window.Activate();
} }
} }
@ -185,13 +144,6 @@ namespace STranslate.View
vm.OpenMainWin(); vm.OpenMainWin();
} }
private void AutoStart_Click(object sender, EventArgs e)
{
if (StartupHelper.IsStartup()) StartupHelper.UnSetStartup();
else StartupHelper.SetStartup();
((MenuItem)sender).Checked = StartupHelper.IsStartup();
}
private void Exit_Click(object sender, EventArgs e) private void Exit_Click(object sender, EventArgs e)
{ {
vm.ExitApp(0); vm.ExitApp(0);

@ -6,7 +6,6 @@
xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:STranslate.View" xmlns:local="clr-namespace:STranslate.View"
mc:Ignorable="d" mc:Ignorable="d"
Topmost="True"
SizeToContent="Height" SizeToContent="Height"
WindowStartupLocation="CenterScreen" WindowStartupLocation="CenterScreen"
Icon="/Images/translate.ico" Icon="/Images/translate.ico"
@ -33,7 +32,12 @@
</ControlTemplate.Triggers> </ControlTemplate.Triggers>
</ControlTemplate> </ControlTemplate>
</Window.Resources> </Window.Resources>
<Window.InputBindings>
<KeyBinding Key="Esc"
Command="{Binding EscCmd}"
CommandParameter="{Binding ElementName=window, Mode=OneWay}"/>
</Window.InputBindings>
<i:Interaction.Triggers> <i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded"> <i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding}"/> <i:InvokeCommandAction Command="{Binding}"/>
@ -51,24 +55,29 @@
<GroupBox Header="开机启动"> <GroupBox Header="开机启动">
<WrapPanel Margin="0,5,0,5"> <WrapPanel Margin="0,5,0,5">
<CheckBox Content="开机启动" <CheckBox Content="开机启动"
FontWeight="Bold"/> IsChecked="{Binding IsStartup}"
FontWeight="Bold">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding StartupCmd}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding StartupCmd}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</WrapPanel> </WrapPanel>
</GroupBox> </GroupBox>
<GroupBox Header="取词间隔" <GroupBox Header="取词间隔"
ToolTip="划词翻译实现原理是用户用鼠标划词后模拟Ctrl+C间隔一定时间后程序再从剪切板取词如果电脑反应慢可能会出现取不到词的问题将间隔调大可以适配一些老旧的机器"> ToolTip="划词翻译实现原理是用户用鼠标划词后模拟Ctrl+C间隔一定时间后程序再从剪切板取词如果电脑反应慢可能会出现取不到词的问题将间隔调大可以适配一些老旧的机器">
<WrapPanel Margin="0,5,0,5"> <WrapPanel Margin="0,5,0,5">
<Slider Value="{Binding WordPickupInterval}" <Slider Value="{Binding DataContext.WordPickupInterval, ElementName=window}"
Minimum="100" Minimum="100"
Maximum="1000" Maximum="1000"
MinWidth="200" MinWidth="200"
TickFrequency="100" TickFrequency="100"
TickPlacement="None" TickPlacement="None"
IsSnapToTickEnabled="True"> IsSnapToTickEnabled="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ValueChanged">
<i:InvokeCommandAction Command="{Binding ResetWordPickupIntervalCmd}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Slider> </Slider>
<TextBlock Text="{Binding WordPickupInterval, StringFormat={}{0}ms}" Margin="10,0,0,0" /> <TextBlock Text="{Binding WordPickupInterval, StringFormat={}{0}ms}" Margin="10,0,0,0" />
</WrapPanel> </WrapPanel>
@ -77,6 +86,7 @@
</TabItem> </TabItem>
<!--
<TabItem Header="控制" <TabItem Header="控制"
Padding="10,2"> Padding="10,2">
@ -203,9 +213,9 @@
</StackPanel> </StackPanel>
</TabItem> </TabItem>
-->
<TabItem Header="关于" <TabItem Header="关于"
IsSelected="True"
Padding="10,2"> Padding="10,2">
<ScrollViewer VerticalScrollBarVisibility="Auto"> <ScrollViewer VerticalScrollBarVisibility="Auto">
@ -216,7 +226,7 @@
FontSize="18" FontSize="18"
Margin="5,0"> Margin="5,0">
<Run Text="STranslate" <Run Text="STranslate"
FontWeight="Black"/> FontWeight="Bold"/>
</TextBlock> </TextBlock>
<!--<Border BorderBrush="Transparent" <!--<Border BorderBrush="Transparent"
Background="Black" Background="Black"

@ -1,36 +0,0 @@
using STranslate.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace STranslate.ViewModel
{
public class BaseMainVM : BaseVM
{
public BaseMainVM()
{
Version = HandleVersion(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() ?? "1.0.0.0");
}
/// <summary>
/// 同步Github版本命名
/// </summary>
/// <param name="version"></param>
/// <returns></returns>
private static string HandleVersion(string version)
{
var ret = string.Empty;
ret = version.Substring(0, version.Length - 2);
var location = ret.LastIndexOf('.');
ret = ret.Remove(location, 1);
return ret;
}
private string _version;
public string Version { get => _version; set => UpdateProperty(ref _version, value); }
}
}

@ -13,7 +13,7 @@ using System.Windows.Controls;
namespace STranslate.ViewModel namespace STranslate.ViewModel
{ {
public class MainVM : BaseMainVM public class MainVM : BaseVM
{ {
public MainVM() public MainVM()
{ {
@ -173,9 +173,23 @@ namespace STranslate.ViewModel
/// </summary> /// </summary>
public void ScreenShotTranslate() public void ScreenShotTranslate()
{ {
var screen = new ScreenShotWindow(); ScreenShotWindow window = null;
screen.Show(); foreach (Window item in Application.Current.Windows)
screen.Activate(); {
if (item is ScreenShotWindow)
{
window = (ScreenShotWindow)item;
window.WindowState = WindowState.Normal;
window.Activate();
break;
}
}
if (window == null)
{
window = new ScreenShotWindow();
window.Show();
window.Activate();
}
} }
/// <summary> /// <summary>
/// 截屏翻译Ex /// 截屏翻译Ex
@ -217,6 +231,10 @@ namespace STranslate.ViewModel
{ {
_globalConfig = ConfigHelper.Instance.ReadConfig<ConfigModel>(); _globalConfig = ConfigHelper.Instance.ReadConfig<ConfigModel>();
//读取间隔
var tmp = _globalConfig.WordPickupInterval;
SettingsVM.Instance.WordPickupInterval = (tmp == 0 || tmp > 1000 || tmp < 100) ? 200 : tmp;
//配置读取主题 //配置读取主题
Application.Current.Resources.MergedDictionaries[0].Source = _globalConfig.IsBright ? new Uri(ThemeDefault) : new Uri(ThemeDark); Application.Current.Resources.MergedDictionaries[0].Source = _globalConfig.IsBright ? new Uri(ThemeDefault) : new Uri(ThemeDark);
@ -253,6 +271,7 @@ namespace STranslate.ViewModel
{ {
ConfigHelper.Instance.WriteConfig(new ConfigModel ConfigHelper.Instance.WriteConfig(new ConfigModel
{ {
WordPickupInterval = SettingsVM.Instance.WordPickupInterval,
IsBright = Application.Current.Resources.MergedDictionaries[0].Source.ToString() == ThemeDefault ? true : false, IsBright = Application.Current.Resources.MergedDictionaries[0].Source.ToString() == ThemeDefault ? true : false,
SourceLanguage = InputComboSelected, SourceLanguage = InputComboSelected,
TargetLanguage = OutputComboSelected, TargetLanguage = OutputComboSelected,

@ -10,10 +10,14 @@ using System.Windows.Input;
namespace STranslate.ViewModel namespace STranslate.ViewModel
{ {
public class SettingsVM : BaseMainVM public class SettingsVM : BaseVM
{ {
public SettingsVM() public SettingsVM()
{ {
IsStartup = StartupHelper.IsStartup();
Version = HandleVersion(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() ?? "1.0.0.0");
LoadedCmd = new RelayCommand((_) => true, (_) => LoadedCmd = new RelayCommand((_) => true, (_) =>
{ {
Console.WriteLine("123"); Console.WriteLine("123");
@ -24,18 +28,6 @@ namespace STranslate.ViewModel
Console.WriteLine("123"); Console.WriteLine("123");
}); });
//重置快捷键
ResetHotKeysCmd = new RelayCommand((_) => true, (_) =>
{
Console.WriteLine("123");
});
//重置取词间隔
ResetWordPickupIntervalCmd = new RelayCommand((_) => true, (_) =>
{
System.Diagnostics.Debug.Print(WordPickupInterval.ToString());
});
//更新 //更新
UpdateCmd = new RelayCommand((_) => true, (_) => UpdateCmd = new RelayCommand((_) => true, (_) =>
{ {
@ -73,18 +65,61 @@ namespace STranslate.ViewModel
MessageBox.Show($"无法正确启动检查更新程序\n{ex.Message}"); MessageBox.Show($"无法正确启动检查更新程序\n{ex.Message}");
} }
}); });
StartupCmd = new RelayCommand((_) => true, (_) =>
{
if (StartupHelper.IsStartup()) StartupHelper.UnSetStartup();
else StartupHelper.SetStartup();
IsStartup = StartupHelper.IsStartup();
});
EscCmd = new RelayCommand((_) => true, (o) =>
{
(o as Window)?.Close();
});
} }
/// <summary>
/// 同步Github版本命名
/// </summary>
/// <param name="version"></param>
/// <returns></returns>
private static string HandleVersion(string version)
{
var ret = string.Empty;
ret = version.Substring(0, version.Length - 2);
var location = ret.LastIndexOf('.');
ret = ret.Remove(location, 1);
return ret;
}
public ICommand LoadedCmd { get; private set; } public ICommand LoadedCmd { get; private set; }
public ICommand ClosedCmd { get; private set; } public ICommand ClosedCmd { get; private set; }
public ICommand UpdateCmd { get; private set; } public ICommand UpdateCmd { get; private set; }
public ICommand StartupCmd { get; private set; }
public ICommand EscCmd { get; private set; }
public ICommand ResetHotKeysCmd { get; private set; } private static SettingsVM _instance;
public ICommand ResetWordPickupIntervalCmd { get; private set; } public static SettingsVM Instance => _instance ?? (_instance = new SettingsVM());
/// <summary>
/// 是否开机自启
/// </summary>
private bool _isStartup;
public bool IsStartup { get => _isStartup; set => UpdateProperty(ref _isStartup, value); }
private double _wordPickupInterval = 200; /// <summary>
/// 版本
/// </summary>
private string _version;
public string Version { get => _version; set => UpdateProperty(ref _version, value); }
/// <summary>
/// 取词间隔
/// </summary>
private double _wordPickupInterval;
public double WordPickupInterval { get => _wordPickupInterval; set => UpdateProperty(ref _wordPickupInterval, value); } public double WordPickupInterval { get => _wordPickupInterval; set => UpdateProperty(ref _wordPickupInterval, value); }
private static SettingsVM _instance;
public static SettingsVM Instance => _instance ?? (_instance = new SettingsVM());
} }
} }

@ -6,6 +6,7 @@
xmlns:local="clr-namespace:Updater" xmlns:local="clr-namespace:Updater"
mc:Ignorable="d" mc:Ignorable="d"
WindowStartupLocation="CenterScreen" WindowStartupLocation="CenterScreen"
Icon="update.ico"
Title="STranslate 升级程序" ResizeMode="NoResize" Height="300" Width="420"> Title="STranslate 升级程序" ResizeMode="NoResize" Height="300" Width="420">
<Grid Margin="10"> <Grid Margin="10">
<Grid.RowDefinitions> <Grid.RowDefinitions>

@ -40,6 +40,9 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit> <Prefer32Bit>false</Prefer32Bit>
</PropertyGroup> </PropertyGroup>
<PropertyGroup>
<ApplicationIcon>update.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
@ -109,5 +112,8 @@
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Resource Include="update.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Loading…
Cancel
Save