using Avalonia; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Media; using JdeScoping.ConfigManager.ViewModels.Forms; using JdeScoping.ConfigManager.ViewModels.PipelineSteps; namespace JdeScoping.ConfigManager.Views.Controls; public partial class PipelineStepCard : UserControl { public static readonly StyledProperty StepColorProperty = AvaloniaProperty.Register(nameof(StepColor), "#3B82F6"); public PipelineStepCard() { InitializeComponent(); PointerPressed += OnPointerPressed; PropertyChanged += OnPropertyChangedHandler; DataContextChanged += OnDataContextChanged; } /// /// Gets or sets the step color for the icon background. /// public string StepColor { get => GetValue(StepColorProperty); set => SetValue(StepColorProperty, value); } private void OnPropertyChangedHandler(object? sender, AvaloniaPropertyChangedEventArgs e) { if (e.Property == StepColorProperty) { UpdateIconColor(); } } private void OnDataContextChanged(object? sender, EventArgs e) { UpdateIconColor(); UpdateSelectionState(); if (DataContext is PipelineStepViewModelBase vm) { vm.PropertyChanged += (s, e) => { if (e.PropertyName == nameof(PipelineStepViewModelBase.IsSelected)) { UpdateSelectionState(); } }; } } private void UpdateIconColor() { var iconBg = this.FindControl("IconBackground"); if (iconBg != null && !string.IsNullOrEmpty(StepColor)) { if (Color.TryParse(StepColor, out var color)) { iconBg.Background = new SolidColorBrush(color); } } } private void UpdateSelectionState() { var cardBorder = this.FindControl("CardBorder"); if (cardBorder != null && DataContext is PipelineStepViewModelBase vm) { if (vm.IsSelected) { cardBorder.BorderBrush = new SolidColorBrush(Color.Parse("#3B82F6")); cardBorder.Background = new SolidColorBrush(Color.Parse("#1E2A3A")); } else { cardBorder.BorderBrush = new SolidColorBrush(Color.Parse("#3D4550")); cardBorder.Background = new SolidColorBrush(Color.Parse("#1A1F26")); } } } private void OnPointerPressed(object? sender, PointerPressedEventArgs e) { // Find the PipelineEditorViewModel from the visual tree var parent = this.Parent; while (parent != null) { if (parent is UserControl uc && uc.DataContext is PipelineEditorViewModel editorVm) { if (DataContext is PipelineStepViewModelBase stepVm) { editorVm.SelectedStep = stepVm; } break; } parent = parent.Parent; } e.Handled = true; } }