Commit WIP changes.

This commit is contained in:
H. Utku Maden 2025-04-29 23:20:54 +03:00
parent 1c3c730e82
commit 2690c5bec0
3 changed files with 87 additions and 1 deletions

View File

@ -0,0 +1,86 @@
namespace Dashboard
{
public enum MeasurementUnit
{
/// <summary>
/// The default unit. A size of a single picture element.
/// </summary>
Pixel,
/// <summary>
/// 1/72th of an inch traditional in graphics design.
/// </summary>
Point,
/// <summary>
/// The universal length unit for small distances.
/// </summary>
Millimeter,
/// <summary>
/// An inverse proportional unit with respect to the container size.
/// </summary>
Star,
/// <summary>
/// A directly proportional unit with respect to the container size.
/// </summary>
Percent,
}
public record struct AdvancedMetric(MeasurementUnit Unit, float Value)
{
public AdvancedMetric Convert(MeasurementUnit target, float dpi, float rel, int stars)
{
if (Unit == target)
return this;
float pixels = Unit switch {
MeasurementUnit.Pixel => Value,
MeasurementUnit.Point => Value * (72f / dpi),
MeasurementUnit.Millimeter => Value * (28.3464566929f / dpi),
MeasurementUnit.Star => Value * rel / stars,
MeasurementUnit.Percent => Value * rel / 100,
_ => throw new Exception(),
};
float value = target switch {
MeasurementUnit.Pixel => pixels,
MeasurementUnit.Point => Value * (dpi / 72f),
// MeasurementUnit.Millimeter =>
};
return new AdvancedMetric(target, value);
}
public override string ToString()
{
return $"{Value} {Unit.ToShortString()}";
}
public static bool TryParse(ReadOnlySpan<char> str, out AdvancedMetric metric)
{
metric = default;
return false;
}
public static AdvancedMetric Parse(ReadOnlySpan<char> str) =>
TryParse(str, out AdvancedMetric metric)
? metric
: throw new Exception($"Could not parse the value '{str}'.");
}
public static class MeasurementExtensions
{
public static bool IsRelative(this MeasurementUnit unit) => unit switch {
MeasurementUnit.Star or MeasurementUnit.Percent => true,
_ => false,
};
public static bool IsAbsolute(this MeasurementUnit unit) => !IsRelative(unit);
public static string ToShortString(this MeasurementUnit unit) => unit switch {
MeasurementUnit.Pixel => "px",
MeasurementUnit.Point => "pt",
MeasurementUnit.Millimeter => "mm",
MeasurementUnit.Star => "*",
MeasurementUnit.Percent => "%",
_ => throw new Exception("Unknown unit."),
};
}
}

View File

@ -67,6 +67,7 @@ namespace Dashboard
public virtual void Leave()
{
Backend.Leave();
Leaving?.Invoke(this, EventArgs.Empty);
}
public void Run() => Run(true, CancellationToken.None);

View File

@ -4,7 +4,6 @@ using System.Text;
using Dashboard.Drawing.OpenGL;
using Dashboard.ImmediateUI;
using Dashboard.OpenTK.PAL2;
using OpenTK.Graphics;
using OpenTK.Platform;
using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics;