Files
ucvl-zonghengjia-open-source/windows/MainForm.Chrome.cs

76 lines
2.4 KiB
C#

namespace UCVL.VPNPlus;
internal sealed partial class MainForm
{
private WindowChrome? _windowChrome;
private bool _minimizedToTray;
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, MinimizeToTray);
_windowChrome.SetLoginMode(true);
}
private void LayoutWindowChrome() => _windowChrome?.LayoutButtons();
// Keep the core and timers alive. Only the window presentation changes;
// restore/exit remain the existing tray menu actions.
private void MinimizeToTray()
{
if (_exiting || IsDisposed || Disposing || _windowChrome is not { LoginMode: false }) return;
_minimizedToTray = true;
Hide();
ShowInTaskbar = false;
UpdateTaskbarPresentation();
}
private void RestoreFromTrayPresentation()
{
_minimizedToTray = false;
ShowInTaskbar = true;
WindowState = FormWindowState.Normal;
UpdateTaskbarPresentation();
}
private void UpdateTaskbarPresentation()
{
if (_exitResourcesDisposed) return;
if (!_minimizedToTray && Visible && WindowState == FormWindowState.Minimized
&& _windowChrome is { LoginMode: false })
{
MinimizeToTray();
return;
}
_notifyIcon.Visible = ShouldShowTrayIcon(WindowState, _preview || _uiOnly || _exiting);
}
internal static bool ShouldShowTrayIcon(FormWindowState state, bool suppressed) =>
!suppressed;
internal bool MinimizedToTrayForTest => _minimizedToTray && !Visible && !ShowInTaskbar && !_exiting
&& (_preview || _uiOnly || _notifyIcon.Visible);
}