Skip to content

Commit

Permalink
feat: create window rule dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Oct 8, 2023
1 parent ec59928 commit 39258f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public CommandResponse Handle(ReloadUserConfigCommand command)

foreach (var window in _windowService.GetWindows())
{
var windowRules = _userConfigService.GetMatchingWindowRules(window);
var windowRules = _userConfigService.GetWindowRules(
window,
WindowRuleType.Manage
);

var windowRuleCommands = windowRules
.SelectMany(rule => rule.CommandList)
.Select(CommandParsingService.FormatCommand);
Expand Down
42 changes: 34 additions & 8 deletions GlazeWM.Domain/UserConfigs/UserConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ namespace GlazeWM.Domain.UserConfigs
{
public class UserConfigService
{
private UserConfig _userConfig;

/// <summary>
/// The deserialized user config. Sections of the config can be accessed via getters.
/// </summary>
public UserConfig UserConfig { private get; set; }
public UserConfig UserConfig {
get => _userConfig;
set {
_userConfig = value;
_windowRulesByType = GetWindowRulesByType(value);
}
};

public GapsConfig GapsConfig => UserConfig.Gaps;
public FocusBordersConfig FocusBorderConfig => UserConfig.FocusBorderColor;
Expand All @@ -28,11 +36,7 @@ public class UserConfigService
/// Dictionary of window rule types (eg. 'Manage', 'Focus') and the corresponding
/// window rules of that type.
/// </summary>
private Dictionary<WindowRuleType, List<WindowRuleConfig>> WindowRulesByType =
new(Enum.GetValues(typeof(WindowRuleType)).ToDictionary(
(ruleType) => ruleType,
(_) => new List<WindowRuleConfig>()
));
private Dictionary<WindowRuleType, List<WindowRuleConfig>> _windowRulesByType = new();

/// <summary>
/// Path to the user's config file.
Expand Down Expand Up @@ -115,6 +119,26 @@ private static List<WindowRuleConfig> GetDefaultWindowRules()
return windowRules;
}

private Dictionary<WindowRuleType, List<WindowRuleConfig>> GetWindowRulesByType(
UserConfig userConfig)
{
var windowRulesByType = Enum.GetValues(typeof(WindowRuleType)).ToDictionary(
(ruleType) => ruleType,
(_) => new List<WindowRuleConfig>()
);

foreach (var windowRule in userConfig.WindowRules)
{
foreach (var ruleType in windowRule.On)
{
var windowRules = windowRulesByType[ruleType];
windowRules.Add(windowRule);
}
}

return windowRulesByType;
}

public WorkspaceConfig GetWorkspaceConfigByName(string workspaceName)
{
return WorkspaceConfigs.Find(
Expand All @@ -138,9 +162,11 @@ public BarConfig GetBarConfigForMonitor(Monitor monitor)
return boundMonitor ?? BarConfigs[0];
}

public List<WindowRuleConfig> GetMatchingWindowRules(Window window)
public List<WindowRuleConfig> GetWindowRules(Window window, WindowRuleType ruleType)
{
return WindowRules.Where(rule =>
var windowRules = _windowRulesByType[ruleType];

return windowRules.Where(rule =>
{
return rule.ProcessNameRegex?.IsMatch(window.ProcessName) != false &&
rule.ClassNameRegex?.IsMatch(window.ClassName) != false &&
Expand Down

0 comments on commit 39258f4

Please sign in to comment.