|
1 | | -using Avalonia.Controls.Primitives; |
| 1 | +using AsyncAwaitBestPractices; |
| 2 | +using Avalonia.Controls; |
| 3 | +using Avalonia.Controls.Primitives; |
| 4 | +using StabilityMatrix.Avalonia.ViewModels.Inference; |
2 | 5 | using StabilityMatrix.Core.Attributes; |
| 6 | +using StabilityMatrix.Core.Models; |
3 | 7 |
|
4 | 8 | namespace StabilityMatrix.Avalonia.Controls; |
5 | 9 |
|
6 | 10 | [Transient] |
7 | | -public class UnetModelCard : TemplatedControl; |
| 11 | +public class UnetModelCard : TemplatedControl |
| 12 | +{ |
| 13 | + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) |
| 14 | + { |
| 15 | + base.OnApplyTemplate(e); |
| 16 | + |
| 17 | + var clip1ComboBox = e.NameScope.Find("Clip1ComboBox") as BetterComboBox; |
| 18 | + clip1ComboBox!.SelectionChanged += UpscalerComboBox_OnSelectionChanged; |
| 19 | + |
| 20 | + var clip2ComboBox = e.NameScope.Find("Clip2ComboBox") as BetterComboBox; |
| 21 | + clip2ComboBox!.SelectionChanged += UpscalerComboBox_OnSelectionChanged; |
| 22 | + } |
| 23 | + |
| 24 | + private void UpscalerComboBox_OnSelectionChanged(object? sender, SelectionChangedEventArgs e) |
| 25 | + { |
| 26 | + if (e.AddedItems.Count == 0) |
| 27 | + return; |
| 28 | + |
| 29 | + var item = e.AddedItems[0]; |
| 30 | + if (item is HybridModelFile { IsDownloadable: true }) |
| 31 | + { |
| 32 | + // Reset the selection |
| 33 | + e.Handled = true; |
| 34 | + |
| 35 | + if ( |
| 36 | + e.RemovedItems.Count > 0 |
| 37 | + && e.RemovedItems[0] is HybridModelFile { IsDownloadable: false } removedItem |
| 38 | + ) |
| 39 | + { |
| 40 | + (sender as BetterComboBox)!.SelectedItem = removedItem; |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + (sender as BetterComboBox)!.SelectedItem = null; |
| 45 | + } |
| 46 | + |
| 47 | + // Show dialog to download the model |
| 48 | + (DataContext as UnetModelCardViewModel)! |
| 49 | + .RemoteDownloadCommand.ExecuteAsync(item) |
| 50 | + .SafeFireAndForget(); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments