Skip to content

Commit

Permalink
[OOBE]Fix not shortcut not updating (#21175)
Browse files Browse the repository at this point in the history
* [OOBE]Fix not shortcut not updating

Fix for issue #20953.
Activation key update in the OOBE window
when the user changes it in the settings window.
Add settings repository reference to the ViewModel
constructor to use the repository settings object
(and not create a second instance of it).

* Fix for issue #20953.

Unit test fixed.
  • Loading branch information
donlaci committed Oct 13, 2022
1 parent ee904ae commit ab41b61
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 39 deletions.
Expand Up @@ -34,7 +34,11 @@ public class ColorPickerViewModel : Observable, IDisposable

private Func<string, int> SendConfigMSG { get; }

public ColorPickerViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc)
public ColorPickerViewModel(
ISettingsUtils settingsUtils,
ISettingsRepository<GeneralSettings> settingsRepository,
ISettingsRepository<ColorPickerSettings> colorPickerSettingsRepository,
Func<string, int> ipcMSGCallBackFunc)
{
// Obtain the general PowerToy settings configurations
if (settingsRepository == null)
Expand Down Expand Up @@ -62,15 +66,14 @@ public ColorPickerViewModel(ISettingsUtils settingsUtils, ISettingsRepository<Ge
GeneralSettingsConfig = settingsRepository.SettingsConfig;

_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
if (_settingsUtils.SettingsExists(ColorPickerSettings.ModuleName))
{
_colorPickerSettings = _settingsUtils.GetSettingsOrDefault<ColorPickerSettings>(ColorPickerSettings.ModuleName);
}
else

if (colorPickerSettingsRepository == null)
{
_colorPickerSettings = new ColorPickerSettings();
throw new ArgumentNullException(nameof(colorPickerSettingsRepository));
}

_colorPickerSettings = colorPickerSettingsRepository.SettingsConfig;

_isEnabled = GeneralSettingsConfig.Enabled.ColorPicker;

// set the callback functions value to hangle outgoing IPC message.
Expand Down
Expand Up @@ -175,21 +175,22 @@ public HotkeySettings ActivationShortcut
if (Settings.Properties.ActivationShortcut != value)
{
Settings.Properties.ActivationShortcut = value;

NotifyPropertyChanged();

SendConfigMSG(
string.Format(
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
MeasureToolSettings.ModuleName,
JsonSerializer.Serialize(Settings)));
string.Format(
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
MeasureToolSettings.ModuleName,
JsonSerializer.Serialize(Settings)));
}
}
}

public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
OnPropertyChanged(propertyName);

