| 1 | using System.Windows.Input; |
| 2 | using Avalonia; |
| 3 | using Avalonia.Controls; |
| 4 | using Avalonia.Controls.Primitives; |
| 5 | using Avalonia.Input.Platform; |
| 6 | using Avalonia.Interactivity; |
| 7 | using Avalonia.VisualTree; |
| 8 | |
| 9 | namespace CascadeIDE.Views; |
| 10 | |
| 11 | public partial class PanelChromeHeader : UserControl |
| 12 | { |
| 13 | public static readonly StyledProperty<string?> TitleProperty = |
| 14 | AvaloniaProperty.Register<PanelChromeHeader, string?>(nameof(Title)); |
| 15 | |
| 16 | public static readonly StyledProperty<bool> ShowOverflowProperty = |
| 17 | AvaloniaProperty.Register<PanelChromeHeader, bool>(nameof(ShowOverflow), defaultValue: true); |
| 18 | |
| 19 | /// <summary>Как на концепте: короткие метки панелей в верхнем регистре (латиница/кириллица).</summary> |
| 20 | public static readonly StyledProperty<bool> UppercaseTitleProperty = |
| 21 | AvaloniaProperty.Register<PanelChromeHeader, bool>(nameof(UppercaseTitle), defaultValue: false); |
| 22 | |
| 23 | /// <summary>Кнопка «EMERGENCY STOP» в полосе заголовка (режим Power, концепт cockpit).</summary> |
| 24 | public static readonly StyledProperty<bool> ShowEmergencyStopProperty = |
| 25 | AvaloniaProperty.Register<PanelChromeHeader, bool>(nameof(ShowEmergencyStop), defaultValue: false); |
| 26 | |
| 27 | public static readonly StyledProperty<ICommand?> EmergencyStopCommandProperty = |
| 28 | AvaloniaProperty.Register<PanelChromeHeader, ICommand?>(nameof(EmergencyStopCommand)); |
| 29 | |
| 30 | public string? Title |
| 31 | { |
| 32 | get => GetValue(TitleProperty); |
| 33 | set => SetValue(TitleProperty, value); |
| 34 | } |
| 35 | |
| 36 | public bool ShowOverflow |
| 37 | { |
| 38 | get => GetValue(ShowOverflowProperty); |
| 39 | set => SetValue(ShowOverflowProperty, value); |
| 40 | } |
| 41 | |
| 42 | public bool UppercaseTitle |
| 43 | { |
| 44 | get => GetValue(UppercaseTitleProperty); |
| 45 | set => SetValue(UppercaseTitleProperty, value); |
| 46 | } |
| 47 | |
| 48 | public bool ShowEmergencyStop |
| 49 | { |
| 50 | get => GetValue(ShowEmergencyStopProperty); |
| 51 | set => SetValue(ShowEmergencyStopProperty, value); |
| 52 | } |
| 53 | |
| 54 | public ICommand? EmergencyStopCommand |
| 55 | { |
| 56 | get => GetValue(EmergencyStopCommandProperty); |
| 57 | set => SetValue(EmergencyStopCommandProperty, value); |
| 58 | } |
| 59 | |
| 60 | static PanelChromeHeader() |
| 61 | { |
| 62 | UppercaseTitleProperty.Changed.AddClassHandler<PanelChromeHeader>((h, _) => h.SyncTitleCapsClass()); |
| 63 | } |
| 64 | |
| 65 | public PanelChromeHeader() |
| 66 | { |
| 67 | InitializeComponent(); |
| 68 | SyncTitleCapsClass(); |
| 69 | } |
| 70 | |
| 71 | /// <summary>Класс <c>panelChromeTitleCaps</c> для межбуквенного интервала; текст задаётся привязкой <see cref="PanelChromeTitleDisplayConverter"/>.</summary> |
| 72 | void SyncTitleCapsClass() |
| 73 | { |
| 74 | if (TitleText is null) |
| 75 | return; |
| 76 | TitleText.Classes.Remove("panelChromeTitleCaps"); |
| 77 | if (UppercaseTitle) |
| 78 | TitleText.Classes.Add("panelChromeTitleCaps"); |
| 79 | } |
| 80 | |
| 81 | void OnOverflowButtonClick(object? sender, RoutedEventArgs e) |
| 82 | { |
| 83 | e.Handled = true; |
| 84 | if (sender is not Button { ContextMenu: { } menu } btn) |
| 85 | return; |
| 86 | menu.PlacementTarget = btn; |
| 87 | menu.Placement = PlacementMode.BottomEdgeAlignedRight; |
| 88 | menu.Open(btn); |
| 89 | } |
| 90 | |
| 91 | async void OnCopyTitleMenuClick(object? sender, RoutedEventArgs e) |
| 92 | { |
| 93 | var text = TitleText?.Text ?? Title ?? ""; |
| 94 | if (string.IsNullOrEmpty(text)) |
| 95 | return; |
| 96 | var top = TopLevel.GetTopLevel(this); |
| 97 | if (top?.Clipboard is { } clip) |
| 98 | await clip.SetTextAsync(text); |
| 99 | } |
| 100 | } |
| 101 | |