using System.Drawing.Drawing2D; namespace UCVL.VPNPlus; internal sealed partial class MainForm { private WindowChrome? _windowChrome; private ContextMenuStrip? _settingsMenu; private long _settingsDismissedAt = long.MinValue; 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 = "设置" }; private readonly DashboardHeaderButton _dashboardClose = new("", DashboardHeaderIcon.Close) { 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); _windowChrome.SetLoginMode(true); InitializeDashboardHeader(header); } private void LayoutWindowChrome() => RefreshDashboardHeaderPresentation(); private void InitializeDashboardHeader(Panel header) { _dashboardHeaderName.Visible = _dashboardSettings.Visible = _dashboardAbout.Visible = _dashboardLogout.Visible = _dashboardMore.Visible = _dashboardClose.Visible = false; _dashboardSettings.TabIndex = 890; _dashboardAbout.TabIndex = 891; _dashboardLogout.TabIndex = 892; _dashboardMore.TabIndex = 890; _logoutButton.TabIndex = 892; _dashboardClose.TabIndex = 901; _dashboardSettings.Click += (_, _) => OpenDashboardMenu(_dashboardSettings, includeApplicationActions: false); _dashboardMore.Click += (_, _) => OpenDashboardMenu(_dashboardMore, includeApplicationActions: true); _dashboardAbout.Click += (_, _) => ShowDashboardAbout(); _dashboardLogout.Click += async (_, _) => await LogoutAsync(); _dashboardClose.Click += (_, _) => { if (_settingsMenu?.Visible == true) { _settingsMenu.Close(); return; } if (_settingsDismissedAt != long.MinValue && Environment.TickCount64 - _settingsDismissedAt < 350) return; RequestExit(); }; 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(_dashboardClose, "断开并退出"); tip.SetToolTip(_dashboardSettings, "打开调度、节点、代理模式及趋势范围设置"); tip.SetToolTip(_dashboardMore, "设置与关于"); header.Disposed += (_, _) => tip.Dispose(); header.Controls.AddRange([_dashboardHeaderName, _dashboardSettings, _dashboardAbout, _dashboardLogout, _dashboardMore, _dashboardClose]); } 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; _dashboardClose.Visible = chrome.DashboardCloseSlot = _dashboardView.Visible; chrome.LayoutButtons(); _dashboardClose.Bounds = chrome.DashboardCloseBounds; _dashboardClose.BringToFront(); 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; _dashboardClose.Bounds = chrome.DashboardCloseBounds; 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; 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); var nameWidth = Math.Clamp(_logoutButton.Left - S(191), S(70), S(110)); _welcomeLabel.Bounds = new Rectangle(_logoutButton.Left - nameWidth - S(6), caption.Top, nameWidth, caption.Height); foreach (var label in _brandHeader.Controls.OfType