Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

fix(domain): correctly create regex for matching process name, class name, or title of a window rule

Lars Bergercc035d774eca
diff --git a/LarsWM.Domain/UserConfigs/WindowRuleConfig.cs b/LarsWM.Domain/UserConfigs/WindowRuleConfig.cs
index 48e98e9a..703263bc 100644
--- a/LarsWM.Domain/UserConfigs/WindowRuleConfig.cs
+++ b/LarsWM.Domain/UserConfigs/WindowRuleConfig.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
-using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
namespace LarsWM.Domain.UserConfigs
@@ -9,69 +8,37 @@ namespace LarsWM.Domain.UserConfigs
{
public string MatchProcessName { get; set; } = null;
- public Regex ProcessNameRegex
- {
- get
- {
- if (MatchProcessName == null || MatchProcessName.Trim() == String.Empty)
- return new Regex(".*");
-
- var startIndex = MatchProcessName.IndexOf("/") + "/".Length;
- var endIndex = MatchProcessName.LastIndexOf("/");
-
- var pattern = MatchProcessName.Substring(startIndex, endIndex - startIndex);
-
- if (pattern != null)
- return new Regex(pattern);
-
- return new Regex(MatchProcessName);
- }
- }
+ public Regex ProcessNameRegex => CreateRegex(MatchProcessName);
public string MatchClassName { get; set; } = null;
- public Regex ClassNameRegex
- {
- get
- {
- if (MatchClassName == null || MatchClassName.Trim() == String.Empty)
- return new Regex(".*");
-
- var startIndex = MatchClassName.IndexOf("/") + "/".Length;
- var endIndex = MatchClassName.LastIndexOf("/");
+ public Regex ClassNameRegex => CreateRegex(MatchClassName);
- var pattern = MatchClassName.Substring(startIndex, endIndex - startIndex);
+ public string MatchTitle { get; set; } = null;
- if (pattern != null)
- return new Regex(pattern);
+ public Regex TitleRegex => CreateRegex(MatchTitle);
- return new Regex(MatchClassName);
- }
- }
+ public string Action { get; set; } = null;
- public string MatchTitle { get; set; } = null;
+ public List<string> Actions { get; set; } = new List<string>();
- public Regex TitleRegex
+ /// <summary>
+ /// Creates an exact match regex for the given string.
+ /// </summary>
+ /// <returns>The corresponding regex, or null if input is invalid.</returns>
+ private Regex CreateRegex(string input)
{
- get
- {
- if (MatchTitle == null || MatchTitle.Trim() == String.Empty)
- return new Regex(".*");
-
- var startIndex = MatchTitle.IndexOf("/") + "/".Length;
- var endIndex = MatchTitle.LastIndexOf("/");
+ if (String.IsNullOrWhiteSpace(input))
+ return null;
- var pattern = MatchTitle.Substring(startIndex, endIndex - startIndex);
+ var isRegexLiteral = input.StartsWith("/") && input.EndsWith("/");
- if (pattern != null)
- return new Regex(pattern);
+ // Allow user to pass a string that should be interpreted as regex (eg. "/steam/").
+ if (isRegexLiteral)
+ return new Regex(input.Substring(1, input.Length - 2));
- return new Regex(MatchTitle);
- }
+ // Otherwise, create an exact match regex.
+ return new Regex($"^{input}$");
}
-
- public string Action { get; set; } = null;
-
- public List<string> Actions { get; set; } = new List<string>();
}
}