104 lines
3.3 KiB
C#
104 lines
3.3 KiB
C#
using System.Text.Json;
|
|
using System.IO;
|
|
|
|
namespace ShortcutsHelper.Models
|
|
{
|
|
public class AppSettings
|
|
{
|
|
public Dictionary<string, ApplicationWindowSettings> ApplicationSettings { get; set; } = new();
|
|
}
|
|
|
|
public class ApplicationWindowSettings
|
|
{
|
|
public double Left { get; set; } = 100;
|
|
public double Top { get; set; } = 100;
|
|
public double Width { get; set; } = 400;
|
|
public double Height { get; set; } = 300;
|
|
public double FavoriteColumnWidth { get; set; } = 40;
|
|
public double ShortcutColumnWidth { get; set; } = 140;
|
|
public double DescriptionColumnWidth { get; set; } = 200;
|
|
}
|
|
|
|
public static class SettingsManager
|
|
{
|
|
private static readonly string SettingsPath = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
|
"ShortcutsHelper",
|
|
"app-settings.json");
|
|
|
|
private static AppSettings? _settings;
|
|
|
|
public static AppSettings LoadSettings()
|
|
{
|
|
if (_settings != null)
|
|
return _settings;
|
|
|
|
try
|
|
{
|
|
if (File.Exists(SettingsPath))
|
|
{
|
|
var json = File.ReadAllText(SettingsPath);
|
|
_settings = JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
|
|
}
|
|
else
|
|
{
|
|
_settings = new AppSettings();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
_settings = new AppSettings();
|
|
}
|
|
|
|
return _settings;
|
|
}
|
|
|
|
public static void SaveSettings(AppSettings settings)
|
|
{
|
|
try
|
|
{
|
|
var dir = Path.GetDirectoryName(SettingsPath);
|
|
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
|
|
Directory.CreateDirectory(dir);
|
|
|
|
var json = JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true });
|
|
File.WriteAllText(SettingsPath, json);
|
|
_settings = settings;
|
|
}
|
|
catch
|
|
{
|
|
// Ignorar errores al guardar
|
|
}
|
|
}
|
|
|
|
public static ApplicationWindowSettings GetApplicationSettings(string applicationName)
|
|
{
|
|
var settings = LoadSettings();
|
|
|
|
if (!settings.ApplicationSettings.ContainsKey(applicationName))
|
|
{
|
|
// Configuración por defecto para aplicaciones nuevas
|
|
settings.ApplicationSettings[applicationName] = new ApplicationWindowSettings
|
|
{
|
|
Left = 100,
|
|
Top = 100,
|
|
Width = 400,
|
|
Height = 300,
|
|
FavoriteColumnWidth = 40,
|
|
ShortcutColumnWidth = 140,
|
|
DescriptionColumnWidth = 200
|
|
};
|
|
SaveSettings(settings);
|
|
}
|
|
|
|
return settings.ApplicationSettings[applicationName];
|
|
}
|
|
|
|
public static void SaveApplicationSettings(string applicationName, ApplicationWindowSettings appSettings)
|
|
{
|
|
var settings = LoadSettings();
|
|
settings.ApplicationSettings[applicationName] = appSettings;
|
|
SaveSettings(settings);
|
|
}
|
|
}
|
|
} |