PowerControl: Expose all settings and apply them in order

Since some settings impact others, the application will
apply them in a correct order with a fixed delay.

This additionally exposes all settings, just some of them
are not persisted on create, only on change.
This commit is contained in:
Kamil Trzciński 2023-01-10 11:12:31 +01:00
parent 12de2267bf
commit 2ff2864f23
11 changed files with 115 additions and 32 deletions

View file

@ -8,7 +8,10 @@ namespace PowerControl.Menu
public string? ProfileOption { get; set; }
public int ApplyDelay { get; set; }
public bool CycleOptions { get; set; } = true;
public string? PersistentKey;
public string? PersistentKey { get; set; }
public bool PersistOnCreate { get; set; } = true;
public IList<MenuItemWithOptions> Impacts { get; set; } = new List<MenuItemWithOptions>();
public Func<string?>? CurrentValue { get; set; }
public Func<string[]?>? OptionsValues { get; set; }
@ -20,6 +23,7 @@ namespace PowerControl.Menu
private System.Windows.Forms.Timer delayTimer = new System.Windows.Forms.Timer();
private ToolStripMenuItem toolStripItem = new ToolStripMenuItem();
private bool runAfterApply = false;
public MenuItemWithOptions()
{
@ -32,7 +36,7 @@ namespace PowerControl.Menu
if (delayTimer != null)
delayTimer.Stop();
FinalizeSet(delayTimer.Interval > ApplyDelay);
FinalizeSet();
};
}
@ -77,16 +81,17 @@ namespace PowerControl.Menu
ActiveOption = Options.First();
}
public void Set(String value, int overrideDelay = -1, bool silent = false)
public void Set(String value, int overrideDelay = -1, bool refresh = true)
{
if (delayTimer != null)
delayTimer.Stop();
SelectedOption = value;
runAfterApply = refresh;
if (ApplyDelay == 0 || overrideDelay == 0)
{
FinalizeSet(silent);
FinalizeSet();
return;
}
@ -94,7 +99,7 @@ namespace PowerControl.Menu
delayTimer.Enabled = true;
}
private void FinalizeSet(bool silent = false)
private void FinalizeSet()
{
var wasOption = ActiveOption;
@ -102,7 +107,7 @@ namespace PowerControl.Menu
{
ActiveOption = ApplyValue(SelectedOption);
if (AfterApply != null && !silent)
if (AfterApply != null && runAfterApply)
AfterApply();
}
else
@ -110,7 +115,7 @@ namespace PowerControl.Menu
SelectedOption = null;
if (wasOption != ActiveOption && ActiveOption != null && !silent)
if (wasOption != ActiveOption && ActiveOption != null)
ValueChanged(this, wasOption, ActiveOption);
}
@ -202,5 +207,44 @@ namespace PowerControl.Menu
return text;
}
public static IEnumerable<MenuItemWithOptions> Order(IEnumerable<MenuItemWithOptions> items)
{
HashSet<MenuItemWithOptions> processed = new HashSet<MenuItemWithOptions>();
// Try to run iteratively up to 10 times
for (int i = 0; i < 10; i++)
{
List<MenuItemWithOptions> leftItems = new List<MenuItemWithOptions>();
foreach (var item in items)
{
bool valid = item.Impacts.All((impactsItem) => processed.Contains(impactsItem));
if (valid)
{
processed.Add(item);
yield return item;
}
else
{
leftItems.Add(item);
}
}
if (leftItems.Count() == 0)
yield break;
items = leftItems;
}
CommonHelpers.Log.TraceLine("PowerControl: Failed to order items: {0}",
string.Join(", ", items.Select((item) => item.Name)));
foreach (var item in items)
{
yield return item;
}
}
}
}

View file

