Skip to content

Commit

Permalink
Fix style when mouse is selected.
Browse files Browse the repository at this point in the history
Added an example of modifying the Node border style.
  • Loading branch information
MakesYT committed Jan 27, 2024
1 parent e71b043 commit 368c2d6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 20 deletions.
6 changes: 6 additions & 0 deletions Nodify.Avalonia.Example/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
Header="{Binding Title}"
VerticalAlignment="Center"
Output="{Binding Output}">
<controls:Node.Styles>
<Style Selector="controls|Node[IsSelected=False]:pointerover">
<Setter Property="BorderBrush" Value="AliceBlue"></Setter>
<Setter Property="BorderThickness" Value="2"></Setter>
</Style>
</controls:Node.Styles>
<controls:Node.InputConnectorTemplate>
<DataTemplate DataType="{x:Type viewModelBase:ConnectorViewModelBase}">
<controls:NodeInput
Expand Down
18 changes: 18 additions & 0 deletions Nodify.Avalonia.Example/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- This manifest is used on Windows only.
Don't remove it as it might cause problems with window transparency and embedded controls.
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
<assemblyIdentity version="1.0.0.0" name="Nodify.Avalonia.Example.Desktop"/>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
</assembly>
2 changes: 1 addition & 1 deletion Nodify.Avalonia/Controls/Node.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
</ControlTemplate>
</Setter>
</Style>
<Style Selector="controls|Node:pointerover">
<Style Selector="controls|Node[IsSelected=False]:pointerover">
<Setter Property="BorderBrush" Value="LightBlue"></Setter>
<Setter Property="BorderThickness" Value="2"></Setter>
</Style>
Expand Down
12 changes: 3 additions & 9 deletions Nodify.Avalonia/Controls/Node.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,15 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)

private void OnPointerPressed(object sender, PointerPressedEventArgs e)
{
this.GetVisualParent().ZIndex = 2;

var visualParent = this.GetVisualParent();
var parent = visualParent.GetVisualParent().GetVisualChildren();
foreach (var visual in parent)
{
visual.ZIndex = 0;
((Node)visual.GetVisualChildren().First()).IsSelected = false;
}

visualParent.ZIndex = 1;
this.IsSelected = true;
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return;
// 启动拖动
Expand All @@ -236,14 +236,8 @@ private void OnPointerPressed(object sender, PointerPressedEventArgs e)

private void OnPointerReleased(object sender, PointerReleasedEventArgs e)
{
var visualParent = this.GetVisualParent();
var parent = visualParent.GetVisualParent().GetVisualChildren();
foreach (var visual in parent)
{
visual.ZIndex = 0;
}

visualParent.ZIndex = 1;

if (!isDragging) return;
// 停止拖动
isDragging = false;
Expand Down
5 changes: 3 additions & 2 deletions Nodify.Avalonia/Controls/NodifyEditor.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@
</Setter>
</Style>
</Canvas.Resources>

<ItemsPresenter Canvas.Top="{TemplateBinding OffsetY}" Canvas.Left="{TemplateBinding OffsetX}" ItemsPanel="{TemplateBinding ItemsPanel}" />
<ContentPresenter ZIndex="-2" ContentTemplate="{TemplateBinding GridLineTemplate}"/>
<ItemsPresenter Canvas.Top="{TemplateBinding OffsetY}" Canvas.Left="{TemplateBinding OffsetX}" ItemsPanel="{TemplateBinding ItemsPanel}" Name="ItemsPresenter" />
<ContentPresenter Name="PendingConnection" Content="{TemplateBinding PendingConnection}"
ContentTemplate="{TemplateBinding PendingConnectionTemplate}" />
<ItemsControl
Expand Down Expand Up @@ -96,7 +97,7 @@
</Setter>
<Setter Property="ItemsPanel">
<ItemsPanelTemplate>
<Canvas x:Name="PART_ItemsHost" Background="{TemplateBinding Background}" Name="ZoomCanvas" >
<Canvas x:Name="PART_ItemsHost" Background="{TemplateBinding Background}" Name="NodeItemsPresenter" >

</Canvas>
</ItemsPanelTemplate>
Expand Down
10 changes: 5 additions & 5 deletions Nodify.Avalonia/Controls/NodifyEditor.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Input;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml.Templates;
using Avalonia.Media;
using Avalonia.Styling;
Expand Down Expand Up @@ -539,19 +540,18 @@ protected override void OnApplyTemplate( TemplateAppliedEventArgs e)

private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
foreach (var visualChild in this.GetVisualChildren())
var visual = ((Control)sender).GetLogicalChildren();
foreach (var visualChild in visual)
{
if (visualChild.GetVisualChildren().First() is Node n)

if (visualChild.GetLogicalChildren().First() is Node n)
{
n.IsSelected = false;
}
}
if (!e.GetCurrentPoint(this).Properties.IsLeftButtonPressed) return;
// 启动拖动
isDragging = true;
// 记录当前坐标
lastMousePosition = e.GetPosition(this);
// Debug.WriteLine($"记录当前坐标X:{lastMousePosition.X} Y:{lastMousePosition.Y}");
_startOffsetX = OffsetX;
_startOffsetY = OffsetY;
e.Handled = true;
Expand Down
6 changes: 3 additions & 3 deletions Nodify.Avalonia/Helpers/DependencyObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ internal static class DependencyObjectExtensions
public static T? GetChildOfType<T>(this Control control,string name)
where T : Control
{
if (control.Name == name)
if (control.Name == name&& control is T)
return (T)control;

foreach (var child in control.GetVisualChildren())
{
var foundChild = child as Control;
if (foundChild != null && foundChild.Name == name)
if (foundChild != null && foundChild.Name == name&& foundChild is T)
return (T)foundChild;
else
{
var result = (T)(foundChild).GetChildOfType<Control>(name);
var result = (T)((foundChild).GetChildOfType<Control>(name));
if (result != null)
return result;
}
Expand Down

0 comments on commit 368c2d6

Please sign in to comment.