desktop/rule: cleanup inheritance, use templates to avoid dup

This commit is contained in:
Vaxry 2026-04-28 15:55:41 +01:00 committed by Vaxry
parent 89e2380da8
commit 97886cf0da
7 changed files with 94 additions and 88 deletions

View file

@ -104,10 +104,22 @@ std::underlying_type_t<eRuleProperty> IRule::getPropertiesMask() {
return m_mask;
}
void IRule::setEnabled(bool enable) {
m_enabled = enable;
}
bool IRule::isEnabled() const {
return m_enabled;
}
bool IRule::has(eRuleProperty p) {
return m_matchEngines.contains(p);
}
bool IRule::canMatch() const {
return !m_matchEngines.empty() && m_enabled;
}
bool IRule::matches(eRuleProperty p, const std::string& s) {
if (!has(p))
return false;

View file

@ -52,6 +52,8 @@ namespace Desktop::Rule {
virtual std::underlying_type_t<eRuleProperty> getPropertiesMask();
void registerMatch(eRuleProperty, const std::string&);
void setEnabled(bool enable);
bool isEnabled() const;
void markAsExecRule(const std::string& token, uint64_t pid, bool persistent = false);
bool isExecRule();
bool isExecPersistent();
@ -66,13 +68,15 @@ namespace Desktop::Rule {
bool matches(eRuleProperty, const std::string& s);
bool matches(eRuleProperty, bool b);
bool has(eRuleProperty);
bool canMatch() const;
//
std::unordered_map<eRuleProperty, UP<IMatchEngine>> m_matchEngines;
private:
std::underlying_type_t<eRuleProperty> m_mask = 0;
std::string m_name = "";
std::underlying_type_t<eRuleProperty> m_mask = 0;
std::string m_name = "";
bool m_enabled = true;
struct {
bool isExecRule = false;

View file

@ -0,0 +1,51 @@
#pragma once
#include "Rule.hpp"
#include <expected>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
namespace Desktop::Rule {
template <typename TEffect, eRuleType RULE_TYPE>
class CRuleWithEffects : public IRule {
public:
using effectType = TEffect;
using storageType = TEffect::storageType;
using valueType = TEffect::valueType;
eRuleType type() override {
return RULE_TYPE;
}
std::expected<void, std::string> addEffect(storageType e, const std::string& result) {
auto parsed = parseEffect(e, result);
if (!parsed)
return std::unexpected(parsed.error());
m_effects.emplace_back(TEffect{.key = e, .raw = result, .value = std::move(*parsed)});
m_effectSet.emplace(e);
return {};
}
const std::vector<TEffect>& effects() {
return m_effects;
}
const std::unordered_set<storageType>& effectsSet() {
return m_effectSet;
}
protected:
CRuleWithEffects(const std::string& name = "") : IRule(name) {}
virtual std::expected<valueType, std::string> parseEffect(storageType e, const std::string& result) = 0;
private:
std::vector<TEffect> m_effects;
std::unordered_set<storageType> m_effectSet;
};
}

View file

@ -85,30 +85,16 @@ static std::expected<LayerRuleEffectValue, std::string> parseLayerRuleEffect(CLa
}
}
CLayerRule::CLayerRule(const std::string& name) : IRule(name) {
CLayerRule::CLayerRule(const std::string& name) : CRuleWithEffects<SLayerRuleEffect, RULE_TYPE_LAYER>(name) {
;
}
eRuleType CLayerRule::type() {
return RULE_TYPE_LAYER;
}
std::expected<void, std::string> CLayerRule::addEffect(CLayerRule::storageType e, const std::string& result) {
auto parsed = parseLayerRuleEffect(e, result);
if (!parsed)
return std::unexpected(parsed.error());
m_effects.emplace_back(SLayerRuleEffect{.key = e, .raw = result, .value = std::move(*parsed)});
return {};
}
const std::vector<SLayerRuleEffect>& CLayerRule::effects() {
return m_effects;
std::expected<LayerRuleEffectValue, std::string> CLayerRule::parseEffect(CLayerRule::storageType e, const std::string& result) {
return parseLayerRuleEffect(e, result);
}
bool CLayerRule::matches(PHLLS ls) {
if (m_matchEngines.empty() || !m_enabled)
if (!canMatch())
return false;
for (const auto& [prop, engine] : m_matchEngines) {
@ -127,11 +113,3 @@ bool CLayerRule::matches(PHLLS ls) {
return true;
}
void CLayerRule::setEnabled(bool enable) {
m_enabled = enable;
}
bool CLayerRule::isEnabled() const {
return m_enabled;
}

View file

@ -1,6 +1,6 @@
#pragma once
#include "../Rule.hpp"
#include "../RuleWithEffects.hpp"
#include "../../DesktopTypes.hpp"
#include "LayerRuleEffectContainer.hpp"
@ -11,30 +11,25 @@ namespace Desktop::Rule {
using LayerRuleEffectValue = std::variant<std::monostate, bool, int64_t, float, std::string>;
struct SLayerRuleEffect {
using storageType = CLayerRuleEffectContainer::storageType;
using valueType = LayerRuleEffectValue;
CLayerRuleEffectContainer::storageType key = LAYER_RULE_EFFECT_NONE;
std::string raw;
LayerRuleEffectValue value;
};
class CLayerRule : public IRule {
class CLayerRule : public CRuleWithEffects<SLayerRuleEffect, RULE_TYPE_LAYER> {
public:
using storageType = CLayerRuleEffectContainer::storageType;
using Base = CRuleWithEffects<SLayerRuleEffect, RULE_TYPE_LAYER>;
using storageType = Base::storageType;
CLayerRule(const std::string& name = "");
virtual ~CLayerRule() = default;
virtual eRuleType type();
std::expected<void, std::string> addEffect(storageType e, const std::string& result);
const std::vector<SLayerRuleEffect>& effects();
void setEnabled(bool enable);
bool isEnabled() const;
bool matches(PHLLS w);
bool matches(PHLLS w);
private:
std::vector<SLayerRuleEffect> m_effects;
bool m_enabled = true;
std::expected<LayerRuleEffectValue, std::string> parseEffect(storageType e, const std::string& result) override;
};
};

View file

@ -330,31 +330,16 @@ static std::expected<WindowRuleEffectValue, std::string> parseWindowRuleEffect(C
}
}
CWindowRule::CWindowRule(const std::string& name) : IRule(name) {
CWindowRule::CWindowRule(const std::string& name) : CRuleWithEffects<SWindowRuleEffect, RULE_TYPE_WINDOW>(name) {
;
}
eRuleType CWindowRule::type() {
return RULE_TYPE_WINDOW;
}
std::expected<void, std::string> CWindowRule::addEffect(CWindowRule::storageType e, const std::string& result) {
auto parsed = parseWindowRuleEffect(e, result);
if (!parsed)
return std::unexpected(parsed.error());
m_effects.emplace_back(SWindowRuleEffect{.key = e, .raw = result, .value = std::move(*parsed)});
m_effectSet.emplace(e);
return {};
}
const std::vector<SWindowRuleEffect>& CWindowRule::effects() {
return m_effects;
std::expected<WindowRuleEffectValue, std::string> CWindowRule::parseEffect(CWindowRule::storageType e, const std::string& result) {
return parseWindowRuleEffect(e, result);
}
bool CWindowRule::matches(PHLWINDOW w, bool allowEnvLookup) {
if (m_matchEngines.empty() || !m_enabled)
if (!canMatch())
return false;
for (const auto& [prop, engine] : m_matchEngines) {
@ -491,15 +476,3 @@ std::expected<SP<CWindowRule>, std::string> CWindowRule::buildFromExecString(std
return wr;
}
const std::unordered_set<CWindowRule::storageType>& CWindowRule::effectsSet() {
return m_effectSet;
}
void CWindowRule::setEnabled(bool enable) {
m_enabled = enable;
}
bool CWindowRule::isEnabled() const {
return m_enabled;
}

View file

@ -1,6 +1,6 @@
#pragma once
#include "../Rule.hpp"
#include "../RuleWithEffects.hpp"
#include "../../DesktopTypes.hpp"
#include "../../types/OverridableVar.hpp"
#include "WindowRuleEffectContainer.hpp"
@ -33,14 +33,18 @@ namespace Desktop::Rule {
using WindowRuleEffectValue = std::variant<std::monostate, bool, int64_t, float, std::string, std::vector<std::string>, SFullscreenStateRule, SOpacityRule, SBorderColorRule>;
struct SWindowRuleEffect {
using storageType = CWindowRuleEffectContainer::storageType;
using valueType = WindowRuleEffectValue;
CWindowRuleEffectContainer::storageType key = WINDOW_RULE_EFFECT_NONE;
std::string raw;
WindowRuleEffectValue value;
};
class CWindowRule : public IRule {
class CWindowRule : public CRuleWithEffects<SWindowRuleEffect, RULE_TYPE_WINDOW> {
private:
using storageType = CWindowRuleEffectContainer::storageType;
using Base = CRuleWithEffects<SWindowRuleEffect, RULE_TYPE_WINDOW>;
using storageType = Base::storageType;
public:
CWindowRule(const std::string& name = "");
@ -52,20 +56,9 @@ namespace Desktop::Rule {
static std::expected<SP<CWindowRule>, std::string> buildFromExecString(std::string&&);
virtual eRuleType type();
std::expected<void, std::string> addEffect(storageType e, const std::string& result);
const std::vector<SWindowRuleEffect>& effects();
const std::unordered_set<storageType>& effectsSet();
void setEnabled(bool enable);
bool isEnabled() const;
bool matches(PHLWINDOW w, bool allowEnvLookup = false);
private:
std::vector<SWindowRuleEffect> m_effects;
std::unordered_set<storageType> m_effectSet;
bool m_enabled = true;
std::expected<WindowRuleEffectValue, std::string> parseEffect(storageType e, const std::string& result) override;
};
};