Merge pull request #5216 from LukashonakV/enum-refactor
util/enum: make header-only and drop mandatory Hyprland dependency
This commit is contained in:
@@ -167,9 +167,8 @@ class Workspaces : public AModule, public EventHandler {
|
|||||||
std::map<WindowAddress, WindowRepr, std::less<>> m_orphanWindowMap;
|
std::map<WindowAddress, WindowRepr, std::less<>> m_orphanWindowMap;
|
||||||
|
|
||||||
enum class SortMethod { ID, NAME, NUMBER, SPECIAL_CENTERED, DEFAULT };
|
enum class SortMethod { ID, NAME, NUMBER, SPECIAL_CENTERED, DEFAULT };
|
||||||
util::EnumParser<SortMethod> m_enumParser;
|
|
||||||
SortMethod m_sortBy = SortMethod::DEFAULT;
|
SortMethod m_sortBy = SortMethod::DEFAULT;
|
||||||
std::map<std::string, SortMethod> m_sortMap = {{"ID", SortMethod::ID},
|
static inline const std::map<std::string, SortMethod> m_sortMap = {{"ID", SortMethod::ID},
|
||||||
{"NAME", SortMethod::NAME},
|
{"NAME", SortMethod::NAME},
|
||||||
{"NUMBER", SortMethod::NUMBER},
|
{"NUMBER", SortMethod::NUMBER},
|
||||||
{"SPECIAL-CENTERED", SortMethod::SPECIAL_CENTERED},
|
{"SPECIAL-CENTERED", SortMethod::SPECIAL_CENTERED},
|
||||||
@@ -207,9 +206,8 @@ class Workspaces : public AModule, public EventHandler {
|
|||||||
int m_taskbarMaxIcons = 0; // 0 means unlimited
|
int m_taskbarMaxIcons = 0; // 0 means unlimited
|
||||||
Gtk::Orientation m_taskbarOrientation = Gtk::ORIENTATION_HORIZONTAL;
|
Gtk::Orientation m_taskbarOrientation = Gtk::ORIENTATION_HORIZONTAL;
|
||||||
bool m_taskbarReverseDirection = false;
|
bool m_taskbarReverseDirection = false;
|
||||||
util::EnumParser<ActiveWindowPosition> m_activeWindowEnumParser;
|
|
||||||
ActiveWindowPosition m_activeWindowPosition = ActiveWindowPosition::NONE;
|
ActiveWindowPosition m_activeWindowPosition = ActiveWindowPosition::NONE;
|
||||||
std::map<std::string, ActiveWindowPosition> m_activeWindowPositionMap = {
|
static inline std::map<std::string, ActiveWindowPosition> m_activeWindowPositionMap = {
|
||||||
{"NONE", ActiveWindowPosition::NONE},
|
{"NONE", ActiveWindowPosition::NONE},
|
||||||
{"FIRST", ActiveWindowPosition::FIRST},
|
{"FIRST", ActiveWindowPosition::FIRST},
|
||||||
{"LAST", ActiveWindowPosition::LAST},
|
{"LAST", ActiveWindowPosition::LAST},
|
||||||
|
|||||||
+21
-7
@@ -1,19 +1,33 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "util/string.hpp"
|
||||||
|
|
||||||
namespace waybar::util {
|
namespace waybar::util {
|
||||||
|
|
||||||
template <typename EnumType>
|
template <typename EnumType>
|
||||||
struct EnumParser {
|
//struct EnumParser {
|
||||||
public:
|
|
||||||
EnumParser();
|
|
||||||
~EnumParser();
|
|
||||||
|
|
||||||
EnumType parseStringToEnum(const std::string& str,
|
EnumType parseStringToEnum(const std::string& str,
|
||||||
const std::map<std::string, EnumType>& enumMap);
|
const std::map<std::string, EnumType>& enumMap) {
|
||||||
};
|
std::string uppercaseStr = capitalize(str);
|
||||||
|
std::map<std::string, EnumType> capitalizedEnumMap;
|
||||||
|
std::transform(
|
||||||
|
enumMap.begin(), enumMap.end(),
|
||||||
|
std::inserter(capitalizedEnumMap, capitalizedEnumMap.end()),
|
||||||
|
[](const auto& pair) {
|
||||||
|
return std::make_pair(capitalize(pair.first), pair.second);
|
||||||
|
});
|
||||||
|
|
||||||
|
auto it = capitalizedEnumMap.find(uppercaseStr);
|
||||||
|
if (it != capitalizedEnumMap.end()) return it->second;
|
||||||
|
|
||||||
|
throw std::invalid_argument("Invalid string representation for enum");
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace waybar::util
|
} // namespace waybar::util
|
||||||
|
|||||||
@@ -13,14 +13,14 @@ enum class KillSignalAction : std::uint8_t {
|
|||||||
HIDE,
|
HIDE,
|
||||||
NOOP,
|
NOOP,
|
||||||
};
|
};
|
||||||
const std::map<std::string, KillSignalAction> userKillSignalActions = {
|
inline const std::map<std::string, KillSignalAction> userKillSignalActions = {
|
||||||
{"TOGGLE", KillSignalAction::TOGGLE},
|
{"TOGGLE", KillSignalAction::TOGGLE},
|
||||||
{"RELOAD", KillSignalAction::RELOAD},
|
{"RELOAD", KillSignalAction::RELOAD},
|
||||||
{"SHOW", KillSignalAction::SHOW},
|
{"SHOW", KillSignalAction::SHOW},
|
||||||
{"HIDE", KillSignalAction::HIDE},
|
{"HIDE", KillSignalAction::HIDE},
|
||||||
{"NOOP", KillSignalAction::NOOP}};
|
{"NOOP", KillSignalAction::NOOP}};
|
||||||
|
|
||||||
const KillSignalAction SIGNALACTION_DEFAULT_SIGUSR1 = KillSignalAction::TOGGLE;
|
inline const KillSignalAction SIGNALACTION_DEFAULT_SIGUSR1 = KillSignalAction::TOGGLE;
|
||||||
const KillSignalAction SIGNALACTION_DEFAULT_SIGUSR2 = KillSignalAction::RELOAD;
|
inline const KillSignalAction SIGNALACTION_DEFAULT_SIGUSR2 = KillSignalAction::RELOAD;
|
||||||
|
|
||||||
}; // namespace waybar::util
|
}; // namespace waybar::util
|
||||||
|
|||||||
@@ -185,7 +185,6 @@ src_files = files(
|
|||||||
'src/config.cpp',
|
'src/config.cpp',
|
||||||
'src/group.cpp',
|
'src/group.cpp',
|
||||||
'src/util/portal.cpp',
|
'src/util/portal.cpp',
|
||||||
'src/util/enum.cpp',
|
|
||||||
'src/util/prepare_for_sleep.cpp',
|
'src/util/prepare_for_sleep.cpp',
|
||||||
'src/util/ustring_clen.cpp',
|
'src/util/ustring_clen.cpp',
|
||||||
'src/util/sanitize_str.cpp',
|
'src/util/sanitize_str.cpp',
|
||||||
|
|||||||
+2
-3
@@ -296,13 +296,12 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
waybar::util::EnumParser<util::KillSignalAction> m_signalActionEnumParser;
|
|
||||||
const auto& configSigusr1 = config["on-sigusr1"];
|
const auto& configSigusr1 = config["on-sigusr1"];
|
||||||
if (configSigusr1.isString()) {
|
if (configSigusr1.isString()) {
|
||||||
auto strSigusr1 = configSigusr1.asString();
|
auto strSigusr1 = configSigusr1.asString();
|
||||||
try {
|
try {
|
||||||
onSigusr1 =
|
onSigusr1 =
|
||||||
m_signalActionEnumParser.parseStringToEnum(strSigusr1, util::userKillSignalActions);
|
util::parseStringToEnum<util::KillSignalAction>(strSigusr1, util::userKillSignalActions);
|
||||||
} catch (const std::invalid_argument& e) {
|
} catch (const std::invalid_argument& e) {
|
||||||
onSigusr1 = util::SIGNALACTION_DEFAULT_SIGUSR1;
|
onSigusr1 = util::SIGNALACTION_DEFAULT_SIGUSR1;
|
||||||
spdlog::warn(
|
spdlog::warn(
|
||||||
@@ -314,7 +313,7 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
|
|||||||
auto strSigusr2 = configSigusr2.asString();
|
auto strSigusr2 = configSigusr2.asString();
|
||||||
try {
|
try {
|
||||||
onSigusr2 =
|
onSigusr2 =
|
||||||
m_signalActionEnumParser.parseStringToEnum(strSigusr2, util::userKillSignalActions);
|
util::parseStringToEnum<util::KillSignalAction>(strSigusr2, util::userKillSignalActions);
|
||||||
} catch (const std::invalid_argument& e) {
|
} catch (const std::invalid_argument& e) {
|
||||||
onSigusr2 = util::SIGNALACTION_DEFAULT_SIGUSR2;
|
onSigusr2 = util::SIGNALACTION_DEFAULT_SIGUSR2;
|
||||||
spdlog::warn(
|
spdlog::warn(
|
||||||
|
|||||||
@@ -682,7 +682,7 @@ auto Workspaces::populateSortByConfig(const Json::Value& config) -> void {
|
|||||||
if (configSortBy.isString()) {
|
if (configSortBy.isString()) {
|
||||||
auto sortByStr = configSortBy.asString();
|
auto sortByStr = configSortBy.asString();
|
||||||
try {
|
try {
|
||||||
m_sortBy = m_enumParser.parseStringToEnum(sortByStr, m_sortMap);
|
m_sortBy = waybar::util::parseStringToEnum<SortMethod>(sortByStr, m_sortMap);
|
||||||
} catch (const std::invalid_argument& e) {
|
} catch (const std::invalid_argument& e) {
|
||||||
m_sortBy = SortMethod::DEFAULT;
|
m_sortBy = SortMethod::DEFAULT;
|
||||||
spdlog::warn(
|
spdlog::warn(
|
||||||
@@ -806,7 +806,7 @@ auto Workspaces::populateWorkspaceTaskbarConfig(const Json::Value& config) -> vo
|
|||||||
auto posStr = workspaceTaskbar["active-window-position"].asString();
|
auto posStr = workspaceTaskbar["active-window-position"].asString();
|
||||||
try {
|
try {
|
||||||
m_activeWindowPosition =
|
m_activeWindowPosition =
|
||||||
m_activeWindowEnumParser.parseStringToEnum(posStr, m_activeWindowPositionMap);
|
util::parseStringToEnum<ActiveWindowPosition>(posStr, m_activeWindowPositionMap);
|
||||||
} catch (const std::invalid_argument& e) {
|
} catch (const std::invalid_argument& e) {
|
||||||
spdlog::warn(
|
spdlog::warn(
|
||||||
"Invalid string representation for active-window-position. Falling back to 'none'.");
|
"Invalid string representation for active-window-position. Falling back to 'none'.");
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
#include "util/enum.hpp"
|
|
||||||
|
|
||||||
#include <algorithm> // for std::transform
|
|
||||||
#include <cctype> // for std::toupper
|
|
||||||
#include <iostream>
|
|
||||||
#include <map>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "modules/hyprland/workspaces.hpp"
|
|
||||||
#include "util/string.hpp"
|
|
||||||
|
|
||||||
namespace waybar::util {
|
|
||||||
|
|
||||||
template <typename EnumType>
|
|
||||||
EnumParser<EnumType>::EnumParser() = default;
|
|
||||||
|
|
||||||
template <typename EnumType>
|
|
||||||
EnumParser<EnumType>::~EnumParser() = default;
|
|
||||||
|
|
||||||
template <typename EnumType>
|
|
||||||
EnumType EnumParser<EnumType>::parseStringToEnum(const std::string& str,
|
|
||||||
const std::map<std::string, EnumType>& enumMap) {
|
|
||||||
// Convert the input string to uppercase
|
|
||||||
std::string uppercaseStr = capitalize(str);
|
|
||||||
|
|
||||||
// Capitalize the map keys before searching
|
|
||||||
std::map<std::string, EnumType> capitalizedEnumMap;
|
|
||||||
std::transform(
|
|
||||||
enumMap.begin(), enumMap.end(), std::inserter(capitalizedEnumMap, capitalizedEnumMap.end()),
|
|
||||||
[](const auto& pair) { return std::make_pair(capitalize(pair.first), pair.second); });
|
|
||||||
|
|
||||||
// Return enum match of string
|
|
||||||
auto it = capitalizedEnumMap.find(uppercaseStr);
|
|
||||||
if (it != capitalizedEnumMap.end()) return it->second;
|
|
||||||
|
|
||||||
// Throw error if it doesn't return
|
|
||||||
throw std::invalid_argument("Invalid string representation for enum");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Explicit instantiations for specific EnumType types you intend to use
|
|
||||||
// Add explicit instantiations for all relevant EnumType types
|
|
||||||
template struct EnumParser<modules::hyprland::Workspaces::SortMethod>;
|
|
||||||
template struct EnumParser<modules::hyprland::Workspaces::ActiveWindowPosition>;
|
|
||||||
template struct EnumParser<util::KillSignalAction>;
|
|
||||||
|
|
||||||
} // namespace waybar::util
|
|
||||||
Reference in New Issue
Block a user