2020-08-18 01:00:56 +08:00
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
// See the LICENSE file in the project root for more information.
|
2020-08-19 23:12:07 +08:00
|
|
|
|
|
|
|
using System;
|
2021-02-26 19:21:58 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.ComponentModel;
|
2020-10-20 04:32:05 +08:00
|
|
|
using System.Globalization;
|
2021-02-26 19:21:58 +08:00
|
|
|
using System.Linq;
|
2020-08-18 01:00:56 +08:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Text.Json;
|
2020-10-24 06:05:07 +08:00
|
|
|
using ManagedCommon;
|
2020-10-23 00:45:48 +08:00
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
2020-08-18 01:00:56 +08:00
|
|
|
|
2020-10-23 00:45:48 +08:00
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
|
|
|
public class PowerLauncherViewModel : Observable
|
|
|
|
{
|
2020-10-24 06:05:07 +08:00
|
|
|
private bool _isDarkThemeRadioButtonChecked;
|
|
|
|
private bool _isLightThemeRadioButtonChecked;
|
|
|
|
private bool _isSystemThemeRadioButtonChecked;
|
|
|
|
|
2021-03-10 01:20:49 +08:00
|
|
|
private bool _isCursorPositionRadioButtonChecked;
|
|
|
|
private bool _isPrimaryMonitorPositionRadioButtonChecked;
|
|
|
|
private bool _isFocusPositionRadioButtonChecked;
|
|
|
|
|
2020-09-24 04:20:32 +08:00
|
|
|
private GeneralSettings GeneralSettingsConfig { get; set; }
|
|
|
|
|
2020-09-22 01:14:44 +08:00
|
|
|
private readonly ISettingsUtils _settingsUtils;
|
|
|
|
|
2020-08-18 01:00:56 +08:00
|
|
|
private PowerLauncherSettings settings;
|
|
|
|
|
|
|
|
public delegate void SendCallback(PowerLauncherSettings settings);
|
|
|
|
|
|
|
|
private readonly SendCallback callback;
|
|
|
|
|
2021-02-26 19:21:58 +08:00
|
|
|
private readonly Func<bool> isDark;
|
|
|
|
|
2020-08-19 23:12:07 +08:00
|
|
|
private Func<string, int> SendConfigMSG { get; }
|
|
|
|
|
2021-02-26 19:21:58 +08:00
|
|
|
public PowerLauncherViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, int defaultKeyCode, Func<bool> isDark)
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
2020-09-22 01:14:44 +08:00
|
|
|
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
2021-02-26 19:21:58 +08:00
|
|
|
this.isDark = isDark;
|
2020-09-22 01:14:44 +08:00
|
|
|
|
2020-09-24 04:20:32 +08:00
|
|
|
// To obtain the general Settings configurations of PowerToys
|
2020-10-20 04:32:05 +08:00
|
|
|
if (settingsRepository == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(settingsRepository));
|
|
|
|
}
|
|
|
|
|
2020-09-24 04:20:32 +08:00
|
|
|
GeneralSettingsConfig = settingsRepository.SettingsConfig;
|
|
|
|
|
2020-09-11 00:44:10 +08:00
|
|
|
// set the callback functions value to hangle outgoing IPC message.
|
|
|
|
SendConfigMSG = ipcMSGCallBackFunc;
|
|
|
|
callback = (PowerLauncherSettings settings) =>
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
2020-09-11 00:44:10 +08:00
|
|
|
// Propagate changes to Power Launcher through IPC
|
2020-10-20 04:32:05 +08:00
|
|
|
// Using InvariantCulture as this is an IPC message
|
2020-09-11 00:44:10 +08:00
|
|
|
SendConfigMSG(
|
2020-10-20 04:32:05 +08:00
|
|
|
string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
|
|
|
|
PowerLauncherSettings.ModuleName,
|
|
|
|
JsonSerializer.Serialize(settings)));
|
2020-09-11 00:44:10 +08:00
|
|
|
};
|
2020-09-24 04:20:32 +08:00
|
|
|
|
2020-09-22 01:14:44 +08:00
|
|
|
if (_settingsUtils.SettingsExists(PowerLauncherSettings.ModuleName))
|
2020-09-11 00:44:10 +08:00
|
|
|
{
|
2021-01-14 20:14:29 +08:00
|
|
|
settings = _settingsUtils.GetSettingsOrDefault<PowerLauncherSettings>(PowerLauncherSettings.ModuleName);
|
2020-09-11 00:44:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
settings = new PowerLauncherSettings();
|
|
|
|
settings.Properties.OpenPowerLauncher.Alt = true;
|
|
|
|
settings.Properties.OpenPowerLauncher.Code = defaultKeyCode;
|
|
|
|
settings.Properties.MaximumNumberOfResults = 4;
|
|
|
|
callback(settings);
|
|
|
|
}
|
2020-10-24 06:05:07 +08:00
|
|
|
|
|
|
|
switch (settings.Properties.Theme)
|
|
|
|
{
|
|
|
|
case Theme.Light:
|
|
|
|
_isLightThemeRadioButtonChecked = true;
|
|
|
|
break;
|
|
|
|
case Theme.Dark:
|
|
|
|
_isDarkThemeRadioButtonChecked = true;
|
|
|
|
break;
|
|
|
|
case Theme.System:
|
|
|
|
_isSystemThemeRadioButtonChecked = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-02-26 19:21:58 +08:00
|
|
|
|
2021-03-10 01:20:49 +08:00
|
|
|
switch (settings.Properties.Position)
|
|
|
|
{
|
|
|
|
case StartupPosition.Cursor:
|
|
|
|
_isCursorPositionRadioButtonChecked = true;
|
|
|
|
break;
|
|
|
|
case StartupPosition.PrimaryMonitor:
|
|
|
|
_isPrimaryMonitorPositionRadioButtonChecked = true;
|
|
|
|
break;
|
|
|
|
case StartupPosition.Focus:
|
|
|
|
_isFocusPositionRadioButtonChecked = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-26 19:21:58 +08:00
|
|
|
foreach (var plugin in Plugins)
|
|
|
|
{
|
|
|
|
plugin.PropertyChanged += OnPluginInfoChange;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnPluginInfoChange(object sender, PropertyChangedEventArgs e)
|
|
|
|
{
|
|
|
|
OnPropertyChanged(nameof(ShowAllPluginsDisabledWarning));
|
|
|
|
UpdateSettings();
|
2020-08-18 01:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public PowerLauncherViewModel(PowerLauncherSettings settings, SendCallback callback)
|
|
|
|
{
|
|
|
|
this.settings = settings;
|
|
|
|
this.callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateSettings([CallerMemberName] string propertyName = null)
|
|
|
|
{
|
|
|
|
// Notify UI of property change
|
|
|
|
OnPropertyChanged(propertyName);
|
|
|
|
|
|
|
|
callback(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool EnablePowerLauncher
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2020-09-24 04:20:32 +08:00
|
|
|
return GeneralSettingsConfig.Enabled.PowerLauncher;
|
2020-08-18 01:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
2020-09-24 04:20:32 +08:00
|
|
|
if (GeneralSettingsConfig.Enabled.PowerLauncher != value)
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
2020-09-24 04:20:32 +08:00
|
|
|
GeneralSettingsConfig.Enabled.PowerLauncher = value;
|
2020-08-18 01:00:56 +08:00
|
|
|
OnPropertyChanged(nameof(EnablePowerLauncher));
|
2021-02-26 19:21:58 +08:00
|
|
|
OnPropertyChanged(nameof(ShowAllPluginsDisabledWarning));
|
2020-09-24 04:20:32 +08:00
|
|
|
OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig);
|
2020-08-19 23:12:07 +08:00
|
|
|
SendConfigMSG(outgoing.ToString());
|
2020-08-18 01:00:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string SearchResultPreference
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return settings.Properties.SearchResultPreference;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (settings.Properties.SearchResultPreference != value)
|
|
|
|
{
|
|
|
|
settings.Properties.SearchResultPreference = value;
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string SearchTypePreference
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return settings.Properties.SearchTypePreference;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (settings.Properties.SearchTypePreference != value)
|
|
|
|
{
|
|
|
|
settings.Properties.SearchTypePreference = value;
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int MaximumNumberOfResults
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return settings.Properties.MaximumNumberOfResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (settings.Properties.MaximumNumberOfResults != value)
|
|
|
|
{
|
|
|
|
settings.Properties.MaximumNumberOfResults = value;
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 06:05:07 +08:00
|
|
|
public bool IsDarkThemeRadioButtonChecked
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _isDarkThemeRadioButtonChecked;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == true)
|
|
|
|
{
|
|
|
|
settings.Properties.Theme = Theme.Dark;
|
|
|
|
_isDarkThemeRadioButtonChecked = value;
|
|
|
|
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsLightThemeRadioButtonChecked
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _isLightThemeRadioButtonChecked;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == true)
|
|
|
|
{
|
|
|
|
settings.Properties.Theme = Theme.Light;
|
|
|
|
_isDarkThemeRadioButtonChecked = value;
|
|
|
|
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsSystemThemeRadioButtonChecked
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _isSystemThemeRadioButtonChecked;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == true)
|
|
|
|
{
|
|
|
|
settings.Properties.Theme = Theme.System;
|
|
|
|
_isDarkThemeRadioButtonChecked = value;
|
|
|
|
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 01:20:49 +08:00
|
|
|
public bool IsCursorPositionRadioButtonChecked
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _isCursorPositionRadioButtonChecked;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == true)
|
|
|
|
{
|
|
|
|
settings.Properties.Position = StartupPosition.Cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isCursorPositionRadioButtonChecked = value;
|
|
|
|
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsPrimaryMonitorPositionRadioButtonChecked
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _isPrimaryMonitorPositionRadioButtonChecked;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == true)
|
|
|
|
{
|
|
|
|
settings.Properties.Position = StartupPosition.PrimaryMonitor;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isPrimaryMonitorPositionRadioButtonChecked = value;
|
|
|
|
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsFocusPositionRadioButtonChecked
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _isFocusPositionRadioButtonChecked;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value == true)
|
|
|
|
{
|
|
|
|
settings.Properties.Position = StartupPosition.Focus;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isFocusPositionRadioButtonChecked = value;
|
|
|
|
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 01:00:56 +08:00
|
|
|
public HotkeySettings OpenPowerLauncher
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return settings.Properties.OpenPowerLauncher;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (settings.Properties.OpenPowerLauncher != value)
|
|
|
|
{
|
|
|
|
settings.Properties.OpenPowerLauncher = value;
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public HotkeySettings OpenFileLocation
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return settings.Properties.OpenFileLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (settings.Properties.OpenFileLocation != value)
|
|
|
|
{
|
|
|
|
settings.Properties.OpenFileLocation = value;
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public HotkeySettings CopyPathLocation
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return settings.Properties.CopyPathLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (settings.Properties.CopyPathLocation != value)
|
|
|
|
{
|
|
|
|
settings.Properties.CopyPathLocation = value;
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool OverrideWinRKey
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return settings.Properties.OverrideWinkeyR;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (settings.Properties.OverrideWinkeyR != value)
|
|
|
|
{
|
|
|
|
settings.Properties.OverrideWinkeyR = value;
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool OverrideWinSKey
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return settings.Properties.OverrideWinkeyS;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (settings.Properties.OverrideWinkeyS != value)
|
|
|
|
{
|
|
|
|
settings.Properties.OverrideWinkeyS = value;
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IgnoreHotkeysInFullScreen
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return settings.Properties.IgnoreHotkeysInFullscreen;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (settings.Properties.IgnoreHotkeysInFullscreen != value)
|
|
|
|
{
|
|
|
|
settings.Properties.IgnoreHotkeysInFullscreen = value;
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool ClearInputOnLaunch
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return settings.Properties.ClearInputOnLaunch;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (settings.Properties.ClearInputOnLaunch != value)
|
|
|
|
{
|
|
|
|
settings.Properties.ClearInputOnLaunch = value;
|
|
|
|
UpdateSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 19:21:58 +08:00
|
|
|
private ObservableCollection<PowerLauncherPluginViewModel> _plugins;
|
|
|
|
|
|
|
|
public ObservableCollection<PowerLauncherPluginViewModel> Plugins
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2021-02-26 19:21:58 +08:00
|
|
|
if (_plugins == null)
|
2020-08-18 01:00:56 +08:00
|
|
|
{
|
2021-02-26 19:21:58 +08:00
|
|
|
_plugins = new ObservableCollection<PowerLauncherPluginViewModel>(settings.Plugins.Select(x => new PowerLauncherPluginViewModel(x, isDark)));
|
2020-08-18 01:00:56 +08:00
|
|
|
}
|
2021-02-26 19:21:58 +08:00
|
|
|
|
|
|
|
return _plugins;
|
2020-08-18 01:00:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-26 19:21:58 +08:00
|
|
|
|
|
|
|
public bool ShowAllPluginsDisabledWarning
|
|
|
|
{
|
|
|
|
get => EnablePowerLauncher && Plugins.All(x => x.Disabled);
|
|
|
|
}
|
2020-08-18 01:00:56 +08:00
|
|
|
}
|
|
|
|
}
|