using System.Drawing.Drawing2D; namespace UCVL.VPNPlus; internal sealed partial class MainForm { private WindowChrome? _windowChrome; private ContextMenuStrip? _settingsMenu; private readonly DashboardHeaderIdentity _dashboardHeaderName = new(); private readonly DashboardHeaderButton _dashboardSettings = new("设置", DashboardHeaderIcon.Settings); private readonly DashboardHeaderButton _dashboardAbout = new("关于", DashboardHeaderIcon.About); private readonly DashboardHeaderButton _dashboardLogout = new("退出账号", DashboardHeaderIcon.Logout); private readonly DashboardHeaderButton _dashboardMore = new("", DashboardHeaderIcon.Settings) { AccessibleName = "设置" }; internal WindowChrome? WindowChromeForTest => _windowChrome; // Remove the native caption before HWND creation, not only after its first // style/paint messages. This avoids a transient second caption at startup. protected override CreateParams CreateParams { get { var parameters = base.CreateParams; parameters.Style = unchecked((int)ChromeNative.ClientChromeStyle(parameters.Style)); return parameters; } } protected override void OnLoad(EventArgs e) { base.OnLoad(e); LayoutBrandHeader(); CenterCards(); LayoutDashboard(); } // Explicit title/tray Exit and installer IPC share the graceful exit path. private void InitializeWindowChrome(Panel header) { if (_windowChrome is not null) return; _windowChrome = new WindowChrome(this, header, RequestExit, MinimizeToTrayPresentation); _windowChrome.SetLoginMode(true); InitializeDashboardHeader(header); } private void LayoutWindowChrome() => RefreshDashboardHeaderPresentation(); private void InitializeDashboardHeader(Panel header) { _dashboardHeaderName.Visible = _dashboardSettings.Visible = _dashboardAbout.Visible = _dashboardLogout.Visible = _dashboardMore.Visible = false; _dashboardSettings.TabIndex = 890; _dashboardAbout.TabIndex = 891; _dashboardLogout.TabIndex = 892; _dashboardMore.TabIndex = 890; _logoutButton.TabIndex = 892; _dashboardSettings.Click += (_, _) => OpenDashboardMenu(_dashboardSettings, includeApplicationActions: false); _dashboardMore.Click += (_, _) => OpenDashboardMenu(_dashboardMore, includeApplicationActions: true); _dashboardAbout.Click += (_, _) => ShowDashboardAbout(); _dashboardLogout.Click += async (_, _) => await LogoutAsync(); var tip = new ToolTip(); _welcomeLabel.TextChanged += (_, _) => { _dashboardHeaderName.Text = _welcomeLabel.Text; tip.SetToolTip(_dashboardHeaderName, _welcomeLabel.Text); tip.SetToolTip(_welcomeLabel, _welcomeLabel.Text); }; _logoutButton.EnabledChanged += (_, _) => _dashboardLogout.Enabled = _logoutButton.Enabled; _dashboardView.VisibleChanged += (_, _) => { if (!Disposing && !IsDisposed) LayoutBrandHeader(); }; header.SizeChanged += (_, _) => RefreshDashboardHeaderPresentation(); tip.SetToolTip(_dashboardSettings, "打开调度、节点、代理模式及趋势范围设置"); tip.SetToolTip(_dashboardMore, "设置与关于"); header.Disposed += (_, _) => { tip.Dispose(); _settingsMenu?.Dispose(); }; header.Controls.AddRange([_dashboardHeaderName, _dashboardSettings, _dashboardAbout, _dashboardLogout, _dashboardMore]); } private void RefreshDashboardHeaderPresentation() { if (Disposing || IsDisposed || _windowChrome is not { } chrome || _brandHeader.IsDisposed) return; _content.HeaderArtworkOffset = _dashboardView.HeaderArtworkOffset = _brandHeader.Height; var wide = WideDashboardPresentation && _dashboardView.Visible; _dashboardHeaderName.Visible = _dashboardSettings.Visible = _dashboardAbout.Visible = _dashboardLogout.Visible = wide; chrome.LayoutButtons(); if (!CompactPresentation) LayoutExpandedBrandBounds(); if (!wide) { _headerPoem.Visible = !CompactPresentation && _brandHeader.Width >= ScaleLogical(780); LayoutCompactDashboardActions(); return; } _dashboardMore.Visible = false; _headerPoem.Visible = false; var caption = chrome.MinimizeButton.Bounds; int S(int value) => ScaleLogical(value); int x = caption.Left - S(10); foreach (var (button, width) in new[] { (_dashboardLogout, 106), (_dashboardAbout, 76), (_dashboardSettings, 76) }) { x -= S(width); button.Bounds = new Rectangle(x, caption.Top, S(width), caption.Height); x -= S(8); } var nameWidth = Math.Min(S(224), Math.Max(S(96), x - S(316))); _dashboardHeaderName.Bounds = new Rectangle(x - nameWidth, caption.Top, nameWidth, caption.Height); } private void LayoutExpandedBrandBounds() { int S(int value) => ScaleLogical(value); foreach (Control control in _brandHeader.Controls) { if (control.Tag is not Rectangle) continue; if (control is PictureBox) control.Bounds = new Rectangle(S(24), S(9), S(64), S(68)); else if (control is Label { Text: "纵横家" }) control.Bounds = new Rectangle(S(99), S(10), S(146), S(36)); else control.Bounds = new Rectangle(S(101), S(47), S(210), S(24)); } } private void LayoutCompactDashboardActions() { var show = CompactPresentation && _dashboardView.Visible && _logoutButton.Parent == _brandHeader; _dashboardMore.Visible = show; _welcomeLabel.Visible = false; if (!show) return; int S(int value) => ScaleLogical(value); var caption = _windowChrome!.MinimizeButton.Bounds; var settingsLeft = caption.Left - S(38); _dashboardMore.Bounds = new Rectangle(settingsLeft, caption.Top, S(32), caption.Height); _logoutButton.Bounds = new Rectangle(settingsLeft - S(70), caption.Top, S(64), caption.Height); } private void ShowDashboardAbout() => MessageBox.Show(this, $"纵横家 v{GetDisplayVersion()}\n智能安全网络\n\n优创未来(UCVL)\n管理服务器 {PlatformBootstrapTransport.ManagementAddress}", "关于纵横家", MessageBoxButtons.OK, MessageBoxIcon.Information); private void OpenDashboardMenu(Control anchor, bool includeApplicationActions) { if (_exiting || _settingsMenu?.Visible == true) return; _settingsMenu?.Dispose(); var menu = _settingsMenu = new ContextMenuStrip { BackColor = Brand.Surface, ForeColor = Brand.Text, ShowImageMargin = false }; var options = menu.Items; if (includeApplicationActions) { var account = _accountLabel.Text; if (!string.IsNullOrWhiteSpace(account)) { options.Add(new ToolStripLabel("当前账号 " + account)); options.Add(new ToolStripSeparator()); } menu.Items.Add("关于纵横家", null, (_, _) => ShowDashboardAbout()); menu.Items.Add(new ToolStripSeparator()); } foreach (var (label, combo) in new[] { ("调度方式", _connectionModeCombo), ("工作节点", _manualNodeCombo), ("代理模式", _routeCombo), ("流量趋势时间范围", _historyRangeCombo) }) { var item = options.Add(label); item.Enabled = combo.Visible && combo.Enabled && combo.Items.Count > 0; item.Click += (_, _) => BeginInvoke(() => { if (_exiting || !combo.Visible || !combo.Enabled) return; combo.Focus(); combo.DroppedDown = true; }); } menu.Show(anchor, new Point(anchor.Width, anchor.Height), ToolStripDropDownDirection.BelowLeft); } private void MinimizeToTrayPresentation() { if (_exiting || IsDisposed || Disposing) return; UpdateTaskbarPresentation(); Hide(); ShowInTaskbar = false; } private void RestoreFromTrayPresentation() { ShowInTaskbar = true; WindowState = FormWindowState.Normal; UpdateTaskbarPresentation(); } private void UpdateTaskbarPresentation() { if (_exitResourcesDisposed) return; _notifyIcon.Visible = ShouldShowTrayIcon(WindowState, _preview || _uiOnly || _exiting); } internal static bool ShouldShowTrayIcon(FormWindowState state, bool suppressed) => !suppressed; internal bool MinimizedOnTaskbarForTest => WindowState == FormWindowState.Minimized && Visible && ShowInTaskbar && !_exiting; } internal enum DashboardHeaderIcon { User, Settings, About, Logout, More } internal sealed class DashboardHeaderButton : Button { private readonly DashboardHeaderIcon _glyph; private bool _hovered; internal DashboardHeaderButton(string text, DashboardHeaderIcon glyph) { _glyph = glyph; Text = text; AccessibleName = text; Font = Brand.Font(10f); Cursor = Cursors.Hand; FlatStyle = FlatStyle.Flat; FlatAppearance.BorderSize = 0; UseVisualStyleBackColor = false; SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.Opaque, false); BackColor = Color.Transparent; } protected override void OnMouseEnter(EventArgs e) { _hovered = true; Invalidate(); base.OnMouseEnter(e); } protected override void OnMouseLeave(EventArgs e) { _hovered = false; Invalidate(); base.OnMouseLeave(e); } protected override void OnGotFocus(EventArgs e) { Invalidate(); base.OnGotFocus(e); } protected override void OnLostFocus(EventArgs e) { Invalidate(); base.OnLostFocus(e); } protected override void OnPaint(PaintEventArgs e) { if (Width < 8 || Height < 8) return; var g = e.Graphics; var s = DeviceDpi / 96f; int S(int value) => Math.Max(1, (int)Math.Round(value * s)); g.SmoothingMode = SmoothingMode.AntiAlias; using var path = UiGeometry.RoundedRectangle(Rectangle.Inflate(ClientRectangle, -2, -2), S(10)); using var fill = new SolidBrush(_hovered ? Color.FromArgb(237, 220, 239, 255) : Color.FromArgb(176, 242, 250, 255)); g.FillPath(fill, path); using var border = new Pen(Color.FromArgb(177, 151, 189, 225), Math.Max(1, s)); g.DrawPath(border, path); var color = Enabled ? Brand.Navy : Brand.Muted; var flags = TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding; var iconSize = S(17); var hasText = Text.Length != 0; var textWidth = hasText ? TextRenderer.MeasureText(g, Text, Font, Size.Empty, flags).Width : 0; var gap = hasText ? S(5) : 0; var left = Math.Max(S(3), (Width - iconSize - gap - textWidth) / 2); DashboardHeaderDrawing.Paint(g, _glyph, new Rectangle(left, (Height - iconSize) / 2, iconSize, iconSize), color); if (hasText) TextRenderer.DrawText(g, Text, Font, new Rectangle(left + iconSize + gap, 0, Math.Max(1, Width - left - iconSize - gap - S(3)), Height), color, flags | TextFormatFlags.EndEllipsis); if (Focused && ShowFocusCues) DashboardHeaderDrawing.Focus(g, ClientRectangle, DeviceDpi, color); } } internal sealed class DashboardHeaderIdentity : Label { internal DashboardHeaderIdentity() { BackColor = Color.Transparent; ForeColor = Brand.Navy; Font = Brand.Font(11f, FontStyle.Bold); AccessibleName = "当前登录用户"; } protected override void OnPaint(PaintEventArgs e) { var iconSize = (int)Math.Round(18 * DeviceDpi / 96.0); var gap = (int)Math.Round(6 * DeviceDpi / 96.0); var flags = TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding; var textWidth = TextRenderer.MeasureText(e.Graphics, Text, Font, Size.Empty, flags).Width; var left = Math.Max(0, Width - iconSize - gap - textWidth - gap); DashboardHeaderDrawing.Paint(e.Graphics, DashboardHeaderIcon.User, new Rectangle(left, (Height - iconSize) / 2, iconSize, iconSize), ForeColor); var textLeft = left + iconSize + gap; TextRenderer.DrawText(e.Graphics, Text, Font, new Rectangle(textLeft, 0, Math.Max(1, Width - textLeft), Height), ForeColor, flags | TextFormatFlags.EndEllipsis); } } internal static class DashboardHeaderDrawing { internal static void Focus(Graphics graphics, Rectangle bounds, int dpi, Color color) { var scale = dpi / 96f; var inset = Math.Max(3, (int)Math.Round(4 * scale)); var inner = Rectangle.Inflate(bounds, -inset, -inset); if (inner.Width <= 0 || inner.Height <= 0) return; using var path = UiGeometry.RoundedRectangle(inner, Math.Max(2, (int)Math.Round(5 * scale))); using var pen = new Pen(SystemInformation.HighContrast ? SystemColors.Highlight : color, Math.Max(1.25f, 1.4f * scale)); graphics.DrawPath(pen, path); } internal static void Paint(Graphics g, DashboardHeaderIcon glyph, Rectangle bounds, Color color) { if (glyph == DashboardHeaderIcon.Settings) { DashboardDrawing.Icon(g, DashboardIcon.Settings, bounds, color); return; } var state = g.Save(); try { g.SmoothingMode = SmoothingMode.AntiAlias; g.TranslateTransform(bounds.Left, bounds.Top); g.ScaleTransform(bounds.Width / 24f, bounds.Height / 24f); using var pen = new Pen(color, 1.85f) { StartCap = LineCap.Round, EndCap = LineCap.Round, LineJoin = LineJoin.Round }; switch (glyph) { case DashboardHeaderIcon.User: g.DrawEllipse(pen, 8, 2, 8, 8); g.DrawArc(pen, 3, 11, 18, 15, 180, 180); g.DrawLine(pen, 3, 18.5f, 21, 18.5f); break; case DashboardHeaderIcon.About: g.DrawEllipse(pen, 2, 2, 20, 20); g.DrawLine(pen, 12, 11, 12, 17); g.DrawLine(pen, 12, 7.5f, 12, 8); break; case DashboardHeaderIcon.Logout: g.DrawLines(pen, [new PointF(11, 3), new(3, 3), new(3, 21), new(11, 21)]); g.DrawLine(pen, 9, 12, 21, 12); g.DrawLines(pen, [new PointF(16, 7), new(21, 12), new(16, 17)]); break; case DashboardHeaderIcon.More: using (var dot = new SolidBrush(color)) for (var x = 3; x <= 19; x += 8) g.FillEllipse(dot, x, 10.5f, 3.25f, 3.25f); break; } } finally { g.Restore(state); } } }