Update PowerToys Run page on settings.json change (#10204)

This commit is contained in:
Mykhailo Pylyp 2021-03-15 14:52:03 +02:00 committed by GitHub
parent d872be4310
commit 9a061d74b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 27 deletions

View File

@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Globalization; using System.Globalization;
@ -28,8 +27,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
private GeneralSettings GeneralSettingsConfig { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; }
private readonly ISettingsUtils _settingsUtils;
private PowerLauncherSettings settings; private PowerLauncherSettings settings;
public delegate void SendCallback(PowerLauncherSettings settings); public delegate void SendCallback(PowerLauncherSettings settings);
@ -40,9 +37,14 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
private Func<string, int> SendConfigMSG { get; } private Func<string, int> SendConfigMSG { get; }
public PowerLauncherViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, int defaultKeyCode, Func<bool> isDark) public PowerLauncherViewModel(PowerLauncherSettings settings, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<bool> isDark)
{ {
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils)); if (settings == null)
{
throw new ArgumentException("settings argument can not be null");
}
this.settings = settings;
this.isDark = isDark; this.isDark = isDark;
// To obtain the general Settings configurations of PowerToys // To obtain the general Settings configurations of PowerToys
@ -55,7 +57,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
// set the callback functions value to hangle outgoing IPC message. // set the callback functions value to hangle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc; SendConfigMSG = ipcMSGCallBackFunc;
callback = (PowerLauncherSettings settings) => callback = (PowerLauncherSettings s) =>
{ {
// Propagate changes to Power Launcher through IPC // Propagate changes to Power Launcher through IPC
// Using InvariantCulture as this is an IPC message // Using InvariantCulture as this is an IPC message
@ -64,22 +66,9 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
CultureInfo.InvariantCulture, CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}", "{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
PowerLauncherSettings.ModuleName, PowerLauncherSettings.ModuleName,
JsonSerializer.Serialize(settings))); JsonSerializer.Serialize(s)));
}; };
if (_settingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
{
settings = _settingsUtils.GetSettingsOrDefault<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
}
else
{
settings = new PowerLauncherSettings();
settings.Properties.OpenPowerLauncher.Alt = true;
settings.Properties.OpenPowerLauncher.Code = defaultKeyCode;
settings.Properties.MaximumNumberOfResults = 4;
callback(settings);
}
switch (settings.Properties.Theme) switch (settings.Properties.Theme)
{ {
case Theme.Light: case Theme.Light:
@ -146,6 +135,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
GeneralSettingsConfig.Enabled.PowerLauncher = value; GeneralSettingsConfig.Enabled.PowerLauncher = value;
OnPropertyChanged(nameof(EnablePowerLauncher)); OnPropertyChanged(nameof(EnablePowerLauncher));
OnPropertyChanged(nameof(ShowAllPluginsDisabledWarning)); OnPropertyChanged(nameof(ShowAllPluginsDisabledWarning));
OnPropertyChanged(nameof(ShowPluginsLoadingMessage));
OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig); OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig);
SendConfigMSG(outgoing.ToString()); SendConfigMSG(outgoing.ToString());
} }
@ -456,7 +446,12 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
public bool ShowAllPluginsDisabledWarning public bool ShowAllPluginsDisabledWarning
{ {
get => EnablePowerLauncher && Plugins.All(x => x.Disabled); get => EnablePowerLauncher && Plugins.Any() && Plugins.All(x => x.Disabled);
}
public bool ShowPluginsLoadingMessage
{
get => EnablePowerLauncher && !Plugins.Any();
} }
} }
} }

View File

