Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show adorner while hovering any part of a control's TreeViewItem in DevTools #14231

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
<TreeView Name="tree"
BorderThickness="0"
ItemsSource="{Binding Nodes}"
SelectedItem="{Binding SelectedNode, Mode=TwoWay}">
SelectedItem="{Binding SelectedNode, Mode=TwoWay}"
PointerMoved="UpdateAdorner">
<TreeView.DataTemplates>
<TreeDataTemplate DataType="vm:TreeNode"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" Spacing="8"
PointerEntered="AddAdorner"
PointerExited="RemoveAdorner">
<StackPanel Orientation="Horizontal" Spacing="8">
<TextBlock Text="{Binding Type}" FontWeight="{Binding FontWeight}"/>
<TextBlock Text="{Binding Classes}"/>
<TextBlock Foreground="Gray" Text="{Binding ElementName}"/>
Expand Down
41 changes: 32 additions & 9 deletions src/Avalonia.Diagnostics/Diagnostics/Views/TreePageView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
using System;
using System.Diagnostics;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Controls.Generators;
using Avalonia.Controls.Primitives;
using Avalonia.Diagnostics.ViewModels;
using Avalonia.Input;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.VisualTree;

namespace Avalonia.Diagnostics.Views
{
internal class TreePageView : UserControl
{
private readonly Panel _adorner;
private AdornerLayer? _currentLayer;
private TreeViewItem? _hovered;
private TreeView _tree;

public TreePageView()
Expand All @@ -39,6 +37,11 @@ public TreePageView()
AdornerLayer.SetIsClipEnabled(_adorner, false);
}

private static Thickness InvertThickness(Thickness input)
{
return new Thickness(-input.Left, -input.Top, -input.Right, -input.Bottom);
}

protected void AddAdorner(object? sender, PointerEventArgs e)
{
var node = (TreeNode?)((Control)sender!).DataContext;
Expand Down Expand Up @@ -80,11 +83,6 @@ protected void AddAdorner(object? sender, PointerEventArgs e)
}
}

private static Thickness InvertThickness(Thickness input)
{
return new Thickness(-input.Left, -input.Top, -input.Right, -input.Bottom);
}

protected void RemoveAdorner(object? sender, PointerEventArgs e)
{
foreach (var border in _adorner.Children.OfType<Border>())
Expand All @@ -98,6 +96,31 @@ protected void RemoveAdorner(object? sender, PointerEventArgs e)
_currentLayer = null;
}

protected void UpdateAdorner(object? sender, PointerEventArgs e)
{
if (e.Source is not StyledElement source)
{
return;
}

var item = source.FindLogicalAncestorOfType<TreeViewItem>();
if (item == _hovered)
{
return;
}

RemoveAdorner(sender, e);

if (item is null || item.TreeViewOwner != _tree)
{
_hovered = null;
return;
}

_hovered = item;
AddAdorner(item, e);
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
Expand Down
Loading