Files
BeWoPlaner/BeWo/View/Windows/AnimatedBeWoWindow.xaml.cs

289 lines
6.5 KiB
C#

using BeWo.ServiceProxy;
using BeWo.ViewModel.View;
using DevExpress.Web.Internal.XmlProcessor;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading;
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 bool closePopupStoryBoardCompleted = false;
private bool openPopupStoryBoardCompleted = false;
private bool isClosing = false;
private WaitLayer2 _WaitLayer;
private object _GroupBoxContent;
public object GroupBoxContent
{
get { return _GroupBoxContent; }
set
{
_GroupBoxContent = value;
rootGroupBox.Content = value;
}
}
public bool IsMaximizable = true;
public WindowViewModel ViewModel
{
get => DataContext as WindowViewModel;
set => DataContext = value;
}
public AnimatedBeWoWindow()
{
InitializeComponent();
CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, (s, e) => Close()));
if(ServiceFacade.IsWaiting())
StartWaiting();
if (BeWoApp.MainControl.CurrentAnimatedBeWoWindow == null)
{
BeWoApp.MainControl.CurrentAnimatedBeWoWindow = this;
}
else
{
throw new InvalidOperationException("Opening, BeWoApp.CurrentAnimatedBeWoWindow already set");
}
}
public AnimatedBeWoWindow(FrameworkElement fe) : this()
{
rootGroupBox.Content = fe;
}
public void Window_Closing(object sender, CancelEventArgs e)
{
if (!closeStoryBoardCompleted && !isClosing)
{
isClosing = true;
if (BeWoApp.MainControl.CurrentAnimatedBeWoWindow == this)
{
BeWoApp.MainControl.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;
isClosing = false;
Close();
}
private void RootGroupBox_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
var p = e.GetPosition(this);
if (p.Y == 0 || p.Y > 40)
{
return;
}
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) || e.OriginalSource is TextBlock && Mouse.DirectlyOver is Button)
{
return;
}
var pos2 = e.GetPosition((this));
if(pos2.Y == 0 || pos2.Y > 40)
{
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()
{
if (openPopupStoryBoardCompleted)
return;
Dispatcher.BeginInvoke(DispatcherPriority.Render, (Action)delegate
{
if (FindResource("PopupLoadedAnimation") is Storyboard storyboard)
{
openPopupStoryBoardCompleted = true;
StartWaitingImmediately();
storyboard.Completed += openPopupStoryBoard_Completed;
Storyboard.SetTarget(storyboard.Children[0], _WaitingLayer);
Storyboard.SetTarget(storyboard.Children[1], _WaitingLayer);
storyboard.Begin();
}
else
{
throw new InvalidOperationException("PopupLoadedAnimation not found");
}
});
}
private void openPopupStoryBoard_Completed(object sender, EventArgs e)
{
if (!openPopupStoryBoardCompleted)
return;
openPopupStoryBoardCompleted = false;
if (!ServiceFacade.IsWaiting())
EndWaiting();
}
public void StartWaitingImmediately()
{
ViewModel.IsWaiting = true;
Keyboard.Focus(_WaitingLayer);
_WaitingLayer.Focus();
}
public void EndWaiting()
{
if (closePopupStoryBoardCompleted)
return;
Dispatcher.BeginInvoke(DispatcherPriority.Render, (Action)delegate
{
if (FindResource("PopupClosingAnimation") is Storyboard storyboard)
{
closePopupStoryBoardCompleted = true;
storyboard.Completed += closePopupStoryBoard_Completed;
Storyboard.SetTarget(storyboard.Children[0], _WaitingLayer);
Storyboard.SetTarget(storyboard.Children[1], _WaitingLayer);
storyboard.Begin();
}
else
{
throw new InvalidOperationException("PopupClosingAnimation not found");
}
});
}
private void closePopupStoryBoard_Completed(object sender, EventArgs e)
{
if (!closePopupStoryBoardCompleted)
return;
closePopupStoryBoardCompleted = false;
Dispatcher.BeginInvoke(DispatcherPriority.Render, (Action)EndWaitingImmediately);
}
public void EndWaitingImmediately()
{
ViewModel.IsWaiting = false;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (!ServiceFacade.IsWaiting() && _WaitLayer != null)
EndWaiting();
}
private void CommandBinding_Close_Executed(object sender, ExecutedRoutedEventArgs e)
{
var content = rootGroupBox.Content;
if (content is BeWoView view)
{
var canSave = view.DataContext is AcceptCancelDialogViewModel viewModel
&& viewModel.SaveCommand.CanExecute(null);
if ((!canSave) || view.DoSaveCheck())
{
Close();
}
else
{
e.Handled = true;
}
}
}
private void CommandBinding_Save_Executed(object sender, ExecutedRoutedEventArgs e)
{
var content = rootGroupBox.Content;
if (content is BeWoView view)
{
view.SaveData();
}
}
}
}