| 1 | using System.IO; |
| 2 | using Avalonia.Controls; |
| 3 | |
| 4 | using Avalonia.Input; |
| 5 | |
| 6 | using Avalonia.Interactivity; |
| 7 | |
| 8 | using Avalonia.Threading; |
| 9 | |
| 10 | using CascadeIDE.Models; |
| 11 | |
| 12 | using CascadeIDE.ViewModels; |
| 13 | |
| 14 | |
| 15 | |
| 16 | namespace CascadeIDE.Views; |
| 17 | |
| 18 | |
| 19 | |
| 20 | public partial class SolutionExplorerView : UserControl |
| 21 | |
| 22 | { |
| 23 | |
| 24 | private MainWindowViewModel? _vm; |
| 25 | |
| 26 | private TreeView? _tree; |
| 27 | |
| 28 | |
| 29 | |
| 30 | public SolutionExplorerView() |
| 31 | |
| 32 | { |
| 33 | |
| 34 | InitializeComponent(); |
| 35 | |
| 36 | DataContextChanged += OnDataContextChanged; |
| 37 | |
| 38 | AddHandler(InputElement.KeyDownEvent, OnPreviewKeyDown, RoutingStrategies.Tunnel, handledEventsToo: true); |
| 39 | |
| 40 | } |
| 41 | |
| 42 | |
| 43 | |
| 44 | private TextBox? FilterBox => this.FindControl<TextBox>("SolutionFilterBox"); |
| 45 | |
| 46 | |
| 47 | |
| 48 | private void OnDataContextChanged(object? sender, EventArgs e) |
| 49 | |
| 50 | { |
| 51 | |
| 52 | if (_vm is not null) |
| 53 | |
| 54 | _vm.SolutionExplorerFilterFocusRequested -= OnFilterFocusRequested; |
| 55 | |
| 56 | |
| 57 | |
| 58 | _vm = DataContext as MainWindowViewModel; |
| 59 | |
| 60 | if (_vm is not null) |
| 61 | |
| 62 | _vm.SolutionExplorerFilterFocusRequested += OnFilterFocusRequested; |
| 63 | |
| 64 | |
| 65 | |
| 66 | _tree ??= this.FindControl<TreeView>("SolutionTree"); |
| 67 | |
| 68 | if (_tree is null) |
| 69 | |
| 70 | return; |
| 71 | |
| 72 | |
| 73 | |
| 74 | if (_vm is not null) |
| 75 | |
| 76 | _vm.RefreshSolutionExplorerTreeFilter(); |
| 77 | |
| 78 | |
| 79 | |
| 80 | if (_treeDoubleTapHandler is null) |
| 81 | |
| 82 | { |
| 83 | |
| 84 | _treeDoubleTapHandler = OnTreeDoubleTapped; |
| 85 | |
| 86 | _tree.AddHandler(InputElement.DoubleTappedEvent, _treeDoubleTapHandler); |
| 87 | |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | |
| 93 | |
| 94 | private EventHandler<RoutedEventArgs>? _treeDoubleTapHandler; |
| 95 | |
| 96 | |
| 97 | |
| 98 | private void OnFilterFocusRequested() |
| 99 | |
| 100 | { |
| 101 | |
| 102 | Dispatcher.UIThread.Post(() => |
| 103 | |
| 104 | { |
| 105 | |
| 106 | FilterBox?.Focus(); |
| 107 | |
| 108 | FilterBox?.SelectAll(); |
| 109 | |
| 110 | }); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | private void OnPreviewKeyDown(object? sender, KeyEventArgs e) |
| 117 | |
| 118 | { |
| 119 | |
| 120 | if (_vm is null) |
| 121 | |
| 122 | return; |
| 123 | |
| 124 | |
| 125 | |
| 126 | if (e.Key == Key.Oem1 && e.KeyModifiers.HasFlag(KeyModifiers.Control)) |
| 127 | |
| 128 | { |
| 129 | |
| 130 | FilterBox?.Focus(); |
| 131 | |
| 132 | e.Handled = true; |
| 133 | |
| 134 | return; |
| 135 | |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | if (e.Key == Key.Escape && FilterBox?.IsFocused == true |
| 141 | |
| 142 | && !string.IsNullOrEmpty(_vm.SolutionExplorerFilterText)) |
| 143 | |
| 144 | { |
| 145 | |
| 146 | _vm.SolutionExplorerFilterText = ""; |
| 147 | |
| 148 | e.Handled = true; |
| 149 | |
| 150 | } |
| 151 | |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | private void OnTreeDoubleTapped(object? sender, RoutedEventArgs e) |
| 157 | |
| 158 | { |
| 159 | |
| 160 | if (_vm is null) |
| 161 | |
| 162 | return; |
| 163 | |
| 164 | if (e.Source is not Control { DataContext: SolutionItem item }) |
| 165 | |
| 166 | return; |
| 167 | |
| 168 | if (item.FullPath is not { } path || Directory.Exists(path)) |
| 169 | |
| 170 | return; |
| 171 | |
| 172 | _vm.SolutionExplorerSelectedItem = item; |
| 173 | |
| 174 | _vm.OpenSelectedSolutionItemCommand.Execute(null); |
| 175 | |
| 176 | e.Handled = true; |
| 177 | |
| 178 | } |
| 179 | |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |