PowerToys/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerLauncherViewModel.cs

227 lines
6.1 KiB
C#
Raw Normal View History

// 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.
using System.Runtime.CompilerServices;
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Views;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class PowerLauncherViewModel : Observable
{
2020-04-08 15:19:00 +08:00
private PowerLauncherSettings settings;
public delegate void SendCallback(PowerLauncherSettings settings);
private readonly SendCallback callback;
public PowerLauncherViewModel()
{
if (SettingsUtils.SettingsExists(PowerLauncherSettings.POWERTOYNAME))
{
settings = SettingsUtils.GetSettings<PowerLauncherSettings>(PowerLauncherSettings.POWERTOYNAME);
2020-04-08 15:19:00 +08:00
}
else
{
settings = new PowerLauncherSettings();
}
callback = (PowerLauncherSettings settings) =>
{
// Propagate changes to Power Launcher through IPC
var propertiesJson = JsonSerializer.Serialize(settings.properties);
ShellPage.DefaultSndMSGCallback(
string.Format("{{ \"{0}\": {1} }}", PowerLauncherSettings.POWERTOYNAME, JsonSerializer.Serialize(settings.properties)));
};
}
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);
settings.Save();
callback(settings);
}
public bool EnablePowerLauncher
{
2020-04-08 15:19:00 +08:00
get
{
return settings.properties.enable_powerlauncher;
2020-04-08 15:19:00 +08:00
}
set
{
if (settings.properties.enable_powerlauncher != value)
{
settings.properties.enable_powerlauncher = value;
UpdateSettings();
}
}
}
public string SearchResultPreference
{
2020-04-08 15:19:00 +08:00
get
{
return settings.properties.search_result_preference;
2020-04-08 15:19:00 +08:00
}
set
{
if (settings.properties.search_result_preference != value)
{
settings.properties.search_result_preference = value;
UpdateSettings();
}
}
}
public string SearchTypePreference
{
2020-04-08 15:19:00 +08:00
get
{
return settings.properties.search_type_preference;
2020-04-08 15:19:00 +08:00
}
set
{
if (settings.properties.search_type_preference != value)
{
settings.properties.search_type_preference = value;
UpdateSettings();
}
}
}
public int MaximumNumberOfResults
{
2020-04-08 15:19:00 +08:00
get
{
return settings.properties.maximum_number_of_results;
2020-04-08 15:19:00 +08:00
}
set
{
if (settings.properties.maximum_number_of_results != value)
{
settings.properties.maximum_number_of_results = value;
UpdateSettings();
}
}
}
public HotkeySettings OpenPowerLauncher
{
2020-04-08 15:19:00 +08:00
get
{
return settings.properties.open_powerlauncher;
2020-04-08 15:19:00 +08:00
}
set
{
if (settings.properties.open_powerlauncher != value)
{
settings.properties.open_powerlauncher = value;
UpdateSettings();
2020-04-08 15:19:00 +08:00
}
}
}
public HotkeySettings OpenFileLocation
{
2020-04-08 15:19:00 +08:00
get
{
return settings.properties.open_file_location;
2020-04-08 15:19:00 +08:00
}
set
{
if (settings.properties.open_file_location != value)
{
settings.properties.open_file_location = value;
UpdateSettings();
}
}
}
public HotkeySettings CopyPathLocation
{
2020-04-08 15:19:00 +08:00
get
{
return settings.properties.copy_path_location;
2020-04-08 15:19:00 +08:00
}
set
{
if (settings.properties.copy_path_location != value)
{
settings.properties.copy_path_location = value;
UpdateSettings();
}
}
}
public HotkeySettings OpenConsole
{
2020-04-08 15:19:00 +08:00
get
{
return settings.properties.open_console;
2020-04-08 15:19:00 +08:00
}
set
{
if (settings.properties.open_console != value)
{
settings.properties.open_console = value;
UpdateSettings();
}
}
}
public bool OverrideWinRKey
{
2020-04-08 15:19:00 +08:00
get
{
return settings.properties.override_win_r_key;
2020-04-08 15:19:00 +08:00
}
set
{
if (settings.properties.override_win_r_key != value)
{
settings.properties.override_win_r_key = value;
UpdateSettings();
}
}
}
public bool OverrideWinSKey
{
2020-04-08 15:19:00 +08:00
get
{
return settings.properties.override_win_s_key;
2020-04-08 15:19:00 +08:00
}
set
{
if (settings.properties.override_win_s_key != value)
{
settings.properties.override_win_s_key = value;
UpdateSettings();
}
}
}
}
2020-04-08 15:19:00 +08:00
}