164 lines
6.0 KiB
C#
164 lines
6.0 KiB
C#
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Interactivity;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using Avalonia.Controls.Primitives;
|
|
using Avalonia.Platform;
|
|
using ToolsForm.Views;
|
|
using ToolsForm.Views.TemporaryStorage;
|
|
|
|
namespace ToolsForm.Models
|
|
{
|
|
public enum MainType
|
|
{
|
|
主窗口,
|
|
子窗口
|
|
}
|
|
|
|
|
|
public abstract class AppWindowManager
|
|
{
|
|
public static MainWindow? MainWindow; //主窗口
|
|
public static IDEWindow? IdeWindow; //IDE 窗口
|
|
public static IDEWindow2? IdeWindow2; //IDE 2窗口
|
|
public static IDEErrorWindow? IdeErrorWindow; //IDE 2窗口
|
|
public static OverviewWindow? OverviewWindow; //总览
|
|
public static ResetSceneWindow? ResetSceneWindow; //重置
|
|
public static RecordingWindow? RecordingWindow; //录屏
|
|
public static TemporaryStorageMessage? TemporaryStorageMessage; //暂存消息框
|
|
public static TemporaryStorageWindow? TemporaryStorageWindow; //暂存
|
|
public static ReadCodeWindow? ReadCodeWindow; //暂存
|
|
public static DownCodeWindow? DownCodeWindow; //暂存
|
|
public static ExperimentWindow? ExperimentWindow; //暂存
|
|
public static UploadExperimentWindow? UploadExperimentWindow; //暂存
|
|
public static UploadExperimentMessageWindow? UploadExperimentMessageWindow; //暂存
|
|
public static AIWindow? AiWindow; //AI
|
|
|
|
/// <summary>
|
|
/// 创建窗口
|
|
/// </summary>
|
|
/// <param name="windowRef"></param>
|
|
/// <param name="createWindowFunc"></param>
|
|
/// <param name="isUpdatePos"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
public static void ToggleWindow<T>(ref T? windowRef, Func<T> createWindowFunc, bool isUpdatePos, bool isAnima = true, MainType mainType = MainType.主窗口) where T : Window
|
|
{
|
|
if (windowRef == null)
|
|
{
|
|
CloseAllWindows();
|
|
// 创建窗口
|
|
var newWindow = createWindowFunc();
|
|
var tempWindow = newWindow;
|
|
|
|
// // 在窗口关闭时,设置引用为 null
|
|
// newWindow.Closed += (s, args) =>
|
|
// {
|
|
// tempWindow = null;
|
|
// if (MainWindow.FindControl<ToggleButton>("OpenWindowToggleButton") is ToggleButton toggleButton)
|
|
// toggleButton.IsChecked = false;
|
|
// };
|
|
|
|
windowRef = newWindow;
|
|
newWindow.Show();
|
|
|
|
if (isUpdatePos)
|
|
UpdateWindowPosition(newWindow, 0, 0, mainType);
|
|
|
|
if (isAnima)
|
|
// 启动透明度动画
|
|
AnimateOpacity(newWindow);
|
|
}
|
|
else
|
|
{
|
|
windowRef.Close();
|
|
windowRef = null;
|
|
}
|
|
}
|
|
|
|
private static async void AnimateOpacity(Window window)
|
|
{
|
|
for (double opacity = 0; opacity < 1; opacity += 0.05) // 使用 < 1
|
|
{
|
|
window.Opacity = opacity;
|
|
Console.WriteLine(opacity);
|
|
await Task.Delay(1); // 每次变化后等待 1 毫秒
|
|
}
|
|
|
|
window.Opacity = 1; // 最后手动设置为 1
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新窗口位置
|
|
/// </summary>
|
|
/// <param name="window"></param>
|
|
/// <param name="X"></param>
|
|
/// <param name="Y"></param>
|
|
public static void UpdateWindowPosition(Window? window, int X, int Y, MainType mainType = MainType.主窗口)
|
|
{
|
|
if (window != null)
|
|
{
|
|
Screen? screen = null; // 初始化为 null
|
|
|
|
Console.WriteLine(mainType);
|
|
switch (mainType)
|
|
{
|
|
case MainType.主窗口:
|
|
screen = MainWindow.Screens.ScreenFromWindow(MainWindow);
|
|
var scalingFactor = screen.PixelDensity;
|
|
var mainWindowPosition = MainWindow.Position;
|
|
var mainWindowWidth = MainWindow.Bounds.Width * scalingFactor;
|
|
|
|
window.Position = new PixelPoint(
|
|
(int)(mainWindowPosition.X + (mainWindowWidth / 2 + 10) + X),
|
|
mainWindowPosition.Y + Y
|
|
);
|
|
break;
|
|
case MainType.子窗口:
|
|
screen = ExperimentWindow.Screens.ScreenFromWindow(ExperimentWindow);
|
|
var scalingFactor1 = screen.PixelDensity;
|
|
var mainWindowPosition1 = ExperimentWindow.Position;
|
|
var mainWindowWidth1 = ExperimentWindow.Bounds.Width * scalingFactor1;
|
|
|
|
window.Position = new PixelPoint(
|
|
(int)(mainWindowPosition1.X + (mainWindowWidth1 / 2 + 10) + X),
|
|
mainWindowPosition1.Y + Y
|
|
);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 关闭所有窗口
|
|
/// </summary>
|
|
/// <param name="window"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
public static void CloseWindow<T>(ref T? window) where T : Window
|
|
{
|
|
if (window != null)
|
|
{
|
|
window.Close();
|
|
window = null;
|
|
}
|
|
}
|
|
|
|
|
|
public static void CloseAllWindows()
|
|
{
|
|
CloseWindow(ref IdeWindow);
|
|
CloseWindow(ref OverviewWindow);
|
|
CloseWindow(ref ResetSceneWindow);
|
|
CloseWindow(ref IdeWindow2);
|
|
CloseWindow(ref RecordingWindow);
|
|
CloseWindow(ref TemporaryStorageWindow);
|
|
CloseWindow(ref TemporaryStorageMessage);
|
|
CloseWindow(ref ReadCodeWindow);
|
|
CloseWindow(ref DownCodeWindow);
|
|
CloseWindow(ref IdeErrorWindow);
|
|
CloseWindow(ref UploadExperimentMessageWindow);
|
|
CloseWindow(ref AiWindow);
|
|
}
|
|
}
|
|
} |