using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using TeamAAS.FlowEngine.Execution;
using TeamAAS.ViewModels;
namespace TeamAAS.Views
{
public partial class ProductView : UserControl
{
private const double ExpandedWidth = 280;
private const double CollapsedWidth = 56;
private const double AnimationMs = 250;
private ProductViewModel _vm;
public ProductView()
{
InitializeComponent();
DataContext = new ProductViewModel();
DataContextChanged += OnDataContextChanged;
Loaded += OnLoaded;
Unloaded += OnUnloaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
HookViewModel(DataContext as ProductViewModel);
// 运行/停止状态变化 → 刷新"运行中锁定"遮罩与角标
FlowRunner.Instance.RunStateChanged += OnRunStateChanged;
// 切回产品页时立即按当前运行状态同步一次,避免在别处启停流程后画布锁定状态滞后
_vm?.RefreshRunState();
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
FlowRunner.Instance.RunStateChanged -= OnRunStateChanged;
}
private void OnRunStateChanged()
{
_vm?.RefreshRunState();
}
private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
HookViewModel(e.NewValue as ProductViewModel);
}
private void HookViewModel(ProductViewModel vm)
{
if (_vm != null)
_vm.PropertyChanged -= OnVmPropertyChanged;
_vm = vm;
if (_vm != null)
{
_vm.PropertyChanged += OnVmPropertyChanged;
ApplySidebarState(_vm.IsSidebarCollapsed, animate: false);
}
}
private void OnVmPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ProductViewModel.IsSidebarCollapsed))
{
ApplySidebarState(_vm.IsSidebarCollapsed, animate: true);
}
}
///
/// 产品列表右键菜单"属性":直接走 code-behind 事件(Context 弹窗在 Popup 独立可视树中,
/// MenuItem 的 Command 绑定无法可靠命中 ViewModel,故不经过绑定)。
/// 右键哪个产品就编辑哪个产品(PlacementTarget 的 DataContext = 产品名)。
///
private void ProductProperties_Click(object sender, RoutedEventArgs e)
{
var vm = DataContext as ProductViewModel;
if (vm == null) return;
string productName = null;
if (sender is MenuItem mi
&& mi.Parent is ContextMenu cm
&& cm.PlacementTarget is FrameworkElement target)
{
productName = target.DataContext as string;
}
if (string.IsNullOrEmpty(productName)) return;
vm.ShowProductProperties(productName);
}
/// 产品列表右键菜单“全局变量”:编辑该产品的 Product 作用域变量(弹窗)。
private void ProductGlobalVars_Click(object sender, RoutedEventArgs e)
{
var vm = DataContext as ProductViewModel;
if (vm == null) return;
string productName = null;
if (sender is MenuItem mi
&& mi.Parent is ContextMenu cm
&& cm.PlacementTarget is FrameworkElement target)
{
productName = target.DataContext as string;
}
if (string.IsNullOrEmpty(productName)) return;
vm.ShowProductGlobalVars(productName);
}
private void ApplySidebarState(bool collapsed, bool animate)
{
double targetWidth = collapsed ? CollapsedWidth : ExpandedWidth;
if (animate)
{
var widthAnim = new DoubleAnimation(targetWidth, TimeSpan.FromMilliseconds(AnimationMs))
{
EasingFunction = new CubicEase { EasingMode = EasingMode.EaseInOut }
};
SidebarPanel.BeginAnimation(FrameworkElement.WidthProperty, widthAnim);
}
else
{
SidebarPanel.Width = targetWidth;
}
if (collapsed)
{
if (animate)
{
var fadeOut = new DoubleAnimation(1.0, 0.0, TimeSpan.FromMilliseconds(120));
fadeOut.Completed += (s, args) =>
{
SidebarExpanded.Visibility = Visibility.Collapsed;
SidebarCollapsed.Visibility = Visibility.Visible;
SidebarCollapsed.Opacity = 0;
var fadeIn = new DoubleAnimation(0.0, 1.0, TimeSpan.FromMilliseconds(130));
SidebarCollapsed.BeginAnimation(UIElement.OpacityProperty, fadeIn);
};
SidebarExpanded.BeginAnimation(UIElement.OpacityProperty, fadeOut);
}
else
{
SidebarExpanded.Visibility = Visibility.Collapsed;
SidebarCollapsed.Visibility = Visibility.Visible;
SidebarCollapsed.Opacity = 1.0;
}
}
else
{
if (animate)
{
var fadeOut = new DoubleAnimation(1.0, 0.0, TimeSpan.FromMilliseconds(120));
fadeOut.Completed += (s, args) =>
{
SidebarCollapsed.Visibility = Visibility.Collapsed;
SidebarExpanded.Visibility = Visibility.Visible;
SidebarExpanded.Opacity = 0;
var fadeIn = new DoubleAnimation(0.0, 1.0, TimeSpan.FromMilliseconds(130));
SidebarExpanded.BeginAnimation(UIElement.OpacityProperty, fadeIn);
};
SidebarCollapsed.BeginAnimation(UIElement.OpacityProperty, fadeOut);
}
else
{
SidebarCollapsed.Visibility = Visibility.Collapsed;
SidebarExpanded.Visibility = Visibility.Visible;
SidebarExpanded.Opacity = 1.0;
}
}
}
}
}