Skip to content

Commit

Permalink
Refactor how data context is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Sep 28, 2024
1 parent d15749f commit 16bfdd2
Show file tree
Hide file tree
Showing 31 changed files with 268 additions and 388 deletions.
6 changes: 4 additions & 2 deletions samples/NodeEditor.Base/ViewModels/MainViewViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,12 @@ public async Task Export()
{
var control = new DrawingNode
{
DataContext = Editor.Drawing
DrawingSource = Editor.Drawing,
Width = Editor.Drawing.Width,
Height = Editor.Drawing.Height,
};

var root = new ExportRoot()
var root = new ExportRoot
{
Width = Editor.Drawing.Width,
Height = Editor.Drawing.Height,
Expand Down
14 changes: 1 addition & 13 deletions samples/NodeEditor.Base/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
</UserControl.KeyBindings>
<DockPanel>
<views:MenuView ZoomControl="{Binding #EditorControl.ZoomControl}"
DrawingNode="{Binding #EditorControl.DrawingNode}"
x:CompileBindings="False"
DockPanel.Dock="Top" />
<Separator IsVisible="False" Classes="horizontal" DockPanel.Dock="Top" />
Expand All @@ -92,23 +91,12 @@
IsVisible="{Binding IsToolboxVisible}">
<Separator Classes="vertical" DockPanel.Dock="Right" />
<editor:Toolbox TemplatesSource="{Binding Editor.Templates}"
Drawing="{Binding Editor.Drawing}"
Name="ToolboxView" />
</DockPanel>
<ThemeVariantScope RequestedThemeVariant="Light"
Grid.Column="1" Grid.ColumnSpan="2">
<editor:Editor Name="EditorControl"
DataContext="{Binding Editor.Drawing, FallbackValue={x:Null}}"
DrawingWidth="{Binding Width}"
DrawingHeight="{Binding Height}"
NodesSource="{Binding Nodes}"
ConnectorsSource="{Binding Connectors}"
EnableSnap="{Binding Settings.EnableSnap}"
SnapX="{Binding Settings.SnapX}"
SnapY="{Binding Settings.SnapY}"
EnableGrid="{Binding Settings.EnableGrid}"
GridCellWidth="{Binding Settings.GridCellWidth}"
GridCellHeight="{Binding Settings.GridCellHeight}"/>
DrawingSource="{Binding Editor.Drawing, FallbackValue={x:Null}}" />
</ThemeVariantScope>
<GridSplitter Grid.Column="1" Background="Transparent" />
</Grid>
Expand Down
11 changes: 1 addition & 10 deletions samples/NodeEditor.Base/Views/MenuView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ public partial class MenuView : UserControl
{
public static readonly StyledProperty<NodeZoomBorder?> ZoomControlProperty =
AvaloniaProperty.Register<MenuView, NodeZoomBorder?>(nameof(ZoomControl));

public static readonly StyledProperty<DrawingNode?> DrawingNodeProperty =
AvaloniaProperty.Register<MenuView, DrawingNode?>(nameof(DrawingNode));


public MenuView()
{
InitializeComponent();
Expand All @@ -22,10 +19,4 @@ public NodeZoomBorder? ZoomControl
get => GetValue(ZoomControlProperty);
set => SetValue(ZoomControlProperty, value);
}

public DrawingNode? DrawingNode
{
get => GetValue(DrawingNodeProperty);
set => SetValue(DrawingNodeProperty, value);
}
}
11 changes: 10 additions & 1 deletion src/NodeEditorAvalonia/Behaviors/ConnectorsSelectedBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ namespace NodeEditor.Behaviors;

public class ConnectorsSelectedBehavior : Behavior<ItemsControl>
{
public static readonly StyledProperty<IDrawingNode?> DrawingSourceProperty =
AvaloniaProperty.Register<ConnectorsSelectedBehavior, IDrawingNode?>(nameof(DrawingSource));

public IDrawingNode? DrawingSource
{
get => GetValue(DrawingSourceProperty);
set => SetValue(DrawingSourceProperty, value);
}

private IDisposable? _dataContextDisposable;
private IDrawingNode? _drawingNode;

Expand Down Expand Up @@ -66,7 +75,7 @@ protected override void OnDetaching()

private void DrawingNode_SelectionChanged(object? sender, EventArgs e)
{
if (AssociatedObject?.DataContext is not IDrawingNode)
if (DrawingSource is not IDrawingNode)
{
return;
}
Expand Down
13 changes: 11 additions & 2 deletions src/NodeEditorAvalonia/Behaviors/DrawingDropHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ namespace NodeEditor.Behaviors;

public class DrawingDropHandler : DefaultDropHandler
{
public static readonly StyledProperty<IDrawingNode?> DrawingSourceProperty =
AvaloniaProperty.Register<DrawingDropHandler, IDrawingNode?>(nameof(DrawingSource));

public static readonly StyledProperty<Control?> RelativeToProperty =
AvaloniaProperty.Register<DrawingDropHandler, Control?>(nameof(RelativeTo));

public IDrawingNode? DrawingSource
{
get => GetValue(DrawingSourceProperty);
set => SetValue(DrawingSourceProperty, value);
}

public Control? RelativeTo
{
get => GetValue(RelativeToProperty);
Expand All @@ -27,9 +36,9 @@ private bool Validate(IDrawingNode drawing, object? sender, DragEventArgs e, boo
}
var point = GetPosition(relativeTo, e);

if (relativeTo is DrawingNode drawingNode)
if (relativeTo is DrawingNode { DrawingSource: not null } drawingNode)
{
point = SnapHelper.Snap(point, drawingNode.SnapX, drawingNode.SnapY, drawingNode.EnableSnap);
point = SnapHelper.Snap(point, drawingNode.DrawingSource.Settings.SnapX, drawingNode.DrawingSource.Settings.SnapY, drawingNode.DrawingSource.Settings.EnableSnap);
}

if (e.Data.Contains(DataFormats.Text))
Expand Down
14 changes: 12 additions & 2 deletions src/NodeEditorAvalonia/Behaviors/DrawingMovedBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Controls;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;
Expand All @@ -8,6 +9,15 @@ namespace NodeEditor.Behaviors;

public class DrawingMovedBehavior : Behavior<ItemsControl>
{
public static readonly StyledProperty<IDrawingNode?> DrawingSourceProperty =
AvaloniaProperty.Register<DrawingMovedBehavior, IDrawingNode?>(nameof(DrawingSource));

public IDrawingNode? DrawingSource
{
get => GetValue(DrawingSourceProperty);
set => SetValue(DrawingSourceProperty, value);
}

protected override void OnAttached()
{
base.OnAttached();
Expand All @@ -30,7 +40,7 @@ protected override void OnDetaching()

private void Moved(object? sender, PointerEventArgs e)
{
if (AssociatedObject?.DataContext is not IDrawingNode drawingNode)
if (DrawingSource is not IDrawingNode drawingNode)
{
return;
}
Expand Down
14 changes: 12 additions & 2 deletions src/NodeEditorAvalonia/Behaviors/DrawingPressedBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Controls;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;
Expand All @@ -8,6 +9,15 @@ namespace NodeEditor.Behaviors;

public class DrawingPressedBehavior : Behavior<ItemsControl>
{
public static readonly StyledProperty<IDrawingNode?> DrawingSourceProperty =
AvaloniaProperty.Register<DrawingPressedBehavior, IDrawingNode?>(nameof(DrawingSource));

public IDrawingNode? DrawingSource
{
get => GetValue(DrawingSourceProperty);
set => SetValue(DrawingSourceProperty, value);
}

protected override void OnAttached()
{
base.OnAttached();
Expand Down Expand Up @@ -35,7 +45,7 @@ private void Pressed(object? sender, PointerPressedEventArgs e)
return;
}

if (AssociatedObject?.DataContext is not IDrawingNode drawingNode)
if (DrawingSource is not IDrawingNode drawingNode)
{
return;
}
Expand Down
Loading

0 comments on commit 16bfdd2

Please sign in to comment.