|
| 1 | +using System; |
| 2 | +using Avalonia.Controls; |
| 3 | +using Avalonia.Input; |
| 4 | +using Avalonia.Interactivity; |
| 5 | + |
| 6 | +namespace PlanViewer.App.Controls; |
| 7 | + |
| 8 | +public partial class ColumnFilterPopup : UserControl |
| 9 | +{ |
| 10 | + public event EventHandler<FilterAppliedEventArgs>? FilterApplied; |
| 11 | + public event EventHandler? FilterCleared; |
| 12 | + |
| 13 | + private string _currentColumnName = ""; |
| 14 | + |
| 15 | + private static readonly (string Display, FilterOperator Op)[] Operators = |
| 16 | + [ |
| 17 | + ("Contains", FilterOperator.Contains), |
| 18 | + ("Equals (=)", FilterOperator.Equals), |
| 19 | + ("Not Equals (!=)", FilterOperator.NotEquals), |
| 20 | + ("Starts With", FilterOperator.StartsWith), |
| 21 | + ("Ends With", FilterOperator.EndsWith), |
| 22 | + ("Greater Than (>)", FilterOperator.GreaterThan), |
| 23 | + ("Greater or Equal (>=)", FilterOperator.GreaterThanOrEqual), |
| 24 | + ("Less Than (<)", FilterOperator.LessThan), |
| 25 | + ("Less or Equal (<=)", FilterOperator.LessThanOrEqual), |
| 26 | + ("Is Empty", FilterOperator.IsEmpty), |
| 27 | + ("Is Not Empty", FilterOperator.IsNotEmpty), |
| 28 | + ]; |
| 29 | + |
| 30 | + public ColumnFilterPopup() |
| 31 | + { |
| 32 | + InitializeComponent(); |
| 33 | + foreach (var (display, _) in Operators) |
| 34 | + OperatorComboBox.Items.Add(display); |
| 35 | + OperatorComboBox.SelectedIndex = 0; |
| 36 | + } |
| 37 | + |
| 38 | + public void Initialize(string columnName, ColumnFilterState? existingFilter) |
| 39 | + { |
| 40 | + _currentColumnName = columnName; |
| 41 | + HeaderText.Text = $"Filter: {columnName}"; |
| 42 | + |
| 43 | + if (existingFilter?.IsActive == true) |
| 44 | + { |
| 45 | + var idx = Array.FindIndex(Operators, o => o.Op == existingFilter.Operator); |
| 46 | + OperatorComboBox.SelectedIndex = idx >= 0 ? idx : 0; |
| 47 | + ValueTextBox.Text = existingFilter.Value; |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + OperatorComboBox.SelectedIndex = 0; |
| 52 | + ValueTextBox.Text = ""; |
| 53 | + } |
| 54 | + |
| 55 | + UpdateValueVisibility(); |
| 56 | + ValueTextBox.Focus(); |
| 57 | + } |
| 58 | + |
| 59 | + private void UpdateValueVisibility() |
| 60 | + { |
| 61 | + var idx = OperatorComboBox.SelectedIndex; |
| 62 | + var op = (idx >= 0 && idx < Operators.Length) ? Operators[idx].Op : FilterOperator.Contains; |
| 63 | + var showValue = op != FilterOperator.IsEmpty && op != FilterOperator.IsNotEmpty; |
| 64 | + ValueLabel.IsVisible = showValue; |
| 65 | + ValueTextBox.IsVisible = showValue; |
| 66 | + } |
| 67 | + |
| 68 | + private void OperatorComboBox_SelectionChanged(object? sender, SelectionChangedEventArgs e) |
| 69 | + { |
| 70 | + UpdateValueVisibility(); |
| 71 | + } |
| 72 | + |
| 73 | + private void ApplyFilter() |
| 74 | + { |
| 75 | + var idx = OperatorComboBox.SelectedIndex; |
| 76 | + if (idx < 0 || idx >= Operators.Length) return; |
| 77 | + |
| 78 | + FilterApplied?.Invoke(this, new FilterAppliedEventArgs |
| 79 | + { |
| 80 | + FilterState = new ColumnFilterState |
| 81 | + { |
| 82 | + ColumnName = _currentColumnName, |
| 83 | + Operator = Operators[idx].Op, |
| 84 | + Value = ValueTextBox.Text ?? "", |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + private void ApplyButton_Click(object? sender, RoutedEventArgs e) => ApplyFilter(); |
| 90 | + |
| 91 | + private void ClearButton_Click(object? sender, RoutedEventArgs e) |
| 92 | + { |
| 93 | + FilterApplied?.Invoke(this, new FilterAppliedEventArgs |
| 94 | + { |
| 95 | + FilterState = new ColumnFilterState { ColumnName = _currentColumnName } |
| 96 | + }); |
| 97 | + FilterCleared?.Invoke(this, EventArgs.Empty); |
| 98 | + } |
| 99 | + |
| 100 | + private void ValueTextBox_KeyDown(object? sender, KeyEventArgs e) |
| 101 | + { |
| 102 | + if (e.Key == Key.Enter) |
| 103 | + { |
| 104 | + ApplyFilter(); |
| 105 | + e.Handled = true; |
| 106 | + } |
| 107 | + else if (e.Key == Key.Escape) |
| 108 | + { |
| 109 | + FilterCleared?.Invoke(this, EventArgs.Empty); |
| 110 | + e.Handled = true; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments