From ffd6cf65e40a7140e82dc6e076c90b5119d41a54 Mon Sep 17 00:00:00 2001 From: MightyPlaza <123664421+MightyPlaza@users.noreply.github.com> Date: Tue, 15 Apr 2025 23:00:40 +0000 Subject: windowrules: allow incrementing window props (#9566) --- src/desktop/Window.cpp | 3 +- src/desktop/Window.hpp | 97 +++---------------------- src/desktop/WindowOverridableVar.hpp | 132 +++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 90 deletions(-) create mode 100644 src/desktop/WindowOverridableVar.hpp (limited to 'src/desktop') diff --git a/src/desktop/Window.cpp b/src/desktop/Window.cpp index 4b9a07fc..323aa03a 100644 --- a/src/desktop/Window.cpp +++ b/src/desktop/Window.cpp @@ -11,6 +11,7 @@ #include "../render/decorations/CHyprGroupBarDecoration.hpp" #include "../render/decorations/CHyprBorderDecoration.hpp" #include "../config/ConfigValue.hpp" +#include "../config/ConfigManager.hpp" #include "../managers/TokenManager.hpp" #include "../managers/AnimationManager.hpp" #include "../managers/ANRManager.hpp" @@ -780,7 +781,7 @@ void CWindow::applyDynamicRule(const SP& r) { const CVarList VARS(r->szRule, 0, ' '); if (auto search = NWindowProperties::intWindowProperties.find(VARS[1]); search != NWindowProperties::intWindowProperties.end()) { try { - *(search->second(m_pSelf.lock())) = CWindowOverridableVar(std::stoi(VARS[2]), priority); + *(search->second(m_pSelf.lock())) = CWindowOverridableVar(Hyprlang::INT(std::stoi(VARS[2])), priority); } catch (std::exception& e) { Debug::log(ERR, "Rule \"{}\" failed with: {}", r->szRule, e.what()); } } else if (auto search = NWindowProperties::floatWindowProperties.find(VARS[1]); search != NWindowProperties::floatWindowProperties.end()) { try { diff --git a/src/desktop/Window.hpp b/src/desktop/Window.hpp index b314eca5..f592aed6 100644 --- a/src/desktop/Window.hpp +++ b/src/desktop/Window.hpp @@ -19,6 +19,7 @@ #include "WLSurface.hpp" #include "Workspace.hpp" #include "WindowRule.hpp" +#include "WindowOverridableVar.hpp" #include "../protocols/types/ContentType.hpp" class CXDGSurfaceResource; @@ -77,88 +78,6 @@ struct SAlphaValue { }; }; -enum eOverridePriority : uint8_t { - PRIORITY_LAYOUT = 0, - PRIORITY_WORKSPACE_RULE, - PRIORITY_WINDOW_RULE, - PRIORITY_SET_PROP, -}; - -template -class CWindowOverridableVar { - public: - CWindowOverridableVar(T const& value, eOverridePriority priority) { - values[priority] = value; - } - CWindowOverridableVar(T const& value) : defaultValue{value} {} - - CWindowOverridableVar() = default; - ~CWindowOverridableVar() = default; - - CWindowOverridableVar& operator=(CWindowOverridableVar const& other) { - // Self-assignment check - if (this == &other) - return *this; - - for (auto const& value : other.values) { - values[value.first] = value.second; - } - - return *this; - } - - void unset(eOverridePriority priority) { - values.erase(priority); - } - - bool hasValue() { - return !values.empty(); - } - - T value() { - if (!values.empty()) - return std::prev(values.end())->second; - else - throw std::bad_optional_access(); - } - - T valueOr(T const& other) { - if (hasValue()) - return value(); - else - return other; - } - - T valueOrDefault() { - return valueOr(defaultValue); - } - - eOverridePriority getPriority() { - if (!values.empty()) - return std::prev(values.end())->first; - else - throw std::bad_optional_access(); - } - - void matchOptional(std::optional const& optValue, eOverridePriority priority) { - if (optValue.has_value()) - values[priority] = optValue.value(); - else - unset(priority); - } - - operator std::optional() { - if (hasValue()) - return value(); - else - return std::nullopt; - } - - private: - std::map values; - T defaultValue; // used for toggling, so required for bool -}; - struct SWindowData { CWindowOverridableVar alpha = SAlphaValue{1.f, false}; CWindowOverridableVar alphaInactive = SAlphaValue{1.f, false}; @@ -186,12 +105,12 @@ struct SWindowData { CWindowOverridableVar xray = false; CWindowOverridableVar renderUnfocused = false; - CWindowOverridableVar rounding; - CWindowOverridableVar roundingPower; - CWindowOverridableVar borderSize; + CWindowOverridableVar borderSize = {std::string("general:border_size"), Hyprlang::INT(0), std::nullopt}; + CWindowOverridableVar rounding = {std::string("decoration:rounding"), Hyprlang::INT(0), std::nullopt}; - CWindowOverridableVar scrollMouse; - CWindowOverridableVar scrollTouchpad; + CWindowOverridableVar roundingPower = {std::string("decoration:rounding_power")}; + CWindowOverridableVar scrollMouse = {std::string("input:scroll_factor")}; + CWindowOverridableVar scrollTouchpad = {std::string("input:touchpad:scroll_factor")}; CWindowOverridableVar animationStyle; CWindowOverridableVar maxSize; @@ -567,12 +486,12 @@ namespace NWindowProperties { {"xray", [](const PHLWINDOW& pWindow) { return &pWindow->m_sWindowData.xray; }}, }; - const std::unordered_map*(const PHLWINDOW&)>> intWindowProperties = { + const std::unordered_map*(const PHLWINDOW&)>> intWindowProperties = { {"rounding", [](const PHLWINDOW& pWindow) { return &pWindow->m_sWindowData.rounding; }}, {"bordersize", [](const PHLWINDOW& pWindow) { return &pWindow->m_sWindowData.borderSize; }}, }; - const std::unordered_map*(PHLWINDOW)>> floatWindowProperties = { + const std::unordered_map*(PHLWINDOW)>> floatWindowProperties = { {"roundingpower", [](const PHLWINDOW& pWindow) { return &pWindow->m_sWindowData.roundingPower; }}, {"scrollmouse", [](const PHLWINDOW& pWindow) { return &pWindow->m_sWindowData.scrollMouse; }}, {"scrolltouchpad", [](const PHLWINDOW& pWindow) { return &pWindow->m_sWindowData.scrollTouchpad; }}, diff --git a/src/desktop/WindowOverridableVar.hpp b/src/desktop/WindowOverridableVar.hpp new file mode 100644 index 00000000..ea113d3e --- /dev/null +++ b/src/desktop/WindowOverridableVar.hpp @@ -0,0 +1,132 @@ +#pragma once + +#include +#include +#include +#include "../config/ConfigValue.hpp" + +enum eOverridePriority : uint8_t { + PRIORITY_LAYOUT = 0, + PRIORITY_WORKSPACE_RULE, + PRIORITY_WINDOW_RULE, + PRIORITY_SET_PROP, +}; + +template +T clampOptional(T const& value, std::optional const& min, std::optional const& max) { + return std::clamp(value, min.value_or(std::numeric_limits::min()), max.value_or(std::numeric_limits::max())); +} + +template || std::is_same_v || std::is_same_v> +class CWindowOverridableVar { + public: + CWindowOverridableVar(T const& value, eOverridePriority priority) { + m_values[priority] = value; + } + + CWindowOverridableVar(T const& value) : m_defaultValue{value} {} + CWindowOverridableVar(T const& value, std::optional const& min, std::optional const& max = std::nullopt) : m_defaultValue{value}, m_minValue{min}, m_maxValue{max} {} + CWindowOverridableVar(std::string const& value) + requires(Extended && !std::is_same_v) + : m_configValue(SP>(new CConfigValue(value))) {} + CWindowOverridableVar(std::string const& value, std::optional const& min, std::optional const& max = std::nullopt) + requires(Extended && !std::is_same_v) + : m_minValue(min), m_maxValue(max), m_configValue(SP>(new CConfigValue(value))) {} + + CWindowOverridableVar() = default; + ~CWindowOverridableVar() = default; + + CWindowOverridableVar& operator=(CWindowOverridableVar const& other) { + // Self-assignment check + if (this == &other) + return *this; + + for (auto const& value : other.m_values) { + if constexpr (Extended && !std::is_same_v) + m_values[value.first] = clampOptional(value.second, m_minValue, m_maxValue); + else + m_values[value.first] = value.second; + } + + return *this; + } + + void unset(eOverridePriority priority) { + m_values.erase(priority); + } + + bool hasValue() { + return !m_values.empty(); + } + + T value() { + if (!m_values.empty()) + return std::prev(m_values.end())->second; + else + throw std::bad_optional_access(); + } + + T valueOr(T const& other) { + if (hasValue()) + return value(); + else + return other; + } + + T valueOrDefault() + requires(Extended && !std::is_same_v) + { + if (hasValue()) + return value(); + else if (m_defaultValue.has_value()) + return m_defaultValue.value(); + else + return **std::any_cast>>(m_configValue); + } + + T valueOrDefault() + requires(!Extended || std::is_same_v) + { + if (hasValue()) + return value(); + else if (!m_defaultValue.has_value()) + throw std::bad_optional_access(); + else + return m_defaultValue.value(); + } + + eOverridePriority getPriority() { + if (!m_values.empty()) + return std::prev(m_values.end())->first; + else + throw std::bad_optional_access(); + } + + void increment(T const& other, eOverridePriority priority) { + if constexpr (std::is_same_v) + m_values[priority] = valueOr(false) ^ other; + else + m_values[priority] = clampOptional(valueOrDefault() + other, m_minValue, m_maxValue); + } + + void matchOptional(std::optional const& optValue, eOverridePriority priority) { + if (optValue.has_value()) + m_values[priority] = optValue.value(); + else + unset(priority); + } + + operator std::optional() { + if (hasValue()) + return value(); + else + return std::nullopt; + } + + private: + std::map m_values; + std::optional m_defaultValue; // used for toggling, so required for bool + std::optional m_minValue; + std::optional m_maxValue; + std::any m_configValue; // only there for select variables +}; -- cgit v1.2.3