@ -66,7 +66,7 @@ namespace ViewModelTests
// Initialise View Model with test Config files // Initialise View Model with test Config files
Func<string, int> sendMockIPCConfigMSG = msg => { return 0; }; Func<string, int> sendMockIPCConfigMSG = msg => { return 0; };
PowerLauncherViewModel viewModel = new PowerLauncherViewModel(mockSettingsUtils, generalSettingsRepository, sendMockIPCConfigMSG, 32, () => true); PowerLauncherViewModel viewModel = new PowerLauncherViewModel(originalSettings, generalSettingsRepository, sendMockIPCConfigMSG, () => true);
// Verify that the old settings persisted // Verify that the old settings persisted
Assert.AreEqual(originalGeneralSettings.Enabled.PowerLauncher, viewModel.EnablePowerLauncher); Assert.AreEqual(originalGeneralSettings.Enabled.PowerLauncher, viewModel.EnablePowerLauncher);
@ -82,7 +82,6 @@ namespace ViewModelTests
// Verify that the stub file was used // Verify that the stub file was used
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>) var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
BackCompatTestProperties.VerifyModuleIOProviderWasRead(mockIOProvider, PowerLauncherSettings.ModuleName, expectedCallCount);
BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(mockGeneralIOProvider, expectedCallCount); BackCompatTestProperties.VerifyGeneralSettingsIOProviderWasRead(mockGeneralIOProvider, expectedCallCount);
} }

View File

@ -1167,4 +1167,7 @@ Win + Shift + O to toggle your video</value>
<data name="Run_Radio_Position_Primary_Monitor.Content" xml:space="preserve"> <data name="Run_Radio_Position_Primary_Monitor.Content" xml:space="preserve">
<value>Primary monitor</value> <value>Primary monitor</value>
</data> </data>
</root> <data name="Run_PluginsLoading.Text" xml:space="preserve">
<value>Plugins are loading...</value>
</data>
</root>

View File

@ -212,6 +212,11 @@
TextWrapping="Wrap" TextWrapping="Wrap"
Margin="{StaticResource SmallTopMargin}"/> Margin="{StaticResource SmallTopMargin}"/>
<TextBlock x:Uid="Run_PluginsLoading"
Visibility="{x:Bind ViewModel.ShowPluginsLoadingMessage, Mode=OneWay, Converter={StaticResource BoolToVisibilityConverter}}"
TextWrapping="Wrap"
Margin="{StaticResource SmallTopMargin}"/>
<ListView ItemsSource="{x:Bind Path=ViewModel.Plugins, Mode=OneWay}" <ListView ItemsSource="{x:Bind Path=ViewModel.Plugins, Mode=OneWay}"
IsItemClickEnabled="True" IsItemClickEnabled="True"
SelectionChanged="PluginsListView_SelectionChanged" SelectionChanged="PluginsListView_SelectionChanged"

View File

@ -4,11 +4,10 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Globalization; using System.IO;
using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Utilities; using Microsoft.PowerToys.Settings.UI.Library.Utilities;
using Microsoft.PowerToys.Settings.UI.Library.ViewModels; using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls;
namespace Microsoft.PowerToys.Settings.UI.Views namespace Microsoft.PowerToys.Settings.UI.Views
@ -24,8 +23,30 @@ namespace Microsoft.PowerToys.Settings.UI.Views
{ {
InitializeComponent(); InitializeComponent();
var settingsUtils = new SettingsUtils(); var settingsUtils = new SettingsUtils();
ViewModel = new PowerLauncherViewModel(settingsUtils, SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage, (int)Windows.System.VirtualKey.Space, App.IsDarkTheme); PowerLauncherSettings settings = settingsUtils.GetSettingsOrDefault<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
ViewModel = new PowerLauncherViewModel(settings, SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage, App.IsDarkTheme);
DataContext = ViewModel; DataContext = ViewModel;
_ = Helper.GetFileWatcher(PowerLauncherSettings.ModuleName, "settings.json", () =>
{
_ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
PowerLauncherSettings powerLauncherSettings = null;
try
{
powerLauncherSettings = settingsUtils.GetSettingsOrDefault<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
}
catch (IOException ex)
{
Logger.LogInfo(ex.Message);
}
if (powerLauncherSettings != null)
{
DataContext = ViewModel = new PowerLauncherViewModel(powerLauncherSettings, SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage, App.IsDarkTheme);
this.Bindings.Update();
}
});
});
var loader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView(); var loader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();