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()
{
SendCtrlC();
System.Diagnostics.Debug.Print(STranslate.ViewModel.SettingsVM.Instance.WordPickupInterval.ToString());
Thread.Sleep((int)STranslate.ViewModel.SettingsVM.Instance.WordPickupInterval);
return NativeMethodHelper.GetText();
}

@ -17,6 +17,7 @@ namespace STranslate.Model
{
return new ConfigModel
{
WordPickupInterval = 200,
IsBright = true,
SourceLanguage = 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>

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

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

@ -6,7 +6,6 @@
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:STranslate.View"
mc:Ignorable="d"
Topmost="True"
SizeToContent="Height"
WindowStartupLocation="CenterScreen"
Icon="/Images/translate.ico"
@ -34,6 +33,11 @@
</ControlTemplate>
</Window.Resources>
<Window.InputBindings>
<KeyBinding Key="Esc"
Command="{Binding EscCmd}"
CommandParameter="{Binding ElementName=window, Mode=OneWay}"/>
</Window.InputBindings>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding}"/>
@ -51,24 +55,29 @@
<GroupBox Header="开机启动">
<WrapPanel Margin="0,5,0,5">
<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>
</GroupBox>
<GroupBox Header="取词间隔"
ToolTip="划词翻译实现原理是用户用鼠标划词后模拟Ctrl+C间隔一定时间后程序再从剪切板取词如果电脑反应慢可能会出现取不到词的问题将间隔调大可以适配一些老旧的机器">
<WrapPanel Margin="0,5,0,5">
<Slider Value="{Binding WordPickupInterval}"
<Slider Value="{Binding DataContext.WordPickupInterval, ElementName=window}"
Minimum="100"
Maximum="1000"
MinWidth="200"
TickFrequency="100"
TickPlacement="None"
IsSnapToTickEnabled="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ValueChanged">
<i:InvokeCommandAction Command="{Binding ResetWordPickupIntervalCmd}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Slider>
<TextBlock Text="{Binding WordPickupInterval, StringFormat={}{0}ms}" Margin="10,0,0,0" />
</WrapPanel>
@ -77,6 +86,7 @@
</TabItem>
<!--
<TabItem Header="控制"
Padding="10,2">
@ -203,9 +213,9 @@
</StackPanel>
</TabItem>
-->
<TabItem Header="关于"
IsSelected="True"
Padding="10,2">
<ScrollViewer VerticalScrollBarVisibility="Auto">
@ -216,7 +226,7 @@
FontSize="18"
Margin="5,0">
<Run Text="STranslate"
FontWeight="Black"/>
FontWeight="Bold"/>
</TextBlock>
<!--<Border BorderBrush="Transparent"
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
{
public class MainVM : BaseMainVM
public class MainVM : BaseVM
{
public MainVM()
{
@ -173,9 +173,23 @@ namespace STranslate.ViewModel
/// </summary>
public void ScreenShotTranslate()
{
var screen = new ScreenShotWindow();
screen.Show();
screen.Activate();
ScreenShotWindow window = null;
foreach (Window item in Application.Current.Windows)
{
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>
/// 截屏翻译Ex
@ -217,6 +231,10 @@ namespace STranslate.ViewModel
{
_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);
@ -253,6 +271,7 @@ namespace STranslate.ViewModel
{
ConfigHelper.Instance.WriteConfig(new ConfigModel
{
WordPickupInterval = SettingsVM.Instance.WordPickupInterval,
IsBright = Application.Current.Resources.MergedDictionaries[0].Source.ToString() == ThemeDefault ? true : false,
SourceLanguage = InputComboSelected,
TargetLanguage = OutputComboSelected,

@ -10,10 +10,14 @@ using System.Windows.Input;
namespace STranslate.ViewModel
{
public class SettingsVM : BaseMainVM
public class SettingsVM : BaseVM
{
public SettingsVM()
{
IsStartup = StartupHelper.IsStartup();
Version = HandleVersion(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() ?? "1.0.0.0");
LoadedCmd = new RelayCommand((_) => true, (_) =>
{
Console.WriteLine("123");
@ -24,18 +28,6 @@ namespace STranslate.ViewModel
Console.WriteLine("123");
});
//重置快捷键
ResetHotKeysCmd = new RelayCommand((_) => true, (_) =>
{
Console.WriteLine("123");
});
//重置取词间隔
ResetWordPickupIntervalCmd = new RelayCommand((_) => true, (_) =>
{
System.Diagnostics.Debug.Print(WordPickupInterval.ToString());
});
//更新
UpdateCmd = new RelayCommand((_) => true, (_) =>
{
@ -73,18 +65,61 @@ namespace STranslate.ViewModel
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 ClosedCmd { 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; }
public ICommand ResetWordPickupIntervalCmd { get; private set; }
private static SettingsVM _instance;
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); }
private static SettingsVM _instance;
public static SettingsVM Instance => _instance ?? (_instance = new SettingsVM());
}
}

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

@ -40,6 +40,9 @@
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>update.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<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>
@ -109,5 +112,8 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Resource Include="update.ico" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Loading…
Cancel
Save