/
githubmirror
/
PowerToys
Обзор
Документация
Войти
/
githubmirror
/
PowerToys
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/settings-ui/Settings.UI/ViewModels/AltWindowCycleViewModel.cs
174 строки
6 KB
Clint Rutkas
New module: AltWindowCycle (#48281)
31 июл 2026, 08:42
Не верифицирован
31 июл 2026, 08:42
bb99c30
Код
Авторство
О чём код?
// 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.Interfaces; using Microsoft.PowerToys.Settings.UI.Library.Utilities; using Microsoft.PowerToys.Settings.UI.SerializationContext; namespace Microsoft.PowerToys.Settings.UI.ViewModels { public partial class AltWindowCycleViewModel : PageViewModelBase { protected override string ModuleName => AltWindowCycleSettings.ModuleName; private SettingsUtils SettingsUtils { get; set; } private GeneralSettings GeneralSettingsConfig { get; set; } private AltWindowCycleSettings Settings { get; set; } private Func<string, int> SendConfigMSG { get; } public AltWindowCycleViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<AltWindowCycleSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc) { ArgumentNullException.ThrowIfNull(settingsUtils); SettingsUtils = settingsUtils; // To obtain the general settings configurations of PowerToys Settings. ArgumentNullException.ThrowIfNull(settingsRepository); GeneralSettingsConfig = settingsRepository.SettingsConfig; InitializeEnabledValue(); // To obtain the settings configurations of AltWindowCycle. ArgumentNullException.ThrowIfNull(moduleSettingsRepository); Settings = moduleSettingsRepository.SettingsConfig; _nextWindowShortcut = Settings.Properties.NextWindowShortcut; _previousWindowShortcut = Settings.Properties.PreviousWindowShortcut; // set the callback functions value to handle outgoing IPC message. SendConfigMSG = ipcMSGCallBackFunc; } private void InitializeEnabledValue() { _enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredAltWindowCycleEnabledValue(); if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled) { // Get the enabled state from GPO. _enabledStateIsGPOConfigured = true; _isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled; } else { _isEnabled = GeneralSettingsConfig.Enabled.AltWindowCycle; } } public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings() { var hotkeysDict = new Dictionary<string, HotkeySettings[]> { [ModuleName] = [NextWindowShortcut, PreviousWindowShortcut], }; return hotkeysDict; } public bool IsEnabled { get => _isEnabled; set { if (_enabledStateIsGPOConfigured) { // If it's GPO configured, shouldn't be able to change this state. return; } if (value != _isEnabled) { _isEnabled = value; // Set the status in the general settings configuration GeneralSettingsConfig.Enabled.AltWindowCycle = value; OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig); SendConfigMSG(snd.ToString()); OnPropertyChanged(nameof(IsEnabled)); } } } public bool IsEnabledGpoConfigured { get => _enabledStateIsGPOConfigured; } public HotkeySettings NextWindowShortcut { get => _nextWindowShortcut; set { if (value != _nextWindowShortcut) { _nextWindowShortcut = value ?? Settings.Properties.DefaultNextWindowShortcut; Settings.Properties.NextWindowShortcut = _nextWindowShortcut; NotifyPropertyChanged(); SendSettingsConfigMessage(); } } } public HotkeySettings PreviousWindowShortcut { get => _previousWindowShortcut; set { if (value != _previousWindowShortcut) { _previousWindowShortcut = value ?? Settings.Properties.DefaultPreviousWindowShortcut; Settings.Properties.PreviousWindowShortcut = _previousWindowShortcut; NotifyPropertyChanged(); SendSettingsConfigMessage(); } } } public void NotifyPropertyChanged([CallerMemberName] string propertyName = null) { OnPropertyChanged(propertyName); SettingsUtils.SaveSettings(Settings.ToJsonString(), AltWindowCycleSettings.ModuleName); } public void RefreshEnabledState() { InitializeEnabledValue(); OnPropertyChanged(nameof(IsEnabled)); } private void SendSettingsConfigMessage() { SendConfigMSG( string.Format( CultureInfo.InvariantCulture, "{{ \"powertoys\": {{ \"{0}\": {1} }} }}", AltWindowCycleSettings.ModuleName, JsonSerializer.Serialize(Settings, SourceGenerationContextContext.Default.AltWindowCycleSettings))); } private GpoRuleConfigured _enabledGpoRuleConfiguration; private bool _enabledStateIsGPOConfigured; private bool _isEnabled; private HotkeySettings _nextWindowShortcut; private HotkeySettings _previousWindowShortcut; } }