if (propertyName == nameof(ShowContinuousCaptureWarning))
{
// Don't trigger a settings update if the changed property is for visual notification.
Expand Down
Expand Up @@ -36,7 +36,11 @@ public class PowerLauncherViewModel : Observable

private Func<string, int> SendConfigMSG { get; }

public PowerLauncherViewModel(PowerLauncherSettings settings, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<bool> isDark)
public PowerLauncherViewModel(
PowerLauncherSettings settings,
ISettingsRepository<GeneralSettings> settingsRepository,
Func<string, int> ipcMSGCallBackFunc,
Func<bool> isDark)
{
if (settings == null)
{
Expand Down
Expand Up @@ -33,6 +33,7 @@ public class PowerOcrViewModel : Observable, IDisposable
public PowerOcrViewModel(
ISettingsUtils settingsUtils,
ISettingsRepository<GeneralSettings> settingsRepository,
ISettingsRepository<PowerOcrSettings> powerOcrsettingsRepository,
Func<string, int> ipcMSGCallBackFunc)
{
// To obtain the general settings configurations of PowerToys Settings.
Expand All @@ -50,15 +51,14 @@ public class PowerOcrViewModel : Observable, IDisposable
}

_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
if (_settingsUtils.SettingsExists(PowerOcrSettings.ModuleName))
{
_powerOcrSettings = _settingsUtils.GetSettingsOrDefault<PowerOcrSettings>(PowerOcrSettings.ModuleName);
}
else

if (powerOcrsettingsRepository == null)
{
_powerOcrSettings = new PowerOcrSettings();
throw new ArgumentNullException(nameof(powerOcrsettingsRepository));
}

_powerOcrSettings = powerOcrsettingsRepository.SettingsConfig;

_isEnabled = GeneralSettingsConfig.Enabled.PowerOCR;

// set the callback functions value to hangle outgoing IPC message.
Expand Down Expand Up @@ -98,6 +98,8 @@ public HotkeySettings ActivationShortcut
{
_powerOcrSettings.Properties.ActivationShortcut = value;
OnPropertyChanged(nameof(ActivationShortcut));

_settingsUtils.SaveSettings(_powerOcrSettings.ToJsonString(), PowerOcrSettings.ModuleName);
NotifySettingsChanged();
}
}
Expand Down
Expand Up @@ -4,9 +4,10 @@

using System;
using System.Collections.Generic;

using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
Expand All @@ -31,7 +32,13 @@ public class VideoConferenceViewModel : Observable

private string _settingsConfigFileFolder = string.Empty;

public VideoConferenceViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, Func<Task<string>> pickFileDialog, string configFileSubfolder = "")
public VideoConferenceViewModel(
ISettingsUtils settingsUtils,
ISettingsRepository<GeneralSettings> settingsRepository,
ISettingsRepository<VideoConferenceSettings> videoConferenceSettingsRepository,
Func<string, int> ipcMSGCallBackFunc,
Func<Task<string>> pickFileDialog,
string configFileSubfolder = "")
{
PickFileDialog = pickFileDialog;

Expand All @@ -48,16 +55,13 @@ public VideoConferenceViewModel(ISettingsUtils settingsUtils, ISettingsRepositor

_settingsConfigFileFolder = configFileSubfolder;

try
{
Settings = _settingsUtils.GetSettings<VideoConferenceSettings>(GetSettingsSubPath());
}
catch
if (videoConferenceSettingsRepository == null)
{
Settings = new VideoConferenceSettings();
_settingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath());
throw new ArgumentNullException(nameof(videoConferenceSettingsRepository));
}

Settings = videoConferenceSettingsRepository.SettingsConfig;

CameraNames = interop.CommonManaged.GetAllVideoCaptureDeviceNames();
MicrophoneNames = interop.CommonManaged.GetAllActiveMicrophoneDeviceNames();
MicrophoneNames.Insert(0, "[All]");
Expand Down Expand Up @@ -396,16 +400,21 @@ public bool HideToolbarWhenUnmuted

public string GetSettingsSubPath()
{
return _settingsConfigFileFolder + "\\" + ModuleName;
return _settingsConfigFileFolder + (string.IsNullOrEmpty(_settingsConfigFileFolder) ? string.Empty : "\\") + ModuleName;
}

public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
OnPropertyChanged(propertyName);
SndVideoConferenceSettings outsettings = new SndVideoConferenceSettings(Settings);
SndModuleSettings<SndVideoConferenceSettings> ipcMessage = new SndModuleSettings<SndVideoConferenceSettings>(outsettings);
SendConfigMSG(ipcMessage.ToJsonString());

_settingsUtils.SaveSettings(Settings.ToJsonString(), GetSettingsSubPath());

SendConfigMSG(
string.Format(
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
ModuleName,
JsonSerializer.Serialize(Settings)));
}
}

Expand Down
Expand Up @@ -36,10 +36,15 @@ public void OriginalFilesModificationTest(string version, string fileName)
var mockGeneralSettingsUtils = new SettingsUtils(mockGeneralIOProvider.Object, settingPathMock.Object);
GeneralSettings originalGeneralSettings = mockGeneralSettingsUtils.GetSettingsOrDefault<GeneralSettings>();
var generalSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<GeneralSettings>(mockGeneralSettingsUtils);
var colorPickerSettingsRepository = new BackCompatTestProperties.MockSettingsRepository<ColorPickerSettings>(mockSettingsUtils);

