settings: get "reduced motion" setting from WinApi

This commit is contained in:
goaaats 2024-03-20 22:14:24 +01:00
parent 95defa200f
commit f9847398d2
5 changed files with 62 additions and 5 deletions

View file

@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Dalamud.Game.Text;
using Dalamud.Interface.FontIdentifier;
@ -370,7 +372,7 @@ internal sealed class DalamudConfiguration : IInternalDisposableService
/// <summary>
/// Gets or sets a value indicating whether to reduce motions (animations).
/// </summary>
public bool ReduceMotions { get; set; } = false;
public bool? ReduceMotions { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not market board data should be uploaded.
@ -486,6 +488,15 @@ internal sealed class DalamudConfiguration : IInternalDisposableService
deserialized ??= new DalamudConfiguration();
deserialized.configPath = path;
try
{
deserialized.SetDefaults();
}
catch (Exception e)
{
Log.Error(e, "Failed to set defaults for DalamudConfiguration");
}
return deserialized;
}
@ -527,6 +538,31 @@ internal sealed class DalamudConfiguration : IInternalDisposableService
}
}
private void SetDefaults()
{
// "Reduced motion"
if (!this.ReduceMotions.HasValue)
{
// https://source.chromium.org/chromium/chromium/src/+/main:ui/gfx/animation/animation_win.cc;l=29?q=ReducedMotion&ss=chromium
var winAnimEnabled = 0;
var success = NativeFunctions.SystemParametersInfo(
(uint)NativeFunctions.AccessibilityParameter.SPI_GETCLIENTAREAANIMATION,
0,
ref winAnimEnabled,
0);
if (!success)
{
Log.Warning("Failed to get Windows animation setting, assuming reduced motion is off (GetLastError: {GetLastError:X})", Marshal.GetLastPInvokeError());
this.ReduceMotions = false;
}
else
{
this.ReduceMotions = winAnimEnabled == 0;
}
}
}
private void Save()
{
ThreadSafety.AssertMainThread();

View file

@ -456,7 +456,7 @@ internal sealed partial class ActiveNotification
private void DrawExpiryPie(bool warrantsExtension, Vector2 offset, Vector2 size)
{
if (!Service<DalamudConfiguration>.Get().ReduceMotions)
if (!Service<DalamudConfiguration>.Get().ReduceMotions ?? false)
return;
// circle here; 0 means 0deg; 1 means 360deg
@ -536,7 +536,7 @@ internal sealed partial class ActiveNotification
private void DrawExpiryBar(bool warrantsExtension)
{
if (Service<DalamudConfiguration>.Get().ReduceMotions)
if (Service<DalamudConfiguration>.Get().ReduceMotions ?? false)
return;
float barL, barR;

View file

@ -188,7 +188,7 @@ internal sealed partial class ActiveNotification : IActiveNotification
set => this.newProgress = value;
}
private static bool ReducedMotions => Service<DalamudConfiguration>.Get().ReduceMotions;
private static bool ReducedMotions => Service<DalamudConfiguration>.Get().ReduceMotions ?? false;
/// <summary>Gets the eased progress.</summary>
private float ProgressEased

View file

@ -134,7 +134,7 @@ public class SettingsTabLook : SettingsTab
new SettingsEntry<bool>(
Loc.Localize("DalamudSettingReducedMotion", "Reduce motions"),
Loc.Localize("DalamudSettingReducedMotion", "This will suppress certain animations from Dalamud, such as the notification popup."),
c => c.ReduceMotions,
c => c.ReduceMotions ?? false,
(v, c) => c.ReduceMotions = v),
};

View file

@ -826,6 +826,27 @@ internal static partial class NativeFunctions
/// </summary>
public uint Timeout;
}
/// <summary>
/// Parameters for use with SystemParametersInfo.
/// </summary>
public enum AccessibilityParameter
{
#pragma warning disable SA1602
SPI_GETCLIENTAREAANIMATION = 0x1042,
#pragma warning restore SA1602
}
/// <summary>
/// Retrieves or sets the value of one of the system-wide parameters. This function can also update the user profile while setting a parameter.
/// </summary>
/// <param name="uiAction">The system-wide parameter to be retrieved or set.</param>
/// <param name="uiParam">A parameter whose usage and format depends on the system parameter being queried or set.</param>
/// <param name="pvParam">A parameter whose usage and format depends on the system parameter being queried or set. If not otherwise indicated, you must specify zero for this parameter.</param>
/// <param name="fWinIni">If a system parameter is being set, specifies whether the user profile is to be updated, and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to notify them of the change.</param>
/// <returns>If the function succeeds, the return value is a nonzero value.</returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni);
}
/// <summary>