Skip to content

Commit

Permalink
Add dropdown settings UI element
Browse files Browse the repository at this point in the history
  • Loading branch information
bzoz committed Oct 4, 2019
1 parent f1c2a66 commit d96db2a
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/common/settings_objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ namespace PowerToysSettings {
}
add_choice_group(name, get_resource(description_resource_id), value, keys_and_texts);
}

void Settings::add_choice_group(const std::wstring& name, const std::wstring& description, const std::wstring& value, const std::vector<std::pair<std::wstring, std::wstring>>& keys_and_texts) {
web::json::value item = web::json::value::object();
item.as_object()[L"display_name"] = web::json::value::string(description);
Expand All @@ -135,6 +136,33 @@ namespace PowerToysSettings {
m_json.as_object()[L"properties"].as_object()[name] = item;
}

void Settings::add_dropdown(const std::wstring& name, UINT description_resource_id, const std::wstring& value, const std::vector<std::pair<std::wstring, UINT>>& keys_and_text_ids) {
std::vector<std::pair<std::wstring, std::wstring>> keys_and_texts;
keys_and_texts.reserve(keys_and_text_ids.size());
for (const auto& kv : keys_and_text_ids) {
keys_and_texts.emplace_back(kv.first, get_resource(kv.second));
}
add_dropdown(name, get_resource(description_resource_id), value, keys_and_texts);
}

void Settings::add_dropdown(const std::wstring& name, const std::wstring& description, const std::wstring& value, const std::vector<std::pair<std::wstring, std::wstring>>& keys_and_texts) {
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"dropdown");
auto options = web::json::value::array(keys_and_texts.size());
for (std::size_t i = 0; i < keys_and_texts.size(); ++i) {
auto entry = web::json::value::object();
entry.as_object()[L"key"] = web::json::value::string(keys_and_texts[i].first);
entry.as_object()[L"text"] = web::json::value::string(keys_and_texts[i].second);
options.as_array()[i] = entry;
}
item.as_object()[L"options"] = options;
item.as_object()[L"value"] = web::json::value::string(value);
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));
Expand Down
3 changes: 3 additions & 0 deletions src/common/settings_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ namespace PowerToysSettings {
void add_choice_group(const std::wstring& name, UINT description_resource_id, const std::wstring& value, const std::vector<std::pair<std::wstring, UINT>>& keys_and_text_ids);
void add_choice_group(const std::wstring& name, const std::wstring& description, const std::wstring& value, const std::vector<std::pair<std::wstring, std::wstring>>& keys_and_texts);

void add_dropdown(const std::wstring& name, UINT description_resource_id, const std::wstring& value, const std::vector<std::pair<std::wstring, UINT>>& keys_and_text_ids);
void add_dropdown(const std::wstring& name, const std::wstring& description, const std::wstring& value, const std::vector<std::pair<std::wstring, std::wstring>>& keys_and_texts);

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);
Expand Down

0 comments on commit d96db2a

Please sign in to comment.