-
-
Notifications
You must be signed in to change notification settings - Fork 850
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#pragma once | ||
|
||
class scoped_modifier_flag final { | ||
public: | ||
scoped_modifier_flag(modifier_flag_manager& modifier_flag_manager, | ||
std::set<modifier_flag> modifier_flags) | ||
: modifier_flag_manager_(modifier_flag_manager) { | ||
for (const auto& m : { | ||
modifier_flag::caps_lock, | ||
modifier_flag::left_control, | ||
modifier_flag::left_shift, | ||
modifier_flag::left_option, | ||
modifier_flag::left_command, | ||
modifier_flag::right_control, | ||
modifier_flag::right_shift, | ||
modifier_flag::right_option, | ||
modifier_flag::right_command, | ||
modifier_flag::fn, | ||
}) { | ||
if (modifier_flags.contains(m)) { | ||
while (!modifier_flag_manager_.is_pressed(m)) { | ||
active_modifier_flag f(active_modifier_flag::type::increase, m, device_id(0)); | ||
modifier_flag_manager_.push_back_active_modifier_flag(f); | ||
scoped_active_modifier_flags_.push_back(f); | ||
} | ||
} else { | ||
if (modifier_flag_manager_.is_pressed(m)) { | ||
auto copy = modifier_flag_manager_.get_active_modifier_flags(); | ||
for (const auto& f : copy) { | ||
if (f.get_modifier_flag() == m) { | ||
active_modifier_flag inverse(f.get_inverse_type(), f.get_modifier_flag(), device_id(0)); | ||
modifier_flag_manager_.push_back_active_modifier_flag(inverse); | ||
scoped_active_modifier_flags_.push_back(inverse); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
~scoped_modifier_flag(void) { | ||
for (const auto& f : get_inverse_active_modifier_flags()) { | ||
modifier_flag_manager_.push_back_active_modifier_flag(f); | ||
} | ||
} | ||
|
||
const std::vector<active_modifier_flag>& get_scoped_active_modifier_flags(void) const { | ||
return scoped_active_modifier_flags_; | ||
} | ||
|
||
std::vector<active_modifier_flag> get_inverse_active_modifier_flags(void) const { | ||
std::vector<active_modifier_flag> flags; | ||
|
||
for (const auto& f : scoped_active_modifier_flags_) { | ||
flags.push_back(active_modifier_flag(f.get_inverse_type(), f.get_modifier_flag(), f.get_device_id())); | ||
} | ||
|
||
return flags; | ||
} | ||
|
||
private: | ||
modifier_flag_manager& modifier_flag_manager_; | ||
std::vector<active_modifier_flag> scoped_active_modifier_flags_; | ||
}; |
181 changes: 181 additions & 0 deletions
181
tests/src/modifier_flag_manager/src/scoped_modifier_flags_test.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
#pragma once | ||
|
||
#include "modifier_flag_manager.hpp" | ||
#include <boost/ut.hpp> | ||
|
||
void run_scoped_modifier_flags_test(void) { | ||
using namespace boost::ut; | ||
using namespace boost::ut::literals; | ||
|
||
"modifier_flag_manager::scoped_modifier_flags"_test = [] { | ||
using namespace krbn; | ||
typedef modifier_flag_manager::active_modifier_flag active_modifier_flag; | ||
typedef modifier_flag_manager::scoped_modifier_flag scoped_modifier_flag; | ||
|
||
{ | ||
modifier_flag_manager modifier_flag_manager; | ||
|
||
modifier_flag_manager.push_back_active_modifier_flag(active_modifier_flag(active_modifier_flag::type::increase, modifier_flag::left_command, device_id(1))); | ||
modifier_flag_manager.push_back_active_modifier_flag(active_modifier_flag(active_modifier_flag::type::increase_lock, modifier_flag::left_command, device_id(1))); | ||
modifier_flag_manager.push_back_active_modifier_flag(active_modifier_flag(active_modifier_flag::type::increase_sticky, modifier_flag::left_command, device_id(1))); | ||
modifier_flag_manager.push_back_active_modifier_flag(active_modifier_flag(active_modifier_flag::type::decrease, modifier_flag::left_option, device_id(1))); | ||
modifier_flag_manager.push_back_active_modifier_flag(active_modifier_flag(active_modifier_flag::type::decrease_lock, modifier_flag::left_option, device_id(1))); | ||
modifier_flag_manager.push_back_active_modifier_flag(active_modifier_flag(active_modifier_flag::type::decrease_sticky, modifier_flag::left_option, device_id(1))); | ||
modifier_flag_manager.push_back_active_modifier_flag(active_modifier_flag(active_modifier_flag::type::increase_led_lock, modifier_flag::caps_lock, device_id(1))); | ||
|
||
auto active_modifier_flags = modifier_flag_manager.get_active_modifier_flags(); | ||
|
||
expect(std::set<modifier_flag>({ | ||
modifier_flag::caps_lock, | ||
modifier_flag::left_command, | ||
}) == modifier_flag_manager.make_modifier_flags()); | ||
|
||
{ | ||
scoped_modifier_flag scoped_modifier_flag(modifier_flag_manager, std::set<modifier_flag>{ | ||
modifier_flag::left_shift, | ||
}); | ||
|
||
expect(std::set<modifier_flag>({ | ||
modifier_flag::left_shift, | ||
}) == modifier_flag_manager.make_modifier_flags()); | ||
|
||
std::cout << std::endl | ||
<< scoped_modifier_flag.get_scoped_active_modifier_flags() | ||
<< std::endl; | ||
|
||
expect(std::vector<active_modifier_flag>({ | ||
active_modifier_flag(active_modifier_flag::type::decrease_led_lock, modifier_flag::caps_lock, device_id(0)), | ||
active_modifier_flag(active_modifier_flag::type::increase, modifier_flag::left_shift, device_id(0)), | ||
active_modifier_flag(active_modifier_flag::type::decrease, modifier_flag::left_command, device_id(0)), | ||
active_modifier_flag(active_modifier_flag::type::decrease_lock, modifier_flag::left_command, device_id(0)), | ||
active_modifier_flag(active_modifier_flag::type::decrease_sticky, modifier_flag::left_command, device_id(0)), | ||
}) == scoped_modifier_flag.get_scoped_active_modifier_flags()); | ||
} | ||
|
||
expect(active_modifier_flags == modifier_flag_manager.get_active_modifier_flags()); | ||
|
||
{ | ||
scoped_modifier_flag scoped_modifier_flag(modifier_flag_manager, std::set<modifier_flag>{ | ||
modifier_flag::left_option, | ||
}); | ||
|
||
expect(std::set<modifier_flag>({ | ||
modifier_flag::left_option, | ||
}) == modifier_flag_manager.make_modifier_flags()); | ||
|
||
std::cout << std::endl | ||
<< scoped_modifier_flag.get_scoped_active_modifier_flags() | ||
<< std::endl; | ||
|
||
expect(std::vector<active_modifier_flag>({ | ||
active_modifier_flag(active_modifier_flag::type::decrease_led_lock, modifier_flag::caps_lock, device_id(0)), | ||
active_modifier_flag(active_modifier_flag::type::increase, modifier_flag::left_option, device_id(0)), | ||
active_modifier_flag(active_modifier_flag::type::increase, modifier_flag::left_option, device_id(0)), | ||
active_modifier_flag(active_modifier_flag::type::increase, modifier_flag::left_option, device_id(0)), | ||
active_modifier_flag(active_modifier_flag::type::increase, modifier_flag::left_option, device_id(0)), | ||
active_modifier_flag(active_modifier_flag::type::decrease, modifier_flag::left_command, device_id(0)), | ||
active_modifier_flag(active_modifier_flag::type::decrease_lock, modifier_flag::left_command, device_id(0)), | ||
active_modifier_flag(active_modifier_flag::type::decrease_sticky, modifier_flag::left_command, device_id(0)), | ||
}) == scoped_modifier_flag.get_scoped_active_modifier_flags()); | ||
} | ||
|
||
expect(active_modifier_flags == modifier_flag_manager.get_active_modifier_flags()); | ||
|
||
{ | ||
scoped_modifier_flag scoped_modifier_flag(modifier_flag_manager, std::set<modifier_flag>{ | ||
modifier_flag::left_command, | ||
}); | ||
|
||
expect(std::set<modifier_flag>({ | ||
modifier_flag::left_command, | ||
}) == modifier_flag_manager.make_modifier_flags()); | ||
|
||
std::cout << std::endl | ||
<< scoped_modifier_flag.get_scoped_active_modifier_flags() | ||
<< std::endl; | ||
|
||
expect(std::vector<active_modifier_flag>({ | ||
active_modifier_flag(active_modifier_flag::type::decrease_led_lock, modifier_flag::caps_lock, device_id(0)), | ||
}) == scoped_modifier_flag.get_scoped_active_modifier_flags()); | ||
} | ||
|
||
expect(active_modifier_flags == modifier_flag_manager.get_active_modifier_flags()); | ||
} | ||
|
||
// | ||
// Tests for type::decrease_led_lock | ||
// | ||
|
||
{ | ||
modifier_flag_manager modifier_flag_manager; | ||
|
||
modifier_flag_manager.push_back_active_modifier_flag(active_modifier_flag(active_modifier_flag::type::decrease_led_lock, modifier_flag::caps_lock, device_id(1))); | ||
|
||
auto active_modifier_flags = modifier_flag_manager.get_active_modifier_flags(); | ||
|
||
expect(std::set<modifier_flag>({}) == modifier_flag_manager.make_modifier_flags()); | ||
|
||
{ | ||
scoped_modifier_flag scoped_modifier_flag(modifier_flag_manager, std::set<modifier_flag>{}); | ||
|
||
expect(std::set<modifier_flag>({}) == modifier_flag_manager.make_modifier_flags()); | ||
|
||
std::cout << std::endl | ||
<< scoped_modifier_flag.get_scoped_active_modifier_flags() | ||
<< std::endl; | ||
|
||
expect(std::vector<active_modifier_flag>({}) == scoped_modifier_flag.get_scoped_active_modifier_flags()); | ||
} | ||
|
||
expect(active_modifier_flags == modifier_flag_manager.get_active_modifier_flags()); | ||
|
||
{ | ||
scoped_modifier_flag scoped_modifier_flag(modifier_flag_manager, std::set<modifier_flag>{modifier_flag::caps_lock}); | ||
|
||
expect(std::set<modifier_flag>({ | ||
modifier_flag::caps_lock, | ||
}) == modifier_flag_manager.make_modifier_flags()); | ||
|
||
std::cout << std::endl | ||
<< scoped_modifier_flag.get_scoped_active_modifier_flags() | ||
<< std::endl; | ||
|
||
expect(std::vector<active_modifier_flag>({ | ||
active_modifier_flag(active_modifier_flag::type::increase, modifier_flag::caps_lock, device_id(0)), | ||
}) == scoped_modifier_flag.get_scoped_active_modifier_flags()); | ||
} | ||
|
||
expect(active_modifier_flags == modifier_flag_manager.get_active_modifier_flags()); | ||
} | ||
|
||
// | ||
// Increase caps_lock by type::increase | ||
// | ||
|
||
{ | ||
modifier_flag_manager modifier_flag_manager; | ||
|
||
auto active_modifier_flags = modifier_flag_manager.get_active_modifier_flags(); | ||
|
||
expect(std::set<modifier_flag>({}) == modifier_flag_manager.make_modifier_flags()); | ||
|
||
{ | ||
scoped_modifier_flag scoped_modifier_flag(modifier_flag_manager, std::set<modifier_flag>{modifier_flag::caps_lock}); | ||
|
||
expect(std::set<modifier_flag>({ | ||
modifier_flag::caps_lock, | ||
}) == modifier_flag_manager.make_modifier_flags()); | ||
|
||
std::cout << std::endl | ||
<< scoped_modifier_flag.get_scoped_active_modifier_flags() | ||
<< std::endl; | ||
|
||
expect(std::vector<active_modifier_flag>({ | ||
active_modifier_flag(active_modifier_flag::type::increase, modifier_flag::caps_lock, device_id(0)), | ||
}) == scoped_modifier_flag.get_scoped_active_modifier_flags()); | ||
} | ||
|
||
expect(active_modifier_flags == modifier_flag_manager.get_active_modifier_flags()); | ||
} | ||
}; | ||
} |
Oops, something went wrong.