| 1 | #nullable enable |
| 2 | |
| 3 | using System.Text.Json; |
| 4 | using Avalonia; |
| 5 | using Avalonia.Controls; |
| 6 | using Avalonia.Input; |
| 7 | using Avalonia.Interactivity; |
| 8 | using CascadeIDE.Services.Intercom; |
| 9 | using CascadeIDE.ViewModels; |
| 10 | |
| 11 | namespace CascadeIDE.Views; |
| 12 | |
| 13 | public partial class SkiaChatSurfaceControl |
| 14 | { |
| 15 | public event EventHandler<IntercomAttachDropEventArgs>? IntercomAttachDropped; |
| 16 | |
| 17 | private void InitializeIntercomAttachDrop() |
| 18 | { |
| 19 | DragDrop.SetAllowDrop(this, true); |
| 20 | AddHandler(DragDrop.DragOverEvent, OnIntercomAttachDragOver, RoutingStrategies.Tunnel); |
| 21 | AddHandler(DragDrop.DropEvent, OnIntercomAttachDrop, RoutingStrategies.Tunnel); |
| 22 | } |
| 23 | |
| 24 | internal bool TryHitComposerBounds(float x, float y) => |
| 25 | ShowIntercomComposer |
| 26 | && _composerBounds.Width > 0 |
| 27 | && x >= _composerBounds.Left |
| 28 | && x <= _composerBounds.Right |
| 29 | && y >= _composerBounds.Top |
| 30 | && y <= _composerBounds.Bottom; |
| 31 | |
| 32 | private void OnIntercomAttachDragOver(object? sender, DragEventArgs e) |
| 33 | { |
| 34 | if (!ShowIntercomComposer || !TryReadAttachPayload(e.DataTransfer, out _)) |
| 35 | { |
| 36 | e.DragEffects = DragDropEffects.None; |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | e.DragEffects = DragDropEffects.Copy; |
| 41 | e.Handled = true; |
| 42 | } |
| 43 | |
| 44 | private void OnIntercomAttachDrop(object? sender, DragEventArgs e) |
| 45 | { |
| 46 | if (!ShowIntercomComposer || !TryReadAttachPayload(e.DataTransfer, out var payload)) |
| 47 | return; |
| 48 | |
| 49 | var point = e.GetPosition(this); |
| 50 | if (!TryHitComposerBounds((float)point.X, (float)point.Y)) |
| 51 | return; |
| 52 | |
| 53 | e.Handled = true; |
| 54 | IntercomAttachDropped?.Invoke(this, new IntercomAttachDropEventArgs(payload, point)); |
| 55 | } |
| 56 | |
| 57 | private static bool TryReadAttachPayload(IDataTransfer dataTransfer, out string payload) |
| 58 | { |
| 59 | payload = ""; |
| 60 | var text = dataTransfer.TryGetText(); |
| 61 | if (!string.IsNullOrEmpty(text) |
| 62 | && text.StartsWith(IntercomAttachDragFormats.TextPrefix, StringComparison.Ordinal)) |
| 63 | { |
| 64 | payload = text[IntercomAttachDragFormats.TextPrefix.Length..]; |
| 65 | return payload.Length > 0; |
| 66 | } |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public sealed class IntercomAttachDropEventArgs : EventArgs |
| 73 | { |
| 74 | public IntercomAttachDropEventArgs(string payload, Point positionInSurface) |
| 75 | { |
| 76 | Payload = payload; |
| 77 | PositionInSurface = positionInSurface; |
| 78 | } |
| 79 | |
| 80 | public string Payload { get; } |
| 81 | |
| 82 | public Point PositionInSurface { get; } |
| 83 | } |
| 84 | |
| 85 | internal static class IntercomAttachDragPayload |
| 86 | { |
| 87 | public static string ForKind(string kind) => IntercomAttachDragFormats.EncodeTextPayload(kind); |
| 88 | |
| 89 | public static string ForProblem(ProblemListItem item) => |
| 90 | IntercomAttachDragFormats.EncodeTextPayload(JsonSerializer.Serialize(new |
| 91 | { |
| 92 | kind = IntercomAttachDragFormats.KindProblem, |
| 93 | filePath = item.FilePath, |
| 94 | line = item.Line, |
| 95 | column = item.Column, |
| 96 | severity = item.Severity, |
| 97 | id = item.Id, |
| 98 | message = item.Message, |
| 99 | })); |
| 100 | } |
| 101 | |