| 1 | using Avalonia; |
| 2 | using Avalonia.Controls; |
| 3 | using Avalonia.Input; |
| 4 | using Avalonia.Media; |
| 5 | using CascadeIDE.Cockpit.PrimitivesKit; |
| 6 | using CascadeIDE.Models; |
| 7 | |
| 8 | namespace CascadeIDE.Views; |
| 9 | |
| 10 | /// <summary> |
| 11 | /// Одна ячейка annunciator / Korry по <see cref="AnnunciatorLampItem"/> (тот же контур, что <see cref="AnnunciatorLampStrip"/>, без фона полосы). |
| 12 | /// Для проекции «лампа в колонке таблицы» (ADR 0068). |
| 13 | /// </summary> |
| 14 | public sealed class AnnunciatorLampCell : Control |
| 15 | { |
| 16 | public static readonly StyledProperty<AnnunciatorLampItem?> ItemProperty = |
| 17 | AvaloniaProperty.Register<AnnunciatorLampCell, AnnunciatorLampItem?>(nameof(Item)); |
| 18 | |
| 19 | private AnnunciatorLampItem? _hovered; |
| 20 | |
| 21 | static AnnunciatorLampCell() |
| 22 | { |
| 23 | AffectsRender<AnnunciatorLampCell>(ItemProperty); |
| 24 | FocusableProperty.OverrideDefaultValue<AnnunciatorLampCell>(false); |
| 25 | } |
| 26 | |
| 27 | public AnnunciatorLampItem? Item |
| 28 | { |
| 29 | get => GetValue(ItemProperty); |
| 30 | set => SetValue(ItemProperty, value); |
| 31 | } |
| 32 | |
| 33 | protected override Size MeasureOverride(Size availableSize) |
| 34 | { |
| 35 | if (Item is null) |
| 36 | return new Size(0, 0); |
| 37 | |
| 38 | return new Size( |
| 39 | AnnunciatorLampMetrics.DefaultCellWidth + 4, |
| 40 | AnnunciatorLampMetrics.DefaultCellHeight + 4); |
| 41 | } |
| 42 | |
| 43 | public override void Render(DrawingContext context) |
| 44 | { |
| 45 | base.Render(context); |
| 46 | var item = Item; |
| 47 | if (item is null) |
| 48 | return; |
| 49 | |
| 50 | var w = Bounds.Width; |
| 51 | var h = Bounds.Height; |
| 52 | if (w <= 0 || h <= 0) |
| 53 | return; |
| 54 | |
| 55 | var cell = AnnunciatorLampMetrics.DefaultCellWidth; |
| 56 | var x = (w - cell) / 2; |
| 57 | var y = (h - cell) / 2; |
| 58 | var outer = new Rect(x, y, cell, cell); |
| 59 | new LabeledAnnunciatorLampFace(item.LampShortLabel, item.Level).Draw(context, outer); |
| 60 | } |
| 61 | |
| 62 | protected override void OnPointerMoved(PointerEventArgs e) |
| 63 | { |
| 64 | base.OnPointerMoved(e); |
| 65 | var item = Item; |
| 66 | if (item is null) |
| 67 | { |
| 68 | ClearTip(); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | var p = e.GetPosition(this); |
| 73 | if (!Bounds.Contains(p)) |
| 74 | { |
| 75 | ClearTip(); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | if (string.Equals(_hovered?.Id, item.Id, StringComparison.Ordinal)) |
| 80 | return; |
| 81 | |
| 82 | _hovered = item; |
| 83 | var tip = new StackPanel { Spacing = 4, MaxWidth = 420 }; |
| 84 | tip.Children.Add(new TextBlock { Text = item.Title, FontWeight = FontWeight.SemiBold, TextWrapping = TextWrapping.Wrap }); |
| 85 | tip.Children.Add(new TextBlock |
| 86 | { |
| 87 | Text = item.Detail, |
| 88 | FontSize = 11, |
| 89 | TextWrapping = TextWrapping.Wrap, |
| 90 | Foreground = new SolidColorBrush(CockpitPrimitivesPalette.Annunciator.TooltipDetailForeground), |
| 91 | }); |
| 92 | ToolTip.SetTip(this, tip); |
| 93 | } |
| 94 | |
| 95 | protected override void OnPointerExited(PointerEventArgs e) |
| 96 | { |
| 97 | base.OnPointerExited(e); |
| 98 | ClearTip(); |
| 99 | } |
| 100 | |
| 101 | private void ClearTip() |
| 102 | { |
| 103 | _hovered = null; |
| 104 | ToolTip.SetTip(this, null); |
| 105 | } |
| 106 | } |
| 107 | |