PowerControl: Base Profiles Implementation (#38)

Small refactor of menu with options

Fix protection error on menuwithoptions

Make profiles controller non static

Dynamicall set and load options

Use IsOSDForeground when retriveing current game name

Better alt-tab functionality

Get rid off thread.sleep

Merged #38
This commit is contained in:
maniman303 2022-12-20 22:52:56 +01:00 committed by Kamil Trzciński
parent ef94d24cfc
commit 3252e799cb
7 changed files with 252 additions and 12 deletions

View file

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using CommonHelpers;
namespace PowerControl.Helper
{
public class ProfileSettings : BaseSettings
{
private static string profilesPath = Path.Combine(Directory.GetCurrentDirectory(), "Profiles");
static ProfileSettings()
{
Directory.CreateDirectory(profilesPath);
}
public ProfileSettings(string profileName) : base("Profile")
{
this.TouchSettings = true;
this.ConfigFile = Path.Combine(profilesPath, profileName + ".ini");
this.SettingChanging += delegate { };
this.SettingChanged += delegate { };
}
public T Get<T>(string key, T defaultValue)
{
return base.Get(key, defaultValue);
}
public new bool Set<T>(string key, T value)
{
return base.Set(key, value);
}
public static bool CheckIfExists(string profileName)
{
foreach (FileInfo fi in Directory.CreateDirectory(profilesPath).GetFiles())
{
if (fi.Name[^4..].Equals(".ini") && fi.Name[..^4].Equals(profileName))
{
return true;
}
}
return false;
}
}
}

View file

@ -0,0 +1,137 @@
using CommonHelpers;
using PowerControl.Helper;
using PowerControl.Menu;
namespace PowerControl.Helpers
{
public class ProfilesController
{
private const string IsTroubledKey = "IsTroubled";
private const string DefaultName = "Default";
private string CurrentGame = string.Empty;
private ProfileSettings DefaultSettings = new ProfileSettings(DefaultName);
private ProfileSettings? CurrentSettings;
private static string[] troubledGames = { "dragonageinquisition" };
private System.Windows.Forms.Timer? timer;
public ProfilesController()
{
timer = new System.Windows.Forms.Timer();
}
public void Initialize()
{
MenuStack.Root.ValueChanged += OnOptionValueChange;
timer.Interval = 1000;
timer.Tick += (_, _) =>
{
timer.Stop();
RefreshProfiles();
timer.Start();
};
timer.Start();
}
private void RefreshProfiles()
{
if (!DeviceManager.IsDeckOnlyDisplay())
{
CurrentGame = string.Empty;
return;
}
string? gameName;
RTSS.IsOSDForeground(out _, out gameName);
// If there's no foreground games keep current profile if possible
if (gameName == null && RTSS.GetCurrentApps().Contains(CurrentGame))
{
gameName = CurrentGame;
}
if (gameName == null && CurrentGame != DefaultName)
{
CurrentGame = DefaultName;
CurrentSettings = null;
ApplyProfile();
}
if (gameName != null && CurrentGame != gameName)
{
CurrentGame = gameName;
CurrentSettings = ProfileSettings.CheckIfExists(CurrentGame) ?
new ProfileSettings(CurrentGame) : null;
ApplyProfile();
}
}
private void ApplyProfile()
{
int delay = GetBoolValue(IsTroubledKey) ? 5200 : 0;
var options = MenuStack.Root.Items.Where(o => o is MenuItemWithOptions).Select(o => (MenuItemWithOptions)o).ToList();
foreach (var option in options)
{
string? key = option.PersistentKey;
if (key != null)
{
option.Set(GetValue(option), delay, true);
}
}
}
private void OnOptionValueChange(MenuItemWithOptions options, string? oldValue, string newValue)
{
string? key = options.PersistentKey;
if (key != null)
{
SetValue(key, newValue);
}
}
private void SetBoolValue(string key, bool value)
{
var settings = CurrentSettings ?? DefaultSettings;
settings.Set(key, value);
}
private bool GetBoolValue(string key)
{
var settings = CurrentSettings ?? DefaultSettings;
return settings.Get(key, false);
}
private void SetValue(string key, string value)
{
var settings = CurrentSettings ?? DefaultSettings;
settings.Set(key, value);
}
private string GetValue(MenuItemWithOptions option)
{
if (CurrentSettings == null)
{
return GetDefaultValue(option);
}
return CurrentSettings.Get(option.PersistentKey, GetDefaultValue(option));
}
private string GetDefaultValue(MenuItemWithOptions option)
{
return DefaultSettings.Get(option.PersistentKey, option.ResetValue?.Invoke() ?? string.Empty);
}
}
}