@ -7,6 +7,8 @@ namespace PowerControl.Options
public static Menu.MenuItemWithOptions Instance = new Menu.MenuItemWithOptions()
{
Name = "CPU",
PersistentKey = "CPUFrequency",
PersistOnCreate = false,
Options = { "Default", "Power-Save", "Balanced", "Max" },
ApplyDelay = 1000,
ActiveOption = "?",

View file

@ -7,6 +7,8 @@ namespace PowerControl.Options
public static Menu.MenuItemWithOptions Instance = new Menu.MenuItemWithOptions()
{
Name = "FAN",
PersistentKey = "FANMode",
PersistOnCreate = false,
ApplyDelay = 500,
OptionsValues = delegate ()
{

View file

@ -7,6 +7,8 @@ namespace PowerControl.Options
public static Menu.MenuItemWithOptions Instance = new Menu.MenuItemWithOptions()
{
Name = "GPU",
PersistentKey = "GPUFrequency",
PersistOnCreate = false,
Options = { "Default", "400MHz", "800MHz", "1200MHz", "1600MHz" },
ApplyDelay = 1000,
Visible = VangoghGPU.IsSupported,

View file

@ -31,14 +31,22 @@ namespace PowerControl.Options
// Since the RadeonSoftware will try to revert values
RadeonSoftware.Kill();
if (!GPUScaling.Enabled)
return "Off";
return GPUScaling.Mode.ToString();
},
Impacts =
{
Resolution.Instance,
RefreshRate.Instance,
FPSLimit.Instance
},
AfterApply = () =>
{
Resolution.Instance.Update();
RefreshRate.Instance.Update();
FPSLimit.Instance.Reset();
FPSLimit.Instance.Update();
if (!GPUScaling.Enabled)
return "Off";
return GPUScaling.Mode.ToString();
}
};
}

View file

@ -33,6 +33,7 @@ namespace PowerControl.Options
public static Menu.MenuItemWithOptions ModeInstance = new Menu.MenuItemWithOptions()
{
Name = "OSD Mode",
PersistentKey = "PerformanceOverlayMode",
ApplyDelay = 500,
OptionsValues = delegate ()
{

View file

@ -28,6 +28,10 @@ namespace PowerControl.Options
return DisplayResolutionController.GetRefreshRate().ToString();
},
Impacts =
{
FPSLimit.Instance
},
AfterApply = () =>
{
// force reset and refresh of FPS limit

View file

@ -33,12 +33,20 @@ namespace PowerControl.Options
{
var selectedResolution = new DisplayResolutionController.DisplayResolution(selected);
DisplayResolutionController.SetResolution(selectedResolution);
return DisplayResolutionController.GetResolution().ToString();
},
Impacts =
{
RefreshRate.Instance,
FPSLimit.Instance
},
AfterApply = () =>
{
// force refresh Refresh Rate
RefreshRate.Instance.Update();
// force reset and refresh of FPS limit
FPSLimit.Instance.Reset();
FPSLimit.Instance.Update();
return DisplayResolutionController.GetResolution().ToString();
}
};
}

View file

@ -7,6 +7,8 @@ namespace PowerControl.Options
public static Menu.MenuItemWithOptions Instance = new Menu.MenuItemWithOptions()
{
Name = "Controller",
PersistentKey = "SteamController",
PersistOnCreate = false,
ApplyDelay = 500,
OptionsValues = delegate ()
{

View file

@ -8,6 +8,8 @@ namespace PowerControl.Options
public static Menu.MenuItemWithOptions Instance = new Menu.MenuItemWithOptions()
{
Name = "TDP",
PersistentKey = "TDP",
PersistOnCreate = false,
Options = { "3W", "4W", "5W", "6W", "7W", "8W", "10W", "12W", "15W" },
ApplyDelay = 1000,
ResetValue = () => { return "15W"; },

View file

@ -9,6 +9,8 @@ namespace PowerControl
public class ProfilesController : IDisposable
{
public const bool AutoCreateProfiles = true;
public const int ApplyProfileDelayMs = 500;
public const int ResetProfileDelayMs = 500;
private Dictionary<int, PowerControl.Helper.ProfileSettings> watchedProcesses = new Dictionary<int, PowerControl.Helper.ProfileSettings>();
private Dictionary<MenuItemWithOptions, String>? changedSettings;
@ -154,11 +156,9 @@ namespace PowerControl
private void ProfileChanged()
{
foreach (var menuItem in MenuStack.Root.AllMenuItemOptions())
foreach (var menuItem in PersistableOptions())
{
if (menuItem.PersistentKey is null)
continue;
menuItem.ProfileOption = CurrentProfileSettings?.GetValue(menuItem.PersistentKey);
menuItem.ProfileOption = CurrentProfileSettings?.GetValue(menuItem.PersistentKey ?? "");
}
}
@ -174,11 +174,11 @@ namespace PowerControl
if (!saveAll)
return;
foreach (var menuItem in MenuStack.Root.AllMenuItemOptions())
foreach (var menuItem in PersistableOptions())
{
if (menuItem.PersistentKey is null || menuItem.ActiveOption is null)
if (menuItem.ActiveOption is null || !menuItem.PersistOnCreate)
continue;
profileSettings?.SetValue(menuItem.PersistentKey, menuItem.ActiveOption);
profileSettings?.SetValue(menuItem.PersistentKey ?? "", menuItem.ActiveOption);
}
ProfileChanged();
@ -200,20 +200,17 @@ namespace PowerControl
if (CurrentProfileSettings is null || CurrentProfileSettings?.Exists != true)
return;
int delay = CurrentProfileSettings.GetInt("ApplyDelay", -1);
int delay = CurrentProfileSettings.GetInt("ApplyDelay", ApplyProfileDelayMs);
foreach (var menuItem in MenuStack.Root.AllMenuItemOptions())
foreach (var menuItem in PersistableOptions())
{
if (menuItem.PersistentKey is null)
continue;
var persistedValue = CurrentProfileSettings.GetValue(menuItem.PersistentKey);
var persistedValue = CurrentProfileSettings.GetValue(menuItem.PersistentKey ?? "");
if (persistedValue is null)
continue;
try
{
menuItem.Set(persistedValue, delay, true);
menuItem.Set(persistedValue, delay, false);
Log.TraceLine("ProfilesController: Applied from Profile: {0}: {1} = {2}",
CurrentProfileSettings.ProfileName, menuItem.PersistentKey, persistedValue);
@ -223,7 +220,7 @@ namespace PowerControl
Log.TraceLine("ProfilesController: Exception Profile: {0}: {1} = {2} => {3}",
CurrentProfileSettings.ProfileName, menuItem.PersistentKey, persistedValue, e);
CurrentProfileSettings.DeleteKey(menuItem.PersistentKey);
CurrentProfileSettings.DeleteKey(menuItem.PersistentKey ?? "");
menuItem.ProfileOption = null;
}
}
@ -241,21 +238,32 @@ namespace PowerControl
var appliedSettings = changedSettings;
changedSettings = null;
foreach (var setting in appliedSettings)
foreach (var menuItem in PersistableOptions())
{
if (!appliedSettings.TryGetValue(menuItem, out var setting))
continue;
try
{
setting.Key.Set(setting.Value);
menuItem.Set(setting, ResetProfileDelayMs, true);
Log.TraceLine("ProfilesController: Reset: {0} = {1} => {2}",
setting.Key.PersistentKey, setting.Value);
Log.TraceLine("ProfilesController: Reset: {0} = {1}",
menuItem.PersistentKey, setting);
}
catch (Exception e)
{
Log.TraceLine("ProfilesController: Reset Exception: {0} = {1} => {2}",
setting.Key.PersistentKey, setting.Value, e);
menuItem.PersistentKey, setting, e);
}
}
}
private IEnumerable<MenuItemWithOptions> PersistableOptions()
{
return MenuItemWithOptions.
Order(MenuStack.Root.AllMenuItemOptions()).
Where((item) => item.PersistentKey is not null).
Reverse();
}
}
}