Files
BeWoPlaner/BeWo/View/Windows/AnimatedBeWoWindow.xaml.cs
2025-04-08 10:22:22 +02:00

187 lines
4.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace BeWo.View.Windows
{
/// <summary>
/// Interaktionslogik für AnimatedBeWoWindow.xaml
/// </summary>
public partial class AnimatedBeWoWindow : Window
{
private bool closeStoryBoardCompleted = false;
private WaitLayer2 _WaitLayer;
private object _GroupBoxContent;
public object GroupBoxContent
{
get { return _GroupBoxContent; }
set
{
_GroupBoxContent = value;
rootGroupBox.Content = value;
}
}
public bool IsMaximizable = true;
public bool IsWaiting { get; set; } = false;
public AnimatedBeWoWindow()
{
InitializeComponent();
CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, (s, e) => Close()));
StartWaiting();
if (BeWoApp.CurrentAnimatedBeWoWindow == null)
{
BeWoApp.CurrentAnimatedBeWoWindow = this;
}
else
{
throw new InvalidOperationException("Opening, BeWoApp.CurrentAnimatedBeWoWindow already set");
}
}
public AnimatedBeWoWindow(FrameworkElement fe) : this()
{
rootGroupBox.Content = fe;
}
public AnimatedBeWoWindow(FrameworkElement fe, string stylestring) : this(fe)
{
rootGroupBox.Style = (Style)FindResource(stylestring);
}
public void Window_Closing(object sender, CancelEventArgs e)
{
if (!closeStoryBoardCompleted)
{
if (BeWoApp.CurrentAnimatedBeWoWindow == this)
{
BeWoApp.CurrentAnimatedBeWoWindow = null;
}
else
{
throw new InvalidOperationException("Closing, BeWoApp.CurrentAnimatedBeWoWindow not found");
}
if (FindResource("WindowClosingAnimation") is Storyboard storyboard)
{
storyboard.Completed += closeStoryBoard_Completed;
Storyboard.SetTarget(storyboard.Children[0], this);
Storyboard.SetTarget(storyboard.Children[1], this);
storyboard.Begin();
e.Cancel = true;
}
else
{
throw new InvalidOperationException("WindowClosingAnimation not found");
}
}
}
private void closeStoryBoard_Completed(object sender, EventArgs e)
{
closeStoryBoardCompleted = true;
Close();
}
private void RootGroupBox_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
var p = e.GetPosition(this);
if (p.Y <= 40)
{
DragMove();
}
}
catch (Exception) { }
}
private void RootGroupBox_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (!IsVisible)
{
return;
}
if (!IsMaximizable)
{
return;
}
var pos = PointToScreen(Mouse.GetPosition(this));
if (!(e.OriginalSource is Border) && !(e.OriginalSource is TextBlock) || pos.Y > 200d || e.OriginalSource is TextBlock && Mouse.DirectlyOver is Button)
{
return;
}
if (WindowState == WindowState.Maximized)
{
WindowState = WindowState.Normal;
return;
}
if (WindowState == WindowState.Normal)
{
WindowState = WindowState.Maximized;
}
}
public void Window_Closed(object sender, EventArgs e)
{
}
public void StartWaiting()
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)StartWaitingImmediately);
}
public void StartWaitingImmediately()
{
if (_WaitLayer == null)
{
_WaitLayer = new WaitLayer2();
Grid.SetColumnSpan(_WaitLayer, 3);
Grid.SetRowSpan(_WaitLayer, 3);
rootGrid.Children.Add(_WaitLayer);
Panel.SetZIndex(_WaitLayer, int.MaxValue);
//_WaitLayer.RefreshUI();
}
}
public void EndWaiting()
{
Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
(Action)delegate
{
if (_WaitLayer != null)
{
rootGrid.Children.Remove(_WaitLayer);
_WaitLayer = null;
}
});
}
}
}