Files
PowerToys/src/settings-ui/Settings.UI.Controls/QuickAccess/QuickAccessViewModel.cs
Jaylyn Barbee a94d010a8d [Light Switch] Fixed issue where Light switch could be toggled from the dashboard while GPO settings are active (#45756)
<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request
Current behavior is that GPO policies are enforced on the Light Switch
settings page but not on the dashboard or quick access menu's. This
allows the user to still toggle Light Switch settings in scenarios where
GPO is either forcing it to be enabled or disabled. This PR addresses
that issue.

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [x] Closes: #43754
## Additional notes
This PR only addresses issues on the dashboard page and in the quick
access menu as described by
https://github.com/microsoft/PowerToys/issues/43754.

This PR also fixed an issue where modules were not showing in the Quick
access menu when GPO is set to "Enabled" until you visited the module
specific page. When modules are forced enabled, they should appear in
Quick Access upon launch if they have a quick access entry.

This issue does not address issue
https://github.com/microsoft/PowerToys/issues/42484 which was fixed with
the PR https://github.com/microsoft/PowerToys/pull/44567

## Validation Steps Performed
Tested locally, photos below
Not configured:
<img width="1371" height="964" alt="image"
src="https://github.com/user-attachments/assets/50ee579d-8ffb-44fd-92a9-e191b61c0318"
/>
Enabled:
<img width="1183" height="988" alt="image"
src="https://github.com/user-attachments/assets/789abf28-d140-4d93-8934-48b3ac92be2e"
/>
Disabled:
<img width="1282" height="965" alt="image"
src="https://github.com/user-attachments/assets/17ec915a-29d9-42fb-a58e-4b769a728e6a"
/>

We can observe the option being locked on the quick toggles and not
present in the quick access menu when disabled.
2026-02-25 16:44:15 +01:00

148 lines
6.9 KiB
C#

// 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.ObjectModel;
using ManagedCommon;
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.Library.ViewModels.Commands;
using Microsoft.UI.Dispatching;
using Microsoft.Windows.ApplicationModel.Resources;
namespace Microsoft.PowerToys.Settings.UI.Controls
{
public partial class QuickAccessViewModel : Observable
{
private readonly ISettingsRepository<GeneralSettings> _settingsRepository;
private readonly IQuickAccessLauncher _launcher;
private readonly Func<ModuleType, bool> _isModuleGpoDisabled;
private readonly Func<ModuleType, bool> _isModuleGpoEnabled;
private readonly ResourceLoader _resourceLoader;
private readonly DispatcherQueue _dispatcherQueue;
private GeneralSettings _generalSettings;
public ObservableCollection<QuickAccessItem> Items { get; } = new();
public QuickAccessViewModel(
ISettingsRepository<GeneralSettings> settingsRepository,
IQuickAccessLauncher launcher,
Func<ModuleType, bool> isModuleGpoDisabled,
Func<ModuleType, bool> isModuleGpoEnabled,
ResourceLoader resourceLoader)
{
_settingsRepository = settingsRepository;
_launcher = launcher;
_isModuleGpoDisabled = isModuleGpoDisabled;
_isModuleGpoEnabled = isModuleGpoEnabled;
_resourceLoader = resourceLoader;
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
_generalSettings = _settingsRepository.SettingsConfig;
_generalSettings.AddEnabledModuleChangeNotification(ModuleEnabledChanged);
_settingsRepository.SettingsChanged += OnSettingsChanged;
InitializeItems();
}
private void OnSettingsChanged(GeneralSettings newSettings)
{
if (_dispatcherQueue != null)
{
_dispatcherQueue.TryEnqueue(() =>
{
_generalSettings = newSettings;
_generalSettings.AddEnabledModuleChangeNotification(ModuleEnabledChanged);
RefreshItemsVisibility();
});
}
}
private void InitializeItems()
{
AddFlyoutMenuItem(ModuleType.ColorPicker);
AddFlyoutMenuItem(ModuleType.CmdPal);
AddFlyoutMenuItem(ModuleType.EnvironmentVariables);
AddFlyoutMenuItem(ModuleType.FancyZones);
AddFlyoutMenuItem(ModuleType.Hosts);
AddFlyoutMenuItem(ModuleType.LightSwitch);
AddFlyoutMenuItem(ModuleType.PowerDisplay);
AddFlyoutMenuItem(ModuleType.PowerLauncher);
AddFlyoutMenuItem(ModuleType.PowerOCR);
AddFlyoutMenuItem(ModuleType.RegistryPreview);
AddFlyoutMenuItem(ModuleType.MeasureTool);
AddFlyoutMenuItem(ModuleType.ShortcutGuide);
AddFlyoutMenuItem(ModuleType.Workspaces);
}
private void AddFlyoutMenuItem(ModuleType moduleType)
{
if (_isModuleGpoDisabled(moduleType))
{
return;
}
Items.Add(new QuickAccessItem
{
Title = _resourceLoader.GetString(Microsoft.PowerToys.Settings.UI.Library.Helpers.ModuleHelper.GetModuleLabelResourceName(moduleType)),
Tag = moduleType,
Visible = _isModuleGpoEnabled(moduleType) || Microsoft.PowerToys.Settings.UI.Library.Helpers.ModuleHelper.GetIsModuleEnabled(_generalSettings, moduleType),
Description = GetModuleToolTip(moduleType),
Icon = Microsoft.PowerToys.Settings.UI.Library.Helpers.ModuleHelper.GetModuleTypeFluentIconName(moduleType),
Command = new RelayCommand(() => _launcher.Launch(moduleType)),
});
}
private void ModuleEnabledChanged()
{
if (_dispatcherQueue != null)
{
_dispatcherQueue.TryEnqueue(() =>
{
_generalSettings = _settingsRepository.SettingsConfig;
_generalSettings.AddEnabledModuleChangeNotification(ModuleEnabledChanged);
RefreshItemsVisibility();
});
}
}
private void RefreshItemsVisibility()
{
foreach (var item in Items)
{
if (item.Tag is ModuleType moduleType)
{
item.Visible = _isModuleGpoEnabled(moduleType) || Microsoft.PowerToys.Settings.UI.Library.Helpers.ModuleHelper.GetIsModuleEnabled(_generalSettings, moduleType);
}
}
}
private string GetModuleToolTip(ModuleType moduleType)
{
return moduleType switch
{
ModuleType.ColorPicker => SettingsRepository<ColorPickerSettings>.GetInstance(SettingsUtils.Default).SettingsConfig.Properties.ActivationShortcut.ToString(),
ModuleType.FancyZones => SettingsRepository<FancyZonesSettings>.GetInstance(SettingsUtils.Default).SettingsConfig.Properties.FancyzonesEditorHotkey.Value.ToString(),
ModuleType.PowerDisplay => SettingsRepository<PowerDisplaySettings>.GetInstance(SettingsUtils.Default).SettingsConfig.Properties.ActivationShortcut.ToString(),
ModuleType.LightSwitch => SettingsRepository<LightSwitchSettings>.GetInstance(SettingsUtils.Default).SettingsConfig.Properties.ToggleThemeHotkey.Value.ToString(),
ModuleType.PowerLauncher => SettingsRepository<PowerLauncherSettings>.GetInstance(SettingsUtils.Default).SettingsConfig.Properties.OpenPowerLauncher.ToString(),
ModuleType.PowerOCR => SettingsRepository<PowerOcrSettings>.GetInstance(SettingsUtils.Default).SettingsConfig.Properties.ActivationShortcut.ToString(),
ModuleType.Workspaces => SettingsRepository<WorkspacesSettings>.GetInstance(SettingsUtils.Default).SettingsConfig.Properties.Hotkey.Value.ToString(),
ModuleType.MeasureTool => SettingsRepository<MeasureToolSettings>.GetInstance(SettingsUtils.Default).SettingsConfig.Properties.ActivationShortcut.ToString(),
ModuleType.ShortcutGuide => GetShortcutGuideToolTip(),
_ => string.Empty,
};
}
private string GetShortcutGuideToolTip()
{
var shortcutGuideSettings = SettingsRepository<ShortcutGuideSettings>.GetInstance(SettingsUtils.Default).SettingsConfig;
return shortcutGuideSettings.Properties.UseLegacyPressWinKeyBehavior.Value
? "Win"
: shortcutGuideSettings.Properties.OpenShortcutGuide.ToString();
}
}
}