From 04588bc7e07e262cdeba489eaf52ddb27ac83466 Mon Sep 17 00:00:00 2001 From: Jaime Bernardo Date: Fri, 1 Apr 2022 14:50:16 +0100 Subject: [PATCH] [PTRun][Settings][Program] Fix 100% CPU load issue (#17414) * [PTRun][Program]Don't reload on settings change * [PTRun][Settings] Don't trigger saves on visual property changes * [PTRun][Settings] Fix manipulation of searched plugins * [PTRun][Settings] Don't reload settings if we wrote recently * fix PR comments nit --- .../Plugins/Microsoft.Plugin.Program/Main.cs | 7 +------ .../ViewModels/PowerLauncherViewModel.cs | 13 +++++++++++++ .../Views/PowerLauncherPage.xaml.cs | 18 +++++++++++++++++- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs index 8d09597b0e..d6d3d4ff97 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs @@ -19,7 +19,7 @@ using Stopwatch = Wox.Infrastructure.Stopwatch; namespace Microsoft.Plugin.Program { - public class Main : IPlugin, IPluginI18n, IContextMenu, ISavable, IReloadable, IDisposable + public class Main : IPlugin, IPluginI18n, IContextMenu, ISavable, IDisposable { // The order of this array is important! The Parsers will be checked in order (index 0 to index Length-1) and the first parser which is able to parse the Query will be used // NoArgumentsArgumentParser does always succeed and therefor should always be last/fallback @@ -194,11 +194,6 @@ namespace Microsoft.Plugin.Program } } - public void ReloadData() - { - IndexPrograms(); - } - public void Dispose() { Dispose(disposing: true); diff --git a/src/settings-ui/Settings.UI.Library/ViewModels/PowerLauncherViewModel.cs b/src/settings-ui/Settings.UI.Library/ViewModels/PowerLauncherViewModel.cs index c23ed10a6e..2f01dc502c 100644 --- a/src/settings-ui/Settings.UI.Library/ViewModels/PowerLauncherViewModel.cs +++ b/src/settings-ui/Settings.UI.Library/ViewModels/PowerLauncherViewModel.cs @@ -104,6 +104,15 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels private void OnPluginInfoChange(object sender, PropertyChangedEventArgs e) { + if ( + e.PropertyName == nameof(PowerLauncherPluginViewModel.ShowNotAccessibleWarning) + || e.PropertyName == nameof(PowerLauncherPluginViewModel.ShowNotAllowedKeywordWarning) + ) + { + // Don't trigger a settings update if the changed property is for visual notification. + return; + } + OnPropertyChanged(nameof(ShowAllPluginsDisabledWarning)); UpdateSettings(); } @@ -430,6 +439,10 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels { var plugins = settings.Plugins.Where(p => p.Name.StartsWith(SearchText, StringComparison.OrdinalIgnoreCase) || p.Name.IndexOf($" {SearchText}", StringComparison.OrdinalIgnoreCase) > 0); _plugins = new ObservableCollection(plugins.Select(x => new PowerLauncherPluginViewModel(x, isDark))); + foreach (var plugin in _plugins) + { + plugin.PropertyChanged += OnPluginInfoChange; + } } else { diff --git a/src/settings-ui/Settings.UI/Views/PowerLauncherPage.xaml.cs b/src/settings-ui/Settings.UI/Views/PowerLauncherPage.xaml.cs index b0f89da38c..e53c046786 100644 --- a/src/settings-ui/Settings.UI/Views/PowerLauncherPage.xaml.cs +++ b/src/settings-ui/Settings.UI/Views/PowerLauncherPage.xaml.cs @@ -19,15 +19,31 @@ namespace Microsoft.PowerToys.Settings.UI.Views private readonly ObservableCollection> searchResultPreferencesOptions; private readonly ObservableCollection> searchTypePreferencesOptions; + private int _lastIPCMessageSentTick; + + // Keep track of the last IPC Message that was sent. + private int SendDefaultIPCMessageTimed(string msg) + { + _lastIPCMessageSentTick = Environment.TickCount; + return ShellPage.SendDefaultIPCMessage(msg); + } + public PowerLauncherPage() { InitializeComponent(); var settingsUtils = new SettingsUtils(); + _lastIPCMessageSentTick = Environment.TickCount; PowerLauncherSettings settings = settingsUtils.GetSettingsOrDefault(PowerLauncherSettings.ModuleName); - ViewModel = new PowerLauncherViewModel(settings, SettingsRepository.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage, App.IsDarkTheme); + ViewModel = new PowerLauncherViewModel(settings, SettingsRepository.GetInstance(settingsUtils), SendDefaultIPCMessageTimed, App.IsDarkTheme); DataContext = ViewModel; _ = Helper.GetFileWatcher(PowerLauncherSettings.ModuleName, "settings.json", () => { + if (Environment.TickCount < _lastIPCMessageSentTick + 500) + { + // Don't try to update data from the file if we tried to write to it through IPC in the last 500 milliseconds. + return; + } + PowerLauncherSettings powerLauncherSettings = null; try {