/
githubmirror
/
PowerToys
Обзор
Документация
Войти
/
githubmirror
/
PowerToys
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/settings-ui/Settings.UI/ViewModels/ShortcutGuideViewModel.cs
241 строка
8 KB
Noraa Junker
Shortcut Guide V2 (#40834)
21 май 2026, 18:27
Не верифицирован
21 май 2026, 18:27
9699d8a
Код
Авторство
О чём код?
// 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; using System.Collections.Generic; using System.Globalization; using System.Runtime.CompilerServices; using System.Text.Json; using global::PowerToys.GPOWrapper; using Microsoft.PowerToys.Settings.UI.Helpers; using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.Library.Helpers; using Microsoft.PowerToys.Settings.UI.Library.Interfaces; using Microsoft.PowerToys.Settings.UI.SerializationContext; namespace Microsoft.PowerToys.Settings.UI.ViewModels { public partial class ShortcutGuideViewModel : PageViewModelBase { protected override string ModuleName => ShortcutGuideSettings.ModuleName; private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } private ShortcutGuideSettings Settings { get; set; } private Func<string, int> SendConfigMSG { get; } private string _settingsConfigFileFolder = string.Empty; private string _disabledApps; public ShortcutGuideViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<ShortcutGuideSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "") { SettingsUtils = settingsUtils; // Update Settings file folder: _settingsConfigFileFolder = configFileSubfolder; // To obtain the general PowerToys settings. ArgumentNullException.ThrowIfNull(settingsRepository); GeneralSettingsConfig = settingsRepository.SettingsConfig; // To obtain the shortcut guide settings, if the file exists. // If not, to create a file with the default settings and to return the default configurations. ArgumentNullException.ThrowIfNull(moduleSettingsRepository); Settings = moduleSettingsRepository.SettingsConfig; // set the callback functions value to handle outgoing IPC message. SendConfigMSG = ipcMSGCallBackFunc; InitializeEnabledValue(); _disabledApps = Settings.Properties.DisabledApps.Value; switch (Settings.Properties.Theme.Value) { case "dark": _themeIndex = 0; break; case "light": _themeIndex = 1; break; case "system": _themeIndex = 2; break; } switch (Settings.Properties.WindowPosition.Value) { case (int)ShortcutGuideWindowPosition.Right: _positionIndex = 1; break; default: _positionIndex = 0; break; } } private void InitializeEnabledValue() { _enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredShortcutGuideEnabledValue(); if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled) { // Get the enabled state from GPO. _enabledStateIsGPOConfigured = true; _isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled; } else { _isEnabled = GeneralSettingsConfig.Enabled.ShortcutGuide; } } public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings() { var hotkeysDict = new Dictionary<string, HotkeySettings[]> { [ModuleName] = [OpenShortcutGuide], }; return hotkeysDict; } private GpoRuleConfigured _enabledGpoRuleConfiguration; private bool _enabledStateIsGPOConfigured; private bool _isEnabled; private int _themeIndex; private int _positionIndex; public bool IsEnabled { get { return _isEnabled; } set { if (_enabledStateIsGPOConfigured) { // If it's GPO configured, shouldn't be able to change this state. return; } if (value != _isEnabled) { _isEnabled = value; // To update the status of shortcut guide in General PowerToy settings. GeneralSettingsConfig.Enabled.ShortcutGuide = value; OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig); SendConfigMSG(snd.ToString()); OnPropertyChanged(nameof(IsEnabled)); } } } public bool IsEnabledGpoConfigured { get => _enabledStateIsGPOConfigured; } public HotkeySettings OpenShortcutGuide { get { return Settings.Properties.OpenShortcutGuide; } set { if (Settings.Properties.OpenShortcutGuide != value) { Settings.Properties.OpenShortcutGuide = value ?? Settings.Properties.DefaultOpenShortcutGuide; NotifyPropertyChanged(); } } } public int ThemeIndex { get { return _themeIndex; } set { if (_themeIndex != value) { switch (value) { case 0: Settings.Properties.Theme.Value = "dark"; break; case 1: Settings.Properties.Theme.Value = "light"; break; case 2: Settings.Properties.Theme.Value = "system"; break; } _themeIndex = value; NotifyPropertyChanged(); } } } public int PositionIndex { get { return _positionIndex; } set { if (_positionIndex != value) { switch (value) { case 1: Settings.Properties.WindowPosition.Value = (int)ShortcutGuideWindowPosition.Right; break; default: Settings.Properties.WindowPosition.Value = (int)ShortcutGuideWindowPosition.Left; break; } _positionIndex = value; NotifyPropertyChanged(); } } } public string DisabledApps { get { return _disabledApps; } set { if (value != _disabledApps) { _disabledApps = value; Settings.Properties.DisabledApps.Value = value; NotifyPropertyChanged(); } } } public string GetSettingsSubPath() { return _settingsConfigFileFolder + "\\" + ModuleName; } public void NotifyPropertyChanged([CallerMemberName] string propertyName = null) { OnPropertyChanged(propertyName); SndShortcutGuideSettings outsettings = new SndShortcutGuideSettings(Settings); SndModuleSettings<SndShortcutGuideSettings> ipcMessage = new SndModuleSettings<SndShortcutGuideSettings>(outsettings); SendConfigMSG(ipcMessage.ToJsonString()); SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName); } public void RefreshEnabledState() { InitializeEnabledValue(); OnPropertyChanged(nameof(IsEnabled)); } } }