HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
keyboard_modifiers.hpp
1// Copyright Take Vos 2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../required.hpp"
8#include "../exception.hpp"
9#include "../strings.hpp"
10#include <cstdint>
11
12namespace tt {
13
19enum class keyboard_modifiers : uint8_t {
20 None = 0x00,
21 Shift = 0x01,
22 Control = 0x02,
23 Alt = 0x04,
24 Super = 0x08,
25};
26
27[[nodiscard]] constexpr keyboard_modifiers operator|(keyboard_modifiers lhs, keyboard_modifiers rhs) noexcept
28{
29 return static_cast<keyboard_modifiers>(static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs));
30}
31
32constexpr keyboard_modifiers &operator|=(keyboard_modifiers &lhs, keyboard_modifiers rhs) noexcept
33{
34 lhs = lhs | rhs;
35 return lhs;
36}
37
38[[nodiscard]] constexpr bool operator>=(keyboard_modifiers lhs, keyboard_modifiers rhs) noexcept {
39 ttlet lhs_ = static_cast<uint8_t>(lhs);
40 ttlet rhs_ = static_cast<uint8_t>(rhs);
41 return (lhs_ & rhs_) == rhs_;
42}
43
47inline keyboard_modifiers to_keyboard_modifiers(std::string_view s)
48{
49 if (std::ssize(s) == 0) {
50 throw parse_error("Empty keyboard modifier");
51 }
52
53 // Remove the canonical trailing '+'.
54 ttlet s_lower = to_lower(
55 (s.back() == '+') ? s.substr(0, std::ssize(s) - 1) : s
56 );
57
58 if (s_lower == "shift") {
59 return keyboard_modifiers::Shift;
60 } else if (s_lower == "control" || s_lower == "ctrl" || s_lower == "cntr") {
61 return keyboard_modifiers::Control;
62 } else if (s_lower == "alt" || s_lower == "option" || s_lower == "meta") {
63 return keyboard_modifiers::Alt;
64 } else if (
65 s_lower == "windows" || s_lower == "win" ||
66 s_lower == "command" || s_lower == "cmd" ||
67 s_lower == "super"
68 ) {
69 return keyboard_modifiers::Super;
70 } else {
71 throw parse_error("Unknown keyboard modifier '{}'", s);
72 }
73}
74
75inline std::string to_string(keyboard_modifiers modifiers)
76{
77 auto r = std::string{};
78
79 if (modifiers >= keyboard_modifiers::Shift) {
80 r += "shift+";
81 }
82 if (modifiers >= keyboard_modifiers::Control) {
83 r += "control+";
84 }
85 if (modifiers >= keyboard_modifiers::Alt) {
86 r += "alt+";
87 }
88 if (modifiers >= keyboard_modifiers::Super) {
89 r += "super+";
90 }
91
92 return r;
93}
94
95inline std::ostream &operator<<(std::ostream &lhs, keyboard_modifiers const &rhs)
96{
97 return lhs << to_string(rhs);
98}
99
100}
101
102namespace std {
103
104template<>
105struct hash<tt::keyboard_modifiers> {
106 [[nodiscard]] size_t operator() (tt::keyboard_modifiers const &rhs) const noexcept {
107 return std::hash<uint8_t>{}(static_cast<uint8_t>(rhs));
108 }
109};
110
111}
STL namespace.
T operator()(T... args)
T operator>=(T... args)
T to_string(T... args)