Files
PowerToys/src/settings-ui/Settings.UI.Library/SettingsFactory.cs
Shawn Yuan 9086995eeb Settings Flyout improvement (#43840)
<!-- 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
This pull request introduces the new Quick Access feature to PowerToys
by integrating its host process management into the runner and system
tray. The changes add the Quick Access host implementation, update
project and build files to include it, and modify the runner and tray
icon logic to launch and interact with the Quick Access UI.

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

- [x] Closes: #43694
<!-- - [ ] Closes: #yyy (add separate lines for additional resolved
issues) -->
- [x] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [x] **Tests:** Added/updated and all pass
- [ ] **Localization:** All end-user-facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [ ] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here: #xxx

<!-- Provide a more detailed description of the PR, other things fixed,
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments
<img width="290" height="420" alt="image"
src="https://github.com/user-attachments/assets/7390a706-171c-479f-a4a2-999b18cfc65f"
/>

<img width="290" height="420" alt="image"
src="https://github.com/user-attachments/assets/99e99bc9-b1a3-46c6-b648-81e3048dec1b"
/>

<img width="490" height="350" alt="image"
src="https://github.com/user-attachments/assets/2cce4ad6-a54e-4587-87b7-fdc7fba1f54f"
/>

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed

---------

Signed-off-by: Shawn Yuan (from Dev Box) <shuaiyuan@microsoft.com>
Signed-off-by: Shuai Yuan <shuai.yuan.zju@gmail.com>
Co-authored-by: Niels Laute <niels.laute@live.nl>
2026-01-07 16:38:09 +08:00

209 lines
8.2 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.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
namespace Microsoft.PowerToys.Settings.UI.Services
{
/// <summary>
/// Factory service for getting PowerToys module Settings that implement IHotkeyConfig
/// </summary>
public class SettingsFactory
{
private readonly SettingsUtils _settingsUtils;
private readonly Dictionary<string, Type> _settingsTypes;
public SettingsFactory(SettingsUtils settingsUtils)
{
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
_settingsTypes = DiscoverSettingsTypes();
}
/// <summary>
/// Dynamically discovers all Settings types that implement IHotkeyConfig
/// </summary>
private Dictionary<string, Type> DiscoverSettingsTypes()
{
var settingsTypes = new Dictionary<string, Type>();
// Get the Settings.UI.Library assembly
var assembly = Assembly.GetAssembly(typeof(IHotkeyConfig));
if (assembly == null)
{
return settingsTypes;
}
try
{
// Find all types that implement IHotkeyConfig and ISettingsConfig
var hotkeyConfigTypes = assembly.GetTypes()
.Where(type =>
type.IsClass &&
!type.IsAbstract &&
typeof(IHotkeyConfig).IsAssignableFrom(type) &&
typeof(ISettingsConfig).IsAssignableFrom(type))
.ToList();
foreach (var type in hotkeyConfigTypes)
{
// Try to get the ModuleName using SettingsRepository
try
{
var repositoryType = typeof(SettingsRepository<>).MakeGenericType(type);
var getInstanceMethod = repositoryType.GetMethod("GetInstance", BindingFlags.Public | BindingFlags.Static);
var repository = getInstanceMethod?.Invoke(null, new object[] { _settingsUtils });
if (repository != null)
{
var settingsConfigProperty = repository.GetType().GetProperty("SettingsConfig");
var settingsInstance = settingsConfigProperty?.GetValue(repository) as ISettingsConfig;
if (settingsInstance != null)
{
var moduleName = settingsInstance.GetModuleName();
if (string.IsNullOrEmpty(moduleName) && type == typeof(GeneralSettings))
{
moduleName = "GeneralSettings";
}
if (!string.IsNullOrEmpty(moduleName))
{
settingsTypes[moduleName] = type;
System.Diagnostics.Debug.WriteLine($"Discovered settings type: {type.Name} for module: {moduleName}");
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error getting module name for {type.Name}: {ex.Message}");
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error scanning assembly {assembly.FullName}: {ex.Message}");
}
return settingsTypes;
}
public IHotkeyConfig GetFreshSettings(string moduleKey)
{
if (!_settingsTypes.TryGetValue(moduleKey, out var settingsType))
{
return null;
}
try
{
// Create a generic method call to _settingsUtils.GetSettingsOrDefault<T>(moduleKey)
var getSettingsMethod = typeof(SettingsUtils).GetMethod("GetSettingsOrDefault", new[] { typeof(string), typeof(string) });
var genericMethod = getSettingsMethod?.MakeGenericMethod(settingsType);
// Call GetSettingsOrDefault<T>(moduleKey) to get fresh settings from file
string actualModuleKey = moduleKey;
if (moduleKey == "GeneralSettings")
{
actualModuleKey = string.Empty;
}
var freshSettings = genericMethod?.Invoke(_settingsUtils, new object[] { actualModuleKey, "settings.json" });
return freshSettings as IHotkeyConfig;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error getting fresh settings for {moduleKey}: {ex.Message}");
return null;
}
}
/// <summary>
/// Gets a settings instance for the specified module using SettingsRepository
/// </summary>
/// <param name="moduleKey">The module key/name</param>
/// <returns>The settings instance implementing IHotkeyConfig, or null if not found</returns>
public IHotkeyConfig GetSettings(string moduleKey)
{
if (!_settingsTypes.TryGetValue(moduleKey, out var settingsType))
{
return null;
}
try
{
var repositoryType = typeof(SettingsRepository<>).MakeGenericType(settingsType);
var getInstanceMethod = repositoryType.GetMethod("GetInstance", BindingFlags.Public | BindingFlags.Static);
var repository = getInstanceMethod?.Invoke(null, new object[] { _settingsUtils });
if (repository != null)
{
var settingsConfigProperty = repository.GetType().GetProperty("SettingsConfig");
return settingsConfigProperty?.GetValue(repository) as IHotkeyConfig;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error getting Settings for {moduleKey}: {ex.Message}");
}
return null;
}
/// <summary>
/// Gets all available module names that have settings implementing IHotkeyConfig
/// </summary>
/// <returns>List of module names</returns>
public List<string> GetAvailableModuleNames()
{
return _settingsTypes.Keys.ToList();
}
/// <summary>
/// Gets all available settings that implement IHotkeyConfig
/// </summary>
/// <returns>Dictionary of module name to settings instance</returns>
public Dictionary<string, IHotkeyConfig> GetAllHotkeySettings()
{
var result = new Dictionary<string, IHotkeyConfig>();
foreach (var moduleKey in _settingsTypes.Keys)
{
try
{
var settings = GetSettings(moduleKey);
if (settings != null)
{
result[moduleKey] = settings;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error getting settings for {moduleKey}: {ex.Message}");
}
}
return result;
}
/// <summary>
/// Gets a specific settings repository instance
/// </summary>
/// <typeparam name="T">The settings type</typeparam>
/// <returns>The settings repository instance</returns>
public ISettingsRepository<T> GetRepository<T>()
where T : class, ISettingsConfig, new()
{
return SettingsRepository<T>.GetInstance(_settingsUtils);
}
}
}