mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 04:07:40 +02:00
[settings] Show uneditable shortcut in CmdPal page (#38060)
* [settings] Show CmdPal shortcut * show in dashboard as well   As noted in #37908
This commit is contained in:
62
src/settings-ui/Settings.UI.Library/CmdPalProperties.cs
Normal file
62
src/settings-ui/Settings.UI.Library/CmdPalProperties.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -53,6 +53,27 @@ namespace Microsoft.PowerToys.Settings.UI.Library.Utilities
|
|||||||
return sendCustomAction.ToJsonString();
|
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)
|
public static IFileSystemWatcher GetFileWatcher(string moduleName, string fileName, Action onChangedCallback, IFileSystem fileSystem = null)
|
||||||
{
|
{
|
||||||
fileSystem ??= FileSystem;
|
fileSystem ??= FileSystem;
|
||||||
|
|||||||
@@ -25,6 +25,16 @@
|
|||||||
IsOpen="{x:Bind ViewModel.IsEnabledGpoConfigured, Mode=OneWay}"
|
IsOpen="{x:Bind ViewModel.IsEnabledGpoConfigured, Mode=OneWay}"
|
||||||
IsTabStop="{x:Bind ViewModel.IsEnabledGpoConfigured, Mode=OneWay}"
|
IsTabStop="{x:Bind ViewModel.IsEnabledGpoConfigured, Mode=OneWay}"
|
||||||
Severity="Informational" />
|
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=}">
|
||||||
|
<controls:ShortcutControl
|
||||||
|
MinWidth="{StaticResource SettingActionControlMinWidth}"
|
||||||
|
HotkeySettings="{x:Bind Path=ViewModel.Hotkey, Mode=OneWay}"
|
||||||
|
IsEnabled="False" />
|
||||||
|
</tkcontrols:SettingsCard>
|
||||||
|
</controls:SettingsGroup>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</controls:SettingsPageControl.ModuleContent>
|
</controls:SettingsPageControl.ModuleContent>
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
|
|||||||
ViewModel = new CmdPalViewModel(
|
ViewModel = new CmdPalViewModel(
|
||||||
settingsUtils,
|
settingsUtils,
|
||||||
SettingsRepository<GeneralSettings>.GetInstance(settingsUtils),
|
SettingsRepository<GeneralSettings>.GetInstance(settingsUtils),
|
||||||
ShellPage.SendDefaultIPCMessage);
|
ShellPage.SendDefaultIPCMessage,
|
||||||
|
DispatcherQueue);
|
||||||
DataContext = ViewModel;
|
DataContext = ViewModel;
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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">
|
<data name="RetryLabel.Text" xml:space="preserve">
|
||||||
<value>Retry</value>
|
<value>Retry</value>
|
||||||
</data>
|
</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>
|
</root>
|
||||||
@@ -4,15 +4,19 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.IO.Abstractions;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using global::PowerToys.GPOWrapper;
|
using global::PowerToys.GPOWrapper;
|
||||||
using ManagedCommon;
|
using ManagedCommon;
|
||||||
using Microsoft.PowerToys.Settings.UI.Library;
|
using Microsoft.PowerToys.Settings.UI.Library;
|
||||||
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
|
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
|
||||||
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
|
||||||
|
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
|
||||||
using Microsoft.PowerToys.Settings.UI.ViewModels.Commands;
|
using Microsoft.PowerToys.Settings.UI.ViewModels.Commands;
|
||||||
|
using Microsoft.UI.Dispatching;
|
||||||
using Windows.Management.Deployment;
|
using Windows.Management.Deployment;
|
||||||
|
|
||||||
namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
||||||
@@ -21,12 +25,16 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
{
|
{
|
||||||
private GpoRuleConfigured _enabledGpoRuleConfiguration;
|
private GpoRuleConfigured _enabledGpoRuleConfiguration;
|
||||||
private bool _isEnabled;
|
private bool _isEnabled;
|
||||||
|
private HotkeySettings _hotkey;
|
||||||
|
private IFileSystemWatcher _watcher;
|
||||||
|
private DispatcherQueue _uiDispatcherQueue;
|
||||||
|
private CmdPalProperties _cmdPalProperties;
|
||||||
|
|
||||||
private GeneralSettings GeneralSettingsConfig { get; set; }
|
private GeneralSettings GeneralSettingsConfig { get; set; }
|
||||||
|
|
||||||
private Func<string, int> SendConfigMSG { get; }
|
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);
|
ArgumentNullException.ThrowIfNull(settingsUtils);
|
||||||
|
|
||||||
@@ -35,8 +43,32 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
|
|
||||||
GeneralSettingsConfig = settingsRepository.SettingsConfig;
|
GeneralSettingsConfig = settingsRepository.SettingsConfig;
|
||||||
|
|
||||||
|
_uiDispatcherQueue = uiDispatcherQueue;
|
||||||
|
_cmdPalProperties = new CmdPalProperties();
|
||||||
|
|
||||||
InitializeEnabledValue();
|
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.
|
// set the callback functions value to handle outgoing IPC message.
|
||||||
SendConfigMSG = ipcMSGCallBackFunc;
|
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 bool IsEnabledGpoConfigured { get; private set; }
|
||||||
|
|
||||||
public void RefreshEnabledState()
|
public void RefreshEnabledState()
|
||||||
|
|||||||
@@ -223,9 +223,11 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
|
|||||||
|
|
||||||
private ObservableCollection<DashboardModuleItem> GetModuleItemsCmdPal()
|
private ObservableCollection<DashboardModuleItem> GetModuleItemsCmdPal()
|
||||||
{
|
{
|
||||||
|
var hotkey = new CmdPalProperties().Hotkey;
|
||||||
|
|
||||||
var list = new List<DashboardModuleItem>
|
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);
|
return new ObservableCollection<DashboardModuleItem>(list);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user