Files
Lattice/Lattice.UI.Docking.WinUI/Controls/LatticeSplitter.cs
2026-01-18 16:33:35 +03:00

51 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Lattice.Core.Docking.Models;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using System;
namespace Lattice.UI;
public class LatticeSplitter : Control
{
public LatticeSplitter()
{
this.DefaultStyleKey = typeof(LatticeSplitter);
this.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
this.PointerEntered += (s, e) =>
this.ProtectedCursor = InputSystemCursor.Create(InputSystemCursorShape.SizeWestEast);
this.PointerExited += (s, e) =>
this.ProtectedCursor = null;
this.ManipulationDelta += OnManipulationDelta;
}
private void OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
// 1. Находим модель DockGroup через DataContext
if (this.DataContext is not DockGroup group) return;
// 2. Находим родительский Grid, чтобы знать общие размеры
if (VisualTreeHelper.GetParent(this) is not Grid parentGrid ||
parentGrid.ActualWidth <= 0 || parentGrid.ActualHeight <= 0)
return;
double totalSize = group.Orientation == SplitDirection.Horizontal
? parentGrid.ActualWidth
: parentGrid.ActualHeight;
if (totalSize <= 0) return;
// 3. Вычисляем изменение Ratio (от -1.0 до 1.0)
double delta = group.Orientation == SplitDirection.Horizontal
? e.Delta.Translation.X
: e.Delta.Translation.Y;
double ratioChange = delta / totalSize;
// 4. Обновляем модель (с ограничением от 0.05 до 0.95)
group.SplitRatio = Math.Clamp(group.SplitRatio + ratioChange, 0.05, 0.95);
}
}