From e01a50c42a2d61fc7ab46444d119e9fae9d7492c Mon Sep 17 00:00:00 2001 From: clowwindy Date: Mon, 14 Jan 2013 19:45:28 +0800 Subject: [PATCH] implement window events --- shadowsocks-csharp/Config.cs | 23 ++++++- shadowsocks-csharp/Form1.Designer.cs | 34 ++++++++-- shadowsocks-csharp/Form1.cs | 70 ++++++++++++++++++-- shadowsocks-csharp/shadowsocks-csharp.csproj | 2 +- 4 files changed, 112 insertions(+), 17 deletions(-) diff --git a/shadowsocks-csharp/Config.cs b/shadowsocks-csharp/Config.cs index 41afe134..b981e782 100755 --- a/shadowsocks-csharp/Config.cs +++ b/shadowsocks-csharp/Config.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Runtime.Serialization.Json; using System.Text; using System.IO; +using System.Diagnostics; namespace shadowsocks_csharp { @@ -14,6 +15,17 @@ namespace shadowsocks_csharp public int local_port; public string password; + [NonSerialized] + public bool isDefault; + + private static void assert(bool condition) + { + if(!condition) + { + throw new Exception("assertion failure"); + } + } + public static Config Load() { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Config)); @@ -22,17 +34,24 @@ namespace shadowsocks_csharp using (FileStream fs = File.OpenRead(@"config.json")) { Config config = ser.ReadObject(fs) as Config; + assert(!string.IsNullOrEmpty(config.server)); + assert(!string.IsNullOrEmpty(config.password)); + assert(config.local_port > 0); + assert(config.server_port > 0); + config.isDefault = false; return config; } } - catch (IOException) + catch (Exception e) { + Console.WriteLine(e); return new Config { server = "127.0.0.1", server_port = 8388, local_port = 1081, - password = "barfoo!" + password = "barfoo!", + isDefault = true }; } } diff --git a/shadowsocks-csharp/Form1.Designer.cs b/shadowsocks-csharp/Form1.Designer.cs index 31b165e1..7cca0a29 100755 --- a/shadowsocks-csharp/Form1.Designer.cs +++ b/shadowsocks-csharp/Form1.Designer.cs @@ -45,7 +45,9 @@ this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.ConfigItem = new System.Windows.Forms.ToolStripMenuItem(); + this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.QuitItem = new System.Windows.Forms.ToolStripMenuItem(); + this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.contextMenuStrip1.SuspendLayout(); @@ -175,7 +177,7 @@ this.button2.TabIndex = 1; this.button2.Text = "Cancel"; this.button2.UseVisualStyleBackColor = true; - this.button2.Click += new System.EventHandler(this.button2_Click); + this.button2.Click += new System.EventHandler(this.cancelButton_Click); // // button1 // @@ -186,39 +188,54 @@ this.button1.TabIndex = 0; this.button1.Text = "OK"; this.button1.UseVisualStyleBackColor = true; - this.button1.Click += new System.EventHandler(this.button1_Click); + this.button1.Click += new System.EventHandler(this.OKButton_Click); // // notifyIcon1 // this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1; this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon"))); - this.notifyIcon1.Text = "notifyIcon1"; + this.notifyIcon1.Text = "shadowsocks"; this.notifyIcon1.Visible = true; + this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick); // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.ConfigItem, + this.aboutToolStripMenuItem, + this.toolStripSeparator2, this.QuitItem}); this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System; - this.contextMenuStrip1.ShowImageMargin = false; - this.contextMenuStrip1.Size = new System.Drawing.Size(86, 48); + this.contextMenuStrip1.Size = new System.Drawing.Size(111, 76); // // ConfigItem // + this.ConfigItem.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold); this.ConfigItem.Name = "ConfigItem"; - this.ConfigItem.Size = new System.Drawing.Size(85, 22); + this.ConfigItem.Size = new System.Drawing.Size(110, 22); this.ConfigItem.Text = "Config"; this.ConfigItem.Click += new System.EventHandler(this.Config_Click); // + // aboutToolStripMenuItem + // + this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem"; + this.aboutToolStripMenuItem.Size = new System.Drawing.Size(110, 22); + this.aboutToolStripMenuItem.Text = "About"; + this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); + // // QuitItem // this.QuitItem.Name = "QuitItem"; - this.QuitItem.Size = new System.Drawing.Size(85, 22); + this.QuitItem.Size = new System.Drawing.Size(110, 22); this.QuitItem.Text = "Quit"; this.QuitItem.Click += new System.EventHandler(this.Quit_Click); // + // toolStripSeparator2 + // + this.toolStripSeparator2.Name = "toolStripSeparator2"; + this.toolStripSeparator2.Size = new System.Drawing.Size(107, 6); + // // Form1 // this.AcceptButton = this.button1; @@ -233,6 +250,7 @@ this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Shadowsocks"; + this.Load += new System.EventHandler(this.Form1_Load); this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed); this.tableLayoutPanel1.ResumeLayout(false); this.tableLayoutPanel1.PerformLayout(); @@ -261,6 +279,8 @@ private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.ToolStripMenuItem ConfigItem; private System.Windows.Forms.ToolStripMenuItem QuitItem; + private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem; + private System.Windows.Forms.ToolStripSeparator toolStripSeparator2; } } diff --git a/shadowsocks-csharp/Form1.cs b/shadowsocks-csharp/Form1.cs index 587d0411..b6a8d8d8 100755 --- a/shadowsocks-csharp/Form1.cs +++ b/shadowsocks-csharp/Form1.cs @@ -5,6 +5,7 @@ using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; +using System.Diagnostics; namespace shadowsocks_csharp { @@ -16,14 +17,36 @@ namespace shadowsocks_csharp public Form1() { config = Config.Load(); - reload(config); InitializeComponent(); + configToTextBox(); + } + + private void showWindow() + { + this.Opacity = 1; + this.Show(); + } + + private void configToTextBox() + { textBox1.Text = config.server; textBox2.Text = config.server_port.ToString(); textBox3.Text = config.password; textBox4.Text = config.local_port.ToString(); } + private void Form1_Load(object sender, EventArgs e) + { + if (!config.isDefault) + { + this.Opacity = 0; + reload(config); BeginInvoke(new MethodInvoker(delegate + { + this.Hide(); + })); + } + } + private void reload(Config config) { if (local != null) @@ -37,22 +60,45 @@ namespace shadowsocks_csharp private void Config_Click(object sender, EventArgs e) { - + showWindow(); } private void Quit_Click(object sender, EventArgs e) { - + this.Close(); } - private void button1_Click(object sender, EventArgs e) + private void OKButton_Click(object sender, EventArgs e) { - reload(Config.Load()); + try + { + Config config = new Config + { + server = textBox1.Text, + server_port = int.Parse(textBox2.Text), + password = textBox3.Text, + local_port = int.Parse(textBox4.Text), + isDefault = false + }; + Config.Save(config); + this.config = config; + reload(config); + this.Hide(); + } + catch (FormatException) + { + MessageBox.Show("there is format problem"); + } + catch (Exception) + { + MessageBox.Show("there is some problem with parameters"); + } } - private void button2_Click(object sender, EventArgs e) + private void cancelButton_Click(object sender, EventArgs e) { - + this.Hide(); + configToTextBox(); } private void Form1_FormClosed(object sender, FormClosedEventArgs e) @@ -60,5 +106,15 @@ namespace shadowsocks_csharp local.Stop(); } + private void aboutToolStripMenuItem_Click(object sender, EventArgs e) + { + Process.Start("https://github.com/clowwindy/shadowsocks-csharp"); + } + + private void notifyIcon1_DoubleClick(object sender, EventArgs e) + { + showWindow(); + } + } } diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 349a69cb..51c38f8b 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -6,7 +6,7 @@ 9.0.21022 2.0 {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062} - Exe + WinExe Properties shadowsocks_csharp shadowsocks-csharp