mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 11:46:30 +02:00
Remove instance logic for BinaryStorage and JsonStorage, part 1
1. part of #389 2. huge refactoring
This commit is contained in:
@@ -10,9 +10,9 @@ namespace Wox
|
||||
public partial class ActionKeywords : Window
|
||||
{
|
||||
private PluginPair _plugin;
|
||||
private UserSettingStorage _settings;
|
||||
private Settings _settings;
|
||||
|
||||
public ActionKeywords(string pluginId, UserSettingStorage settings)
|
||||
public ActionKeywords(string pluginId, Settings settings)
|
||||
{
|
||||
InitializeComponent();
|
||||
_plugin = PluginManager.GetPluginForId(pluginId);
|
||||
|
||||
@@ -9,9 +9,11 @@ using System.Windows;
|
||||
using Wox.CommandArgs;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Core.Resource;
|
||||
using Wox.Core.Updater;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using Wox.ViewModel;
|
||||
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
||||
|
||||
@@ -49,18 +51,14 @@ namespace Wox
|
||||
ThreadPool.QueueUserWorkItem(_ => { ImageLoader.ImageLoader.PreloadImages(); });
|
||||
|
||||
PluginManager.Initialize();
|
||||
UserSettingStorage settings = UserSettingStorage.Instance;
|
||||
|
||||
// happlebao temp fix for instance code logic
|
||||
HttpProxy.Instance.Settings = settings;
|
||||
InternationalizationManager.Instance.Settings = settings;
|
||||
ThemeManager.Instance.Settings = settings;
|
||||
|
||||
MainViewModel mainVM = new MainViewModel(settings);
|
||||
API = new PublicAPIInstance(mainVM, settings);
|
||||
|
||||
MainViewModel mainVM = new MainViewModel();
|
||||
API = new PublicAPIInstance(mainVM, mainVM._settings);
|
||||
PluginManager.InitializePlugins(API);
|
||||
|
||||
Window = new MainWindow (settings, mainVM);
|
||||
mainVM._settings.UpdatePluginSettings();
|
||||
|
||||
Window = new MainWindow (mainVM._settings, mainVM);
|
||||
NotifyIconManager notifyIconManager = new NotifyIconManager(API);
|
||||
CommandArgsFactory.Execute(e.Args.ToList());
|
||||
|
||||
|
||||
@@ -15,9 +15,9 @@ namespace Wox
|
||||
private SettingWindow _settingWidow;
|
||||
private bool update;
|
||||
private CustomPluginHotkey updateCustomHotkey;
|
||||
private UserSettingStorage _settings;
|
||||
private Settings _settings;
|
||||
|
||||
public CustomQueryHotkeySetting(SettingWindow settingWidow, UserSettingStorage settings)
|
||||
public CustomQueryHotkeySetting(SettingWindow settingWidow, Settings settings)
|
||||
{
|
||||
_settingWidow = settingWidow;
|
||||
InitializeComponent();
|
||||
@@ -78,7 +78,6 @@ namespace Wox
|
||||
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed"));
|
||||
}
|
||||
|
||||
_settings.Save();
|
||||
_settingWidow.ReloadCustomPluginHotkeyView();
|
||||
Close();
|
||||
}
|
||||
|
||||
@@ -6,14 +6,12 @@ using Wox.Infrastructure.Storage;
|
||||
namespace Wox.ImageLoader
|
||||
{
|
||||
[Serializable]
|
||||
public class ImageCacheStroage : BinaryStorage<ImageCacheStroage>
|
||||
public class ImageCache
|
||||
{
|
||||
private int counter;
|
||||
private const int maxCached = 200;
|
||||
public Dictionary<string, int> TopUsedImages = new Dictionary<string, int>();
|
||||
|
||||
protected override string FileName { get; } = "ImageCache";
|
||||
|
||||
public void Add(string path)
|
||||
{
|
||||
if (TopUsedImages.ContainsKey(path))
|
||||
@@ -35,7 +33,6 @@ namespace Wox.ImageLoader
|
||||
if (++counter == 30)
|
||||
{
|
||||
counter = 0;
|
||||
Save();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,13 @@ using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Storage;
|
||||
|
||||
namespace Wox.ImageLoader
|
||||
{
|
||||
public class ImageLoader
|
||||
{
|
||||
private static readonly Dictionary<string, ImageSource> imageCache = new Dictionary<string, ImageSource>();
|
||||
private static readonly Dictionary<string, ImageSource> ImageSources = new Dictionary<string, ImageSource>();
|
||||
|
||||
private static readonly List<string> imageExts = new List<string>
|
||||
{
|
||||
@@ -36,11 +37,18 @@ namespace Wox.ImageLoader
|
||||
".appref-ms"
|
||||
};
|
||||
|
||||
private static ImageCacheStroage _imageCache;
|
||||
private static readonly ImageCache _cache;
|
||||
private static readonly BinaryStorage<ImageCache> _storage;
|
||||
|
||||
static ImageLoader()
|
||||
{
|
||||
_imageCache = ImageCacheStroage.Instance;
|
||||
_storage = new BinaryStorage<ImageCache>();
|
||||
_cache = _storage.Load();
|
||||
}
|
||||
|
||||
~ImageLoader()
|
||||
{
|
||||
_storage.Save();
|
||||
}
|
||||
|
||||
private static ImageSource GetIcon(string fileName)
|
||||
@@ -63,21 +71,21 @@ namespace Wox.ImageLoader
|
||||
public static void PreloadImages()
|
||||
{
|
||||
//ImageCacheStroage.Instance.TopUsedImages can be changed during foreach, so we need to make a copy
|
||||
var imageList = new Dictionary<string, int>(_imageCache.TopUsedImages);
|
||||
var imageList = new Dictionary<string, int>(_cache.TopUsedImages);
|
||||
Stopwatch.Debug($"Preload {imageList.Count} images", () =>
|
||||
{
|
||||
foreach (var image in imageList)
|
||||
{
|
||||
if (!imageCache.ContainsKey(image.Key))
|
||||
if (!ImageSources.ContainsKey(image.Key))
|
||||
{
|
||||
ImageSource img = Load(image.Key, false);
|
||||
if (img != null)
|
||||
{
|
||||
img.Freeze(); //to make it copy to UI thread
|
||||
if (!imageCache.ContainsKey(image.Key))
|
||||
if (!ImageSources.ContainsKey(image.Key))
|
||||
{
|
||||
KeyValuePair<string, int> copyedImg = image;
|
||||
imageCache.Add(copyedImg.Key, img);
|
||||
ImageSources.Add(copyedImg.Key, img);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,13 +102,13 @@ namespace Wox.ImageLoader
|
||||
|
||||
if (addToCache)
|
||||
{
|
||||
_imageCache.Add(path);
|
||||
_cache.Add(path);
|
||||
}
|
||||
|
||||
|
||||
if (imageCache.ContainsKey(path))
|
||||
if (ImageSources.ContainsKey(path))
|
||||
{
|
||||
img = imageCache[path];
|
||||
img = ImageSources[path];
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -122,9 +130,9 @@ namespace Wox.ImageLoader
|
||||
|
||||
if (img != null && addToCache)
|
||||
{
|
||||
if (!imageCache.ContainsKey(path))
|
||||
if (!ImageSources.ContainsKey(path))
|
||||
{
|
||||
imageCache.Add(path, img);
|
||||
ImageSources.Add(path, img);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<system:String x:Key="iconTrayExit">Exit</system:String>
|
||||
|
||||
<!--Setting General-->
|
||||
<system:String x:Key="woxsettings">Wox Settings</system:String>
|
||||
<system:String x:Key="wox_settings">Wox Settings</system:String>
|
||||
<system:String x:Key="general">General</system:String>
|
||||
<system:String x:Key="startWoxOnSystemStartup">Start Wox on system startup</system:String>
|
||||
<system:String x:Key="hideWoxWhenLoseFocus">Hide Wox when loses focus</system:String>
|
||||
|
||||
@@ -25,11 +25,11 @@ namespace Wox
|
||||
#region Private Fields
|
||||
|
||||
private readonly Storyboard _progressBarStoryboard = new Storyboard();
|
||||
private UserSettingStorage _settings;
|
||||
private Settings _settings;
|
||||
|
||||
#endregion
|
||||
|
||||
public MainWindow(UserSettingStorage settings, MainViewModel mainVM)
|
||||
public MainWindow(Settings settings, MainViewModel mainVM)
|
||||
{
|
||||
DataContext = mainVM;
|
||||
InitializeComponent();
|
||||
@@ -43,7 +43,6 @@ namespace Wox
|
||||
{
|
||||
_settings.WindowLeft = Left;
|
||||
_settings.WindowTop = Top;
|
||||
_settings.Save();
|
||||
}
|
||||
|
||||
private void OnLoaded(object sender, RoutedEventArgs _)
|
||||
@@ -71,13 +70,12 @@ namespace Wox
|
||||
QueryTextBox.Focus();
|
||||
Left = GetWindowsLeft();
|
||||
Top = GetWindowsTop();
|
||||
_settings.IncreaseActivateTimes();
|
||||
_settings.ActivateTimes++;
|
||||
}
|
||||
else
|
||||
{
|
||||
_settings.WindowLeft = Left;
|
||||
_settings.WindowTop = Top;
|
||||
_settings.Save();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<SettingsFile xmlns="uri:settings" CurrentProfile="(Default)">
|
||||
<SettingsFile xmlns="uri:_settings" CurrentProfile="(Default)">
|
||||
<Profiles>
|
||||
<Profile Name="(Default)" />
|
||||
</Profiles>
|
||||
|
||||
@@ -19,10 +19,10 @@ namespace Wox
|
||||
{
|
||||
public class PublicAPIInstance : IPublicAPI
|
||||
{
|
||||
private UserSettingStorage _settings;
|
||||
private Settings _settings;
|
||||
#region Constructor
|
||||
|
||||
public PublicAPIInstance(MainViewModel mainVM, UserSettingStorage settings)
|
||||
public PublicAPIInstance(MainViewModel mainVM, Settings settings)
|
||||
{
|
||||
MainVM = mainVM;
|
||||
_settings = settings;
|
||||
|
||||
@@ -32,9 +32,9 @@ namespace Wox
|
||||
bool settingsLoaded;
|
||||
private Dictionary<ISettingProvider, Control> featureControls = new Dictionary<ISettingProvider, Control>();
|
||||
private bool themeTabLoaded;
|
||||
private UserSettingStorage _settings;
|
||||
private Settings _settings;
|
||||
|
||||
public SettingWindow(IPublicAPI api, UserSettingStorage settings)
|
||||
public SettingWindow(IPublicAPI api, Settings settings)
|
||||
{
|
||||
InitializeComponent();
|
||||
_settings = settings;
|
||||
@@ -49,50 +49,42 @@ namespace Wox
|
||||
cbHideWhenDeactive.Checked += (o, e) =>
|
||||
{
|
||||
_settings.HideWhenDeactive = true;
|
||||
_settings.Save();
|
||||
};
|
||||
|
||||
cbHideWhenDeactive.Unchecked += (o, e) =>
|
||||
{
|
||||
_settings.HideWhenDeactive = false;
|
||||
_settings.Save();
|
||||
};
|
||||
|
||||
cbRememberLastLocation.Checked += (o, e) =>
|
||||
{
|
||||
_settings.RememberLastLaunchLocation = true;
|
||||
_settings.Save();
|
||||
};
|
||||
|
||||
cbRememberLastLocation.Unchecked += (o, e) =>
|
||||
{
|
||||
_settings.RememberLastLaunchLocation = false;
|
||||
_settings.Save();
|
||||
};
|
||||
|
||||
cbDontPromptUpdateMsg.Checked += (o, e) =>
|
||||
{
|
||||
_settings.DontPromptUpdateMsg = true;
|
||||
_settings.Save();
|
||||
};
|
||||
|
||||
cbDontPromptUpdateMsg.Unchecked += (o, e) =>
|
||||
{
|
||||
_settings.DontPromptUpdateMsg = false;
|
||||
_settings.Save();
|
||||
};
|
||||
|
||||
cbIgnoreHotkeysOnFullscreen.Checked += (o, e) =>
|
||||
{
|
||||
_settings.IgnoreHotkeysOnFullscreen = true;
|
||||
_settings.Save();
|
||||
};
|
||||
|
||||
|
||||
cbIgnoreHotkeysOnFullscreen.Unchecked += (o, e) =>
|
||||
{
|
||||
_settings.IgnoreHotkeysOnFullscreen = false;
|
||||
_settings.Save();
|
||||
};
|
||||
|
||||
|
||||
@@ -100,7 +92,6 @@ namespace Wox
|
||||
comboMaxResultsToShow.SelectionChanged += (o, e) =>
|
||||
{
|
||||
_settings.MaxResultsToShow = (int)comboMaxResultsToShow.SelectedItem;
|
||||
_settings.Save();
|
||||
//MainWindow.pnlResult.lbResults.GetBindingExpression(MaxHeightProperty).UpdateTarget();
|
||||
};
|
||||
|
||||
@@ -215,14 +206,12 @@ namespace Wox
|
||||
{
|
||||
AddApplicationToStartup();
|
||||
_settings.StartWoxOnSystemStartup = true;
|
||||
_settings.Save();
|
||||
}
|
||||
|
||||
private void CbStartWithWindows_OnUnchecked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
RemoveApplicationFromStartup();
|
||||
_settings.StartWoxOnSystemStartup = false;
|
||||
_settings.Save();
|
||||
}
|
||||
|
||||
private void AddApplicationToStartup()
|
||||
@@ -270,7 +259,6 @@ namespace Wox
|
||||
});
|
||||
RemoveHotkey(_settings.Hotkey);
|
||||
_settings.Hotkey = ctlHotkey.CurrentHotkey.ToString();
|
||||
_settings.Save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,7 +305,6 @@ namespace Wox
|
||||
{
|
||||
_settings.CustomPluginHotkeys.Remove(item);
|
||||
lvCustomHotkey.Items.Refresh();
|
||||
_settings.Save();
|
||||
RemoveHotkey(item.Hotkey);
|
||||
}
|
||||
}
|
||||
@@ -481,7 +468,6 @@ namespace Wox
|
||||
string queryBoxFontName = cbQueryBoxFont.SelectedItem.ToString();
|
||||
_settings.QueryBoxFont = queryBoxFontName;
|
||||
cbQueryBoxFontFaces.SelectedItem = ((FontFamily)cbQueryBoxFont.SelectedItem).ChooseRegularFamilyTypeface();
|
||||
_settings.Save();
|
||||
ThemeManager.Instance.ChangeTheme(_settings.Theme);
|
||||
}
|
||||
|
||||
@@ -499,7 +485,6 @@ namespace Wox
|
||||
_settings.QueryBoxFontStretch = typeface.Stretch.ToString();
|
||||
_settings.QueryBoxFontWeight = typeface.Weight.ToString();
|
||||
_settings.QueryBoxFontStyle = typeface.Style.ToString();
|
||||
_settings.Save();
|
||||
ThemeManager.Instance.ChangeTheme(_settings.Theme);
|
||||
}
|
||||
}
|
||||
@@ -510,7 +495,6 @@ namespace Wox
|
||||
string resultItemFont = ResultFontComboBox.SelectedItem.ToString();
|
||||
_settings.ResultFont = resultItemFont;
|
||||
ResultFontFacesComboBox.SelectedItem = ((FontFamily)ResultFontComboBox.SelectedItem).ChooseRegularFamilyTypeface();
|
||||
_settings.Save();
|
||||
ThemeManager.Instance.ChangeTheme(_settings.Theme);
|
||||
}
|
||||
|
||||
@@ -528,7 +512,6 @@ namespace Wox
|
||||
_settings.ResultFontStretch = typeface.Stretch.ToString();
|
||||
_settings.ResultFontWeight = typeface.Weight.ToString();
|
||||
_settings.ResultFontStyle = typeface.Style.ToString();
|
||||
_settings.Save();
|
||||
ThemeManager.Instance.ChangeTheme(_settings.Theme);
|
||||
}
|
||||
}
|
||||
@@ -569,7 +552,7 @@ namespace Wox
|
||||
pluginId = pair.Metadata.ID;
|
||||
pluginIcon.Source = ImageLoader.ImageLoader.Load(pair.Metadata.FullIcoPath);
|
||||
|
||||
var customizedPluginConfig = _settings.CustomizedPluginConfigs[pluginId];
|
||||
var customizedPluginConfig = _settings.PluginSettings[pluginId];
|
||||
cbDisablePlugin.IsChecked = customizedPluginConfig != null && customizedPluginConfig.Disabled;
|
||||
|
||||
PluginContentPanel.Content = null;
|
||||
@@ -609,31 +592,19 @@ namespace Wox
|
||||
if (cbDisabled == null) return;
|
||||
|
||||
var pair = lbPlugins.SelectedItem as PluginPair;
|
||||
var id = string.Empty;
|
||||
var name = string.Empty;
|
||||
if (pair != null)
|
||||
{
|
||||
//third-party plugin
|
||||
id = pair.Metadata.ID;
|
||||
name = pair.Metadata.Name;
|
||||
}
|
||||
var customizedPluginConfig = _settings.CustomizedPluginConfigs[id];
|
||||
if (customizedPluginConfig == null)
|
||||
{
|
||||
// todo when this part will be invoked
|
||||
_settings.CustomizedPluginConfigs[id] = new CustomizedPluginConfig
|
||||
var id = pair.Metadata.ID;
|
||||
var customizedPluginConfig = _settings.PluginSettings[id];
|
||||
if (customizedPluginConfig.Disabled)
|
||||
{
|
||||
Disabled = cbDisabled.IsChecked ?? true,
|
||||
ID = id,
|
||||
Name = name,
|
||||
ActionKeywords = null
|
||||
};
|
||||
PluginManager.DisablePlugin(pair);
|
||||
}
|
||||
else
|
||||
{
|
||||
PluginManager.EnablePlugin(pair);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
customizedPluginConfig.Disabled = cbDisabled.IsChecked ?? true;
|
||||
}
|
||||
_settings.Save();
|
||||
}
|
||||
|
||||
private void PluginActionKeywords_OnMouseUp(object sender, MouseButtonEventArgs e)
|
||||
@@ -744,7 +715,6 @@ namespace Wox
|
||||
_settings.ProxyPort = port;
|
||||
_settings.ProxyUserName = tbProxyUserName.Text;
|
||||
_settings.ProxyPassword = tbProxyPassword.Password;
|
||||
_settings.Save();
|
||||
|
||||
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("saveProxySuccessfully"));
|
||||
}
|
||||
|
||||
@@ -7,10 +7,9 @@ using Wox.Plugin;
|
||||
|
||||
namespace Wox.Storage
|
||||
{
|
||||
public class QueryHistoryStorage : JsonStrorage<QueryHistoryStorage>
|
||||
public class QueryHistory
|
||||
{
|
||||
[JsonProperty]
|
||||
private List<HistoryItem> History = new List<HistoryItem>();
|
||||
public List<HistoryItem> History = new List<HistoryItem>();
|
||||
|
||||
private int MaxHistory = 300;
|
||||
private int cursor;
|
||||
@@ -18,8 +17,6 @@ namespace Wox.Storage
|
||||
public static PluginMetadata MetaData { get; } = new PluginMetadata
|
||||
{ ID = "Query history", Name = "Query history" };
|
||||
|
||||
protected override string FileName { get; } = "QueryHistory";
|
||||
|
||||
public HistoryItem Previous()
|
||||
{
|
||||
if (History.Count == 0 || cursor == 0) return null;
|
||||
@@ -58,11 +55,6 @@ namespace Wox.Storage
|
||||
});
|
||||
}
|
||||
|
||||
if (History.Count % 5 == 0)
|
||||
{
|
||||
Save();
|
||||
}
|
||||
|
||||
Reset();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,11 +5,9 @@ using Wox.Plugin;
|
||||
|
||||
namespace Wox.Storage
|
||||
{
|
||||
public class TopMostRecordStorage : JsonStrorage<TopMostRecordStorage>
|
||||
public class TopMostRecord
|
||||
{
|
||||
public Dictionary<string, TopMostRecord> records = new Dictionary<string, TopMostRecord>();
|
||||
|
||||
protected override string FileName { get; } = "TopMostRecords";
|
||||
public Dictionary<string, Record> records = new Dictionary<string, Record>();
|
||||
|
||||
internal bool IsTopMost(Result result)
|
||||
{
|
||||
@@ -24,7 +22,6 @@ namespace Wox.Storage
|
||||
if (records.ContainsKey(result.OriginQuery.RawQuery))
|
||||
{
|
||||
records.Remove(result.OriginQuery.RawQuery);
|
||||
Save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,20 +35,18 @@ namespace Wox.Storage
|
||||
}
|
||||
else
|
||||
{
|
||||
records.Add(result.OriginQuery.RawQuery, new TopMostRecord
|
||||
records.Add(result.OriginQuery.RawQuery, new Record
|
||||
{
|
||||
PluginID = result.PluginID,
|
||||
Title = result.Title,
|
||||
SubTitle = result.SubTitle
|
||||
});
|
||||
}
|
||||
|
||||
Save();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class TopMostRecord
|
||||
public class Record
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public string SubTitle { get; set; }
|
||||
|
||||
@@ -5,13 +5,11 @@ using Wox.Plugin;
|
||||
|
||||
namespace Wox.Storage
|
||||
{
|
||||
public class UserSelectedRecordStorage : JsonStrorage<UserSelectedRecordStorage>
|
||||
public class UserSelectedRecord
|
||||
{
|
||||
[JsonProperty]
|
||||
private Dictionary<string, int> records = new Dictionary<string, int>();
|
||||
|
||||
protected override string FileName { get; } = "UserSelectedRecords";
|
||||
|
||||
public void Add(Result result)
|
||||
{
|
||||
if (records.ContainsKey(result.ToString()))
|
||||
@@ -22,7 +20,6 @@ namespace Wox.Storage
|
||||
{
|
||||
records.Add(result.ToString(), 1);
|
||||
}
|
||||
Save();
|
||||
}
|
||||
|
||||
public int GetSelectedCount(Result result)
|
||||
|
||||
@@ -7,10 +7,12 @@ using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using Wox.Core.Plugin;
|
||||
using Wox.Core.Resource;
|
||||
using Wox.Core.Updater;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Helper;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using Wox.Plugin;
|
||||
using Wox.Storage;
|
||||
using Stopwatch = Wox.Infrastructure.Stopwatch;
|
||||
@@ -36,24 +38,41 @@ namespace Wox.ViewModel
|
||||
private string _queryTextBeforeLoadContextMenu;
|
||||
private string _queryText;
|
||||
|
||||
private UserSettingStorage _settings;
|
||||
private QueryHistoryStorage _queryHistory;
|
||||
private UserSelectedRecordStorage _userSelectedRecord;
|
||||
private TopMostRecordStorage _topMostRecord;
|
||||
private readonly JsonStrorage<Settings> _settingsStorage;
|
||||
private readonly JsonStrorage<QueryHistory> _queryHistoryStorage;
|
||||
private readonly JsonStrorage<UserSelectedRecord> _userSelectedRecordStorage;
|
||||
private readonly JsonStrorage<TopMostRecord> _topMostRecordStorage;
|
||||
// todo happlebao this field should be private in the future
|
||||
public readonly Settings _settings;
|
||||
private readonly QueryHistory _queryHistory;
|
||||
private readonly UserSelectedRecord _userSelectedRecord;
|
||||
private readonly TopMostRecord _topMostRecord;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
|
||||
public MainViewModel(UserSettingStorage settings)
|
||||
public MainViewModel()
|
||||
{
|
||||
_queryTextBeforeLoadContextMenu = "";
|
||||
_queryText = "";
|
||||
_lastQuery = new Query();
|
||||
_settings = settings;
|
||||
_queryHistory = QueryHistoryStorage.Instance;
|
||||
_userSelectedRecord = UserSelectedRecordStorage.Instance;
|
||||
_topMostRecord = TopMostRecordStorage.Instance;
|
||||
|
||||
_settingsStorage = new JsonStrorage<Settings>();
|
||||
_settings = _settingsStorage.Load();
|
||||
|
||||
// happlebao todo temp fix for instance code logic
|
||||
HttpProxy.Instance.Settings = _settings;
|
||||
UpdaterManager.Instance.Settings = _settings;
|
||||
InternationalizationManager.Instance.Settings = _settings;
|
||||
ThemeManager.Instance.Settings = _settings;
|
||||
|
||||
_queryHistoryStorage = new JsonStrorage<QueryHistory>();
|
||||
_userSelectedRecordStorage = new JsonStrorage<UserSelectedRecord>();
|
||||
_topMostRecordStorage = new JsonStrorage<TopMostRecord>();
|
||||
_queryHistory = _queryHistoryStorage.Load();
|
||||
_userSelectedRecord = _userSelectedRecordStorage.Load();
|
||||
_topMostRecord = _topMostRecordStorage.Load();
|
||||
|
||||
InitializeResultListBox();
|
||||
InitializeContextMenu();
|
||||
@@ -61,9 +80,12 @@ namespace Wox.ViewModel
|
||||
|
||||
}
|
||||
|
||||
public MainViewModel()
|
||||
~MainViewModel()
|
||||
{
|
||||
|
||||
_settingsStorage.Save();
|
||||
_queryHistoryStorage.Save();
|
||||
_userSelectedRecordStorage.Save();
|
||||
_topMostRecordStorage.Save();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -433,7 +455,7 @@ namespace Wox.ViewModel
|
||||
var plugins = PluginManager.ValidPluginsForQuery(query);
|
||||
foreach (var plugin in plugins)
|
||||
{
|
||||
var config = _settings.CustomizedPluginConfigs[plugin.Metadata.ID];
|
||||
var config = _settings.PluginSettings[plugin.Metadata.ID];
|
||||
if (!config.Disabled)
|
||||
{
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
@@ -450,7 +472,7 @@ namespace Wox.ViewModel
|
||||
|
||||
private void ResetQueryHistoryIndex()
|
||||
{
|
||||
Results.RemoveResultsFor(QueryHistoryStorage.MetaData);
|
||||
Results.RemoveResultsFor(QueryHistory.MetaData);
|
||||
_queryHistory.Reset();
|
||||
}
|
||||
|
||||
@@ -464,7 +486,7 @@ namespace Wox.ViewModel
|
||||
{
|
||||
if (history != null)
|
||||
{
|
||||
var historyMetadata = QueryHistoryStorage.MetaData;
|
||||
var historyMetadata = QueryHistory.MetaData;
|
||||
|
||||
QueryText = history.Query;
|
||||
OnTextBoxSelected();
|
||||
|
||||
@@ -37,6 +37,25 @@ namespace Wox.ViewModel
|
||||
|
||||
public string FullIcoPath => RawResult.FullIcoPath;
|
||||
|
||||
public string PluginID => RawResult.PluginID;
|
||||
public int Score
|
||||
{
|
||||
get { return RawResult.Score; }
|
||||
set { RawResult.Score = value; }
|
||||
}
|
||||
|
||||
public Query OriginQuery
|
||||
{
|
||||
get { return RawResult.OriginQuery; }
|
||||
set { RawResult.OriginQuery = value; }
|
||||
}
|
||||
|
||||
public Func<ActionContext, bool> Action
|
||||
{
|
||||
get { return RawResult.Action; }
|
||||
set { RawResult.Action = value; }
|
||||
}
|
||||
|
||||
public bool IsSelected
|
||||
{
|
||||
get { return _isSelected; }
|
||||
@@ -51,10 +70,16 @@ namespace Wox.ViewModel
|
||||
|
||||
#region Properties
|
||||
|
||||
public Result RawResult { get; }
|
||||
internal Result RawResult { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
public void Update(ResultViewModel newResult)
|
||||
{
|
||||
RawResult.Score = newResult.RawResult.Score;
|
||||
RawResult.OriginQuery = newResult.RawResult.OriginQuery;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
ResultViewModel r = obj as ResultViewModel;
|
||||
|
||||
@@ -20,10 +20,10 @@ namespace Wox.ViewModel
|
||||
private Thickness _margin;
|
||||
|
||||
private readonly object _resultsUpdateLock = new object();
|
||||
private UserSettingStorage _settings;
|
||||
private TopMostRecordStorage _topMostRecord;
|
||||
private Settings _settings;
|
||||
private TopMostRecord _topMostRecord;
|
||||
|
||||
public ResultsViewModel(UserSettingStorage settings, TopMostRecordStorage topMostRecord)
|
||||
public ResultsViewModel(Settings settings, TopMostRecord topMostRecord)
|
||||
{
|
||||
_settings = settings;
|
||||
_topMostRecord = topMostRecord;
|
||||
@@ -191,7 +191,7 @@ namespace Wox.ViewModel
|
||||
{
|
||||
lock (_resultsUpdateLock)
|
||||
{
|
||||
Results.RemoveAll(r => r.RawResult.PluginID == metadata.ID);
|
||||
Results.RemoveAll(r => r.PluginID == metadata.ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ namespace Wox.ViewModel
|
||||
var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList();
|
||||
// todo use async to do new result calculation
|
||||
var resultsCopy = Results.ToList();
|
||||
var oldResults = resultsCopy.Where(r => r.RawResult.PluginID == resultId).ToList();
|
||||
var oldResults = resultsCopy.Where(r => r.PluginID == resultId).ToList();
|
||||
// intersection of A (old results) and B (new newResults)
|
||||
var intersection = oldResults.Intersect(newResults).ToList();
|
||||
// remove result of relative complement of B in A
|
||||
@@ -216,7 +216,7 @@ namespace Wox.ViewModel
|
||||
{
|
||||
if (IsTopMostResult(result.RawResult))
|
||||
{
|
||||
result.RawResult.Score = int.MaxValue;
|
||||
result.Score = int.MaxValue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -224,23 +224,26 @@ namespace Wox.ViewModel
|
||||
foreach (var commonResult in intersection)
|
||||
{
|
||||
int oldIndex = resultsCopy.IndexOf(commonResult);
|
||||
int oldScore = resultsCopy[oldIndex].RawResult.Score;
|
||||
int newScore = newResults[newResults.IndexOf(commonResult)].RawResult.Score;
|
||||
int oldScore = resultsCopy[oldIndex].Score;
|
||||
var newResult = newResults[newResults.IndexOf(commonResult)];
|
||||
int newScore = newResult.Score;
|
||||
if (newScore != oldScore)
|
||||
{
|
||||
var oldResult = resultsCopy[oldIndex];
|
||||
oldResult.RawResult.Score = newScore;
|
||||
|
||||
oldResult.Score = newScore;
|
||||
oldResult.OriginQuery = newResult.OriginQuery;
|
||||
|
||||
resultsCopy.RemoveAt(oldIndex);
|
||||
int newIndex = InsertIndexOf(newScore, resultsCopy);
|
||||
resultsCopy.Insert(newIndex, oldResult);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// insert result in relative complement of A in B
|
||||
foreach (var result in newResults.Except(intersection))
|
||||
{
|
||||
int newIndex = InsertIndexOf(result.RawResult.Score, resultsCopy);
|
||||
int newIndex = InsertIndexOf(result.Score, resultsCopy);
|
||||
resultsCopy.Insert(newIndex, result);
|
||||
}
|
||||
|
||||
@@ -299,9 +302,9 @@ namespace Wox.ViewModel
|
||||
{
|
||||
this[i] = newResult;
|
||||
}
|
||||
else if (oldResult.RawResult.Score != newResult.RawResult.Score)
|
||||
else if (oldResult.Score != newResult.Score)
|
||||
{
|
||||
this[i].RawResult.Score = newResult.RawResult.Score;
|
||||
this[i].Score = newResult.Score;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user