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/module.hpp"
8#include "../strings.hpp"
9#include <cstdint>
10
11namespace hi::inline v1 {
12
18enum class keyboard_modifiers : uint8_t {
19 none = 0x00,
20 shift = 0x01,
21 control = 0x02,
22 alt = 0x04,
23 super = 0x08,
24};
25
26[[nodiscard]] constexpr keyboard_modifiers operator|(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) noexcept
27{
28 return static_cast<keyboard_modifiers>(to_underlying(lhs) | to_underlying(rhs));
29}
30
31[[nodiscard]] constexpr keyboard_modifiers operator&(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) noexcept
32{
33 return static_cast<keyboard_modifiers>(to_underlying(lhs) & to_underlying(rhs));
34}
35
36constexpr keyboard_modifiers& operator|=(keyboard_modifiers& lhs, keyboard_modifiers const& rhs) noexcept
37{
38 return lhs = lhs | rhs;
39}
40
41[[nodiscard]] constexpr bool to_bool(keyboard_modifiers const& rhs) noexcept
42{
43 return to_bool(to_underlying(rhs));
44}
45
46bool operator>=(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) = delete;
47
51inline keyboard_modifiers to_keyboard_modifiers(std::string_view s)
52{
53 if (ssize(s) == 0) {
54 throw parse_error("Empty keyboard modifier");
55 }
56
57 // Remove the canonical trailing '+'.
58 hilet s_lower = to_lower((s.back() == '+') ? s.substr(0, ssize(s) - 1) : s);
59
60 if (s_lower == "shift") {
61 return keyboard_modifiers::shift;
62 } else if (s_lower == "control" || s_lower == "ctrl" || s_lower == "cntr") {
63 return keyboard_modifiers::control;
64 } else if (s_lower == "alt" || s_lower == "option" || s_lower == "meta") {
65 return keyboard_modifiers::alt;
66 } else if (s_lower == "windows" || s_lower == "win" || s_lower == "command" || s_lower == "cmd" || s_lower == "super") {
67 return keyboard_modifiers::super;
68 } else {
69 throw parse_error(std::format("Unknown keyboard modifier '{}'", s));
70 }
71}
72
73inline std::string to_string(keyboard_modifiers modifiers)
74{
75 auto r = std::string{};
76
77 if (to_bool(modifiers & keyboard_modifiers::shift)) {
78 r += "shift+";
79 }
80 if (to_bool(modifiers & keyboard_modifiers::control)) {
81 r += "control+";
82 }
83 if (to_bool(modifiers & keyboard_modifiers::alt)) {
84 r += "alt+";
85 }
86 if (to_bool(modifiers & keyboard_modifiers::super)) {
87 r += "super+";
88 }
89
90 return r;
91}
92
93inline std::ostream& operator<<(std::ostream& lhs, keyboard_modifiers const& rhs)
94{
95 return lhs << to_string(rhs);
96}
97
98} // namespace hi::inline v1
99
100template<>
101struct std::hash<hi::keyboard_modifiers> {
102 [[nodiscard]] std::size_t operator()(hi::keyboard_modifiers const& rhs) const noexcept
103 {
104 return std::hash<uint8_t>{}(static_cast<uint8_t>(rhs));
105 }
106};
107
108template<typename CharT>
109struct std::formatter<hi::keyboard_modifiers, CharT> : std::formatter<std::string_view, CharT> {
110 auto format(hi::keyboard_modifiers const& t, auto& fc)
111 {
112 return std::formatter<std::string_view, CharT>::format(hi::to_string(t), fc);
113 }
114};
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
constexpr std::string to_string(std::u32string_view rhs) noexcept
Conversion from UTF-32 to UTF-8.
Definition to_string.hpp:215
DOXYGEN BUG.
Definition algorithm.hpp:13
keyboard_modifiers
Key modification keys pressed at the same time as another key.
Definition keyboard_modifiers.hpp:18
@ 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:51
geometry/margins.hpp
Definition cache.hpp:11
T operator()(T... args)
T operator>=(T... args)