Files
Lattice/Lattice.Core.Docking/Models/DockLeaf.cs
2026-02-01 09:26:13 +03:00

131 lines
3.0 KiB
C#

using Lattice.Core.Docking.Abstractions;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Lattice.Core.Docking.Models;
public class DockLeaf : IDockContainer, INotifyPropertyChanged
{
private readonly ObservableCollection<IDockContent> _items = new();
private IDockContent? _activeContent;
private IDockElement? _parent;
private double _width;
private double _height;
private TabPlacement _tabPlacement = TabPlacement.Top;
public event PropertyChangedEventHandler? PropertyChanged;
public DockLeaf()
{
_items.CollectionChanged += (s, e) => OnPropertyChanged(nameof(Children));
}
public string Id { get; } = Guid.NewGuid().ToString();
public IDockElement? Parent
{
get => _parent;
set
{
if (_parent != value)
{
_parent = value;
OnPropertyChanged();
}
}
}
public IList<IDockContent> Children => _items;
public IDockContent? ActiveContent
{
get => _activeContent;
set
{
if (_activeContent != value)
{
_activeContent = value;
OnPropertyChanged();
}
}
}
public double Width
{
get => _width;
set
{
if (Math.Abs(_width - value) > 0.001)
{
_width = value;
OnPropertyChanged();
}
}
}
public double Height
{
get => _height;
set
{
if (Math.Abs(_height - value) > 0.001)
{
_height = value;
OnPropertyChanged();
}
}
}
public double MinWidth { get; set; } = 100;
public double MinHeight { get; set; } = 100;
public TabPlacement TabPlacement
{
get => _tabPlacement;
set
{
if (_tabPlacement != value)
{
_tabPlacement = value;
OnPropertyChanged();
}
}
}
public void AddContent(IDockContent content)
{
if (content == null)
throw new ArgumentNullException(nameof(content));
if (!_items.Contains(content))
{
_items.Add(content);
}
ActiveContent = content;
}
public void RemoveContent(IDockContent content)
{
if (content == null)
throw new ArgumentNullException(nameof(content));
int index = _items.IndexOf(content);
if (index == -1) return;
_items.RemoveAt(index);
if (ActiveContent == content)
{
if (_items.Count > 0)
ActiveContent = _items[Math.Min(index, _items.Count - 1)];
else
ActiveContent = null;
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}