// 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. #pragma once #include #include #include #include /// /// Utility class for discovering and loading PowerToy module settings /// class SettingsLoader { public: SettingsLoader(); ~SettingsLoader(); /// /// Load settings for a PowerToy module /// /// Name of the module (e.g., "CursorWrap") /// Full path to the module DLL (for checking local settings.json) /// JSON settings string, or empty string if not found std::wstring LoadSettings(const std::wstring& moduleName, const std::wstring& moduleDllPath); /// /// Get the settings file path for a module /// /// Name of the module /// Full path to the settings.json file std::wstring GetSettingsPath(const std::wstring& moduleName) const; /// /// Display settings information for a module /// /// Name of the module /// Path to the module DLL void DisplaySettingsInfo(const std::wstring& moduleName, const std::wstring& moduleDllPath); /// /// Get a specific setting value /// /// Name of the module /// Path to the module DLL /// Setting key to retrieve /// Value as string, or empty if not found std::wstring GetSettingValue(const std::wstring& moduleName, const std::wstring& moduleDllPath, const std::wstring& key); /// /// Set a specific setting value /// /// Name of the module /// Path to the module DLL /// Setting key to set /// Value to set /// True if successful bool SetSettingValue(const std::wstring& moduleName, const std::wstring& moduleDllPath, const std::wstring& key, const std::wstring& value); /// /// Find the actual settings file path (handles case-insensitivity) /// /// Name of the module /// Path to the module DLL /// Actual path to settings.json, or empty if not found std::wstring FindSettingsFilePath(const std::wstring& moduleName, const std::wstring& moduleDllPath); private: /// /// Get the PowerToys root settings directory /// /// Path to %LOCALAPPDATA%\Microsoft\PowerToys std::wstring GetPowerToysSettingsRoot() const; /// /// Read a text file into a string /// /// Path to the file /// File contents as a string std::wstring ReadFileContents(const std::wstring& filePath) const; /// /// Write a string to a text file /// /// Path to the file /// Contents to write /// True if successful bool WriteFileContents(const std::wstring& filePath, const std::wstring& contents) const; /// /// Parse settings properties from JSON and display them /// /// JSON string containing settings /// Indentation level void DisplayJsonProperties(const std::wstring& settingsJson, int indent = 0); /// /// Parse a hotkey object from JSON and format it as a string (e.g., "Win+Alt+U") /// /// JSON string /// Start position of the hotkey object /// Output: end position of the hotkey object /// Formatted hotkey string, or empty if not a valid hotkey std::string ParseHotkeyObject(const std::string& json, size_t objStart, size_t& objEnd); /// /// Check if a JSON object appears to be a hotkey settings object /// /// JSON string /// Start position of the object /// True if this looks like a hotkey object bool IsHotkeyObject(const std::string& json, size_t objStart); /// /// Prompt user for yes/no confirmation /// /// The question to ask /// True if user answered yes bool PromptYesNo(const std::wstring& prompt); /// /// Add a new property to the JSON settings file /// /// The JSON string to modify /// The property key to add /// The value to set /// Modified JSON string, or empty if failed std::string AddNewProperty(const std::string& json, const std::string& key, const std::string& value); };