| 1 | using System.Text.Json; |
| 2 | using CascadeIDE.Models; |
| 3 | using CascadeIDE.ViewModels; |
| 4 | using static CascadeIDE.Services.IdeCommands; |
| 5 | |
| 6 | namespace CascadeIDE.Features.IdeMcp.Execution; |
| 7 | |
| 8 | /// <summary>MCP-хендлеры видимости панелей, режима UI, PFD/MFD, навигации по страницам MFD и палитре команд.</summary> |
| 9 | internal sealed partial class IdeMcpCommandExecutor |
| 10 | { |
| 11 | private void RegisterUiVisibilityAndModes(Action<string, Handler> add) |
| 12 | { |
| 13 | add(ToggleTerminal, async (_, _) => |
| 14 | { |
| 15 | if (_vm.ToggleTerminalCommand.CanExecute(null)) |
| 16 | _vm.ToggleTerminalCommand.Execute(null); |
| 17 | return "OK"; |
| 18 | }); |
| 19 | add(ToggleWorkspaceSplittersLock, async (_, _) => |
| 20 | { |
| 21 | if (_vm.ToggleWorkspaceSplittersLockCommand.CanExecute(null)) |
| 22 | _vm.ToggleWorkspaceSplittersLockCommand.Execute(null); |
| 23 | return "OK"; |
| 24 | }); |
| 25 | add(ToggleBuildOutput, async (_, _) => |
| 26 | { |
| 27 | if (_vm.ToggleBuildOutputCommand.CanExecute(null)) |
| 28 | _vm.ToggleBuildOutputCommand.Execute(null); |
| 29 | return "OK"; |
| 30 | }); |
| 31 | add(TogglePfdRegionExpanded, async (_, _) => |
| 32 | { |
| 33 | if (_vm.TogglePfdRegionExpandedCommand.CanExecute(null)) |
| 34 | _vm.TogglePfdRegionExpandedCommand.Execute(null); |
| 35 | return "OK"; |
| 36 | }); |
| 37 | add(CycleCodeNavigationMapPresentation, async (_, _) => |
| 38 | { |
| 39 | _vm.NavigationMap.CycleCodeNavigationMapPresentation(); |
| 40 | return "OK"; |
| 41 | }); |
| 42 | add(CycleCodeNavigationMapLevel, async (_, _) => |
| 43 | { |
| 44 | _vm.NavigationMap.CycleCodeNavigationMapLevel(); |
| 45 | return "OK"; |
| 46 | }); |
| 47 | add(SetCodeNavigationMapLevel, async (args, _) => |
| 48 | { |
| 49 | var level = McpCommandJsonArgs.String(args, "level")?.Trim(); |
| 50 | if (string.IsNullOrEmpty(level)) |
| 51 | return "Missing level (file | controlFlow)"; |
| 52 | _vm.NavigationMap.SetCodeNavigationMapLevel(level); |
| 53 | return $"OK: {CodeNavigationMapLevelKind.Normalize(level)}"; |
| 54 | }); |
| 55 | add(CycleCodeNavigationMapDetailLevel, async (_, _) => |
| 56 | { |
| 57 | _vm.NavigationMap.CycleCodeNavigationMapDetailLevel(); |
| 58 | return "OK"; |
| 59 | }); |
| 60 | add(CycleCodeNavigationMapRelatedGraphLayout, async (_, _) => |
| 61 | { |
| 62 | _vm.NavigationMap.CycleCodeNavigationMapRelatedGraphLayout(); |
| 63 | return "OK"; |
| 64 | }); |
| 65 | |
| 66 | add(SetTerminalVisible, async (args, _) => |
| 67 | { |
| 68 | if (args is null || !args.TryGetValue("visible", out var tv) || tv.ValueKind is not (JsonValueKind.True or JsonValueKind.False)) |
| 69 | return "Missing or invalid visible (boolean)"; |
| 70 | var on = tv.GetBoolean(); |
| 71 | _vm.IsTerminalVisible = on; |
| 72 | return "OK"; |
| 73 | }); |
| 74 | add(SetBuildOutputVisible, async (args, _) => |
| 75 | { |
| 76 | if (args is null || !args.TryGetValue("visible", out var bv) || bv.ValueKind is not (JsonValueKind.True or JsonValueKind.False)) |
| 77 | return "Missing or invalid visible (boolean)"; |
| 78 | var on = bv.GetBoolean(); |
| 79 | _vm.IsBuildOutputVisible = on; |
| 80 | return "OK"; |
| 81 | }); |
| 82 | add(SetUiMode, async (args, _) => |
| 83 | { |
| 84 | var m = McpCommandJsonArgs.String(args, "mode")?.Trim(); |
| 85 | if (string.IsNullOrEmpty(m)) |
| 86 | return "Missing mode (см. UiModes/index.toml)"; |
| 87 | var norm = MainWindowViewModel.NormalizeUiMode(m); |
| 88 | _vm.UiMode = norm; |
| 89 | return "OK"; |
| 90 | }); |
| 91 | |
| 92 | add(SetPfdRegionExpanded, async (args, _) => |
| 93 | { |
| 94 | if (args is null || !args.TryGetValue("visible", out var sev) || sev.ValueKind is not (JsonValueKind.True or JsonValueKind.False)) |
| 95 | return "Missing or invalid visible (boolean)"; |
| 96 | _vm.ApplyPfdRegionExpanded(sev.GetBoolean()); |
| 97 | return "OK"; |
| 98 | }); |
| 99 | add(SetMfdRegionExpanded, async (args, _) => |
| 100 | { |
| 101 | if (args is null || !args.TryGetValue("visible", out var cev) || cev.ValueKind is not (JsonValueKind.True or JsonValueKind.False)) |
| 102 | return "Missing or invalid visible (boolean)"; |
| 103 | _vm.ApplyMfdRegionExpanded(cev.GetBoolean()); |
| 104 | return "OK"; |
| 105 | }); |
| 106 | add(SetGitPanelVisible, async (args, _) => |
| 107 | { |
| 108 | if (args is null || !args.TryGetValue("visible", out var gev) || gev.ValueKind is not (JsonValueKind.True or JsonValueKind.False)) |
| 109 | return "Missing or invalid visible (boolean)"; |
| 110 | _vm.IsGitPanelVisible = gev.GetBoolean(); |
| 111 | return "OK"; |
| 112 | }); |
| 113 | add(SetInstrumentationDockVisible, async (args, _) => |
| 114 | { |
| 115 | if (args is null || !args.TryGetValue("visible", out var idv) || idv.ValueKind is not (JsonValueKind.True or JsonValueKind.False)) |
| 116 | return "Missing or invalid visible (boolean)"; |
| 117 | _vm.IsInstrumentationDockVisible = idv.GetBoolean(); |
| 118 | return "OK"; |
| 119 | }); |
| 120 | add(ToggleGitPanel, async (_, _) => |
| 121 | { |
| 122 | _vm.IsGitPanelVisible = !_vm.IsGitPanelVisible; |
| 123 | return "OK"; |
| 124 | }); |
| 125 | add(ToggleInstrumentationDock, async (_, _) => |
| 126 | { |
| 127 | if (_vm.ToggleInstrumentationDockCommand.CanExecute(null)) |
| 128 | _vm.ToggleInstrumentationDockCommand.Execute(null); |
| 129 | return "OK"; |
| 130 | }); |
| 131 | add(ToggleMfdRegionExpanded, async (_, _) => |
| 132 | { |
| 133 | if (_vm.ToggleMfdRegionExpandedCommand.CanExecute(null)) |
| 134 | _vm.ToggleMfdRegionExpandedCommand.Execute(null); |
| 135 | return "OK"; |
| 136 | }); |
| 137 | |
| 138 | add(CycleUiMode, async (_, _) => |
| 139 | { |
| 140 | if (_vm.CycleUiModeCommand.CanExecute(null)) |
| 141 | _vm.CycleUiModeCommand.Execute(null); |
| 142 | return "OK"; |
| 143 | }); |
| 144 | |
| 145 | add(ToggleCommandPalette, async (_, _) => |
| 146 | { |
| 147 | if (_vm.ToggleCommandPaletteCommand.CanExecute(null)) |
| 148 | _vm.ToggleCommandPaletteCommand.Execute(null); |
| 149 | return await Task.FromResult("OK"); |
| 150 | }); |
| 151 | |
| 152 | add(ShowEnvironmentReadinessPage, async (_, _) => |
| 153 | { |
| 154 | _vm.ApplyMfdRegionExpanded(true); |
| 155 | _vm.TryNavigateToMfdShellPage(MfdShellPage.EnvironmentReadiness); |
| 156 | return await Task.FromResult("OK"); |
| 157 | }); |
| 158 | add(ShowHybridIndexPage, async (_, _) => |
| 159 | { |
| 160 | _vm.ApplyMfdRegionExpanded(true); |
| 161 | _vm.TryNavigateToMfdShellPage(MfdShellPage.HybridIndex); |
| 162 | return await Task.FromResult("OK"); |
| 163 | }); |
| 164 | add(ShowWebAiPortalPage, async (args, _) => |
| 165 | { |
| 166 | var hint = args is not null ? McpCommandJsonArgs.String(args, "url")?.Trim() : null; |
| 167 | if (!string.IsNullOrEmpty(hint)) |
| 168 | _vm.WebAiPortalUrlText = hint; |
| 169 | _vm.ApplyMfdRegionExpanded(true); |
| 170 | _vm.TryNavigateToMfdShellPage(MfdShellPage.WebAiPortal); |
| 171 | return await Task.FromResult("OK"); |
| 172 | }); |
| 173 | add(CloseEnvironmentReadinessPage, async (_, _) => |
| 174 | { |
| 175 | if (_vm.CloseEnvironmentReadinessPageCommand.CanExecute(null)) |
| 176 | _vm.CloseEnvironmentReadinessPageCommand.Execute(null); |
| 177 | return await Task.FromResult("OK"); |
| 178 | }); |
| 179 | Handler showCorrespondencePageHandler = async (_, _) => |
| 180 | { |
| 181 | if (_vm.NavigationMap.ShowCorrespondencePageCommand.CanExecute(null)) |
| 182 | _vm.NavigationMap.ShowCorrespondencePageCommand.Execute(null); |
| 183 | return await Task.FromResult("OK"); |
| 184 | }; |
| 185 | add(ShowCorrespondencePage, showCorrespondencePageHandler); |
| 186 | add(CloseCorrespondencePage, async (_, _) => |
| 187 | { |
| 188 | if (_vm.CloseCorrespondencePageCommand.CanExecute(null)) |
| 189 | _vm.CloseCorrespondencePageCommand.Execute(null); |
| 190 | return await Task.FromResult("OK"); |
| 191 | }); |
| 192 | add(ShowMarkdownPreviewPage, async (_, _) => |
| 193 | { |
| 194 | if (_vm.ShowMarkdownPreviewPageCommand.CanExecute(null)) |
| 195 | _vm.ShowMarkdownPreviewPageCommand.Execute(null); |
| 196 | return await Task.FromResult("OK"); |
| 197 | }); |
| 198 | |
| 199 | Handler setMfdShellPageHandler = async (args, _) => |
| 200 | { |
| 201 | var raw = McpCommandJsonArgs.String(args, "page"); |
| 202 | if (string.IsNullOrWhiteSpace(raw)) |
| 203 | return "Missing page (string, MfdShellPage: Chat, Terminal, Build, SolutionExplorer, …)"; |
| 204 | if (!Enum.TryParse<MfdShellPage>(raw.Trim(), ignoreCase: true, out var page)) |
| 205 | return $"Unknown MfdShellPage: {raw}"; |
| 206 | _vm.TryNavigateToMfdShellPage(page); |
| 207 | return await Task.FromResult("OK"); |
| 208 | }; |
| 209 | add(SetMfdShellPage, setMfdShellPageHandler); |
| 210 | add(SetMfdShellPageLegacy, setMfdShellPageHandler); |
| 211 | } |
| 212 | } |
| 213 | |