| 1 | #nullable enable |
| 2 | |
| 3 | using Avalonia; |
| 4 | using Avalonia.Controls; |
| 5 | using Avalonia.VisualTree; |
| 6 | using CascadeIDE.Features.Chat; |
| 7 | using CascadeIDE.Models; |
| 8 | using CascadeIDE.Views; |
| 9 | |
| 10 | namespace CascadeIDE.ViewModels; |
| 11 | |
| 12 | /// <summary>Intercom attach affordances: selection hotkey, drag-to-composer (ADR 0128).</summary> |
| 13 | public partial class MainWindowViewModel |
| 14 | { |
| 15 | internal void WireIntercomAttachAffordances() |
| 16 | { |
| 17 | ChatPanel.SetDiagnosticStripsAccessor(() => |
| 18 | _workspaceDiagnostics.GetStripsForFile(CurrentFilePath)); |
| 19 | ChatPanel.SetFocusIntercomComposerAction(FocusIntercomComposer); |
| 20 | } |
| 21 | |
| 22 | internal void FocusIntercomComposer() |
| 23 | { |
| 24 | if (PrimaryWorkSurface != PrimaryWorkSurfaceKind.Intercom |
| 25 | && TogglePrimaryWorkSurfaceCommand.CanExecute(null)) |
| 26 | { |
| 27 | TogglePrimaryWorkSurfaceCommand.Execute(null); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | internal bool TryCompleteIntercomAttachDragAtScreen(double screenX, double screenY, string kind) |
| 32 | { |
| 33 | if (!TryFindIntercomComposerAtScreen(screenX, screenY, out var chatPanel)) |
| 34 | return false; |
| 35 | |
| 36 | chatPanel.ClarificationStatusText = chatPanel.AttachDragKindToComposer(kind); |
| 37 | FocusIntercomComposer(); |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | internal void AttachSelectedProblemToIntercom(ProblemListItem item) |
| 42 | { |
| 43 | var message = ChatPanel.AttachProblemToComposer(item); |
| 44 | ChatPanel.ClarificationStatusText = message; |
| 45 | FocusIntercomComposer(); |
| 46 | } |
| 47 | |
| 48 | private static bool TryFindIntercomComposerAtScreen(double screenX, double screenY, out ChatPanelViewModel chatPanel) |
| 49 | { |
| 50 | chatPanel = null!; |
| 51 | var point = new PixelPoint((int)Math.Round(screenX), (int)Math.Round(screenY)); |
| 52 | if (Avalonia.Application.Current?.ApplicationLifetime |
| 53 | is not Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime desktop) |
| 54 | { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | foreach (var window in desktop.Windows) |
| 59 | { |
| 60 | if (window is null) |
| 61 | continue; |
| 62 | |
| 63 | var local = window.PointToClient(point); |
| 64 | var hit = window.GetVisualAt(local); |
| 65 | while (hit is not null) |
| 66 | { |
| 67 | if (hit is ChatPanelView chatView |
| 68 | && chatView.DataContext is ChatPanelViewModel vm |
| 69 | && chatView.TryHitComposerAt(local)) |
| 70 | { |
| 71 | chatPanel = vm; |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | hit = hit.GetVisualParent(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | |