Windows 0.3.3 dashboard layout and speed presentation source delta; no private history or other platform changes

This commit is contained in:
2026-11-03 15:15:23 +00:00
parent bfe8d6c62a
commit aff1a50d35
32 changed files with 1673 additions and 652 deletions
+168 -12
View File
@@ -89,6 +89,14 @@ internal sealed class MetricCard : CardPanel
internal MetricCard(string heading, DashboardIcon glyph) { Heading = heading; Glyph = glyph; AccessibleName = heading; }
internal void SetBytes(double? bytes, bool speed = false, string? hint = null)
{
if (speed)
{
(ValueText, UnitText) = SpeedDisplay.Parts(bytes);
Hint = hint ?? "仅代理流量";
AccessibleDescription = $"{Heading} {ValueText} {UnitText},{Hint}";
Invalidate();
return;
}
string[] units = ["B", "KB", "MB", "GB", "TB"];
var value = bytes.HasValue && double.IsFinite(bytes.Value) ? Math.Max(0, bytes.Value) : (double?)null;
var unit = 0;
@@ -105,11 +113,13 @@ internal sealed class MetricCard : CardPanel
var s = DeviceDpi / 96f;
int S(int n) => (int)Math.Round(n * s);
var g = e.Graphics;
if(Height<S(30))
if(Height<S(40))
{
using var denseFont=Brand.Font(8,FontStyle.Bold);
var arrow=Glyph==DashboardIcon.Download?"↓":"↑";
TextRenderer.DrawText(g,$"{arrow} {ValueText} {UnitText}",denseFont,ClientRectangle,Brand.Navy,
var speed=Glyph is DashboardIcon.Upload or DashboardIcon.Download;
using var denseFont=Brand.Font(speed?8.5f:7.5f,FontStyle.Bold);
var prefix=speed?(Glyph==DashboardIcon.Download?"↓ 下载":"↑ 上传"):Heading.Replace("代理流量", "");
TextRenderer.DrawText(g,$"{prefix} {ValueText} {UnitText}",denseFont,ClientRectangle,
Glyph==DashboardIcon.Download?Brand.DeepBlue:Brand.Navy,
ChartAxis.PaintFlags|TextFormatFlags.VerticalCenter|TextFormatFlags.HorizontalCenter|TextFormatFlags.SingleLine|TextFormatFlags.EndEllipsis);
return;
}
@@ -169,17 +179,163 @@ internal sealed class BadgeLabel : Label
internal sealed class ReadOnlyModeLabel : Control
{
internal ReadOnlyModeLabel() { DoubleBuffered = true; ResizeRedraw = true; BackColor = Color.White; Font = Brand.Font(9.5f); AccessibleName = "当前代理模式(连接中不可修改)"; }
internal ReadOnlyModeLabel() { DoubleBuffered = true; ResizeRedraw = true; SetStyle(ControlStyles.SupportsTransparentBackColor, true); BackColor = Color.Transparent; Font = Brand.Font(9.5f); AccessibleName = "当前实际代理模式"; }
protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); Invalidate(); }
protected override void OnPaint(PaintEventArgs e)
{
if (Width < 10 || Height < 10) return;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using var path = UiGeometry.RoundedRectangle(new(1, 1, Width - 3, Height - 3), Math.Max(3, DeviceDpi / 24));
using var border = new Pen(Color.FromArgb(204, 219, 237));
e.Graphics.DrawPath(border, path);
var pad = DeviceDpi / 12;
TextRenderer.DrawText(e.Graphics, Text, Font, new Rectangle(pad, 0, Width - pad * 2, Height), Brand.Text,
TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle, ForeColor,
ChartAxis.PaintFlags | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine | TextFormatFlags.NoPadding | TextFormatFlags.EndEllipsis);
}
}
// An entirely painted selector: the closed field has no native ComboBox child
// HWND, so a compact high-DPI field cannot expose a clipped black/grey edge.
internal sealed class DashboardCombo : Control
{
internal List<object> Items { get; } = [];
private int _selectedIndex = -1;
private bool _hovered;
private ContextMenuStrip? _popup;
private long _itemsRevision;
internal event EventHandler? SelectedIndexChanged;
internal int SelectedIndex
{
get => _selectedIndex;
set
{
var next = value >= 0 && value < Items.Count ? value : -1;
if (_selectedIndex == next) return;
_selectedIndex = next;
Text = SelectedItem?.ToString() ?? "";
Invalidate();
AccessibilityNotifyClients(AccessibleEvents.ValueChange, -1);
SelectedIndexChanged?.Invoke(this, EventArgs.Empty);
}
}
internal object? SelectedItem
{
get => _selectedIndex >= 0 && _selectedIndex < Items.Count ? Items[_selectedIndex] : null;
set => SelectedIndex = value is null ? -1 : Items.IndexOf(value);
}
internal bool DroppedDown
{
get => _popup?.Visible == true;
set { if (value) OpenPopup(); else _popup?.Close(); }
}
internal void BeginUpdate()
{
// Periodic profile refresh may reorder the authorized nodes. Invalidate
// even a queued click from the old popup before its list is changed.
_itemsRevision++;
DroppedDown = false;
}
internal void EndUpdate()
{
if (_selectedIndex >= Items.Count) _selectedIndex = -1;
var oldText = Text;
Text = SelectedItem?.ToString() ?? "";
if (Text != oldText) AccessibilityNotifyClients(AccessibleEvents.ValueChange, -1);
Invalidate();
}
internal DashboardCombo()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer
| ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.Selectable, true);
BackColor = Color.Transparent; ForeColor = Brand.Text; Cursor = Cursors.Hand; TabStop = true;
AccessibleRole = AccessibleRole.ComboBox;
}
protected override AccessibleObject CreateAccessibilityInstance() => new SelectorAccessibleObject(this);
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 OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button != MouseButtons.Left || !Enabled) return;
Focus(); DroppedDown = !DroppedDown;
}
protected override bool IsInputKey(Keys keyData) => keyData is Keys.Up or Keys.Down || base.IsInputKey(keyData);
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode is Keys.Enter or Keys.Space or Keys.F4) { DroppedDown = !DroppedDown; e.Handled = true; }
else if (e.KeyCode is Keys.Up or Keys.Down)
{
SelectedIndex = Math.Clamp(SelectedIndex + (e.KeyCode == Keys.Up ? -1 : 1), 0, Math.Max(0, Items.Count - 1));
e.Handled = true;
}
else if (e.KeyCode == Keys.Escape) { DroppedDown = false; e.Handled = true; }
base.OnKeyDown(e);
}
protected override void OnEnabledChanged(EventArgs e) { if (!Enabled) DroppedDown = false; Invalidate(); base.OnEnabledChanged(e); }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (Width < 10 || Height < 10) return;
var s = DeviceDpi / 96f;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using var path = UiGeometry.RoundedRectangle(new(1, 1, Width - 3, Height - 3), Math.Max(4, (int)(5*s)));
using var fill = new SolidBrush(Color.FromArgb(Enabled ? 208 : 115, 245, 250, 255));
using var border = new Pen(Color.FromArgb(_hovered && Enabled ? 125 : 155, 151, 185, 213), Math.Max(1, s * .65f));
e.Graphics.FillPath(fill, path); e.Graphics.DrawPath(border, path);
var pad = (int)(7*s); var arrowX = Width - (int)(10*s); var arrowY = Height / 2;
TextRenderer.DrawText(e.Graphics, SelectedItem?.ToString() ?? "选择节点", Font,
new Rectangle(pad, 0, Math.Max(1, Width-pad-(int)(20*s)), Height), Enabled ? ForeColor : Brand.Muted,
ChartAxis.PaintFlags | TextFormatFlags.SingleLine | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPadding | TextFormatFlags.EndEllipsis);
using var arrow = new Pen(Enabled ? Brand.RoyalBlue : Brand.Muted, Math.Max(1, s));
e.Graphics.DrawLines(arrow, [new PointF(arrowX-3*s,arrowY-1*s),new(arrowX,arrowY+2*s),new(arrowX+3*s,arrowY-1*s)]);
if (Focused && ShowFocusCues) ControlPaint.DrawFocusRectangle(e.Graphics, Rectangle.Inflate(ClientRectangle, -(int)(4*s), -(int)(4*s)));
}
private void OpenPopup()
{
if (!Enabled || Items.Count == 0 || DroppedDown) return;
_popup?.Dispose();
_popup = new ContextMenuStrip { BackColor = Color.FromArgb(247, 251, 255), ForeColor = Brand.Text,
Font = Font, ShowImageMargin = false, ShowCheckMargin = false, Padding = new Padding(4),
Renderer = new ToolStripProfessionalRenderer(new SelectorColors()), AutoSize = true };
for (var index = 0; index < Items.Count; index++)
{
var selectedItem = Items[index];
var revision = _itemsRevision;
var option = new ToolStripMenuItem(selectedItem.ToString()) { AutoSize = false,
Size = new Size(Math.Max(Width, (int)(170*DeviceDpi/96f)), (int)(26*DeviceDpi/96f)),
Padding = new Padding(8, 2, 8, 2) };
option.Click += (_, _) =>
{
if (!Enabled || revision != _itemsRevision) return;
// Match the original item, never whichever item now occupies
// its numeric index. Removed/replaced items cannot be chosen.
var currentIndex = Items.FindIndex(item => ReferenceEquals(item, selectedItem));
if (currentIndex >= 0) SelectedIndex = currentIndex;
};
_popup.Items.Add(option);
}
_popup.Closed += (_, _) => { Invalidate(); AccessibilityNotifyClients(AccessibleEvents.StateChange, -1); };
_popup.Show(this, new Point(0, Height));
Invalidate();
AccessibilityNotifyClients(AccessibleEvents.StateChange, -1);
}
protected override void Dispose(bool disposing) { if (disposing) _popup?.Dispose(); base.Dispose(disposing); }
private sealed class SelectorAccessibleObject(DashboardCombo owner) : ControlAccessibleObject(owner)
{
public override AccessibleRole Role => AccessibleRole.ComboBox;
public override string? Value { get => owner.SelectedItem?.ToString() ?? ""; set { } }
public override string? Description => owner.AccessibleDescription ?? "选择当前可用选项";
public override string? DefaultAction => owner.DroppedDown ? "关闭选项" : "打开选项";
public override AccessibleStates State => base.State
| (owner.DroppedDown ? AccessibleStates.Expanded : AccessibleStates.Collapsed)
| AccessibleStates.HasPopup;
public override void DoDefaultAction()
{
if (!owner.Enabled) return;
owner.Focus();
owner.DroppedDown = !owner.DroppedDown;
}
}
private sealed class SelectorColors : ProfessionalColorTable
{
public override Color MenuItemSelected => Color.FromArgb(222, 237, 251);
public override Color MenuItemBorder => Color.FromArgb(169, 201, 229);
public override Color MenuBorder => Color.FromArgb(177, 203, 224);
public override Color ToolStripDropDownBackground => Color.FromArgb(247, 251, 255);
}
}