Files
Lattice/Lattice.UI.Docking.WinUI/Factories/WinUIDockControlFactory.cs
2026-02-01 09:26:13 +03:00

85 lines
2.9 KiB
C#

using Lattice.Core.Docking.Abstractions;
using Lattice.Core.Docking.Models;
using Lattice.UI.Docking.Abstractions;
using Lattice.UI.Docking.Factories;
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
namespace Lattice.UI.Docking.WinUI.Factories;
public sealed class WinUIDockControlFactory : DockControlFactoryBase, IDockControlFactory
{
private readonly Dictionary<Type, Func<object, IDockControl>> _creators;
public WinUIDockControlFactory()
{
_creators = new Dictionary<Type, Func<object, IDockControl>>
{
[typeof(DockGroup)] = model => CreateGroupControl((DockGroup)model),
[typeof(DockLeaf)] = model => CreateLeafControl((DockLeaf)model),
[typeof(DockWindow)] = model => CreateFloatingWindowControl((DockWindow)model),
[typeof(AutoHidePanel)] = model => CreateAutoHidePanelControl((AutoHidePanel)model),
};
}
public override IDockGroupControl CreateGroupControl(DockGroup group)
{
if (group == null) throw new ArgumentNullException(nameof(group));
var control = new LatticeDockGroup();
ConfigureControl(control, group);
return control;
}
public override IDockLeafControl CreateLeafControl(DockLeaf leaf)
{
if (leaf == null) throw new ArgumentNullException(nameof(leaf));
var control = new LatticeTabControl();
ConfigureControl(control, leaf);
return control;
}
public override IFloatingWindowControl CreateFloatingWindowControl(DockWindow window)
{
throw new NotImplementedException("Floating windows not implemented yet");
}
public override IAutoHidePanelControl CreateAutoHidePanelControl(AutoHidePanel panel)
{
throw new NotImplementedException("Auto-hide panels not implemented yet");
}
public override IDockSplitterControl CreateSplitterControl(SplitDirection orientation)
{
var control = new LatticeSplitter { Orientation = orientation };
ConfigureControl(control);
return control;
}
public override IDockControl? CreateControlForElement(IDockElement element)
{
if (element == null) throw new ArgumentNullException(nameof(element));
var type = element.GetType();
if (_creators.TryGetValue(type, out var creator))
return creator(element);
return base.CreateControlForElement(element);
}
private void ConfigureControl(IDockControl control, IDockElement? model = null)
{
if (control == null) return;
control.Model = model;
control.LayoutManager = Lattice.UI.Docking.LatticeUIFramework.LayoutManager;
control.ContextManager = Lattice.UI.Docking.LatticeUIFramework.ContextManager;
if (control is FrameworkElement frameworkElement && model != null)
{
frameworkElement.DataContext = model;
}
}
}