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/algorithm.hpp"
9#include "../macros.hpp"
10#include <cstdint>
11#include <utility>
12#include <format>
13#include <string_view>
14#include <string>
15#include <ostream>
16#include <functional>
17
18hi_export_module(hikogui.GUI : keyboard_modifiers);
19
20hi_export namespace hi::inline v1 {
21
27enum class keyboard_modifiers : uint8_t {
28 none = 0x00,
29 shift = 0x01,
30 control = 0x02,
31 alt = 0x04,
32 super = 0x08,
33};
34
35[[nodiscard]] constexpr keyboard_modifiers operator|(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) noexcept
36{
37 return static_cast<keyboard_modifiers>(std::to_underlying(lhs) | std::to_underlying(rhs));
38}
39
40[[nodiscard]] constexpr keyboard_modifiers operator&(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) noexcept
41{
42 return static_cast<keyboard_modifiers>(std::to_underlying(lhs) & std::to_underlying(rhs));
43}
44
45constexpr keyboard_modifiers& operator|=(keyboard_modifiers& lhs, keyboard_modifiers const& rhs) noexcept
46{
47 return lhs = lhs | rhs;
48}
49
50[[nodiscard]] constexpr bool to_bool(keyboard_modifiers const& rhs) noexcept
51{
52 return to_bool(std::to_underlying(rhs));
53}
54
55bool operator>=(keyboard_modifiers const& lhs, keyboard_modifiers const& rhs) = delete;
56
60inline keyboard_modifiers to_keyboard_modifiers(std::string_view s)
61{
62 if (ssize(s) == 0) {
63 throw parse_error("Empty keyboard modifier");
64 }
65
66 // Remove the canonical trailing '+'.
67 auto const s_lower = to_lower((s.back() == '+') ? s.substr(0, ssize(s) - 1) : s);
68
69 if (s_lower == "shift") {
70 return keyboard_modifiers::shift;
71 } else if (s_lower == "control" || s_lower == "ctrl" || s_lower == "cntr") {
72 return keyboard_modifiers::control;
73 } else if (s_lower == "alt" || s_lower == "option" || s_lower == "meta") {
74 return keyboard_modifiers::alt;
75 } else if (s_lower == "windows" || s_lower == "win" || s_lower == "command" || s_lower == "cmd" || s_lower == "super") {
76 return keyboard_modifiers::super;
77 } else {
78 throw parse_error(std::format("Unknown keyboard modifier '{}'", s));
79 }
80}
81
82inline std::string to_string(keyboard_modifiers modifiers)
83{
84 auto r = std::string{};
85
86 if (to_bool(modifiers & keyboard_modifiers::shift)) {
87 r += "shift+";
88 }
89 if (to_bool(modifiers & keyboard_modifiers::control)) {
90 r += "control+";
91 }
92 if (to_bool(modifiers & keyboard_modifiers::alt)) {
93 r += "alt+";
94 }
95 if (to_bool(modifiers & keyboard_modifiers::super)) {
96 r += "super+";
97 }
98
99 return r;
100}
101
102inline std::ostream& operator<<(std::ostream& lhs, keyboard_modifiers const& rhs)
103{
104 return lhs << to_string(rhs);
105}
106
107} // namespace hi::inline v1
108
109template<>
110struct std::hash<hi::keyboard_modifiers> {
111 [[nodiscard]] std::size_t operator()(hi::keyboard_modifiers const& rhs) const noexcept
112 {
113 return std::hash<uint8_t>{}(static_cast<uint8_t>(rhs));
114 }
115};
116
117// XXX #617 MSVC bug does not handle partial specialization in modules.
118hi_export template<>
119struct std::formatter<hi::keyboard_modifiers, char> : std::formatter<std::string_view, char> {
120 auto format(hi::keyboard_modifiers const& t, auto& fc) const
121 {
122 return std::formatter<std::string_view, char>::format(hi::to_string(t), fc);
123 }
124};
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
keyboard_modifiers
Key modification keys pressed at the same time as another key.
Definition keyboard_modifiers.hpp:27
@ 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:60
T operator()(T... args)
T operator>=(T... args)
T to_string(T... args)