This commit is contained in:
TinyHerm committed 2026-11-24 23:40:08 +03:00
commit 1c7a8cf25f
579 files changed
+83196

No files matched your search

+489
View File
@@ -0,0 +1,489 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using VibranceHud.Support;
namespace VibranceHud.Zoom;
public sealed class FullscreenZoom : IDisposable
{
internal const string MagnifierOpen = "Close Windows Magnifier to use Zoom";
internal const string ExclusiveScreen = "Zoom needs Borderless or Windowed";
internal const string WontStart = "Windows would not start the zoom";
private const double InMs = 90.0;
private const double OutMs = 70.0;
private readonly MagRuntime _runtime;
private readonly Func<float, int, int, bool> _set;
private readonly Func<Rectangle> _bounds;
private readonly Func<bool> _magnifierRunning;
private readonly Func<bool> _exclusive;
private bool _leaseHeld;
private bool _pendingIdentity;
private float _current = 1f;
private float _from = 1f;
private float _to = 1f;
private double _elapsedMs;
private double _durationMs;
private bool _animating;
private bool _keyDown;
private int _gen;
private int _animGen;
private bool _dirty;
private bool _logNext;
private bool _disarmOwed;
private const double FirstStepMs = 8.0;
internal const float WarmLevel = 1.001f;
private readonly Action<string> _log;
private readonly Func<bool> _smoothEdges;
private static readonly TimedProbe MagnifierProbe = new TimedProbe(MagnifierIsRunning, () => Environment.TickCount64, 2000L);
public bool Engaged
{
get
{
if (!(_current > 1f) && !_animating)
{
return _pendingIdentity;
}
return true;
}
}
public bool Armed => _leaseHeld;
public string WhyRefused { get; private set; } = "";
public bool NeedsTicks
{
get
{
if (!_animating)
{
return _pendingIdentity;
}
return true;
}
}
public FullscreenZoom(Func<bool>? exclusiveFullscreen = null, Func<bool>? smoothEdges = null)
: this(MagRuntime.Shared, RealSet, () => Screen.PrimaryScreen?.Bounds ?? new Rectangle(0, 0, 1920, 1080), CachedMagnifierCheck, exclusiveFullscreen ?? ((Func<bool>)(() => false)), ZoomLog.Note, smoothEdges)
{
}
internal FullscreenZoom(MagRuntime runtime, Func<float, int, int, bool> set, Func<Rectangle> primaryBounds, Func<bool>? magnifierRunning = null, Func<bool>? exclusive = null, Action<string>? log = null, Func<bool>? smoothEdges = null)
{
_runtime = runtime;
_log = log ?? ((Action<string>)((string _) =>
{
}));
_smoothEdges = smoothEdges ?? ((Func<bool>)(() => false));
_set = set;
_bounds = primaryBounds;
_magnifierRunning = magnifierRunning ?? ((Func<bool>)(() => false));
_exclusive = exclusive ?? ((Func<bool>)(() => false));
}
public bool Arm()
{
try
{
if (_leaseHeld)
{
_disarmOwed = false;
return true;
}
bool flag = _runtime.Holders == 0;
bool flag2 = _runtime.Take();
_log(flag ? ("arm MagInitialize=" + flag2 + " smooth=" + _runtime.Smoothing) : "arm shared runtime");
if (!flag2)
{
return false;
}
_leaseHeld = true;
_disarmOwed = false;
bool flag3 = SafeBool(_magnifierRunning);
if (!flag3 && !SafeBool(_exclusive) && !_animating && !_dirty && _current <= 1f)
{
ZoomEmergency.WeZoomed = true;
_dirty = true;
_logNext = true;
ScaleForZoom();
WriteLevel(1.001f);
_logNext = true;
EndAtIdentity();
}
else if (!flag3 && !_animating && !_dirty && !_pendingIdentity && _current <= 1f)
{
Smooth(on: false);
}
return true;
}
catch
{
return false;
}
}
public void Press(float targetLevel, bool smooth)
{
try
{
WhyRefused = "";
if (SafeBool(_magnifierRunning))
{
Refuse("Close Windows Magnifier to use Zoom");
return;
}
if (SafeBool(_exclusive))
{
Refuse("Zoom needs Borderless or Windowed");
return;
}
if (!_leaseHeld)
{
bool flag = _runtime.Holders == 0;
bool flag2 = _runtime.Take();
if (flag)
{
_log("MagInitialize=" + flag2 + " smooth=" + _runtime.Smoothing);
}
if (!flag2)
{
Refuse("Windows would not start the zoom");
return;
}
_leaseHeld = true;
}
ZoomEmergency.WeZoomed = true;
_disarmOwed = false;
_keyDown = true;
_gen++;
_pendingIdentity = false;
if (!(targetLevel > 1f))
{
targetLevel = 2f;
}
_dirty = true;
ScaleForZoom();
if (!smooth)
{
_animating = false;
WriteLevel(targetLevel);
return;
}
Begin(targetLevel, 90.0);
_elapsedMs = 8.0;
float num = ZoomMath.Ease(_from, _to, 4f / 45f);
if (num <= 1f)
{
num = 1.0001f;
}
_logNext = true;
WriteLevel(num);
}
catch
{
}
}
public void Release(bool smooth)
{
try
{
_keyDown = false;
_gen++;
if (_dirty || _animating || _pendingIdentity || !(_current <= 1f))
{
if (!smooth || _current <= 1.0001f)
{
_animating = false;
EndAtIdentity();
}
else
{
Begin(1f, 70.0);
}
}
}
catch
{
}
}
public void Tick(TimeSpan elapsed)
{
try
{
if (_animating)
{
if (_to > 1f && (!_keyDown || _animGen != _gen))
{
_animating = false;
EndAtIdentity();
return;
}
_elapsedMs += elapsed.TotalMilliseconds;
float num = ((_durationMs <= 0.0) ? 1f : ((float)Math.Min(1.0, _elapsedMs / _durationMs)));
if (num >= 1f)
{
_animating = false;
if (_to <= 1f)
{
EndAtIdentity();
}
else
{
WriteLevel(_to);
}
return;
}
float num2 = ZoomMath.Ease(_from, _to, num);
if (num2 <= 1f)
{
num2 = 1.0001f;
}
if (num2 != _current)
{
WriteLevel(num2);
}
}
else if (_pendingIdentity)
{
EndAtIdentity();
}
}
catch
{
}
}
public void ResetNow()
{
try
{
_animating = false;
_keyDown = false;
_gen++;
if ((_dirty || _current > 1f || _pendingIdentity) && !EndAtIdentity() && !EndAtIdentity())
{
_disarmOwed = true;
}
else
{
GiveTheLease();
}
}
catch
{
}
}
public void Dispose()
{
ResetNow();
}
private void Begin(float to, double ms)
{
_from = _current;
_to = to;
_elapsedMs = 0.0;
_durationMs = ms;
_animGen = _gen;
_animating = true;
}
private bool WriteLevel(float level)
{
(int X, int Y) tuple = ZoomMath.CentreOffsets(_bounds(), level);
int item = tuple.X;
int item2 = tuple.Y;
bool flag = SafeSet(level, item, item2);
if (flag)
{
_current = level;
}
return flag;
}
private bool EndAtIdentity()
{
bool flag = SafeSet(1f, 0, 0);
if (flag)
{
_current = 1f;
_pendingIdentity = false;
_dirty = false;
Smooth(on: false);
if (_disarmOwed)
{
_disarmOwed = false;
GiveTheLease();
return flag;
}
}
else
{
_pendingIdentity = true;
}
return flag;
}
private void Smooth(bool on)
{
long timestamp = Stopwatch.GetTimestamp();
if (_runtime.SetSmoothing(on))
{
long num = (Stopwatch.GetTimestamp() - timestamp) * 1000 / Stopwatch.Frequency;
_log("MagSetFullscreenUseBitmapSmoothing(" + on + ")=" + _runtime.Smoothing + " ms=" + num);
}
}
private void ScaleForZoom()
{
if (SafeBool(_smoothEdges))
{
Smooth(on: true);
return;
}
long timestamp = Stopwatch.GetTimestamp();
if (_runtime.ClearSmoothing())
{
long num = (Stopwatch.GetTimestamp() - timestamp) * 1000 / Stopwatch.Frequency;
_log("MagSetFullscreenUseBitmapSmoothing(False)=" + _runtime.Smoothing + " ms=" + num);
}
}
private void GiveTheLease()
{
if (_leaseHeld)
{
if (!SafeBool(_magnifierRunning))
{
Smooth(on: false);
}
_leaseHeld = false;
_runtime.Give();
_log("disarm holders=" + _runtime.Holders);
}
}
private void Refuse(string why)
{
WhyRefused = why;
_log("refused: " + why);
}
private bool SafeSet(float z, int x, int y)
{
long timestamp = Stopwatch.GetTimestamp();
bool flag;
try
{
flag = _set(z, x, y);
}
catch
{
flag = false;
}
long value = (Stopwatch.GetTimestamp() - timestamp) * 1000 / Stopwatch.Frequency;
if (!_animating || !flag || _logNext)
{
_logNext = false;
Action<string> log = _log;
IFormatProvider invariantCulture = CultureInfo.InvariantCulture;
DefaultInterpolatedStringHandler handler = new DefaultInterpolatedStringHandler(36, 5, invariantCulture);
handler.AppendLiteral("MagSetFullscreenTransform(");
handler.AppendFormatted(z, "0.###");
handler.AppendLiteral(", ");
handler.AppendFormatted(x);
handler.AppendLiteral(", ");
handler.AppendFormatted(y);
handler.AppendLiteral(")=");
handler.AppendFormatted(flag);
handler.AppendLiteral(" ms=");
handler.AppendFormatted(value);
log(string.Create(invariantCulture, ref handler));
}
return flag;
}
private static bool SafeBool(Func<bool> f)
{
try
{
return f();
}
catch
{
return false;
}
}
private static bool RealSet(float z, int x, int y)
{
try
{
return MagRuntime.Native.MagSetFullscreenTransform(z, x, y);
}
catch
{
return false;
}
}
private static bool CachedMagnifierCheck()
{
return MagnifierProbe.Get();
}
private static bool MagnifierIsRunning()
{
try
{
Process[] processesByName = Process.GetProcessesByName("Magnify");
bool result = processesByName.Length != 0;
Process[] array = processesByName;
for (int i = 0; i < array.Length; i++)
{
array[i].Dispose();
}
return result;
}
catch
{
return false;
}
}
}
+193
View File
@@ -0,0 +1,193 @@
using System;
using System.Runtime.InteropServices;
namespace VibranceHud.Zoom;
internal sealed class MagRuntime
{
internal static class Native
{
[DllImport("Magnification.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool MagInitialize();
[DllImport("Magnification.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool MagUninitialize();
[DllImport("Magnification.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool MagSetFullscreenTransform(float magLevel, int xOffset, int yOffset);
[DllImport("Magnification.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool MagSetFullscreenUseBitmapSmoothing([MarshalAs(UnmanagedType.Bool)] bool fUseBitmapSmoothing);
}
private readonly Func<bool> _init;
private readonly Func<bool> _uninit;
private readonly Func<bool, bool>? _smooth;
private int _holders;
private bool _smoothOn;
public static MagRuntime Shared { get; } = new MagRuntime(Native.MagInitialize, Native.MagUninitialize, Native.MagSetFullscreenUseBitmapSmoothing);
public int Holders => _holders;
public string Smoothing { get; private set; } = "not asked";
public bool SmoothingOn => _smoothOn;
internal MagRuntime(Func<bool> init, Func<bool> uninit, Func<bool, bool>? smooth = null)
{
_init = init;
_uninit = uninit;
_smooth = smooth;
}
public bool Take()
{
if (_holders == 0)
{
if (!TryStart())
{
return false;
}
Smoothing = "not asked";
_smoothOn = false;
}
_holders++;
return true;
}
public bool TakeForColour()
{
if (_holders == 0)
{
if (!TryStart())
{
return false;
}
Smoothing = "not asked";
_smoothOn = false;
}
_holders++;
return true;
}
public void Give()
{
if (_holders == 0 || --_holders != 0)
{
return;
}
_smoothOn = false;
try
{
_uninit();
}
catch
{
}
}
public bool SetSmoothing(bool on)
{
if (_smooth == null || _holders == 0 || Smoothing == "missing" || on == _smoothOn)
{
return false;
}
try
{
bool flag = _smooth(on);
if (on)
{
_smoothOn = flag;
Smoothing = (flag ? "on" : "refused");
}
else if (flag)
{
_smoothOn = false;
Smoothing = "off";
}
else
{
Smoothing = "off refused";
}
}
catch (EntryPointNotFoundException)
{
_smoothOn = false;
Smoothing = "missing";
}
catch (DllNotFoundException)
{
_smoothOn = false;
Smoothing = "missing";
}
catch
{
Smoothing = "error";
if (on)
{
_smoothOn = true;
}
}
return true;
}
public bool ClearSmoothing()
{
if (_smooth == null || _holders == 0 || Smoothing == "missing")
{
return false;
}
try
{
if (_smooth(arg: false))
{
_smoothOn = false;
Smoothing = "off";
}
else
{
Smoothing = "off refused";
}
}
catch (EntryPointNotFoundException)
{
_smoothOn = false;
Smoothing = "missing";
}
catch (DllNotFoundException)
{
_smoothOn = false;
Smoothing = "missing";
}
catch
{
Smoothing = "error";
}
return true;
}
private bool TryStart()
{
try
{
return _init();
}
catch (DllNotFoundException)
{
return false;
}
catch (EntryPointNotFoundException)
{
return false;
}
}
}
+38
View File
@@ -0,0 +1,38 @@
using System;
namespace VibranceHud.Zoom;
internal sealed class TimedProbe
{
private readonly Func<bool> _probe;
private readonly Func<long> _nowMs;
private readonly long _ttlMs;
private bool _value;
private long _at;
private bool _have;
public TimedProbe(Func<bool> probe, Func<long> nowMs, long ttlMs)
{
_probe = probe;
_nowMs = nowMs;
_ttlMs = ttlMs;
}
public bool Get()
{
long num = _nowMs();
if (_have && num - _at < _ttlMs)
{
return _value;
}
_value = _probe();
_at = num;
_have = true;
return _value;
}
}
+25
View File
@@ -0,0 +1,25 @@
namespace VibranceHud.Zoom;
public static class ZoomEmergency
{
internal static volatile bool WeZoomed;
public static void Reset()
{
try
{
MagRuntime.Native.MagSetFullscreenTransform(1f, 0, 0);
}
catch
{
}
}
public static void ResetIfWeZoomed()
{
if (WeZoomed)
{
Reset();
}
}
}
+5
View File
@@ -0,0 +1,5 @@
using System;
namespace VibranceHud.Zoom;
public sealed record ZoomHooks(Func<uint, uint, bool> BindKey, Func<string> WhyRefused, Action Changed);
+65
View File
@@ -0,0 +1,65 @@
using System;
namespace VibranceHud.Zoom;
internal sealed class ZoomKeyBinder
{
private readonly Func<bool> _allowed;
private readonly Func<(uint Mask, uint Vk)> _key;
private readonly Func<uint, uint, bool> _conflict;
private readonly Func<uint, uint, bool> _bind;
private readonly Action _unbind;
private readonly Action _reset;
private bool _bound;
private uint _mask;
private uint _vk;
public bool IsBound => _bound;
public ZoomKeyBinder(Func<bool> allowed, Func<(uint Mask, uint Vk)> key, Func<uint, uint, bool> conflict, Func<uint, uint, bool> bind, Action unbind, Action reset)
{
_allowed = allowed;
_key = key;
_conflict = conflict;
_bind = bind;
_unbind = unbind;
_reset = reset;
}
public void Apply(bool force = false)
{
(uint, uint) tuple = _key();
uint item = tuple.Item1;
uint item2 = tuple.Item2;
bool flag = _allowed() && item2 != 0 && !_conflict(item, item2);
if (flag)
{
if (_bound && !force && item == _mask && item2 == _vk)
{
return;
}
if (_bind(item, item2))
{
_bound = true;
_mask = item;
_vk = item2;
return;
}
}
bool bound = _bound;
_bound = false;
if (bound | force | flag)
{
_unbind();
_reset();
}
}
}
+49
View File
@@ -0,0 +1,49 @@
using System;
using System.Drawing;
namespace VibranceHud.Zoom;
internal static class ZoomMath
{
public const float MinLevel = 1.5f;
public const float MaxLevel = 4f;
public const float Step = 0.25f;
public const float DefaultLevel = 2f;
public static float ClampSetting(float level)
{
if (float.IsNaN(level) || float.IsInfinity(level))
{
return 2f;
}
level = Math.Clamp(level, 1.5f, 4f);
return (float)(Math.Round(level / 0.25f, MidpointRounding.AwayFromZero) * 0.25);
}
public static (int X, int Y) CentreOffsets(Rectangle bounds, float level)
{
if (!(level > 1f))
{
return (X: 0, Y: 0);
}
double num = (1.0 - 1.0 / (double)level) / 2.0;
return (X: bounds.Left + (int)((double)bounds.Width * num), Y: bounds.Top + (int)((double)bounds.Height * num));
}
public static float Ease(float from, float to, float t)
{
if (!(t < 1f))
{
return to;
}
if (t <= 0f)
{
return from;
}
float num = 1f - t;
return from + (to - from) * (1f - num * num * num);
}
}