From b63bab0d1a0a34f4c2de4a41da87da72c5c576f9 Mon Sep 17 00:00:00 2001 From: noisyfox Date: Tue, 15 Nov 2016 04:23:59 +1100 Subject: [PATCH] Refine LogViewerConfig. Don't store window size in main settings file --- .../Controller/ShadowsocksController.cs | 1 + shadowsocks-csharp/Model/Configuration.cs | 6 +- shadowsocks-csharp/Model/LogViewerConfig.cs | 113 ++++++++---------- shadowsocks-csharp/Model/ProxyConfig.cs | 8 ++ .../Properties/Settings.Designer.cs | 86 +++++++++++++ .../Properties/Settings.settings | 21 ++++ shadowsocks-csharp/Settings.cs | 28 +++++ shadowsocks-csharp/View/LogForm.cs | 32 ++--- shadowsocks-csharp/app.config | 24 ++++ shadowsocks-csharp/shadowsocks-csharp.csproj | 10 ++ 10 files changed, 247 insertions(+), 82 deletions(-) create mode 100644 shadowsocks-csharp/Properties/Settings.Designer.cs create mode 100644 shadowsocks-csharp/Properties/Settings.settings create mode 100644 shadowsocks-csharp/Settings.cs diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 2c8e1fa5..c23beede 100644 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -359,6 +359,7 @@ namespace Shadowsocks.Controller public void SaveLogViewerConfig(LogViewerConfig newConfig) { _config.logViewer = newConfig; + newConfig.SaveSize(); Configuration.Save(_config); if (ConfigChanged != null) { diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 5b3ab57e..8ff8f19f 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -65,10 +65,8 @@ namespace Shadowsocks.Model if (config.hotkey == null) config.hotkey = new HotkeyConfig(); - if (config.proxy.proxyType < ProxyConfig.PROXY_SOCKS5 || config.proxy.proxyType > ProxyConfig.PROXY_HTTP) - { - config.proxy.proxyType = ProxyConfig.PROXY_SOCKS5; - } + config.proxy.CheckConfig(); + return config; } catch (Exception e) diff --git a/shadowsocks-csharp/Model/LogViewerConfig.cs b/shadowsocks-csharp/Model/LogViewerConfig.cs index 56dc78a2..bfb5567e 100644 --- a/shadowsocks-csharp/Model/LogViewerConfig.cs +++ b/shadowsocks-csharp/Model/LogViewerConfig.cs @@ -1,105 +1,94 @@ -using Shadowsocks.View; -using System; +using System; using System.Drawing; using System.Windows.Forms; +using Newtonsoft.Json; namespace Shadowsocks.Model { [Serializable] public class LogViewerConfig { - public string fontName; - public float fontSize; - public string bgColor; - public string textColor; public bool topMost; public bool wrapText; public bool toolbarShown; - public int width; - public int height; - public int top; - public int left; - public bool maximized; + + public Font Font { get; set; } = new Font("Console", 8F); + + public Color BackgroundColor { get; set; } = Color.Black; + + public Color TextColor { get; set; } = Color.White; public LogViewerConfig() { - fontName = "Consolas"; - fontSize = 8; - bgColor = "black"; - textColor = "white"; topMost = false; wrapText = false; toolbarShown = false; - width = 600; - height = 400; - left = GetBestLeft(); - top = GetBestTop(); - maximized = true; } - // Use GetBestTop() and GetBestLeft() to ensure the log viwer form can be always display IN screen. - public int GetBestLeft() + + #region Size + + public void SaveSize() { - width = (width >= 400) ? width : 400; // set up the minimum size - return Screen.PrimaryScreen.WorkingArea.Width - width; + Properties.Settings.Default.Save(); } - public int GetBestTop() + [JsonIgnore] + public int Width { - height = (height >= 200) ? height : 200; // set up the minimum size - return Screen.PrimaryScreen.WorkingArea.Height - height; + get { return Properties.Settings.Default.LogViewerWidth; } + set { Properties.Settings.Default.LogViewerWidth = value; } } - public Font GetFont() + [JsonIgnore] + public int Height { - try - { - return new Font(fontName, fontSize, FontStyle.Regular); - } - catch (Exception) - { - return new Font("Console", 8F); - } + get { return Properties.Settings.Default.LogViewerHeight; } + set { Properties.Settings.Default.LogViewerHeight = value; } } - - public void SetFont(Font font) + [JsonIgnore] + public int Top { - fontName = font.Name; - fontSize = font.Size; + get { return Properties.Settings.Default.LogViewerTop; } + set { Properties.Settings.Default.LogViewerTop = value; } } - - public Color GetBackgroundColor() + [JsonIgnore] + public int Left { - try - { - return ColorTranslator.FromHtml(bgColor); - } - catch (Exception) - { - return ColorTranslator.FromHtml("black"); - } + get { return Properties.Settings.Default.LogViewerLeft; } + set { Properties.Settings.Default.LogViewerLeft = value; } } - - public void SetBackgroundColor(Color color) + [JsonIgnore] + public bool Maximized { - bgColor = ColorTranslator.ToHtml(color); + get { return Properties.Settings.Default.LogViewerMaximized; } + set { Properties.Settings.Default.LogViewerMaximized = value; } } - public Color GetTextColor() + [JsonIgnore] + // Use GetBestTop() and GetBestLeft() to ensure the log viwer form can be always display IN screen. + public int BestLeft { - try + get { - return ColorTranslator.FromHtml(textColor); - } - catch (Exception) - { - return ColorTranslator.FromHtml("white"); + int width = Width; + width = (width >= 400) ? width : 400; // set up the minimum size + return Screen.PrimaryScreen.WorkingArea.Width - width; } } - public void SetTextColor(Color color) + [JsonIgnore] + public int BestTop { - textColor = ColorTranslator.ToHtml(color); + get + { + int height = Height; + height = (height >= 200) ? height : 200; // set up the minimum size + return Screen.PrimaryScreen.WorkingArea.Height - height; + } } + + #endregion + } } diff --git a/shadowsocks-csharp/Model/ProxyConfig.cs b/shadowsocks-csharp/Model/ProxyConfig.cs index 462df824..6cf7f91e 100644 --- a/shadowsocks-csharp/Model/ProxyConfig.cs +++ b/shadowsocks-csharp/Model/ProxyConfig.cs @@ -25,5 +25,13 @@ namespace Shadowsocks.Model proxyPort = 0; proxyTimeout = DefaultProxyTimeoutSec; } + + public void CheckConfig() + { + if (proxyType < PROXY_SOCKS5 || proxyType > PROXY_HTTP) + { + proxyType = PROXY_SOCKS5; + } + } } } diff --git a/shadowsocks-csharp/Properties/Settings.Designer.cs b/shadowsocks-csharp/Properties/Settings.Designer.cs new file mode 100644 index 00000000..7fb89eda --- /dev/null +++ b/shadowsocks-csharp/Properties/Settings.Designer.cs @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本:4.0.30319.42000 +// +// 对此文件的更改可能会导致不正确的行为,并且如果 +// 重新生成代码,这些更改将会丢失。 +// +//------------------------------------------------------------------------------ + +namespace Shadowsocks.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("600")] + public int LogViewerWidth { + get { + return ((int)(this["LogViewerWidth"])); + } + set { + this["LogViewerWidth"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("400")] + public int LogViewerHeight { + get { + return ((int)(this["LogViewerHeight"])); + } + set { + this["LogViewerHeight"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool LogViewerMaximized { + get { + return ((bool)(this["LogViewerMaximized"])); + } + set { + this["LogViewerMaximized"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("0")] + public int LogViewerTop { + get { + return ((int)(this["LogViewerTop"])); + } + set { + this["LogViewerTop"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("0")] + public int LogViewerLeft { + get { + return ((int)(this["LogViewerLeft"])); + } + set { + this["LogViewerLeft"] = value; + } + } + } +} diff --git a/shadowsocks-csharp/Properties/Settings.settings b/shadowsocks-csharp/Properties/Settings.settings new file mode 100644 index 00000000..e6469363 --- /dev/null +++ b/shadowsocks-csharp/Properties/Settings.settings @@ -0,0 +1,21 @@ + + + + + + 600 + + + 400 + + + True + + + 0 + + + 0 + + + \ No newline at end of file diff --git a/shadowsocks-csharp/Settings.cs b/shadowsocks-csharp/Settings.cs new file mode 100644 index 00000000..a1663c6d --- /dev/null +++ b/shadowsocks-csharp/Settings.cs @@ -0,0 +1,28 @@ +namespace Shadowsocks.Properties { + + + // 通过此类可以处理设置类的特定事件: + // 在更改某个设置的值之前将引发 SettingChanging 事件。 + // 在更改某个设置的值之后将引发 PropertyChanged 事件。 + // 在加载设置值之后将引发 SettingsLoaded 事件。 + // 在保存设置值之前将引发 SettingsSaving 事件。 + internal sealed partial class Settings { + + public Settings() { + // // 若要为保存和更改设置添加事件处理程序,请取消注释下列行: + // + // this.SettingChanging += this.SettingChangingEventHandler; + // + // this.SettingsSaving += this.SettingsSavingEventHandler; + // + } + + private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) { + // 在此处添加用于处理 SettingChangingEvent 事件的代码。 + } + + private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) { + // 在此处添加用于处理 SettingsSaving 事件的代码。 + } + } +} diff --git a/shadowsocks-csharp/View/LogForm.cs b/shadowsocks-csharp/View/LogForm.cs index f910a41a..27864526 100644 --- a/shadowsocks-csharp/View/LogForm.cs +++ b/shadowsocks-csharp/View/LogForm.cs @@ -54,9 +54,9 @@ namespace Shadowsocks.View topMostTrigger = config.topMost; wrapTextTrigger = config.wrapText; toolbarTrigger = config.toolbarShown; - LogMessageTextBox.BackColor = config.GetBackgroundColor(); - LogMessageTextBox.ForeColor = config.GetTextColor(); - LogMessageTextBox.Font = config.GetFont(); + LogMessageTextBox.BackColor = config.BackgroundColor; + LogMessageTextBox.ForeColor = config.TextColor; + LogMessageTextBox.Font = config.Font; controller.TrafficChanged += controller_TrafficChanged; @@ -229,11 +229,11 @@ namespace Shadowsocks.View LogViewerConfig config = controller.GetConfigurationCopy().logViewer; - Height = config.height; - Width = config.width; - Top = config.GetBestTop(); - Left = config.GetBestLeft(); - if (config.maximized) + Height = config.Height; + Width = config.Width; + Top = config.BestTop; + Left = config.BestLeft; + if (config.Maximized) { WindowState = FormWindowState.Maximized; } @@ -258,15 +258,15 @@ namespace Shadowsocks.View config.topMost = topMostTrigger; config.wrapText = wrapTextTrigger; config.toolbarShown = toolbarTrigger; - config.SetFont(LogMessageTextBox.Font); - config.SetBackgroundColor(LogMessageTextBox.BackColor); - config.SetTextColor(LogMessageTextBox.ForeColor); - if (WindowState != FormWindowState.Minimized && !(config.maximized = WindowState == FormWindowState.Maximized)) + config.Font=LogMessageTextBox.Font; + config.BackgroundColor=LogMessageTextBox.BackColor; + config.TextColor=LogMessageTextBox.ForeColor; + if (WindowState != FormWindowState.Minimized && !(config.Maximized = WindowState == FormWindowState.Maximized)) { - config.top = Top; - config.left = Left; - config.height = Height; - config.width = Width; + config.Top = Top; + config.Left = Left; + config.Height = Height; + config.Width = Width; } controller.SaveLogViewerConfig(config); } diff --git a/shadowsocks-csharp/app.config b/shadowsocks-csharp/app.config index 5ee9b318..e37432a5 100755 --- a/shadowsocks-csharp/app.config +++ b/shadowsocks-csharp/app.config @@ -1,5 +1,10 @@ + + +
+ + @@ -15,4 +20,23 @@ + + + + 600 + + + 400 + + + True + + + 0 + + + 0 + + + diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index d1881c87..522a65c8 100644 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -141,6 +141,11 @@ + + True + True + Settings.settings + @@ -179,6 +184,7 @@ + @@ -287,6 +293,10 @@ + + SettingsSingleFileGenerator + Settings.Designer.cs +