[settings] Show uneditable shortcut in CmdPal page (#38060)

* [settings] Show CmdPal shortcut
* show in dashboard as well


![image](https://github.com/user-attachments/assets/395d112d-162c-412c-99c2-2625b23cc451)

![image](https://github.com/user-attachments/assets/a0362e2b-e647-4a19-b4ff-fdb501e236d7)

As noted in #37908
This commit is contained in:
Stefan Markovic
2025-03-20 16:20:19 +01:00
committed by GitHub
parent dadd306555
commit 665e957cde
7 changed files with 149 additions and 3 deletions

View File

@@ -0,0 +1,62 @@
// 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.IO;
using System.IO.Abstractions;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Microsoft.PowerToys.Settings.UI.Library
{
public class CmdPalProperties
{
// Default shortcut - Win + Alt + Space
public static readonly HotkeySettings DefaultHotkeyValue = new HotkeySettings(true, false, true, false, 32);
#pragma warning disable SA1401 // Fields should be private
#pragma warning disable CA1051 // Do not declare visible instance fields
public HotkeySettings Hotkey;
#pragma warning restore CA1051 // Do not declare visible instance fields
#pragma warning restore SA1401 // Fields should be private
private string _settingsFilePath;
public CmdPalProperties()
{
var localAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
#if DEBUG
_settingsFilePath = Path.Combine(localAppDataDir, "Packages", "Microsoft.CommandPalette.Dev_8wekyb3d8bbwe", "LocalState", "settings.json");
#else
_settingsFilePath = Path.Combine(localAppDataDir, "Packages", "Microsoft.CommandPalette_8wekyb3d8bbwe", "LocalState", "settings.json");
#endif
InitializeHotkey();
}
public void InitializeHotkey()
{
try
{
string json = File.ReadAllText(_settingsFilePath); // Read JSON file
using JsonDocument doc = JsonDocument.Parse(json);
if (doc.RootElement.TryGetProperty(nameof(Hotkey), out JsonElement hotkeyElement))
{
Hotkey = JsonSerializer.Deserialize<HotkeySettings>(hotkeyElement.GetRawText());
if (Hotkey == null)
{
Hotkey = DefaultHotkeyValue;
}
}
}
catch (Exception)
{
Hotkey = DefaultHotkeyValue;
}
}
}
}

View File

@@ -53,6 +53,27 @@ namespace Microsoft.PowerToys.Settings.UI.Library.Utilities
return sendCustomAction.ToJsonString();
}
public static IFileSystemWatcher GetFileWatcher(string path, Action onChangedCallback, IFileSystem fileSystem = null)
{
fileSystem ??= FileSystem;
var dirPath = Path.GetDirectoryName(path);
if (!fileSystem.Directory.Exists(dirPath))
{
return null;
}
var watcher = fileSystem.FileSystemWatcher.New();
watcher.Path = dirPath;
watcher.Filter = Path.GetFileName(path);
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.EnableRaisingEvents = true;
watcher.Changed += (o, e) => onChangedCallback();
return watcher;
}
public static IFileSystemWatcher GetFileWatcher(string moduleName, string fileName, Action onChangedCallback, IFileSystem fileSystem = null)
{
fileSystem ??= FileSystem;

View File

@@ -25,6 +25,16 @@
IsOpen="{x:Bind ViewModel.IsEnabledGpoConfigured, Mode=OneWay}"
IsTabStop="{x:Bind ViewModel.IsEnabledGpoConfigured, Mode=OneWay}"
Severity="Informational" />
<controls:SettingsGroup x:Uid="CmdPal_Activation_GroupSettings" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
<tkcontrols:SettingsCard x:Uid="CmdPal_ActivationShortcut" HeaderIcon="{ui:FontIcon Glyph=&#xEDA7;}">
<controls:ShortcutControl
MinWidth="{StaticResource SettingActionControlMinWidth}"
HotkeySettings="{x:Bind Path=ViewModel.Hotkey, Mode=OneWay}"
IsEnabled="False" />
</tkcontrols:SettingsCard>
</controls:SettingsGroup>
</StackPanel>
</controls:SettingsPageControl.ModuleContent>

View File

@@ -19,7 +19,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
ViewModel = new CmdPalViewModel(
settingsUtils,
SettingsRepository<GeneralSettings>.GetInstance(settingsUtils),
ShellPage.SendDefaultIPCMessage);
ShellPage.SendDefaultIPCMessage,
DispatcherQueue);
DataContext = ViewModel;
InitializeComponent();
}

View File

@@ -4990,4 +4990,13 @@ To record a specific window, enter the hotkey with the Alt key in the opposite m
<data name="RetryLabel.Text" xml:space="preserve">
<value>Retry</value>
</data>
<data name="CmdPal_Activation_GroupSettings.Header" xml:space="preserve">
<value>Activation</value>
</data>
<data name="CmdPal_ActivationShortcut.Header" xml:space="preserve">
<value>Activation shortcut</value>
</data>
<data name="CmdPal_ActivationShortcut.Description" xml:space="preserve">
<value>Go to Command Palette settings to customize the activation shortcut.</value>
</data>
</root>

View File

@@ -4,15 +4,19 @@
using System;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Reflection;
using System.Text.Json;
using System.Text.RegularExpressions;
using global::PowerToys.GPOWrapper;
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.Utilities;
using Microsoft.PowerToys.Settings.UI.ViewModels.Commands;
using Microsoft.UI.Dispatching;
using Windows.Management.Deployment;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
@@ -21,12 +25,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
private GpoRuleConfigured _enabledGpoRuleConfiguration;
private bool _isEnabled;
private HotkeySettings _hotkey;
private IFileSystemWatcher _watcher;
private DispatcherQueue _uiDispatcherQueue;
private CmdPalProperties _cmdPalProperties;
private GeneralSettings GeneralSettingsConfig { get; set; }
private Func<string, int> SendConfigMSG { get; }
public CmdPalViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc)
public CmdPalViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, DispatcherQueue uiDispatcherQueue)
{
ArgumentNullException.ThrowIfNull(settingsUtils);
@@ -35,8 +43,32 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
GeneralSettingsConfig = settingsRepository.SettingsConfig;
_uiDispatcherQueue = uiDispatcherQueue;
_cmdPalProperties = new CmdPalProperties();
InitializeEnabledValue();
var localAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
#if DEBUG
var settingsPath = Path.Combine(localAppDataDir, "Packages", "Microsoft.CommandPalette.Dev_8wekyb3d8bbwe", "LocalState", "settings.json");
#else
var settingsPath = Path.Combine(localAppDataDir, "Packages", "Microsoft.CommandPalette_8wekyb3d8bbwe", "LocalState", "settings.json");
#endif
_hotkey = _cmdPalProperties.Hotkey;
_watcher = Helper.GetFileWatcher(settingsPath, () =>
{
_cmdPalProperties.InitializeHotkey();
_hotkey = _cmdPalProperties.Hotkey;
_uiDispatcherQueue.TryEnqueue(() =>
{
OnPropertyChanged(nameof(Hotkey));
});
});
// set the callback functions value to handle outgoing IPC message.
SendConfigMSG = ipcMSGCallBackFunc;
}
@@ -82,6 +114,15 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public HotkeySettings Hotkey
{
get => _hotkey;
private set
{
}
}
public bool IsEnabledGpoConfigured { get; private set; }
public void RefreshEnabledState()

View File

@@ -223,9 +223,11 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private ObservableCollection<DashboardModuleItem> GetModuleItemsCmdPal()
{
var hotkey = new CmdPalProperties().Hotkey;
var list = new List<DashboardModuleItem>
{
new DashboardModuleTextItem() { Label = resourceLoader.GetString("CmdPal_ShortDescription") },
new DashboardModuleShortcutItem() { Label = resourceLoader.GetString("CmdPal_ShortDescription"), Shortcut = hotkey.GetKeysList() },
};
return new ObservableCollection<DashboardModuleItem>(list);
}