GUI桌面环境

This commit is contained in:
2026-03-01 17:03:49 +08:00
parent 545f40cf95
commit f0a9223520
162 changed files with 9170 additions and 135 deletions

View File

@@ -0,0 +1,84 @@
using CMLeonOS.Gui;
namespace CMLeonOS.UILib.Animations
{
/// <summary>
/// A window animation.
/// </summary>
internal abstract class Animation
{
/// <summary>
/// The easing type of the animation.
/// </summary>
internal EasingType EasingType { get; set; } = EasingType.Sine;
/// <summary>
/// The direction of the easing of the animation.
/// </summary>
internal EasingDirection EasingDirection { get; set; } = EasingDirection.Out;
/// <summary>
/// The duration of the animation.
/// </summary>
internal int Duration { get; set; } = 60;
/// <summary>
/// How many frames of the animation have been completed.
/// </summary>
internal int Position { get; set; } = 0;
/// <summary>
/// If the animation has finished.
/// </summary>
internal bool Finished
{
get
{
return Position >= Duration;
}
}
/// <summary>
/// The window associated with the animation.
/// </summary>
internal Window Window { get; set; }
/// <summary>
/// Advance the animation by one frame.
/// </summary>
/// <returns>Whether or not the animation is now finished.</returns>
internal abstract bool Advance();
private int? timerId { get; set; } = null;
/// <summary>
/// Start the animation.
/// </summary>
internal void Start()
{
if (timerId == null)
{
timerId = Cosmos.HAL.Global.PIT.RegisterTimer(new Cosmos.HAL.PIT.PITTimer(() =>
{
Advance();
if (Finished)
{
Stop();
}
}, (ulong)((1000d /* ms */ / 60d) * 1e+6d /* ms -> ns */ ), true));
}
}
/// <summary>
/// Stop the animation.
/// </summary>
internal void Stop()
{
if (timerId != null)
{
Cosmos.HAL.Global.PIT.UnregisterTimer((int)timerId);
timerId = null;
}
}
}
}

View File

@@ -0,0 +1,55 @@
using System;
namespace CMLeonOS.UILib.Animations
{
/// <summary>
/// Easing utilities for animations.
/// </summary>
internal static class Easing
{
/// <summary>
/// Calculate the value of an easing function.
/// </summary>
/// <param name="t">The absolute progress of the animation, from 0 to 1.</param>
/// <param name="type">The type of the easing function.</param>
/// <param name="direction">The direction of the easing function.</param>
/// <returns>The value of the easing function at the given progress.</returns>
/// <exception cref="ArgumentOutOfRangeException">An exception is thrown if the progress is out of range.</exception>
/// <exception cref="ArgumentException">An exception is thrown if the type or direction is ininvalid.</exception>
internal static double Ease(double t, EasingType type, EasingDirection direction)
{
if (t < 0 || t > 1) throw new ArgumentOutOfRangeException();
switch (type)
{
case EasingType.Linear:
return t;
case EasingType.Sine:
switch (direction)
{
case EasingDirection.In:
return 1 - Math.Cos(t * Math.PI / 2);
case EasingDirection.Out:
return Math.Sin(t * Math.PI / 2);
case EasingDirection.InOut:
return -0.5 * (Math.Cos(Math.PI * t) - 1);
default:
throw new ArgumentException("Unknown easing direction.");
}
default:
throw new ArgumentException("Unknown easing type.");
}
}
/// <summary>
/// Linearly interpolate between two values.
/// </summary>
/// <param name="x">The first value.</param>
/// <param name="y">The second value.</param>
/// <param name="z">The value of the interpolation.</param>
/// <returns>The interpolated value.</returns>
internal static double Lerp(double x, double y, double z)
{
return x * (1 - z) + y * z;
}
}
}

View File

@@ -0,0 +1,23 @@
namespace CMLeonOS.UILib.Animations
{
/// <summary>
/// Defines the direction of an easing type.
/// </summary>
internal enum EasingDirection
{
/// <summary>
/// Starts the animation slowly, and finishes at full speed.
/// </summary>
In,
/// <summary>
/// Starts the animation at full speed, and finishes slowly.
/// </summary>
Out,
/// <summary>
/// Starts the animation slowly, reaches full speed at the middle, and finishes slowly.
/// </summary>
InOut
}
}

View File

@@ -0,0 +1,18 @@
namespace CMLeonOS.UILib.Animations
{
/// <summary>
/// Describes the speed of motion over time in an animation.
/// </summary>
internal enum EasingType
{
/// <summary>
/// Linear easing.
/// </summary>
Linear,
/// <summary>
/// Sinusoidal easing.
/// </summary>
Sine
}
}

View File

@@ -0,0 +1,64 @@
using CMLeonOS.Gui;
using CMLeonOS.Gui.UILib;
using System;
using System.Drawing;
namespace CMLeonOS.UILib.Animations
{
/// <summary>
/// An animation that moves or resizes a window.
/// </summary>
internal class MovementAnimation : Animation
{
/// <summary>
/// Initialise the animation.
/// </summary>
/// <param name="window">The window associated with the animation.</param>
/// <param name="to">The goal of the animation.</param>
internal MovementAnimation(Window window)
{
Window = window;
From = new Rectangle(window.X, window.Y, window.Width, window.Height);
}
/// <summary>
/// The starting rectangle of the animation.
/// </summary>
internal Rectangle From;
/// <summary>
/// The goal rectangle of the animation.
/// </summary>
internal Rectangle To;
internal override bool Advance()
{
if (From.IsEmpty || To.IsEmpty) throw new Exception("The From or To value of this MovementAnimation is empty.");
Position++;
if (Position == Duration)
{
Window.MoveAndResize(To.X, To.Y, To.Width, To.Height);
if (Window is Control control)
{
control.Render();
}
}
else
{
double t = Easing.Ease(Position / (double)Duration, EasingType, EasingDirection);
Rectangle current = new Rectangle(
(int)Easing.Lerp(From.X, To.X, t),
(int)Easing.Lerp(From.Y, To.Y, t),
(int)Easing.Lerp(From.Width, To.Width, t),
(int)Easing.Lerp(From.Height, To.Height, t)
);
Window.MoveAndResize(current.X, current.Y, current.Width, current.Height);
if (Window is Control control)
{
control.Render();
}
}
return Finished;
}
}
}