mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
Remove instance logic for BinaryStorage and JsonStorage, part 1
1. part of #389 2. huge refactoring
This commit is contained in:
@@ -10,16 +10,16 @@ namespace Wox.Plugin.Program
|
||||
public partial class AddProgramSource
|
||||
{
|
||||
private ProgramSource _editing;
|
||||
private ProgramStorage _settings;
|
||||
private Settings _settings;
|
||||
|
||||
public AddProgramSource(ProgramStorage settings)
|
||||
public AddProgramSource(Settings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
InitializeComponent();
|
||||
Suffixes.Text = string.Join(";", settings.ProgramSuffixes);
|
||||
}
|
||||
|
||||
public AddProgramSource(ProgramSource edit, ProgramStorage settings)
|
||||
public AddProgramSource(ProgramSource edit, Settings settings)
|
||||
{
|
||||
_editing = edit;
|
||||
Directory.Text = _editing.Location;
|
||||
@@ -65,7 +65,6 @@ namespace Wox.Plugin.Program
|
||||
_editing.Suffixes = Suffixes.Text.Split(ProgramSource.SuffixSeperator);
|
||||
}
|
||||
|
||||
_settings.Save();
|
||||
DialogResult = true;
|
||||
Close();
|
||||
}
|
||||
|
||||
@@ -5,10 +5,8 @@ using Wox.Infrastructure.Storage;
|
||||
namespace Wox.Plugin.Program
|
||||
{
|
||||
[Serializable]
|
||||
public class ProgramCacheStorage : BinaryStorage<ProgramCacheStorage>
|
||||
public class ProgramIndexCache
|
||||
{
|
||||
public List<Program> Programs = new List<Program>();
|
||||
|
||||
protected override string FileName { get; } = "ProgramIndexCache";
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,9 @@ namespace Wox.Plugin.Program
|
||||
public partial class ProgramSetting : UserControl
|
||||
{
|
||||
private PluginInitContext context;
|
||||
private ProgramStorage _settings;
|
||||
private Settings _settings;
|
||||
|
||||
public ProgramSetting(PluginInitContext context, ProgramStorage settings)
|
||||
public ProgramSetting(PluginInitContext context, Settings settings)
|
||||
{
|
||||
this.context = context;
|
||||
InitializeComponent();
|
||||
@@ -58,7 +58,6 @@ namespace Wox.Plugin.Program
|
||||
if (MessageBox.Show(msg, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
_settings.ProgramSources.Remove(selectedProgramSource);
|
||||
_settings.Save();
|
||||
ReIndexing();
|
||||
}
|
||||
}
|
||||
@@ -127,7 +126,6 @@ namespace Wox.Plugin.Program
|
||||
Enabled = true
|
||||
});
|
||||
|
||||
_settings.Save();
|
||||
ReIndexing();
|
||||
}
|
||||
}
|
||||
@@ -137,14 +135,12 @@ namespace Wox.Plugin.Program
|
||||
private void StartMenuEnabled_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_settings.EnableStartMenuSource = StartMenuEnabled.IsChecked ?? false;
|
||||
_settings.Save();
|
||||
ReIndexing();
|
||||
}
|
||||
|
||||
private void RegistryEnabled_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_settings.EnableRegistrySource = RegistryEnabled.IsChecked ?? false;
|
||||
_settings.Save();
|
||||
ReIndexing();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Infrastructure.Storage;
|
||||
|
||||
namespace Wox.Plugin.Program
|
||||
{
|
||||
[Serializable]
|
||||
public class ProgramStorage : JsonStrorage<ProgramStorage>
|
||||
public class Settings
|
||||
{
|
||||
[JsonProperty]
|
||||
public List<ProgramSource> ProgramSources { get; set; }
|
||||
public List<ProgramSource> ProgramSources { get; set; } = new List<ProgramSource>();
|
||||
public string[] ProgramSuffixes { get; set; } = {"bat", "appref-ms", "exe", "lnk"};
|
||||
|
||||
public bool EnableStartMenuSource { get; set; } = true;
|
||||
|
||||
[JsonProperty]
|
||||
public string[] ProgramSuffixes { get; set; }
|
||||
|
||||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
|
||||
[DefaultValue(true)]
|
||||
public bool EnableStartMenuSource { get; set; }
|
||||
|
||||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
|
||||
[DefaultValue(true)]
|
||||
public bool EnableRegistrySource { get; set; }
|
||||
|
||||
protected override ProgramStorage LoadDefault()
|
||||
{
|
||||
ProgramSources = new List<ProgramSource>();
|
||||
EnableStartMenuSource = true;
|
||||
EnableRegistrySource = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected override void OnAfterLoad(ProgramStorage storage)
|
||||
{
|
||||
if (storage.ProgramSuffixes == null || storage.ProgramSuffixes.Length == 0)
|
||||
{
|
||||
storage.ProgramSuffixes = new[] {"bat", "appref-ms", "exe", "lnk"};
|
||||
}
|
||||
}
|
||||
|
||||
protected override string FileName { get; } = "settings_plugin_program";
|
||||
public bool EnableRegistrySource { get; set; } = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,9 +9,9 @@ namespace Wox.Plugin.Program
|
||||
public partial class ProgramSuffixes
|
||||
{
|
||||
private PluginInitContext context;
|
||||
private ProgramStorage _settings;
|
||||
private Settings _settings;
|
||||
|
||||
public ProgramSuffixes(PluginInitContext context, ProgramStorage settings)
|
||||
public ProgramSuffixes(PluginInitContext context, Settings settings)
|
||||
{
|
||||
this.context = context;
|
||||
InitializeComponent();
|
||||
|
||||
@@ -5,10 +5,10 @@ using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using Wox.Plugin.Program.ProgramSources;
|
||||
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
||||
|
||||
@@ -27,8 +27,25 @@ namespace Wox.Plugin.Program
|
||||
{"AppPathsProgramSource", typeof(AppPathsProgramSource)}
|
||||
};
|
||||
private PluginInitContext _context;
|
||||
private static ProgramCacheStorage _cache = ProgramCacheStorage.Instance;
|
||||
private static ProgramStorage _settings = ProgramStorage.Instance;
|
||||
|
||||
private static ProgramIndexCache _cache;
|
||||
private static BinaryStorage<ProgramIndexCache> _cacheStorage;
|
||||
private static Settings _settings ;
|
||||
private readonly PluginSettingsStorage<Settings> _settingsStorage;
|
||||
|
||||
public Programs()
|
||||
{
|
||||
_settingsStorage = new PluginSettingsStorage<Settings>();
|
||||
_settings = _settingsStorage.Load();
|
||||
_cacheStorage = new BinaryStorage<ProgramIndexCache>();
|
||||
_cache = _cacheStorage.Load();
|
||||
}
|
||||
|
||||
~Programs()
|
||||
{
|
||||
_settingsStorage.Save();
|
||||
_cacheStorage.Save();
|
||||
}
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
@@ -62,15 +79,13 @@ namespace Wox.Plugin.Program
|
||||
|
||||
public void Init(PluginInitContext context)
|
||||
{
|
||||
this._context = context;
|
||||
_context = context;
|
||||
Stopwatch.Debug("Preload programs", () =>
|
||||
{
|
||||
programs = _cache.Programs;
|
||||
});
|
||||
Log.Info($"Preload {programs.Count} programs from cache");
|
||||
// happlebao todo fix this
|
||||
//Stopwatch.Debug("Program Index", IndexPrograms);
|
||||
IndexPrograms();
|
||||
Stopwatch.Debug("Program Index", IndexPrograms);
|
||||
}
|
||||
|
||||
public static void IndexPrograms()
|
||||
@@ -120,7 +135,6 @@ namespace Wox.Plugin.Program
|
||||
.Select(g => g.First()).ToList();
|
||||
|
||||
_cache.Programs = programs;
|
||||
_cache.Save();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user