From 9db62b824ca9858034bd89e0617cdf0364eb387a Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Fri, 22 Oct 2021 15:25:01 -0700 Subject: [PATCH] gears.matcher: Add `fallback` rules. Right now, all rules are additive, they are squashed into one big array of properties. This is normally fine, but sometime you want explicit rules for some objects, but also default rules if nothing matches. While this can be expressed in the current system by overriding *all* properties, this require more effort than having "special" and "fallback" rules. --- lib/gears/matcher.lua | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/gears/matcher.lua b/lib/gears/matcher.lua index 6f78b73e1..d024a2134 100644 --- a/lib/gears/matcher.lua +++ b/lib/gears/matcher.lua @@ -324,7 +324,20 @@ function matcher:add_property_setter(name, f) end local function default_rules_callback(self, o, props, callbacks, rules) - for _, entry in ipairs(self:matching_rules(o, rules)) do + -- Get all matching rules. + local matches = self:matching_rules(o, rules) + + -- Split the main ones from the fallback ones. + local main, fallback = {}, {} + + for _, match in ipairs(matches) do + table.insert(match.fallback and fallback or main, match) + end + + -- Select which set to use. + matches = #main > 0 and main or fallback + + for _, entry in ipairs(matches) do gtable.crush(props, entry.properties or {}) if entry.callback then