XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using Microsoft.UI.Input;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using Windows.System;
using XFEWorkflow.Services;

namespace XFEWorkflow.Controls;

/// <summary>A native mouse/touch/keyboard splitter. Window constraints never overwrite the preferred size.</summary>
public sealed class PaneSplitter : Control
{
    public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(nameof(Orientation), typeof(Orientation), typeof(PaneSplitter),
        new PropertyMetadata(Orientation.Vertical, (sender, _) => ((PaneSplitter)sender).UpdateDirection()));
    public Orientation Orientation { get => (Orientation)GetValue(OrientationProperty); set => SetValue(OrientationProperty, value); }
    public bool Reverse { get; set; }
    public double PreferredSize { get; private set; }
    private Func<double>? getSize, available;
    private Action<double>? setSize;
    private string key = "";
    private double minimum, maximum, fallback;
    private Border? grip;
    private Thumb? thumb;
    private bool dragging;
    public event Action<string>? Notice;
    public PaneSplitter()
    {
        DefaultStyleKey = typeof(PaneSplitter); IsTabStop = true;
        ToolTipService.SetToolTip(this, "拖动调整大小 · 方向键微调 · 双击恢复");
        Loaded += (_, _) => { UpdateDirection(); ApplyPreferredSize(); };
        PointerEntered += (_, _) => { if (!dragging) VisualStateManager.GoToState(this, "PointerOver", true); };
        PointerExited += (_, _) => { if (!dragging) VisualStateManager.GoToState(this, "Normal", true); };
        IsEnabledChanged += (_, _) => VisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled", true);
        DoubleTapped += async (_, e) => { e.Handled = true; Reset(); await SaveAsync(); };
        KeyDown += async (_, e) =>
        {
            var delta = e.Key switch
            {
                VirtualKey.Left when Orientation == Orientation.Vertical => -8,
                VirtualKey.Right when Orientation == Orientation.Vertical => 8,
                VirtualKey.Up when Orientation == Orientation.Horizontal => -8,
                VirtualKey.Down when Orientation == Orientation.Horizontal => 8,
                _ => 0
            };
            if (delta == 0 && e.Key != VirtualKey.Home) return;
            e.Handled = true;
            if (e.Key == VirtualKey.Home) Reset(); else ResizeBy(delta * (Reverse ? -1 : 1));
            await SaveAsync();
        };
    }
    protected override void OnApplyTemplate()
    {
        if (thumb is not null) { thumb.DragDelta -= Thumb_DragDelta; thumb.DragStarted -= Thumb_DragStarted; thumb.DragCompleted -= Thumb_DragCompleted; }
        base.OnApplyTemplate(); grip = GetTemplateChild("Grip") as Border; thumb = GetTemplateChild("PART_Thumb") as Thumb;
        if (thumb is not null) { thumb.DragDelta += Thumb_DragDelta; thumb.DragStarted += Thumb_DragStarted; thumb.DragCompleted += Thumb_DragCompleted; }
        UpdateDirection();
    }
    private void Thumb_DragDelta(object sender, DragDeltaEventArgs e) => ResizeBy((Orientation == Orientation.Vertical ? e.HorizontalChange : e.VerticalChange) * (Reverse ? -1 : 1));
    private void Thumb_DragStarted(object sender, DragStartedEventArgs e) { dragging = true; VisualStateManager.GoToState(this, "Pressed", true); }
    private async void Thumb_DragCompleted(object sender, DragCompletedEventArgs e) { dragging = false; VisualStateManager.GoToState(this, "Normal", true); await SaveAsync(); }
    private void UpdateDirection()
    {
        ProtectedCursor = InputSystemCursor.Create(Orientation == Orientation.Vertical ? InputSystemCursorShape.SizeWestEast : InputSystemCursorShape.SizeNorthSouth);
        if (grip is not null) { grip.Width = Orientation == Orientation.Vertical ? 2 : 28; grip.Height = Orientation == Orientation.Vertical ? 28 : 2; }
    }
    public void Bind(string preferenceKey, double defaultSize, double minimumSize, double maximumSize, Func<double> read, Action<double> write, Func<double> availableSize)
    {
        key = preferenceKey; fallback = defaultSize; minimum = minimumSize; maximum = maximumSize;
        getSize = read; setSize = write; available = availableSize;
        PreferredSize = AppServices.Current.State.PaneSize(key, fallback); ApplyPreferredSize();
    }
    private double Limit
    {
        get { var bound = available?.Invoke() ?? maximum; return Math.Max(minimum, Math.Min(maximum, double.IsFinite(bound) && bound > 0 ? bound : maximum)); }
    }
    public void ApplyPreferredSize() => setSize?.Invoke(Math.Clamp(PreferredSize, minimum, Limit));
    private void ResizeBy(double delta)
    {
        if (getSize is null || setSize is null || !double.IsFinite(delta)) return;
        PreferredSize = Math.Clamp(getSize() + delta, minimum, Limit); setSize(PreferredSize);
    }
    public void Reset() { PreferredSize = fallback; ApplyPreferredSize(); }
    public void ReloadPreference() { PreferredSize = AppServices.Current.State.PaneSize(key, fallback); ApplyPreferredSize(); }
    public async Task SaveAsync()
    {
        if (key.Length == 0) return;
        try { AppServices.Current.State.SetPaneSize(key, PreferredSize); await AppServices.Current.State.SaveAsync(); }
        catch (Exception ex) { Notice?.Invoke("布局保存失败:" + ex.Message); System.Diagnostics.Debug.WriteLine(ex); }
    }
}