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:
Bartosz Sosnowski
2019-10-02 17:18:55 +02:00
committed by GitHub
parent de08485db8
commit 64f606daaa
13 changed files with 237 additions and 17 deletions

View File

@@ -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();

View File

@@ -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;
};
}