From 999ed5c45975fd3c7e8685856b142345fffaf466 Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Sat, 14 Jan 2023 17:30:16 +0800 Subject: [PATCH 1/5] chore: add setting view --- STranslate/STranslate.csproj | 8 +++++++ STranslate/View/MainWindow.xaml | 3 --- STranslate/View/MainWindow.xaml.cs | 16 ++++++++++++++ STranslate/View/SettingsWindow.xaml | 29 ++++++++++++++++++++++++++ STranslate/View/SettingsWindow.xaml.cs | 27 ++++++++++++++++++++++++ STranslate/ViewModel/SettingsVM.cs | 27 ++++++++++++++++++++++++ 6 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 STranslate/View/SettingsWindow.xaml create mode 100644 STranslate/View/SettingsWindow.xaml.cs create mode 100644 STranslate/ViewModel/SettingsVM.cs diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj index c3dde3d..8485ea9 100644 --- a/STranslate/STranslate.csproj +++ b/STranslate/STranslate.csproj @@ -106,9 +106,13 @@ + ScreenShotWindow.xaml + + SettingsWindow.xaml + MSBuild:Compile Designer @@ -157,6 +161,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + diff --git a/STranslate/View/MainWindow.xaml b/STranslate/View/MainWindow.xaml index 1684d89..f7cea73 100644 --- a/STranslate/View/MainWindow.xaml +++ b/STranslate/View/MainWindow.xaml @@ -3,10 +3,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:tb="http://www.hardcodet.net/taskbar" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" - xmlns:vm="clr-namespace:STranslate.ViewModel" - xmlns:local="clr-namespace:STranslate.View" mc:Ignorable="d" x:Name="Mwin" Background="Transparent" diff --git a/STranslate/View/MainWindow.xaml.cs b/STranslate/View/MainWindow.xaml.cs index 3fbd424..a1d403f 100644 --- a/STranslate/View/MainWindow.xaml.cs +++ b/STranslate/View/MainWindow.xaml.cs @@ -101,6 +101,9 @@ namespace STranslate.View var autoStartBtn = new MenuItem("开机自启"); autoStartBtn.Click += AutoStart_Click; + var preferenceBtn = new MenuItem("首选项"); + preferenceBtn.Click += Preference_Click; + autoStartBtn.Checked = StartupHelper.IsStartup(); var exitBtn = new MenuItem("退出"); @@ -113,11 +116,24 @@ namespace STranslate.View openMainWinBtn, checkUpdateBtn, autoStartBtn, + preferenceBtn, exitBtn, }; NotifyIcon.ContextMenu = new ContextMenu(items); } + /// + /// 设置 + /// + /// + /// + private void Preference_Click(object sender, EventArgs e) + { + var setting = new SettingsWindow(); + setting.Show(); + setting.Activate(); + } + /// /// 同步Github版本命名 /// diff --git a/STranslate/View/SettingsWindow.xaml b/STranslate/View/SettingsWindow.xaml new file mode 100644 index 0000000..8366213 --- /dev/null +++ b/STranslate/View/SettingsWindow.xaml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + diff --git a/STranslate/View/SettingsWindow.xaml.cs b/STranslate/View/SettingsWindow.xaml.cs new file mode 100644 index 0000000..0d5da45 --- /dev/null +++ b/STranslate/View/SettingsWindow.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; + +namespace STranslate.View +{ + /// + /// SettingsWindow.xaml 的交互逻辑 + /// + public partial class SettingsWindow : Window + { + public SettingsWindow() + { + InitializeComponent(); + } + } +} diff --git a/STranslate/ViewModel/SettingsVM.cs b/STranslate/ViewModel/SettingsVM.cs new file mode 100644 index 0000000..4ae8a41 --- /dev/null +++ b/STranslate/ViewModel/SettingsVM.cs @@ -0,0 +1,27 @@ +using STranslate.Helper; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; + +namespace STranslate.ViewModel +{ + public class SettingsVM : BaseVM + { + public SettingsVM() + { + LoadedCmd = new RelayCommand((_) => true, (_) => + { + Console.WriteLine("123"); + }); + ClosedCmd = new RelayCommand((_) => true, (_) => + { + Console.WriteLine("123"); + }); + } + public ICommand LoadedCmd { get; private set; } + public ICommand ClosedCmd { get; private set; } + } +} From 655335d49f2e928823acb4a21697f791631605e4 Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Mon, 16 Jan 2023 12:06:53 +0800 Subject: [PATCH 2/5] tmp --- STranslate/Helper/GetWordsHelper.cs | 2 +- STranslate/STranslate.csproj | 1 + STranslate/View/MainWindow.xaml.cs | 19 +- STranslate/View/SettingsWindow.xaml | 264 ++++++++++++++++++++++++- STranslate/View/SettingsWindow.xaml.cs | 2 + STranslate/ViewModel/BaseMainVM.cs | 36 ++++ STranslate/ViewModel/MainVM.cs | 2 +- STranslate/ViewModel/SettingsVM.cs | 65 +++++- 8 files changed, 364 insertions(+), 27 deletions(-) create mode 100644 STranslate/ViewModel/BaseMainVM.cs diff --git a/STranslate/Helper/GetWordsHelper.cs b/STranslate/Helper/GetWordsHelper.cs index 52d99c1..4350a2a 100644 --- a/STranslate/Helper/GetWordsHelper.cs +++ b/STranslate/Helper/GetWordsHelper.cs @@ -9,7 +9,7 @@ namespace STranslate.Helper public static String Get() { SendCtrlC(); - Thread.Sleep(200); + Thread.Sleep((int)STranslate.ViewModel.SettingsVM.Instance.WordPickupInterval); return NativeMethodHelper.GetText(); } diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj index 8485ea9..17714fe 100644 --- a/STranslate/STranslate.csproj +++ b/STranslate/STranslate.csproj @@ -104,6 +104,7 @@ Resources.resx + diff --git a/STranslate/View/MainWindow.xaml.cs b/STranslate/View/MainWindow.xaml.cs index a1d403f..d57886a 100644 --- a/STranslate/View/MainWindow.xaml.cs +++ b/STranslate/View/MainWindow.xaml.cs @@ -61,7 +61,6 @@ namespace STranslate.View private MainVM vm = MainVM.Instance; - private string _version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); public readonly NotifyIcon NotifyIcon = new NotifyIcon(); @@ -69,9 +68,8 @@ namespace STranslate.View #region Initial TrayIcon private void InitialTray() { - _version = HandleVersion(_version); var app = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly()?.Location); - NotifyIcon.Text = $@"{app} {_version}"; + NotifyIcon.Text = $@"{app} {vm.Version}"; var stream = Application .GetResourceStream(new Uri("Images/translate.ico", UriKind.Relative))?.Stream; if (stream != null) @@ -134,19 +132,6 @@ namespace STranslate.View setting.Activate(); } - /// - /// 同步Github版本命名 - /// - /// - /// - 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; - } /// /// 检查更新 by https://github.com/Planshit/Tai @@ -181,7 +166,7 @@ namespace STranslate.View "Updater", "Newtonsoft.Json.dll"), true); - ProcessHelper.Run(updaterCacheExePath, new string[] { _version }); + ProcessHelper.Run(updaterCacheExePath, new string[] { vm.Version }); } catch (Exception ex) { diff --git a/STranslate/View/SettingsWindow.xaml b/STranslate/View/SettingsWindow.xaml index 8366213..dbdbf10 100644 --- a/STranslate/View/SettingsWindow.xaml +++ b/STranslate/View/SettingsWindow.xaml @@ -1,18 +1,39 @@ - - - - + Title="STranslate 首选项" Height="450" Width="450"> + + + + + + + + + + + + + + + + @@ -21,9 +42,238 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/STranslate/View/SettingsWindow.xaml.cs b/STranslate/View/SettingsWindow.xaml.cs index 0d5da45..404f505 100644 --- a/STranslate/View/SettingsWindow.xaml.cs +++ b/STranslate/View/SettingsWindow.xaml.cs @@ -22,6 +22,8 @@ namespace STranslate.View public SettingsWindow() { InitializeComponent(); + + DataContext = ViewModel.SettingsVM.Instance; } } } diff --git a/STranslate/ViewModel/BaseMainVM.cs b/STranslate/ViewModel/BaseMainVM.cs new file mode 100644 index 0000000..3eca5d0 --- /dev/null +++ b/STranslate/ViewModel/BaseMainVM.cs @@ -0,0 +1,36 @@ +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"); + } + + + /// + /// 同步Github版本命名 + /// + /// + /// + 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); } + + } +} diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index f1bb44a..e96ac46 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -13,7 +13,7 @@ using System.Windows.Controls; namespace STranslate.ViewModel { - public class MainVM : BaseVM + public class MainVM : BaseMainVM { public MainVM() { diff --git a/STranslate/ViewModel/SettingsVM.cs b/STranslate/ViewModel/SettingsVM.cs index 4ae8a41..07209f4 100644 --- a/STranslate/ViewModel/SettingsVM.cs +++ b/STranslate/ViewModel/SettingsVM.cs @@ -1,14 +1,16 @@ using STranslate.Helper; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows; using System.Windows.Input; namespace STranslate.ViewModel { - public class SettingsVM : BaseVM + public class SettingsVM : BaseMainVM { public SettingsVM() { @@ -16,12 +18,73 @@ namespace STranslate.ViewModel { Console.WriteLine("123"); }); + ClosedCmd = new RelayCommand((_) => true, (_) => { Console.WriteLine("123"); }); + + //重置快捷键 + ResetHotKeysCmd = new RelayCommand((_) => true, (_) => + { + Console.WriteLine("123"); + }); + + //重置取词间隔 + ResetWordPickupIntervalCmd = new RelayCommand((_) => true, (_) => + { + System.Diagnostics.Debug.Print(WordPickupInterval.ToString()); + }); + + //更新 + UpdateCmd = new RelayCommand((_) => true, (_) => + { + try + { + 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)) + { + Directory.CreateDirectory(updateDirPath); + } + + 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[] { Version }); + } + catch (Exception ex) + { + + MessageBox.Show($"无法正确启动检查更新程序\n{ex.Message}"); + } + }); } public ICommand LoadedCmd { get; private set; } public ICommand ClosedCmd { get; private set; } + public ICommand UpdateCmd { get; private set; } + + public ICommand ResetHotKeysCmd { get; private set; } + public ICommand ResetWordPickupIntervalCmd { get; private set; } + + private double _wordPickupInterval = 200; + public double WordPickupInterval { get => _wordPickupInterval; set => UpdateProperty(ref _wordPickupInterval, value); } + + private static SettingsVM _instance; + public static SettingsVM Instance => _instance ?? (_instance = new SettingsVM()); } } From dcd47be44a7f6932081addcc7d84b844eb0502b3 Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Mon, 16 Jan 2023 14:14:37 +0800 Subject: [PATCH 3/5] perf: update setting --- STranslate/Helper/GetWordsHelper.cs | 1 + STranslate/Model/ConfigModel.cs | 6 +++ STranslate/STranslate.csproj | 1 - STranslate/View/MainWindow.xaml.cs | 72 +++++----------------------- STranslate/View/SettingsWindow.xaml | 32 ++++++++----- STranslate/ViewModel/BaseMainVM.cs | 36 -------------- STranslate/ViewModel/MainVM.cs | 27 +++++++++-- STranslate/ViewModel/SettingsVM.cs | 71 ++++++++++++++++++++------- Updater/MainWindow.xaml | 1 + Updater/Updater.csproj | 6 +++ Updater/update.ico | Bin 0 -> 16958 bytes 11 files changed, 123 insertions(+), 130 deletions(-) delete mode 100644 STranslate/ViewModel/BaseMainVM.cs create mode 100644 Updater/update.ico diff --git a/STranslate/Helper/GetWordsHelper.cs b/STranslate/Helper/GetWordsHelper.cs index 4350a2a..692f726 100644 --- a/STranslate/Helper/GetWordsHelper.cs +++ b/STranslate/Helper/GetWordsHelper.cs @@ -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(); } diff --git a/STranslate/Model/ConfigModel.cs b/STranslate/Model/ConfigModel.cs index 113701c..4e109e0 100644 --- a/STranslate/Model/ConfigModel.cs +++ b/STranslate/Model/ConfigModel.cs @@ -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 }; } + /// + /// 是否亮色模式 + /// + [JsonProperty("wordPickupInterval")] + public double WordPickupInterval { get; set; } /// /// 是否亮色模式 /// diff --git a/STranslate/STranslate.csproj b/STranslate/STranslate.csproj index 17714fe..8485ea9 100644 --- a/STranslate/STranslate.csproj +++ b/STranslate/STranslate.csproj @@ -104,7 +104,6 @@ Resources.resx - diff --git a/STranslate/View/MainWindow.xaml.cs b/STranslate/View/MainWindow.xaml.cs index d57886a..4c9a851 100644 --- a/STranslate/View/MainWindow.xaml.cs +++ b/STranslate/View/MainWindow.xaml.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 /// private void Preference_Click(object sender, EventArgs e) { - var setting = new SettingsWindow(); - setting.Show(); - setting.Activate(); - } - - - /// - /// 检查更新 by https://github.com/Planshit/Tai - /// - /// - /// - 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)) - { - Directory.CreateDirectory(updateDirPath); - } - - if (!File.Exists(updaterExePath)) + if (item is SettingsWindow) { - MessageBox.Show("升级程序似乎已被删除,请手动前往发布页查看新版本"); - return; + window = (SettingsWindow)item; + 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) { - - 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); diff --git a/STranslate/View/SettingsWindow.xaml b/STranslate/View/SettingsWindow.xaml index dbdbf10..64161d8 100644 --- a/STranslate/View/SettingsWindow.xaml +++ b/STranslate/View/SettingsWindow.xaml @@ -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" @@ -33,7 +32,12 @@ - + + + + @@ -51,24 +55,29 @@ + IsChecked="{Binding IsStartup}" + FontWeight="Bold"> + + + + + + + + + - - - - - - @@ -77,6 +86,7 @@ + @@ -216,7 +226,7 @@ FontSize="18" Margin="5,0"> + FontWeight="Bold"/> diff --git a/STranslate/View/SettingsWindow.xaml b/STranslate/View/SettingsWindow.xaml index c77d762..f3fdcdf 100644 --- a/STranslate/View/SettingsWindow.xaml +++ b/STranslate/View/SettingsWindow.xaml @@ -211,8 +211,17 @@ --> - - + + + + + + +