mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-06-07 09:28:03 +08:00
Add a hotkey settings control and use it in FancyZones (#450)
Add a hotkey settings control and use it in FancyZones
This commit is contained in:
parent
de08485db8
commit
64f606daaa
@ -95,6 +95,20 @@ namespace PowerToysSettings {
|
||||
m_json.as_object()[L"properties"].as_object()[name] = item;
|
||||
}
|
||||
|
||||
void Settings::add_hotkey(const std::wstring& name, UINT description_resource_id, const HotkeyObject& hotkey) {
|
||||
add_hotkey(name, get_resource(description_resource_id), hotkey);
|
||||
}
|
||||
|
||||
void Settings::add_hotkey(const std::wstring& name, const std::wstring& description, const HotkeyObject& hotkey) {
|
||||
web::json::value item = web::json::value::object();
|
||||
item.as_object()[L"display_name"] = web::json::value::string(description);
|
||||
item.as_object()[L"editor_type"] = web::json::value::string(L"hotkey");
|
||||
item.as_object()[L"value"] = hotkey.get_json();
|
||||
item.as_object()[L"order"] = web::json::value::number(++m_curr_priority);
|
||||
|
||||
m_json.as_object()[L"properties"].as_object()[name] = item;
|
||||
}
|
||||
|
||||
// add_custom_action overloads.
|
||||
void Settings::add_custom_action(const std::wstring& name, UINT description_resource_id, UINT button_text_resource_id, UINT ext_description_resource_id) {
|
||||
add_custom_action(name, get_resource(description_resource_id), get_resource(button_text_resource_id), get_resource(ext_description_resource_id));
|
||||
@ -188,6 +202,11 @@ namespace PowerToysSettings {
|
||||
m_json.as_object()[L"properties"].as_object()[name] = add_property_generic(name, value);
|
||||
};
|
||||
|
||||
template <>
|
||||
void PowerToyValues::add_property(const std::wstring& name, HotkeyObject value) {
|
||||
m_json.as_object()[L"properties"].as_object()[name] = add_property_generic(name, value.get_json());
|
||||
};
|
||||
|
||||
bool PowerToyValues::is_bool_value(const std::wstring& property_name) {
|
||||
return m_json.is_object() &&
|
||||
m_json.has_object_field(L"properties") &&
|
||||
@ -209,6 +228,13 @@ namespace PowerToysSettings {
|
||||
m_json[L"properties"][property_name].has_string_field(L"value");
|
||||
}
|
||||
|
||||
bool PowerToyValues::is_object_value(const std::wstring& property_name) {
|
||||
return m_json.is_object() &&
|
||||
m_json.has_object_field(L"properties") &&
|
||||
m_json[L"properties"].has_object_field(property_name) &&
|
||||
m_json[L"properties"][property_name].has_object_field(L"value");
|
||||
}
|
||||
|
||||
bool PowerToyValues::get_bool_value(const std::wstring& property_name) {
|
||||
return m_json[L"properties"][property_name][L"value"].as_bool();
|
||||
}
|
||||
@ -221,6 +247,10 @@ namespace PowerToysSettings {
|
||||
return m_json[L"properties"][property_name][L"value"].as_string();
|
||||
}
|
||||
|
||||
web::json::value PowerToyValues::get_json(const std::wstring& property_name) {
|
||||
return m_json[L"properties"][property_name][L"value"];
|
||||
}
|
||||
|
||||
std::wstring PowerToyValues::serialize() {
|
||||
set_version();
|
||||
return m_json.serialize();
|
||||
|
@ -4,6 +4,8 @@
|
||||
|
||||
namespace PowerToysSettings {
|
||||
|
||||
class HotkeyObject;
|
||||
|
||||
class Settings {
|
||||
public:
|
||||
Settings(
|
||||
@ -32,14 +34,18 @@ namespace PowerToysSettings {
|
||||
void add_color_picker(const std::wstring& name, UINT description_resource_id, const std::wstring& value);
|
||||
void add_color_picker(const std::wstring& name, const std::wstring& description, const std::wstring& value);
|
||||
|
||||
void add_hotkey(const std::wstring& name, UINT description_resource_id, const HotkeyObject& hotkey);
|
||||
void add_hotkey(const std::wstring& name, const std::wstring& description, const HotkeyObject& hotkey);
|
||||
|
||||
void add_custom_action(const std::wstring& name, UINT description_resource_id, UINT button_text_resource_id, UINT ext_description_resource_id);
|
||||
void add_custom_action(const std::wstring& name, UINT description_resource_id, UINT button_text_resource_id, const std::wstring& value);
|
||||
void add_custom_action(const std::wstring& name, const std::wstring& description, const std::wstring& button_text, const std::wstring& value);
|
||||
|
||||
|
||||
// Serialize the internal json to a string.
|
||||
std::wstring serialize();
|
||||
// Serialize the internal json to the input buffer.
|
||||
bool serialize_to_buffer(wchar_t* buffer, int *buffer_size);
|
||||
bool serialize_to_buffer(wchar_t* buffer, int* buffer_size);
|
||||
|
||||
private:
|
||||
web::json::value m_json;
|
||||
@ -62,11 +68,13 @@ namespace PowerToysSettings {
|
||||
bool is_bool_value(const std::wstring& property_name);
|
||||
bool is_int_value(const std::wstring& property_name);
|
||||
bool is_string_value(const std::wstring& property_name);
|
||||
bool is_object_value(const std::wstring& property_name);
|
||||
|
||||
// Get property value
|
||||
bool get_bool_value(const std::wstring& property_name);
|
||||
int get_int_value(const std::wstring& property_name);
|
||||
std::wstring get_string_value(const std::wstring& property_name);
|
||||
web::json::value get_json(const std::wstring& property_name);
|
||||
|
||||
std::wstring serialize();
|
||||
void save_to_settings_file();
|
||||
@ -93,4 +101,46 @@ namespace PowerToysSettings {
|
||||
CustomActionObject(web::json::value action_json) : m_json(action_json) {};
|
||||
web::json::value m_json;
|
||||
};
|
||||
|
||||
class HotkeyObject {
|
||||
public:
|
||||
static HotkeyObject from_json(web::json::value json) {
|
||||
return HotkeyObject(json);
|
||||
}
|
||||
static HotkeyObject from_json_string(const std::wstring& json) {
|
||||
web::json::value parsed_json = web::json::value::parse(json);
|
||||
return HotkeyObject(parsed_json);
|
||||
}
|
||||
static HotkeyObject from_settings(bool win_pressed, bool ctrl_pressed, bool alt_pressed, bool shift_pressed, UINT vk_code, const std::wstring& key) {
|
||||
web::json::value json = web::json::value::object();
|
||||
json.as_object()[L"win"] = web::json::value::boolean(win_pressed);
|
||||
json.as_object()[L"ctrl"] = web::json::value::boolean(ctrl_pressed);
|
||||
json.as_object()[L"alt"] = web::json::value::boolean(alt_pressed);
|
||||
json.as_object()[L"shift"] = web::json::value::boolean(shift_pressed);
|
||||
json.as_object()[L"code"] = web::json::value::number(vk_code);
|
||||
json.as_object()[L"key"] = web::json::value::string(key);
|
||||
return HotkeyObject(json);
|
||||
}
|
||||
const web::json::value& get_json() const { return m_json; }
|
||||
|
||||
std::wstring get_key() { return m_json[L"key"].as_string(); }
|
||||
UINT get_code() { return m_json[L"code"].as_integer(); }
|
||||
bool win_pressed() { return m_json[L"win"].as_bool(); }
|
||||
bool ctrl_pressed() { return m_json[L"ctrl"].as_bool(); }
|
||||
bool alt_pressed() { return m_json[L"alt"].as_bool(); }
|
||||
bool shift_pressed() { return m_json[L"shift"].as_bool(); }
|
||||
UINT get_modifiers_repeat() {
|
||||
return (win_pressed() ? MOD_WIN : 0) |
|
||||
(ctrl_pressed() ? MOD_CONTROL : 0) |
|
||||
(alt_pressed() ? MOD_ALT : 0) |
|
||||
(shift_pressed() ? MOD_SHIFT : 0);
|
||||
}
|
||||
UINT get_modifiers() {
|
||||
return get_modifiers_repeat() | MOD_NOREPEAT;
|
||||
}
|
||||
protected:
|
||||
HotkeyObject(web::json::value hotkey_json) : m_json(hotkey_json) {};
|
||||
web::json::value m_json;
|
||||
};
|
||||
|
||||
}
|
||||
|
2
src/editor/settings-html/dist/bundle.js
vendored
2
src/editor/settings-html/dist/bundle.js
vendored
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -24,6 +24,7 @@ public:
|
||||
IFACEMETHODIMP_(void) WindowCreated(HWND window) noexcept;
|
||||
IFACEMETHODIMP_(bool) OnKeyDown(PKBDLLHOOKSTRUCT info) noexcept;
|
||||
IFACEMETHODIMP_(void) ToggleEditor() noexcept;
|
||||
IFACEMETHODIMP_(void) SettingsChanged() noexcept;
|
||||
|
||||
// IZoneWindowHost
|
||||
IFACEMETHODIMP_(void) ToggleZoneViewers() noexcept;
|
||||
@ -117,7 +118,7 @@ IFACEMETHODIMP_(void) FancyZones::Run() noexcept
|
||||
m_window = CreateWindowExW(WS_EX_TOOLWINDOW, L"SuperFancyZones", L"", WS_POPUP, 0, 0, 0, 0, nullptr, nullptr, m_hinstance, this);
|
||||
if (!m_window) return;
|
||||
|
||||
RegisterHotKey(m_window, 1, MOD_WIN, VK_OEM_3);
|
||||
RegisterHotKey(m_window, 1, m_settings->GetSettings().editorHotkey.get_modifiers(), m_settings->GetSettings().editorHotkey.get_code());
|
||||
VirtualDesktopChanged();
|
||||
}
|
||||
|
||||
@ -328,6 +329,13 @@ void FancyZones::ToggleEditor() noexcept
|
||||
waitForEditorThread.detach();
|
||||
}
|
||||
|
||||
void FancyZones::SettingsChanged() noexcept
|
||||
{
|
||||
// Update the hotkey
|
||||
UnregisterHotKey(m_window, 1);
|
||||
RegisterHotKey(m_window, 1, m_settings->GetSettings().editorHotkey.get_modifiers(), m_settings->GetSettings().editorHotkey.get_code());
|
||||
}
|
||||
|
||||
// IZoneWindowHost
|
||||
IFACEMETHODIMP_(void) FancyZones::ToggleZoneViewers() noexcept
|
||||
{
|
||||
|
Binary file not shown.
@ -52,7 +52,7 @@ private:
|
||||
} m_configStrings[1] = {
|
||||
{ L"fancyzones_zoneHighlightColor", &m_settings.zoneHightlightColor, IDS_SETTING_DESCRIPTION_ZONEHIGHLIGHTCOLOR },
|
||||
};
|
||||
|
||||
const std::wstring m_editor_hotkey_name = L"fancyzones_editor_hotkey";
|
||||
};
|
||||
|
||||
IFACEMETHODIMP_(bool) FancyZonesSettings::GetConfig(_Out_ PWSTR buffer, _Out_ int *buffer_size) noexcept
|
||||
@ -73,6 +73,7 @@ IFACEMETHODIMP_(bool) FancyZonesSettings::GetConfig(_Out_ PWSTR buffer, _Out_ in
|
||||
IDS_SETTING_LAUNCH_EDITOR_BUTTON,
|
||||
IDS_SETTING_LAUNCH_EDITOR_DESCRIPTION
|
||||
);
|
||||
settings.add_hotkey(m_editor_hotkey_name, IDS_SETTING_LAUNCH_EDITOR_HOTKEY_LABEL, m_settings.editorHotkey);
|
||||
|
||||
for (auto const& setting : m_configBools)
|
||||
{
|
||||
@ -91,6 +92,7 @@ IFACEMETHODIMP_(void) FancyZonesSettings::SetConfig(PCWSTR config) noexcept try
|
||||
{
|
||||
LoadSettings(config, false /*fromFile*/);
|
||||
SaveSettings();
|
||||
m_callback->SettingsChanged();
|
||||
Trace::SettingsChanged(m_settings);
|
||||
}
|
||||
CATCH_LOG();
|
||||
@ -129,6 +131,11 @@ void FancyZonesSettings::LoadSettings(PCWSTR config, bool fromFile) noexcept try
|
||||
*setting.value = values.get_string_value(setting.name);
|
||||
}
|
||||
}
|
||||
|
||||
if (values.is_object_value(m_editor_hotkey_name))
|
||||
{
|
||||
m_settings.editorHotkey = PowerToysSettings::HotkeyObject::from_json(values.get_json(m_editor_hotkey_name));
|
||||
}
|
||||
}
|
||||
CATCH_LOG();
|
||||
|
||||
@ -146,6 +153,8 @@ void FancyZonesSettings::SaveSettings() noexcept try
|
||||
values.add_property(setting.name, *setting.value);
|
||||
}
|
||||
|
||||
values.add_property(m_editor_hotkey_name, m_settings.editorHotkey);
|
||||
|
||||
values.save_to_settings_file();
|
||||
}
|
||||
CATCH_LOG();
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define ZONE_STAMP L"FancyZones_zone"
|
||||
#include <common/settings_objects.h>
|
||||
|
||||
struct Settings
|
||||
{
|
||||
@ -15,6 +16,7 @@ struct Settings
|
||||
bool use_standalone_editor = true;
|
||||
bool use_cursorpos_editor_startupscreen = true;
|
||||
std::wstring zoneHightlightColor = L"#0078D7";
|
||||
PowerToysSettings::HotkeyObject editorHotkey = PowerToysSettings::HotkeyObject::from_settings(true, false, false, false, VK_OEM_3, L"~");
|
||||
};
|
||||
|
||||
interface __declspec(uuid("{BA4E77C4-6F44-4C5D-93D3-CBDE880495C2}")) IFancyZonesSettings : public IUnknown
|
||||
|
@ -17,7 +17,8 @@
|
||||
|