| 1 | using Avalonia; |
| 2 | using Avalonia.Controls; |
| 3 | using Avalonia.Media; |
| 4 | using CascadeIDE.Models; |
| 5 | |
| 6 | namespace CascadeIDE.Views; |
| 7 | |
| 8 | public partial class ZoneInstrumentMountView : UserControl |
| 9 | { |
| 10 | public static readonly StyledProperty<string> InstrumentIdProperty = |
| 11 | AvaloniaProperty.Register<ZoneInstrumentMountView, string>(nameof(InstrumentId), "ide_health_status_v1"); |
| 12 | |
| 13 | public static readonly StyledProperty<string> SlotIdProperty = |
| 14 | AvaloniaProperty.Register<ZoneInstrumentMountView, string>(nameof(SlotId), "pfd"); |
| 15 | |
| 16 | public static readonly StyledProperty<string> MountStyleProperty = |
| 17 | AvaloniaProperty.Register<ZoneInstrumentMountView, string>(nameof(MountStyle), InstrumentMountPolicyIds.V1); |
| 18 | |
| 19 | public static readonly StyledProperty<string> HeaderTextProperty = |
| 20 | AvaloniaProperty.Register<ZoneInstrumentMountView, string>(nameof(HeaderText), string.Empty); |
| 21 | |
| 22 | public static readonly StyledProperty<IBrush?> HostBorderBrushProperty = |
| 23 | AvaloniaProperty.Register<ZoneInstrumentMountView, IBrush?>(nameof(HostBorderBrush), Brush.Parse("#5A6E8C")); |
| 24 | |
| 25 | public static readonly StyledProperty<IBrush?> HeaderBrushProperty = |
| 26 | AvaloniaProperty.Register<ZoneInstrumentMountView, IBrush?>(nameof(HeaderBrush), Brush.Parse("#A9D9FF")); |
| 27 | |
| 28 | public static readonly StyledProperty<IBrush?> LabelBrushProperty = |
| 29 | AvaloniaProperty.Register<ZoneInstrumentMountView, IBrush?>(nameof(LabelBrush), Brush.Parse("#9FB4C9")); |
| 30 | |
| 31 | public static readonly StyledProperty<IBrush?> ValueBrushProperty = |
| 32 | AvaloniaProperty.Register<ZoneInstrumentMountView, IBrush?>(nameof(ValueBrush), Brush.Parse("#DCE8F2")); |
| 33 | |
| 34 | public static readonly StyledProperty<IBrush?> SafetyBrushProperty = |
| 35 | AvaloniaProperty.Register<ZoneInstrumentMountView, IBrush?>(nameof(SafetyBrush), Brush.Parse("#C9F0FF")); |
| 36 | |
| 37 | static ZoneInstrumentMountView() |
| 38 | { |
| 39 | InstrumentIdProperty.Changed.AddClassHandler<ZoneInstrumentMountView>((x, _) => x.ApplyPolicyDefaults()); |
| 40 | SlotIdProperty.Changed.AddClassHandler<ZoneInstrumentMountView>((x, _) => x.ApplyPolicyDefaults()); |
| 41 | MountStyleProperty.Changed.AddClassHandler<ZoneInstrumentMountView>((x, _) => x.ApplyPolicyDefaults()); |
| 42 | } |
| 43 | |
| 44 | public ZoneInstrumentMountView() |
| 45 | { |
| 46 | InitializeComponent(); |
| 47 | ApplyPolicyDefaults(); |
| 48 | } |
| 49 | |
| 50 | public string InstrumentId |
| 51 | { |
| 52 | get => GetValue(InstrumentIdProperty); |
| 53 | set => SetValue(InstrumentIdProperty, value); |
| 54 | } |
| 55 | |
| 56 | public string SlotId |
| 57 | { |
| 58 | get => GetValue(SlotIdProperty); |
| 59 | set => SetValue(SlotIdProperty, value); |
| 60 | } |
| 61 | |
| 62 | public string MountStyle |
| 63 | { |
| 64 | get => GetValue(MountStyleProperty); |
| 65 | set => SetValue(MountStyleProperty, value); |
| 66 | } |
| 67 | |
| 68 | public string HeaderText |
| 69 | { |
| 70 | get => GetValue(HeaderTextProperty); |
| 71 | set => SetValue(HeaderTextProperty, value); |
| 72 | } |
| 73 | |
| 74 | public IBrush? HostBorderBrush |
| 75 | { |
| 76 | get => GetValue(HostBorderBrushProperty); |
| 77 | set => SetValue(HostBorderBrushProperty, value); |
| 78 | } |
| 79 | |
| 80 | public IBrush? HeaderBrush |
| 81 | { |
| 82 | get => GetValue(HeaderBrushProperty); |
| 83 | set => SetValue(HeaderBrushProperty, value); |
| 84 | } |
| 85 | |
| 86 | public IBrush? LabelBrush |
| 87 | { |
| 88 | get => GetValue(LabelBrushProperty); |
| 89 | set => SetValue(LabelBrushProperty, value); |
| 90 | } |
| 91 | |
| 92 | public IBrush? ValueBrush |
| 93 | { |
| 94 | get => GetValue(ValueBrushProperty); |
| 95 | set => SetValue(ValueBrushProperty, value); |
| 96 | } |
| 97 | |
| 98 | public IBrush? SafetyBrush |
| 99 | { |
| 100 | get => GetValue(SafetyBrushProperty); |
| 101 | set => SetValue(SafetyBrushProperty, value); |
| 102 | } |
| 103 | |
| 104 | private void ApplyPolicyDefaults() |
| 105 | { |
| 106 | var skin = ZoneInstrumentMountPolicy.Resolve(InstrumentId, SlotId, MountStyle); |
| 107 | HeaderText = skin.HeaderText; |
| 108 | HostBorderBrush = skin.HostBorderBrush; |
| 109 | HeaderBrush = skin.HeaderBrush; |
| 110 | LabelBrush = skin.LabelBrush; |
| 111 | ValueBrush = skin.ValueBrush; |
| 112 | SafetyBrush = skin.SafetyBrush; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | internal static class ZoneInstrumentMountPolicy |
| 117 | { |
| 118 | public static ZoneInstrumentSkin Resolve(string? instrumentId, string? slotId, string? slotPolicy) |
| 119 | { |
| 120 | var normalizedPolicy = (slotPolicy ?? string.Empty).Trim().ToLowerInvariant(); |
| 121 | var normalizedSlot = (slotId ?? string.Empty).Trim().ToLowerInvariant(); |
| 122 | var normalizedInstrument = (instrumentId ?? string.Empty).Trim().ToLowerInvariant(); |
| 123 | |
| 124 | if (normalizedPolicy != InstrumentMountPolicyIds.V1) |
| 125 | return DefaultSkin(normalizedSlot, normalizedInstrument); |
| 126 | |
| 127 | return normalizedSlot switch |
| 128 | { |
| 129 | "mfd" => BuildSkin( |
| 130 | "MFD STATUS", |
| 131 | "#6E5A8C", |
| 132 | "#DCC1FF", |
| 133 | "#BEA7D6", |
| 134 | "#EDE4F8", |
| 135 | "#F2E8FF"), |
| 136 | "forward" => BuildSkin( |
| 137 | "FORWARD STATUS", |
| 138 | "#8C7A5A", |
| 139 | "#FFE2AD", |
| 140 | "#D9BE8D", |
| 141 | "#F7EFD8", |
| 142 | "#FFF0CF"), |
| 143 | _ => BuildSkin( |
| 144 | "PFD STATUS", |
| 145 | "#5A6E8C", |
| 146 | "#A9D9FF", |
| 147 | "#9FB4C9", |
| 148 | "#DCE8F2", |
| 149 | "#C9F0FF") |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | private static ZoneInstrumentSkin DefaultSkin(string slotId, string instrumentId) |
| 154 | { |
| 155 | var fallbackHeader = slotId switch |
| 156 | { |
| 157 | "mfd" => "MFD STATUS", |
| 158 | "forward" => "FORWARD STATUS", |
| 159 | _ => "PFD STATUS" |
| 160 | }; |
| 161 | |
| 162 | if (instrumentId == "ide_health_status_v1") |
| 163 | return BuildSkin(fallbackHeader, "#5A6E8C", "#A9D9FF", "#9FB4C9", "#DCE8F2", "#C9F0FF"); |
| 164 | |
| 165 | return BuildSkin($"{instrumentId.ToUpperInvariant()} [{slotId.ToUpperInvariant()}]", "#5A6E8C", "#A9D9FF", "#9FB4C9", "#DCE8F2", "#C9F0FF"); |
| 166 | } |
| 167 | |
| 168 | private static ZoneInstrumentSkin BuildSkin( |
| 169 | string headerText, |
| 170 | string hostBorderBrush, |
| 171 | string headerBrush, |
| 172 | string labelBrush, |
| 173 | string valueBrush, |
| 174 | string safetyBrush) => |
| 175 | new( |
| 176 | headerText, |
| 177 | Brush.Parse(hostBorderBrush), |
| 178 | Brush.Parse(headerBrush), |
| 179 | Brush.Parse(labelBrush), |
| 180 | Brush.Parse(valueBrush), |
| 181 | Brush.Parse(safetyBrush)); |
| 182 | } |
| 183 | |
| 184 | internal sealed record ZoneInstrumentSkin( |
| 185 | string HeaderText, |
| 186 | IBrush? HostBorderBrush, |
| 187 | IBrush? HeaderBrush, |
| 188 | IBrush? LabelBrush, |
| 189 | IBrush? ValueBrush, |
| 190 | IBrush? SafetyBrush); |
| 191 | |