Skip to content

Commit

Permalink
Add complex_modifications_rule.enabled_
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Aug 4, 2024
1 parent 805bb83 commit f517558
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/core/grabber/include/grabber/device_grabber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,10 @@ class device_grabber final : public pqrs::dispatcher::extra::dispatcher_client {
complex_modifications_manipulator_manager_->invalidate_manipulators();

for (const auto& rule : core_configuration_->get_selected_profile().get_complex_modifications()->get_rules()) {
if (!rule.get_enabled()) {
continue;
}

for (const auto& manipulator : rule->get_manipulators()) {
try {
auto m = manipulator::manipulator_factory::make_manipulator(manipulator->to_json(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ class complex_modifications_rule final {
gsl::not_null<std::shared_ptr<const core_configuration::details::complex_modifications_parameters>> parameters,
error_handling error_handling)
: json_(json) {
helper_values_.push_back_value<bool>("enabled",
enabled_,
true);

pqrs::json::requires_object(json, "json");

helper_values_.update_value(json, error_handling);

for (const auto& [key, value] : json.items()) {
if (key == "manipulators") {
pqrs::json::requires_array(value, "`" + key + "`");
Expand Down Expand Up @@ -131,21 +137,35 @@ class complex_modifications_rule final {
}

nlohmann::json to_json(void) const {
return json_;
auto j = json_;

helper_values_.update_json(j);

return j;
}

const std::vector<gsl::not_null<std::shared_ptr<manipulator>>>& get_manipulators(void) const {
return manipulators_;
}

const bool& get_enabled(void) const {
return enabled_;
}

void set_enabled(bool value) {
enabled_ = value;
}

const std::string& get_description(void) const {
return description_;
}

private:
nlohmann::json json_;
std::vector<gsl::not_null<std::shared_ptr<manipulator>>> manipulators_;
bool enabled_;
std::string description_;
configuration_json_helper::helper_values helper_values_;
};
} // namespace details
} // namespace core_configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "core_configuration/core_configuration.hpp"
#include <boost/ut.hpp>

void run_complex_modifications_rule_test(void) {
using namespace boost::ut;
using namespace boost::ut::literals;
using namespace std::string_literals;

"complex_modifications_rule"_test = [] {
// `parameters` in `manipulators` are not omitted in to_json.
{
auto json = R"(
{
"description": "example",
"manipulators": [
{
"from": { "key_code": "f12" },
"to": [{ "key_code": "mission_control" }],
"type": "basic",
"parameters": {
"basic.simultaneous_threshold_milliseconds": 50
}
}
]
}
)"_json;
auto parameters = std::make_shared<krbn::core_configuration::details::complex_modifications_parameters>();
krbn::core_configuration::details::complex_modifications_rule rule(json,
parameters,
krbn::core_configuration::error_handling::strict);
expect(1 == rule.get_manipulators().size());
expect(rule.get_enabled());
expect("example"s == rule.get_description());
expect(json == rule.to_json());
}

// enabled
{
auto json = R"(
{
"description": "enabled",
"enabled": false,
"manipulators": [
{
"from": { "key_code": "f12" },
"to": [{ "key_code": "mission_control" }],
"type": "basic"
}
]
}
)"_json;

auto parameters = std::make_shared<krbn::core_configuration::details::complex_modifications_parameters>();
krbn::core_configuration::details::complex_modifications_rule rule(json,
parameters,
krbn::core_configuration::error_handling::strict);
expect(1 == rule.get_manipulators().size());
expect(!rule.get_enabled());
expect("enabled"s == rule.get_description());
expect(json == rule.to_json());

rule.set_enabled(true);
expect(rule.get_enabled());

auto expected_json = R"(
{
"description": "enabled",
"manipulators": [
{
"from": { "key_code": "f12" },
"to": [{ "key_code": "mission_control" }],
"type": "basic"
}
]
}
)"_json;

expect(expected_json == rule.to_json());
}
};
}
2 changes: 2 additions & 0 deletions tests/src/core_configuration/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "complex_modifications_rule_test.hpp"
#include "configuration_json_helper_test.hpp"
#include "core_configuration_test.hpp"
#include "device_test.hpp"
Expand All @@ -6,6 +7,7 @@
#include "machine_specific_test.hpp"

int main(void) {
run_complex_modifications_rule_test();
run_configuration_json_helper_test();
run_core_configuration_test();
run_device_test();
Expand Down

0 comments on commit f517558

Please sign in to comment.