|
9 | 9 | using Avalonia.Controls.ApplicationLifetimes; // Added for IClassicDesktopStyleApplicationLifetime |
10 | 10 | using Application = Avalonia.Application; // Added for Application.Current |
11 | 11 | using System.Linq; // Added for .Any() |
| 12 | +using MsBox.Avalonia; // For message boxes |
| 13 | +using MsBox.Avalonia.Enums; // For message box button/icon enums |
| 14 | +using System.Threading.Tasks; // For Task |
| 15 | +using ImageSort.Avalonia.Views; // For InputDialog |
12 | 16 |
|
13 | 17 | namespace ImageSort.Avalonia.ViewModels; |
14 | 18 |
|
@@ -67,9 +71,79 @@ public MainWindowViewModel(FoldersViewModel foldersViewModel, ImagesViewModel im |
67 | 71 | interaction.SetOutput(null); |
68 | 72 | } |
69 | 73 | }); |
70 | | - } |
71 | 74 |
|
72 | | - // Remove placeholder properties like Greeting and commands, |
73 | | - // as they are now inherited from ImageSort.ViewModels.MainViewModel |
74 | | - // e.g., public ReactiveCommand<Unit, Unit> OpenFolder { get; } is in MainViewModel |
| 75 | + // Handler for the FoldersViewModel.SelectFolder interaction (used by Pin command) |
| 76 | + this.Folders.SelectFolder.RegisterHandler(async interaction => |
| 77 | + { |
| 78 | + var topLevel = TopLevel.GetTopLevel(null); |
| 79 | + if (topLevel == null) |
| 80 | + { |
| 81 | + if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime) |
| 82 | + { |
| 83 | + topLevel = desktopLifetime.MainWindow; |
| 84 | + } |
| 85 | + |
| 86 | + if (topLevel == null) |
| 87 | + { |
| 88 | + interaction.SetOutput(null); |
| 89 | + return; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + var result = await topLevel.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions |
| 94 | + { |
| 95 | + Title = "Select Folder to Pin", |
| 96 | + AllowMultiple = false |
| 97 | + }); |
| 98 | + |
| 99 | + if (result.Any()) |
| 100 | + { |
| 101 | + interaction.SetOutput(result[0].Path.LocalPath); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + interaction.SetOutput(null); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + // Handler for ImagesViewModel.PromptForNewFileName |
| 110 | + this.Images.PromptForNewFileName.RegisterHandler(async interaction => |
| 111 | + { |
| 112 | + var mainWindow = (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow; |
| 113 | + if (mainWindow == null) |
| 114 | + { |
| 115 | + interaction.SetOutput(null); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + var dialog = new InputDialog // Assuming we'll create this view |
| 120 | + { |
| 121 | + Title = "Rename File", |
| 122 | + // We can pass the current name as a default or placeholder if needed |
| 123 | + }; |
| 124 | + |
| 125 | + var result = await dialog.ShowDialog<string>(mainWindow); |
| 126 | + |
| 127 | + interaction.SetOutput(result); |
| 128 | + }); |
| 129 | + |
| 130 | + // Handler for ImagesViewModel.NotifyUserOfError |
| 131 | + this.Images.NotifyUserOfError.RegisterHandler(async interaction => |
| 132 | + { |
| 133 | + var message = interaction.Input; |
| 134 | + var box = MessageBoxManager.GetMessageBoxStandard("Error", message, ButtonEnum.Ok, Icon.Error); |
| 135 | + |
| 136 | + var mainWindow = (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow; |
| 137 | + if (mainWindow != null) |
| 138 | + { |
| 139 | + await box.ShowWindowDialogAsync(mainWindow); |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + await box.ShowAsync(); // Show as a standalone window if main window not found |
| 144 | + } |
| 145 | + |
| 146 | + interaction.SetOutput(Unit.Default); |
| 147 | + }); |
| 148 | + } |
75 | 149 | } |
0 commit comments