private void LoadConfig(bool isFirstLoad) { UpdateTitle(); ServerDataGrid.View?.BeginInit(); ServerLogViewModel.ReadConfig(_controller); ServerDataGrid.View?.EndInit(); Dispatcher.CurrentDispatcher.InvokeAsync(() => { if (isFirstLoad && ServerLogViewModel.SelectedServer != null) { ServerDataGrid.ScrollInView(new RowColumnIndex(ServerLogViewModel.SelectedServer.Index, 2)); } }, DispatcherPriority.Input); }
private void ServerDataGrid_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e) { int width = 0; for (int i = 0; i < ServerDataGrid.Columns.Count; ++i) { if (!ServerDataGrid.Columns[i].Visible) { continue; } width += ServerDataGrid.Columns[i].Width; } this.Width = width + SystemInformation.VerticalScrollBarWidth + (this.Width - this.ClientSize.Width) + 1; ServerDataGrid.AutoResizeColumnHeadersHeight(); }
private void ServerLogWindow_OnStateChanged(object sender, EventArgs e) { if (ServerDataGrid.SelectedCells.Count > 0 && ServerDataGrid.SelectedCells[0].Column != null) { if (ServerDataGrid.SelectedCells[0].Item is Server serverObject) { ServerDataGrid.ScrollIntoView(serverObject, ServerColumn); return; } } if (ServerLogViewModel.SelectedServer != null) { ServerDataGrid.ScrollIntoView(ServerLogViewModel.SelectedServer, ServerColumn); } }
private void ServerDataGrid_OnCellTapped(object sender, GridCellTappedEventArgs e) { if (e.ChangedButton != MouseButton.Left) { return; } if (ServerDataGrid.CurrentColumn != null && ServerDataGrid.SelectedItem is Server server) { var index = server.Index - 1; var mappingName = ServerDataGrid.CurrentColumn.MappingName; if (mappingName == Resources[@"ServerMappingName"].ToString()) { _controller.DisconnectAllConnections(true); _controller.SelectServerIndex(index); } else if (mappingName == Resources[@"GroupMappingName"].ToString()) { var group = server.Group; if (!string.IsNullOrEmpty(group)) { var enable = !server.Enable; foreach (var sameGroupServer in ServerLogViewModel.ServersCollection) { if (sameGroupServer.Group == group) { sameGroupServer.Enable = enable; } } Global.SaveConfig(); } } else { return; } ServerDataGrid.ClearSelections(false); ServerDataGrid.SelectCell(server, ServerDataGrid.Columns[0]); } }
public ServerLogForm(ShadowsocksController controller) { this.controller = controller; try { this.Icon = Icon.FromHandle((new Bitmap("icon.png")).GetHicon()); title_perfix = System.Windows.Forms.Application.StartupPath; if (title_perfix.Length > 20) { title_perfix = title_perfix.Substring(0, 20); } } catch { this.Icon = Icon.FromHandle(Resources.ssw128.GetHicon()); } this.Font = System.Drawing.SystemFonts.MessageBoxFont; InitializeComponent(); this.Width = 810; int dpi_mul = Util.Utils.GetDpiMul(); Configuration config = controller.GetCurrentConfiguration(); if (config.configs.Count < 8) { this.Height = 300 * dpi_mul / 4; } else if (config.configs.Count < 20) { this.Height = (300 + (config.configs.Count - 8) * 16) * dpi_mul / 4; } else { this.Height = 500 * dpi_mul / 4; } UpdateTexts(); UpdateLog(); this.Menu = new MainMenu(new MenuItem[] { CreateMenuGroup("&Control", new MenuItem[] { CreateMenuItem("&Disconnect All", new EventHandler(this.Disconnect_Click)), CreateMenuItem("Clear &MaxSpeed", new EventHandler(this.ClearMaxSpeed_Click)), this.clearItem = CreateMenuItem("&Clear", new EventHandler(this.ClearItem_Click)), new MenuItem("-"), CreateMenuItem("Clear &Selected Total", new EventHandler(this.ClearSelectedTotal_Click)), CreateMenuItem("Clear &Total", new EventHandler(this.ClearTotal_Click)), }), CreateMenuGroup("C&opy", new MenuItem[] { CreateMenuItem("Copy current link", new EventHandler(this.copyLinkItem_Click)), CreateMenuItem("Copy all enable links", new EventHandler(this.copyEnableLinksItem_Click)), CreateMenuItem("Copy all links", new EventHandler(this.copyLinksItem_Click)), }), CreateMenuGroup("&Window", new MenuItem[] { CreateMenuItem("Auto &size", new EventHandler(this.autosizeItem_Click)), this.topmostItem = CreateMenuItem("Always On &Top", new EventHandler(this.topmostItem_Click)), }), }); controller.ConfigChanged += controller_ConfigChanged; for (int i = 0; i < ServerDataGrid.Columns.Count; ++i) { ServerDataGrid.Columns[i].Width = ServerDataGrid.Columns[i].Width * dpi_mul / 4; } ServerDataGrid.RowTemplate.Height = 20 * dpi_mul / 4; //ServerDataGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None; int width = 0; for (int i = 0; i < ServerDataGrid.Columns.Count; ++i) { if (!ServerDataGrid.Columns[i].Visible) { continue; } width += ServerDataGrid.Columns[i].Width; } this.Width = width + SystemInformation.VerticalScrollBarWidth + (this.Width - this.ClientSize.Width) + 1; ServerDataGrid.AutoResizeColumnHeadersHeight(); }
public void RefreshLog() { if (ServerSpeedLogList == null) { return; } int last_rowcount = ServerDataGrid.RowCount; Configuration config = controller.GetCurrentConfiguration(); if (listOrder.Count > config.configs.Count) { listOrder.RemoveRange(config.configs.Count, listOrder.Count - config.configs.Count); } while (listOrder.Count < config.configs.Count) { listOrder.Add(0); } while (ServerDataGrid.RowCount < config.configs.Count) { ServerDataGrid.Rows.Add(); int id = ServerDataGrid.RowCount - 1; ServerDataGrid[0, id].Value = id; } if (ServerDataGrid.RowCount > config.configs.Count) { for (int list_index = 0; list_index < ServerDataGrid.RowCount; ++list_index) { DataGridViewCell id_cell = ServerDataGrid[0, list_index]; int id = (int)id_cell.Value; if (id >= config.configs.Count) { ServerDataGrid.Rows.RemoveAt(list_index); --list_index; } } } try { for (int list_index = (lastRefreshIndex >= ServerDataGrid.RowCount) ? 0 : lastRefreshIndex, rowChangeCnt = 0; list_index < ServerDataGrid.RowCount && rowChangeCnt <= 200; ++list_index, ++rowChangeCnt) { lastRefreshIndex = list_index + 1; DataGridViewCell id_cell = ServerDataGrid[0, list_index]; int id = (int)id_cell.Value; Server server = config.configs[id]; ServerSpeedLogShow serverSpeedLog = ServerSpeedLogList[id]; listOrder[id] = list_index; rowChange = false; for (int curcol = 0; curcol < ServerDataGrid.Columns.Count; ++curcol) { DataGridViewCell cell = ServerDataGrid[curcol, list_index]; string columnName = ServerDataGrid.Columns[curcol].Name; // Server if (columnName == "Server") { if (config.index == id) { SetBackColor(cell, Color.Cyan); } else { SetBackColor(cell, Color.White); } SetCellText(cell, server.FriendlyName()); } if (columnName == "Group") { SetCellText(cell, server.group); } // Enable if (columnName == "Enable") { if (server.isEnable()) { SetBackColor(cell, Color.White); } else { SetBackColor(cell, Color.Red); } } // TotalConnectTimes else if (columnName == "TotalConnect") { SetCellText(cell, serverSpeedLog.totalConnectTimes); } // TotalConnecting else if (columnName == "Connecting") { long connections = serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes; //long ref_connections = server.GetConnections().Count; //if (ref_connections < connections) //{ // connections = ref_connections; //} Color[] colList = new Color[5] { Color.White, Color.LightGreen, Color.Yellow, Color.Red, Color.Red }; long[] bytesList = new long[5] { 0, 16, 32, 64, 65536 }; for (int i = 1; i < colList.Length; ++i) { if (connections < bytesList[i]) { SetBackColor(cell, ColorMix(colList[i - 1], colList[i], (double)(connections - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1]) ) ); break; } } SetCellText(cell, serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes); } // AvgConnectTime else if (columnName == "AvgLatency") { if (serverSpeedLog.avgConnectTime >= 0) { SetCellText(cell, serverSpeedLog.avgConnectTime); } else { SetCellText(cell, "-"); } } // AvgDownSpeed else if (columnName == "AvgDownSpeed") { long avgBytes = serverSpeedLog.avgDownloadBytes; string valStr = FormatBytes(avgBytes); Color[] colList = new Color[6] { Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red }; long[] bytesList = new long[6] { 0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024L * 1024 * 1024 * 1024 }; for (int i = 1; i < colList.Length; ++i) { if (avgBytes < bytesList[i]) { SetBackColor(cell, ColorMix(colList[i - 1], colList[i], (double)(avgBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1]) ) ); break; } } SetCellText(cell, valStr); } // MaxDownSpeed else if (columnName == "MaxDownSpeed") { long maxBytes = serverSpeedLog.maxDownloadBytes; string valStr = FormatBytes(maxBytes); Color[] colList = new Color[6] { Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red }; long[] bytesList = new long[6] { 0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024 * 1024 * 1024 }; for (int i = 1; i < colList.Length; ++i) { if (maxBytes < bytesList[i]) { SetBackColor(cell, ColorMix(colList[i - 1], colList[i], (double)(maxBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1]) ) ); break; } } SetCellText(cell, valStr); } // AvgUpSpeed else if (columnName == "AvgUpSpeed") { long avgBytes = serverSpeedLog.avgUploadBytes; string valStr = FormatBytes(avgBytes); Color[] colList = new Color[6] { Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red }; long[] bytesList = new long[6] { 0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024L * 1024 * 1024 * 1024 }; for (int i = 1; i < colList.Length; ++i) { if (avgBytes < bytesList[i]) { SetBackColor(cell, ColorMix(colList[i - 1], colList[i], (double)(avgBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1]) ) ); break; } } SetCellText(cell, valStr); } // MaxUpSpeed else if (columnName == "MaxUpSpeed") { long maxBytes = serverSpeedLog.maxUploadBytes; string valStr = FormatBytes(maxBytes); Color[] colList = new Color[6] { Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red }; long[] bytesList = new long[6] { 0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024 * 1024 * 1024 }; for (int i = 1; i < colList.Length; ++i) { if (maxBytes < bytesList[i]) { SetBackColor(cell, ColorMix(colList[i - 1], colList[i], (double)(maxBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1]) ) ); break; } } SetCellText(cell, valStr); } // TotalUploadBytes else if (columnName == "Upload") { string valStr = FormatBytes(serverSpeedLog.totalUploadBytes); string fullVal = serverSpeedLog.totalUploadBytes.ToString(); if (cell.ToolTipText != fullVal) { if (fullVal == "0") { SetBackColor(cell, Color.FromArgb(0xf4, 0xff, 0xf4)); } else { SetBackColor(cell, Color.LightGreen); cell.Tag = 8; } } else if (cell.Tag != null) { cell.Tag = (int)cell.Tag - 1; if ((int)cell.Tag == 0) { SetBackColor(cell, Color.FromArgb(0xf4, 0xff, 0xf4)); } //Color col = cell.Style.BackColor; //SetBackColor(cell, Color.FromArgb(Math.Min(255, col.R + colAdd), Math.Min(255, col.G + colAdd), Math.Min(255, col.B + colAdd))); } SetCellToolTipText(cell, fullVal); SetCellText(cell, valStr); } // TotalDownloadBytes else if (columnName == "Download") { string valStr = FormatBytes(serverSpeedLog.totalDownloadBytes); string fullVal = serverSpeedLog.totalDownloadBytes.ToString(); if (cell.ToolTipText != fullVal) { if (fullVal == "0") { SetBackColor(cell, Color.FromArgb(0xff, 0xf0, 0xf0)); } else { SetBackColor(cell, Color.LightGreen); cell.Tag = 8; } } else if (cell.Tag != null) { cell.Tag = (int)cell.Tag - 1; if ((int)cell.Tag == 0) { SetBackColor(cell, Color.FromArgb(0xff, 0xf0, 0xf0)); } //Color col = cell.Style.BackColor; //SetBackColor(cell, Color.FromArgb(Math.Min(255, col.R + colAdd), Math.Min(255, col.G + colAdd), Math.Min(255, col.B + colAdd))); } SetCellToolTipText(cell, fullVal); SetCellText(cell, valStr); } else if (columnName == "DownloadRaw") { string valStr = FormatBytes(serverSpeedLog.totalDownloadRawBytes); string fullVal = serverSpeedLog.totalDownloadRawBytes.ToString(); if (cell.ToolTipText != fullVal) { if (fullVal == "0") { SetBackColor(cell, Color.FromArgb(0xff, 0x80, 0x80)); } else { SetBackColor(cell, Color.LightGreen); cell.Tag = 8; } } else if (cell.Tag != null) { cell.Tag = (int)cell.Tag - 1; if ((int)cell.Tag == 0) { SetBackColor(cell, Color.FromArgb(0xf0, 0xf0, 0xff)); } //Color col = cell.Style.BackColor; //SetBackColor(cell, Color.FromArgb(Math.Min(255, col.R + colAdd), Math.Min(255, col.G + colAdd), Math.Min(255, col.B + colAdd))); } SetCellToolTipText(cell, fullVal); SetCellText(cell, valStr); } // ErrorConnectTimes else if (columnName == "ConnectError") { long val = serverSpeedLog.errorConnectTimes + serverSpeedLog.errorDecodeTimes; Color col = Color.FromArgb(255, (byte)Math.Max(0, 255 - val * 2.5), (byte)Math.Max(0, 255 - val * 2.5)); SetBackColor(cell, col); SetCellText(cell, val); } // ErrorTimeoutTimes else if (columnName == "ConnectTimeout") { SetCellText(cell, serverSpeedLog.errorTimeoutTimes); } // ErrorTimeoutTimes else if (columnName == "ConnectEmpty") { long val = serverSpeedLog.errorEmptyTimes; Color col = Color.FromArgb(255, (byte)Math.Max(0, 255 - val * 8), (byte)Math.Max(0, 255 - val * 8)); SetBackColor(cell, col); SetCellText(cell, val); } // ErrorContinurousTimes else if (columnName == "Continuous") { long val = serverSpeedLog.errorContinurousTimes; Color col = Color.FromArgb(255, (byte)Math.Max(0, 255 - val * 8), (byte)Math.Max(0, 255 - val * 8)); SetBackColor(cell, col); SetCellText(cell, val); } // ErrorPersent else if (columnName == "ErrorPercent") { if (serverSpeedLog.errorLogTimes + serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes > 0) { double percent = (serverSpeedLog.errorConnectTimes + serverSpeedLog.errorTimeoutTimes + serverSpeedLog.errorDecodeTimes) * 100.00 / (serverSpeedLog.errorLogTimes + serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes); SetBackColor(cell, Color.FromArgb(255, (byte)(255 - percent * 2), (byte)(255 - percent * 2))); SetCellText(cell, percent.ToString("F0") + "%"); } else { SetBackColor(cell, Color.White); SetCellText(cell, "-"); } } if (columnName == "Server") { if (cell.Style.Alignment != DataGridViewContentAlignment.MiddleLeft) { cell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft; } } else { if (cell.Style.Alignment != DataGridViewContentAlignment.MiddleRight) { cell.Style.Alignment = DataGridViewContentAlignment.MiddleRight; } } } if (rowChange) { rowChangeCnt++; } } } catch { } if (ServerDataGrid.SortedColumn != null) { ServerDataGrid.Sort(ServerDataGrid.SortedColumn, (ListSortDirection)((int)ServerDataGrid.SortOrder - 1)); } if (last_rowcount == 0 && config.index >= 0 && config.index < ServerDataGrid.RowCount) { ServerDataGrid[0, config.index].Selected = true; } }