HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
keyboard_modifiers.hpp
1// Copyright Take Vos 2020-2022.
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 "../utility/utility.hpp"
8#include "../algorithm/module.hpp"
9#include "../macros.hpp"
10#include <cstdint>
11
12
13
14namespace hi::inline v1 {
15
21enum class keyboard_modifiers : uint8_t {
22 none = 0x00,
23 shift = 0x01,
24 control = 0x02,
25 alt = 0x04,
26 super = 0x08,
27};
28
29[[nodiscard]] constexpr keyboard_modifiers operator|(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) noexcept
30{
31 return static_cast<keyboard_modifiers>(std::to_underlying(lhs) | std::to_underlying(rhs));
32}
33
34[[nodiscard]] constexpr keyboard_modifiers operator&(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) noexcept
35{
36 return static_cast<keyboard_modifiers>(std::to_underlying(lhs) & std::to_underlying(rhs));
37}
38
39constexpr keyboard_modifiers& operator|=(keyboard_modifiers& lhs, keyboard_modifiers const& rhs) noexcept
40{
41 return lhs = lhs | rhs;
42}
43
44[[nodiscard]] constexpr bool to_bool(keyboard_modifiers const& rhs) noexcept
45{
46 return to_bool(std::to_underlying(rhs));
47}
48
49bool operator>=(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) = delete;
50
54inline keyboard_modifiers to_keyboard_modifiers(std::string_view s)
55{
56 if (ssize(s) == 0) {
57 throw parse_error("Empty keyboard modifier");
58 }
59
60 // Remove the canonical trailing '+'.
61 hilet s_lower = to_lower((s.back() == '+') ? s.substr(0, ssize(s) - 1) : s);
62
63 if (s_lower == "shift") {
64 return keyboard_modifiers::shift;
65 } else if (s_lower == "control" || s_lower == "ctrl" || s_lower == "cntr") {
66 return keyboard_modifiers::control;
67 } else if (s_lower == "alt" || s_lower == "option" || s_lower == "meta") {
68 return keyboard_modifiers::alt;
69 } else if (s_lower == "windows" || s_lower == "win" || s_lower == "command" || s_lower == "cmd" || s_lower == "super") {
70 return keyboard_modifiers::super;
71 } else {
72 throw parse_error(std::format("Unknown keyboard modifier '{}'", s));
73 }
74}
75
76inline std::string to_string(keyboard_modifiers modifiers)
77{
78 auto r = std::string{};
79
80 if (to_bool(modifiers & keyboard_modifiers::shift)) {
81 r += "shift+";
82 }
83 if (to_bool(modifiers & keyboard_modifiers::control)) {
84 r += "control+";
85 }
86 if (to_bool(modifiers & keyboard_modifiers::alt)) {
87 r += "alt+";
88 }
89 if (to_bool(modifiers & keyboard_modifiers::super)) {
90 r += "super+";
91 }
92
93 return r;
94}
95
96inline std::ostream& operator<<(std::ostream& lhs, keyboard_modifiers const& rhs)
97{
98 return lhs << to_string(rhs);
99}
100
101} // namespace hi::inline v1
102
103template<>
104struct std::hash<hi::keyboard_modifiers> {
105 [[nodiscard]] std::size_t operator()(hi::keyboard_modifiers const& rhs) const noexcept
106 {
107 return std::hash<uint8_t>{}(static_cast<uint8_t>(rhs));
108 }
109};
110
111template<typename CharT>
112struct std::formatter<hi::keyboard_modifiers, CharT> : std::formatter<std::string_view, CharT> {
113 auto format(hi::keyboard_modifiers const& t, auto& fc) const
114 {
115 return std::formatter<std::string_view, CharT>::format(hi::to_string(t), fc);
116 }
117};
DOXYGEN BUG.
Definition algorithm.hpp:16
keyboard_modifiers
Key modification keys pressed at the same time as another key.
Definition keyboard_modifiers.hpp:21
@ super
The windows-key, key-key or super-key is being held.
@ shift
The shift key is being held.
@ alt
The alt-key, option-key or meta-key is being held.
@ control
The control key is being held.
keyboard_modifiers to_keyboard_modifiers(std::string_view s)
Parse a key-binding modifier name.
Definition keyboard_modifiers.hpp:54
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
T operator()(T... args)
T operator>=(T... args)
T to_string(T... args)