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.hpp"
8#include "../exception.hpp"
9#include "../strings.hpp"
10#include "../cast.hpp"
11#include <cstdint>
12
13namespace hi::inline v1 {
14
20enum class keyboard_modifiers : uint8_t {
21 none = 0x00,
22 shift = 0x01,
23 control = 0x02,
24 alt = 0x04,
25 super = 0x08,
26};
27
28[[nodiscard]] constexpr keyboard_modifiers operator|(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) noexcept
29{
30 return static_cast<keyboard_modifiers>(to_underlying(lhs) | to_underlying(rhs));
31}
32
33[[nodiscard]] constexpr keyboard_modifiers operator&(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) noexcept
34{
35 return static_cast<keyboard_modifiers>(to_underlying(lhs) & to_underlying(rhs));
36}
37
38constexpr keyboard_modifiers& operator|=(keyboard_modifiers& lhs, keyboard_modifiers const& rhs) noexcept
39{
40 return lhs = lhs | rhs;
41}
42
43[[nodiscard]] constexpr bool any(keyboard_modifiers const& rhs) noexcept
44{
45 return to_bool(to_underlying(rhs));
46}
47
48bool operator>=(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) = delete;
49
53inline keyboard_modifiers to_keyboard_modifiers(std::string_view s)
54{
55 if (ssize(s) == 0) {
56 throw parse_error("Empty keyboard modifier");
57 }
58
59 // Remove the canonical trailing '+'.
60 hilet s_lower = to_lower((s.back() == '+') ? s.substr(0, ssize(s) - 1) : s);
61
62 if (s_lower == "shift") {
63 return keyboard_modifiers::shift;
64 } else if (s_lower == "control" || s_lower == "ctrl" || s_lower == "cntr") {
65 return keyboard_modifiers::control;
66 } else if (s_lower == "alt" || s_lower == "option" || s_lower == "meta") {
67 return keyboard_modifiers::alt;
68 } else if (s_lower == "windows" || s_lower == "win" || s_lower == "command" || s_lower == "cmd" || s_lower == "super") {
69 return keyboard_modifiers::super;
70 } else {
71 throw parse_error(std::format("Unknown keyboard modifier '{}'", s));
72 }
73}
74
75inline std::string to_string(keyboard_modifiers modifiers)
76{
77 auto r = std::string{};
78
79 if (any(modifiers & keyboard_modifiers::shift)) {
80 r += "shift+";
81 }
82 if (any(modifiers & keyboard_modifiers::control)) {
83 r += "control+";
84 }
85 if (any(modifiers & keyboard_modifiers::alt)) {
86 r += "alt+";
87 }
88 if (any(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} // namespace hi::inline v1
101
102template<>
103struct std::hash<hi::keyboard_modifiers> {
104 [[nodiscard]] std::size_t operator()(hi::keyboard_modifiers const& rhs) const noexcept
105 {
106 return std::hash<uint8_t>{}(static_cast<uint8_t>(rhs));
107 }
108};
109
110template<typename CharT>
111struct std::formatter<hi::keyboard_modifiers, CharT> : std::formatter<std::string_view, CharT> {
112 auto format(hi::keyboard_modifiers const& t, auto& fc)
113 {
114 return std::formatter<std::string_view, CharT>::format(hi::to_string(t), fc);
115 }
116};
Utilities used by the HikoGUI library itself.
#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:15
keyboard_modifiers
Key modification keys pressed at the same time as another key.
Definition keyboard_modifiers.hpp:20
@ 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:53
The HikoGUI namespace.
Definition ascii.hpp:19
T operator()(T... args)
T operator>=(T... args)
T to_string(T... args)