Forge
csharpdeeb25a2
1#nullable enable
2
3using System.Text.Json;
4using Avalonia;
5using Avalonia.Controls;
6using Avalonia.Input;
7using Avalonia.Interactivity;
8using CascadeIDE.Services.Intercom;
9using CascadeIDE.ViewModels;
10
11namespace CascadeIDE.Views;
12
13public 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
72public 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
85internal 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
View only · write via MCP/CIDE