Skip to content

Commit

Permalink
feat(DevTools): Add Avalonia_DevTools_Selectors Clipoard format for q…
Browse files Browse the repository at this point in the history
…uicks IDE recongnize. (#14603)
  • Loading branch information
workgroupengineering authored Apr 18, 2024
1 parent 567a55a commit a376229
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
15 changes: 15 additions & 0 deletions src/Avalonia.Diagnostics/Diagnostics/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Avalonia.Diagnostics;

internal static class Constants
{
/// <summary>
/// DevTools Clipboard data format
/// </summary>
static public class DataFormats
{
/// <summary>
/// Clipboard data format for the selector. It is added for quick format recognition in IDEs
/// </summary>
public const string Avalonia_DevTools_Selector = nameof(Avalonia_DevTools_Selector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public TreePageViewModel(MainViewModel mainView, TreeNode[] nodes, ISet<string>
}

public event EventHandler<string>? ClipboardCopyRequested;

public MainViewModel MainView { get; }

public FilterViewModel PropertiesFilter { get; }
Expand Down Expand Up @@ -117,11 +117,11 @@ public void CopySelector()
if (currentVisual is not null)
{
var selector = GetVisualSelector(currentVisual);

ClipboardCopyRequested?.Invoke(this, selector);
}
}

public void CopySelectorFromTemplateParent()
{
var parts = new List<string>();
Expand All @@ -130,7 +130,7 @@ public void CopySelectorFromTemplateParent()
while (currentVisual is not null)
{
parts.Add(GetVisualSelector(currentVisual));

currentVisual = currentVisual.TemplatedParent as Visual;
}

Expand All @@ -148,7 +148,7 @@ public void ExpandRecursively()
if (SelectedNode is { } selectedNode)
{
ExpandNode(selectedNode);

var stack = new Stack<TreeNode>();
stack.Push(selectedNode);

Expand Down Expand Up @@ -192,8 +192,8 @@ public void BringIntoView()
{
(SelectedNode?.Visual as Control)?.BringIntoView();
}


public void Focus()
{
(SelectedNode?.Visual as Control)?.Focus();
Expand All @@ -205,10 +205,10 @@ private static string GetVisualSelector(Visual visual)
var classes = string.Concat(visual.Classes
.Where(c => !c.StartsWith(":"))
.Select(c => '.' + c));
var typeName = StyledElement.GetStyleKey(visual);

return $"{typeName}{name}{classes}";
}
var pseudo = string.Concat(visual.Classes.Where(c => c[0] == ':').Select(c => c));
var type = StyledElement.GetStyleKey(visual);
return $$"""{{{type.Assembly.FullName}}}{{type.Namespace}}|{{type.Name}}{{name}}{{classes}}{{pseudo}}""";
}

private void ExpandNode(TreeNode? node)
{
Expand Down
35 changes: 33 additions & 2 deletions src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,40 @@ private void InitializeComponent()
AvaloniaXamlLoader.Load(this);
}

private void OnClipboardCopyRequested(object? sender, string e)
private void OnClipboardCopyRequested(object? sender, string selector)
{
TopLevel.GetTopLevel(this)?.Clipboard?.SetTextAsync(e);
if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard)
{
var @do = new DataObject();
var text = ToText(selector);
@do.Set(DataFormats.Text, text);
@do.Set(Constants.DataFormats.Avalonia_DevTools_Selector, selector);
clipboard.SetDataObjectAsync(@do);
}
}

private static string ToText(string text)
{
var sb = new System.Text.StringBuilder();
var bufferStartIndex = -1;
for (var ic = 0; ic < text.Length; ic++)
{
var c = text[ic];
switch (c)
{
case '{':
bufferStartIndex = sb.Length;
break;
case '}' when bufferStartIndex > -1:
sb.Remove(bufferStartIndex, sb.Length - bufferStartIndex);
bufferStartIndex = sb.Length;
break;
default:
sb.Append(c);
break;
}
}
return sb.ToString();
}
}
}

0 comments on commit a376229

Please sign in to comment.