// Act
// Initialise View Model with test Config files
using (var viewModel = new ColorPickerViewModel(mockSettingsUtils, generalSettingsRepository, ColorPickerIsEnabledByDefaultIPC))
using (var viewModel = new ColorPickerViewModel(
mockSettingsUtils,
generalSettingsRepository,
colorPickerSettingsRepository,
ColorPickerIsEnabledByDefaultIPC))
{
// Assert
// Verify that the old settings persisted
Expand All @@ -58,7 +63,11 @@ public void OriginalFilesModificationTest(string version, string fileName)
public void ColorPickerIsEnabledByDefault()
{
var mockSettingsUtils = ISettingsUtilsMocks.GetStubSettingsUtils<ColorPickerSettings>();
using (var viewModel = new ColorPickerViewModel(ISettingsUtilsMocks.GetStubSettingsUtils<ColorPickerSettings>().Object, SettingsRepository<GeneralSettings>.GetInstance(ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>().Object), ColorPickerIsEnabledByDefaultIPC))
using (var viewModel = new ColorPickerViewModel(
ISettingsUtilsMocks.GetStubSettingsUtils<ColorPickerSettings>().Object,
SettingsRepository<GeneralSettings>.GetInstance(ISettingsUtilsMocks.GetStubSettingsUtils<GeneralSettings>().Object),
SettingsRepository<ColorPickerSettings>.GetInstance(new SettingsUtils()),
ColorPickerIsEnabledByDefaultIPC))
{
Assert.IsTrue(viewModel.IsEnabled);
}
Expand Down
6 changes: 5 additions & 1 deletion src/settings-ui/Settings.UI/Views/ColorPickerPage.xaml.cs
Expand Up @@ -15,7 +15,11 @@ public sealed partial class ColorPickerPage : Page
public ColorPickerPage()
{
var settingsUtils = new SettingsUtils();
ViewModel = new ColorPickerViewModel(settingsUtils, SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage);
ViewModel = new ColorPickerViewModel(
settingsUtils,
SettingsRepository<GeneralSettings>.GetInstance(settingsUtils),
SettingsRepository<ColorPickerSettings>.GetInstance(settingsUtils),
ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
InitializeComponent();
}
Expand Down
6 changes: 4 additions & 2 deletions src/settings-ui/Settings.UI/Views/PowerLauncherPage.xaml.cs
Expand Up @@ -6,6 +6,7 @@
using System.Collections.ObjectModel;
using System.IO;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
using Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -34,7 +35,8 @@ public PowerLauncherPage()
InitializeComponent();
var settingsUtils = new SettingsUtils();
_lastIPCMessageSentTick = Environment.TickCount;
PowerLauncherSettings settings = settingsUtils.GetSettingsOrDefault<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);

PowerLauncherSettings settings = SettingsRepository<PowerLauncherSettings>.GetInstance(settingsUtils)?.SettingsConfig;
ViewModel = new PowerLauncherViewModel(settings, SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), SendDefaultIPCMessageTimed, App.IsDarkTheme);
DataContext = ViewModel;
_ = Helper.GetFileWatcher(PowerLauncherSettings.ModuleName, "settings.json", () =>
Expand All @@ -48,7 +50,7 @@ public PowerLauncherPage()
PowerLauncherSettings powerLauncherSettings = null;
try
{
powerLauncherSettings = settingsUtils.GetSettingsOrDefault<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
powerLauncherSettings = SettingsRepository<PowerLauncherSettings>.GetInstance(settingsUtils)?.SettingsConfig;
}
catch (IOException ex)
{
Expand Down
1 change: 1 addition & 0 deletions src/settings-ui/Settings.UI/Views/PowerOcrPage.xaml.cs
Expand Up @@ -18,6 +18,7 @@ public PowerOcrPage()
ViewModel = new PowerOcrViewModel(
settingsUtils,
SettingsRepository<GeneralSettings>.GetInstance(settingsUtils),
SettingsRepository<PowerOcrSettings>.GetInstance(settingsUtils),
ShellPage.SendDefaultIPCMessage);
DataContext = ViewModel;
InitializeComponent();
Expand Down
7 changes: 6 additions & 1 deletion src/settings-ui/Settings.UI/Views/VideoConference.xaml.cs
Expand Up @@ -34,7 +34,12 @@ private static async Task<string> PickFileDialog()
public VideoConferencePage()
{
var settingsUtils = new SettingsUtils();
ViewModel = new VideoConferenceViewModel(settingsUtils, SettingsRepository<GeneralSettings>.GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage, PickFileDialog);
ViewModel = new VideoConferenceViewModel(
settingsUtils,
SettingsRepository<GeneralSettings>.GetInstance(settingsUtils),
SettingsRepository<VideoConferenceSettings>.GetInstance(settingsUtils),
ShellPage.SendDefaultIPCMessage,
PickFileDialog);
DataContext = ViewModel;
InitializeComponent();
}
Expand Down

0 comments on commit ab41b61

Please sign in to comment.