Skip to content

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

refactor: roslynator fixes and code improvements (#190)

lars-bergerf9109c9f9dec
diff --git a/GlazeWM.Domain/Common/Enums/Sequence.cs b/GlazeWM.Domain/Common/Enums/Sequence.cs
index 40b573c6..8733ea76 100644
--- a/GlazeWM.Domain/Common/Enums/Sequence.cs
+++ b/GlazeWM.Domain/Common/Enums/Sequence.cs
@@ -1,6 +1,8 @@
namespace GlazeWM.Domain.Common.Enums
{
- // Used to select the direction when switching the previous/next workspace
+ /// <summary>
+ /// Used to represent a given order (eg. previous/next workspace).
+ /// </summary>
public enum Sequence
{
PREVIOUS,
diff --git a/GlazeWM.Domain/Common/Utils/Keywords.cs b/GlazeWM.Domain/Common/Utils/Keywords.cs
deleted file mode 100644
index 39331a6f..00000000
--- a/GlazeWM.Domain/Common/Utils/Keywords.cs
+++ /dev/null
@@ -1,13 +0,0 @@
-using System.Collections.Generic;
-
-namespace GlazeWM.Domain.Common.Utils
-{
- public static class Keywords
- {
- /// <summary>
- /// Keywords that are part of the "focus workspace" commands
- /// Keywords cannot be used as a workspace name
- /// </summary>
- public static HashSet<string> WorkspaceKeyswords = new HashSet<string>() { "prev", "next", "recent" };
- }
-}
diff --git a/GlazeWM.Domain/Containers/RootContainer.cs b/GlazeWM.Domain/Containers/RootContainer.cs
index 63e54a38..d83df3b2 100644
--- a/GlazeWM.Domain/Containers/RootContainer.cs
+++ b/GlazeWM.Domain/Containers/RootContainer.cs
@@ -4,6 +4,6 @@ namespace GlazeWM.Domain.Containers
{
public sealed class RootContainer : Container
{
- public override string Id { get; init; } = $"ROOT/{new Guid().ToString()}";
+ public override string Id { get; init; } = $"ROOT/{new Guid()}";
}
}
diff --git a/GlazeWM.Domain/Containers/SplitContainer.cs b/GlazeWM.Domain/Containers/SplitContainer.cs
index 9753dedc..916c8890 100644
--- a/GlazeWM.Domain/Containers/SplitContainer.cs
+++ b/GlazeWM.Domain/Containers/SplitContainer.cs
@@ -6,7 +6,7 @@ namespace GlazeWM.Domain.Containers
{
public class SplitContainer : Container, IResizable
{
- public override string Id { get; init; } = $"SPLIT/{new Guid().ToString()}";
+ public override string Id { get; init; } = $"SPLIT/{new Guid()}";
public Layout Layout { get; set; } = Layout.HORIZONTAL;
public double SizePercentage { get; set; } = 1;
diff --git a/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs b/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs
index d4a197bf..9bf67b8e 100644
--- a/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs
+++ b/GlazeWM.Domain/UserConfigs/CommandHandlers/EvaluateUserConfigHandler.cs
@@ -1,5 +1,4 @@
-using GlazeWM.Domain.Common.Utils;
-using GlazeWM.Domain.UserConfigs.Commands;
+using GlazeWM.Domain.UserConfigs.Commands;
using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Infrastructure.Exceptions;
using GlazeWM.Infrastructure.Serialization;
@@ -22,6 +21,16 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlers
private readonly YamlService _yamlService;
private readonly CommandParsingService _commandParsingService;
+ /// <summary>
+ /// Keywords that cannot be used as workspace names.
+ /// </summary>
+ private static readonly HashSet<string> RESERVED_WORKSPACE_NAMES = new()
+ {
+ "prev",
+ "next",
+ "recent"
+ };
+
public EvaluateUserConfigHandler(
Bus bus,
UserConfigService userConfigService,
@@ -114,8 +123,10 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlers
if (workspaceConfig.Name is null)
throw new FatalUserException("Property 'name' is required in workspace config.");
- if (Keywords.WorkspaceKeyswords.Contains(workspaceConfig.Name))
- throw new FatalUserException($"Can not use keyword '{workspaceConfig.Name}' as workspace name");
+ if (RESERVED_WORKSPACE_NAMES.Contains(workspaceConfig.Name))
+ throw new FatalUserException(
+ $"Cannot use keyword '{workspaceConfig.Name}' as workspace name."
+ );
}
var componentConfigs = deserializedConfig.Bar.ComponentsLeft
@@ -137,25 +148,19 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlers
throw new FatalUserException(
"Property 'binding' or 'bindings' is required in keybinding config."
);
- else
+
+ // Check that all keybindings in the config can be cast to `Keys` enum.
+ foreach (var keybinding in keybindingConfig.BindingList)
{
- // Checking that all keyboard shortcuts specified in the config can be cast to Keys values
- foreach (var keybinding in keybindingConfig.BindingList)
+ try
+ {
+ var _ = KeybindingHelper.TryGetKeys(keybinding);
+ }
+ catch (ArgumentException exception)
{
- var keybindingsParts = KeybindingHelper.GetFormattedKeybingingsParts(keybinding);
-
- foreach (var keybindingPart in keybindingsParts)
- {
- try
- {
- var key = Enum.Parse(typeof(Keys), keybindingPart);
- }
- catch (ArgumentException)
- {
- throw new FatalUserException(
- $"Unsuccessful bindings '{keybinding}' in keybinding config: unknown key '{keybindingPart}'");
- }
- }
+ throw new FatalUserException(
+ $"Invalid binding '{keybinding}' in keybinding config: {exception.Message}"
+ );
}
}
diff --git a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
index 521bf513..f5673aec 100644
--- a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
+++ b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
@@ -4,7 +4,6 @@ using System.Linq;
using System.Text.RegularExpressions;
using GlazeWM.Domain.Common.Commands;
using GlazeWM.Domain.Common.Enums;
-using GlazeWM.Domain.Common.Utils;
using GlazeWM.Domain.Containers;
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.UserConfigs.Commands;
@@ -114,8 +113,7 @@ namespace GlazeWM.Domain.UserConfigs
"right" => new FocusInDirectionCommand(Direction.RIGHT),
"up" => new FocusInDirectionCommand(Direction.UP),
"down" => new FocusInDirectionCommand(Direction.DOWN),
- "workspace" when IsValidWorkspace(commandParts[2]) =>
- ParseFocusWorkspaceCommand(commandParts),
+ "workspace" => ParseFocusWorkspaceCommand(commandParts),
_ => throw new ArgumentException(null, nameof(commandParts)),
};
}
@@ -127,8 +125,8 @@ namespace GlazeWM.Domain.UserConfigs
"recent" => new FocusWorkspaceRecentCommand(),
"prev" => new FocusWorkspaceSequenceCommand(Sequence.PREVIOUS),
"next" => new FocusWorkspaceSequenceCommand(Sequence.NEXT),
- // errors already checked at the previous level parsing
- _ => new FocusWorkspaceCommand(commandParts[2]),
+ _ when IsValidWorkspace(commandParts[2]) => new FocusWorkspaceCommand(commandParts[2]),
+ _ => throw new ArgumentException(null, nameof(commandParts)),
};
}
@@ -151,12 +149,12 @@ namespace GlazeWM.Domain.UserConfigs
"to" when IsValidWorkspace(commandParts[3]) => subjectContainer is Window
? new MoveWindowToWorkspaceCommand(subjectContainer as Window, commandParts[3])
: new NoopCommand(),
- "workspace" => ParseMoveWorkspaceCommand(commandParts, subjectContainer),
+ "workspace" => ParseMoveWorkspaceCommand(commandParts),
_ => throw new ArgumentException(null, nameof(commandParts)),
};
}
- private Command ParseMoveWorkspaceCommand(string[] commandParts, Container subjectContainer)
+ private static Command ParseMoveWorkspaceCommand(string[] commandParts)
{
return commandParts[2] switch
{
@@ -256,21 +254,18 @@ namespace GlazeWM.Domain.UserConfigs
}
/// <summary>
- /// Whether a workspace exists with the given name
- /// or workspace name is part of focus workspace command.
+ /// Whether a workspace exists with the given name.
/// </summary>
private bool IsValidWorkspace(string workspaceName)
{
- if (Keywords.WorkspaceKeyswords.Contains(workspaceName))
- {
- return true;
- }
-
var workspaceConfig = _userConfigService.GetWorkspaceConfigByName(workspaceName);
return workspaceConfig is not null;
}
+ /// <summary>
+ /// Whether a binding mode exists with the given name.
+ /// </summary>
private bool IsValidBindingMode(string bindingModeName)
{
var bindingMode = _userConfigService.GetBindingModeByName(bindingModeName);
diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs
index 00c6c74f..d0aaaafb 100644
--- a/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs
+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowHiddenHandler.cs
@@ -42,7 +42,7 @@ namespace GlazeWM.Domain.Windows.EventHandlers
// Ignore events where the window isn't managed or is actually supposed to be hidden. Since
// window events are processed in a sequence, also handle case where the window is not
// actually hidden anymore when the event is processed.
- if (window is null || !window.IsDisplayed || WindowService.IsHandleVisible(window.Handle))
+ if (window?.IsDisplayed != true || WindowService.IsHandleVisible(window.Handle))
return;
_logger.LogWindowEvent("Window hidden", window);
diff --git a/GlazeWM.Domain/Windows/Window.cs b/GlazeWM.Domain/Windows/Window.cs
index ace0fd41..09b8ca52 100644
--- a/GlazeWM.Domain/Windows/Window.cs
+++ b/GlazeWM.Domain/Windows/Window.cs
@@ -31,7 +31,7 @@ namespace GlazeWM.Domain.Windows
protected Window(IntPtr handle, Rect floatingPlacement, RectDelta borderDelta)
{
- Id = $"WINDOW/{handle.ToString("x")}";
+ Id = $"WINDOW/{handle:x}";
Handle = handle;
FloatingPlacement = floatingPlacement;
BorderDelta = borderDelta;
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceToDirectionHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceToDirectionHandler.cs
index a3117188..e796c53d 100644
--- a/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceToDirectionHandler.cs
+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/MoveWorkspaceToDirectionHandler.cs
@@ -13,7 +13,7 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
private readonly MonitorService _monitorService;
private readonly WorkspaceService _workspaceService;
- public MoveWorkspaceInDirectionHandler (
+ public MoveWorkspaceInDirectionHandler(
Bus bus,
MonitorService monitorService,
WorkspaceService workspaceService)
@@ -23,7 +23,7 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
_workspaceService = workspaceService;
}
- public CommandResponse Handle (MoveWorkspaceInDirectionCommand command)
+ public CommandResponse Handle(MoveWorkspaceInDirectionCommand command)
{
// Get focused workspace
var workspace = _workspaceService.GetFocusedWorkspace();
@@ -46,7 +46,7 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
_bus.Invoke(new RedrawContainersCommand());
}
-
+
return CommandResponse.Ok;
}
}
diff --git a/GlazeWM.Domain/Workspaces/WorkspaceService.cs b/GlazeWM.Domain/Workspaces/WorkspaceService.cs
index 664594c5..1af510f5 100644
--- a/GlazeWM.Domain/Workspaces/WorkspaceService.cs
+++ b/GlazeWM.Domain/Workspaces/WorkspaceService.cs
@@ -55,10 +55,7 @@ namespace GlazeWM.Domain.Workspaces
var unreservedWorkspaceConfig = inactiveWorkspaceConfigs
.FirstOrDefault(config => string.IsNullOrWhiteSpace(config.BindToMonitor));
- if (unreservedWorkspaceConfig is not null)
- return unreservedWorkspaceConfig;
-
- return inactiveWorkspaceConfigs.ElementAtOrDefault(0);
+ return unreservedWorkspaceConfig ?? inactiveWorkspaceConfigs.ElementAtOrDefault(0);
}
public static Workspace GetWorkspaceFromChildContainer(Container container)
diff --git a/GlazeWM.Infrastructure/DependencyInjection.cs b/GlazeWM.Infrastructure/DependencyInjection.cs
index 0b5b994f..2d3b448b 100644
--- a/GlazeWM.Infrastructure/DependencyInjection.cs
+++ b/GlazeWM.Infrastructure/DependencyInjection.cs
@@ -1,7 +1,6 @@
using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Infrastructure.Common.Commands;
using GlazeWM.Infrastructure.Common.CommandHandlers;
-using GlazeWM.Infrastructure.Exceptions;
using GlazeWM.Infrastructure.Serialization;
using GlazeWM.Infrastructure.WindowsApi;
using Microsoft.Extensions.DependencyInjection;
diff --git a/GlazeWM.Infrastructure/Utils/KeybindingHelper.cs b/GlazeWM.Infrastructure/Utils/KeybindingHelper.cs
index 7472aa21..ec29a16b 100644
--- a/GlazeWM.Infrastructure/Utils/KeybindingHelper.cs
+++ b/GlazeWM.Infrastructure/Utils/KeybindingHelper.cs
@@ -1,39 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
using System.Windows.Forms;
namespace GlazeWM.Infrastructure.Utils
{
public static class KeybindingHelper
{
- public static IEnumerable<string> GetFormattedKeybingingsParts (string keybingingString)
+ public static List<Keys> TryGetKeys(string keybindingString)
{
- var keybindingParts = keybingingString
- .Split('+')
- .Select(key => FormatKeybinding(key));
-
- return keybindingParts;
+ return keybindingString
+ .Split('+')
+ .Select(keyString => FormatKeyString(keyString))
+ .Select(keyString => TryParseKeyString(keyString))
+ .ToList();
}
- public static IEnumerable<Keys> GetKeys (string keybindingString)
+ private static string FormatKeyString(string keyString)
{
- var keybingingParts = GetFormattedKeybingingsParts(keybindingString);
-
- var keys = keybingingParts
- .Select(key => Enum.Parse(typeof(Keys), key))
- .Cast<Keys>();
+ var isNumeric = int.TryParse(keyString, out var _);
- return keys;
+ return isNumeric ? $"D{keyString}" : keyString;
}
- private static string FormatKeybinding (string key)
+ private static Keys TryParseKeyString(string keyString)
{
- var isNumeric = int.TryParse(key, out var _);
-
- return isNumeric ? $"D{key}" : key;
+ try
+ {
+ return Enum.Parse<Keys>(keyString);
+ }
+ catch (ArgumentException)
+ {
+ throw new ArgumentException($"Unknown key '{keyString}'");
+ }
}
}
}
diff --git a/GlazeWM.Infrastructure/WindowsApi/KeybindingService.cs b/GlazeWM.Infrastructure/WindowsApi/KeybindingService.cs
index 6528c12a..0d5b250c 100644
--- a/GlazeWM.Infrastructure/WindowsApi/KeybindingService.cs
+++ b/GlazeWM.Infrastructure/WindowsApi/KeybindingService.cs
@@ -63,10 +63,10 @@ namespace GlazeWM.Infrastructure.WindowsApi
public void AddGlobalKeybinding(string keybindingString, Action callback)
{
- var keybindingParts = KeybindingHelper.GetKeys(keybindingString).ToList();
+ var keybindingKeys = KeybindingHelper.TryGetKeys(keybindingString);
- var triggerKey = keybindingParts.Last();
- var keybinding = new Keybinding(keybindingParts, callback);
+ var triggerKey = keybindingKeys.Last();
+ var keybinding = new Keybinding(keybindingKeys, callback);
if (_keybindingsByTriggerKey.ContainsKey(triggerKey))
{