Skip to content

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

refactor(domain): create common `WorkspaceService` method for getting workspace config to activate

Lars Bergerb74842c93252
diff --git a/GlazeWM.Domain/Monitors/CommandHandlers/AddMonitorHandler.cs b/GlazeWM.Domain/Monitors/CommandHandlers/AddMonitorHandler.cs
index 35ae4861..4bbdc550 100644
--- a/GlazeWM.Domain/Monitors/CommandHandlers/AddMonitorHandler.cs
+++ b/GlazeWM.Domain/Monitors/CommandHandlers/AddMonitorHandler.cs
@@ -50,15 +50,13 @@ namespace GlazeWM.Domain.Monitors.CommandHandlers
private void ActivateWorkspaceOnMonitor(Monitor monitor)
{
// Get name of first workspace that is not active for that specified monitor or any.
- var inactiveWorkspaceName = _workspaceService.GetInactiveWorkspaceNameForMonitor(monitor) ??
- _workspaceService.GetInactiveWorkspaceNamesNotDedicatedToAMonitor().ElementAtOrDefault(0) ??
- _workspaceService.GetInactiveWorkspaceNames().ElementAtOrDefault(0);
+ var inactiveWorkspaceConfig = _workspaceService.GetWorkspaceConfigToActivate(monitor);
- if (inactiveWorkspaceName == null)
+ if (inactiveWorkspaceConfig is null)
throw new FatalUserException("At least 1 workspace is required per monitor.");
// Assign the workspace to the newly added monitor.
- _bus.Invoke(new ActivateWorkspaceCommand(inactiveWorkspaceName, monitor));
+ _bus.Invoke(new ActivateWorkspaceCommand(inactiveWorkspaceConfig.Name, monitor));
}
}
}
diff --git a/GlazeWM.Domain/Workspaces/CommandHandlers/UpdateWorkspacesFromConfigHandler.cs b/GlazeWM.Domain/Workspaces/CommandHandlers/UpdateWorkspacesFromConfigHandler.cs
index e79d0f36..76406237 100644
--- a/GlazeWM.Domain/Workspaces/CommandHandlers/UpdateWorkspacesFromConfigHandler.cs
+++ b/GlazeWM.Domain/Workspaces/CommandHandlers/UpdateWorkspacesFromConfigHandler.cs
@@ -1,27 +1,18 @@
-using System.Linq;
-using GlazeWM.Infrastructure.Bussing;
+using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Domain.Workspaces.Commands;
using GlazeWM.Domain.Monitors;
using GlazeWM.Infrastructure.Exceptions;
-using GlazeWM.Domain.UserConfigs;
namespace GlazeWM.Domain.Workspaces.CommandHandlers
{
internal class UpdateWorkspacesFromConfigHandler :
ICommandHandler<UpdateWorkspacesFromConfigCommand>
{
- private readonly Bus _bus;
private readonly WorkspaceService _workspaceService;
- private readonly UserConfigService _userConfigService;
- public UpdateWorkspacesFromConfigHandler(
- Bus bus,
- WorkspaceService workspaceService,
- UserConfigService userConfigService)
+ public UpdateWorkspacesFromConfigHandler(WorkspaceService workspaceService)
{
- _bus = bus;
_workspaceService = workspaceService;
- _userConfigService = userConfigService;
}
public CommandResponse Handle(UpdateWorkspacesFromConfigCommand command)
@@ -36,18 +27,12 @@ namespace GlazeWM.Domain.Workspaces.CommandHandlers
if (workspaceConfig is null)
{
var monitor = workspace.Parent as Monitor;
- // TODO: Move this to a dedicated `WorkspaceService` method.
- var inactiveWorkspaceName = _workspaceService.GetInactiveWorkspaceNameForMonitor(monitor) ??
- _workspaceService.GetInactiveWorkspaceNamesNotDedicatedToAMonitor().ElementAtOrDefault(0) ??
- _workspaceService.GetInactiveWorkspaceNames().ElementAtOrDefault(0);
+ var inactiveWorkspaceConfig = _workspaceService.GetWorkspaceConfigToActivate(monitor);
- if (inactiveWorkspaceName is null)
+ if (inactiveWorkspaceConfig is null)
throw new FatalUserException("At least 1 workspace is required per monitor.");
- var inactiveWorkspaceConfig =
- _userConfigService.GetWorkspaceConfigByName(inactiveWorkspaceName);
-
- workspace.Name = inactiveWorkspaceName;
+ workspace.Name = inactiveWorkspaceConfig.Name;
}
// TODO: Update `DisplayName` and `KeepAlive` once they are changed to properties.
diff --git a/GlazeWM.Domain/Workspaces/WorkspaceService.cs b/GlazeWM.Domain/Workspaces/WorkspaceService.cs
index 0162376d..ea20167e 100644
--- a/GlazeWM.Domain/Workspaces/WorkspaceService.cs
+++ b/GlazeWM.Domain/Workspaces/WorkspaceService.cs
@@ -32,29 +32,31 @@ namespace GlazeWM.Domain.Workspaces
return GetActiveWorkspaces().FirstOrDefault(workspace => workspace.Name == name);
}
- public IEnumerable<string> GetInactiveWorkspaceNames()
+ public IEnumerable<WorkspaceConfig> GetInactiveWorkspaceConfigs()
{
var activeWorkspaces = GetActiveWorkspaces();
- var inactiveWorkspaceConfigs = _userConfigService.UserConfig.Workspaces.Where(
+ return _userConfigService.UserConfig.Workspaces.Where(
(config) => !activeWorkspaces.Any((workspace) => workspace.Name == config.Name)
);
-
- return inactiveWorkspaceConfigs.Select(config => config.Name);
- }
-
- public string GetInactiveWorkspaceNameForMonitor(Monitor monitor)
- {
- return GetInactiveWorkspaceNames()
- .FirstOrDefault(w =>
- _userConfigService.UserConfig.Workspaces.First(uw => uw.Name == w).BindToMonitor == monitor.DeviceName);
}
- public IEnumerable<string> GetInactiveWorkspaceNamesNotDedicatedToAMonitor()
+ public WorkspaceConfig GetWorkspaceConfigToActivate(Monitor monitor)
{
- return GetInactiveWorkspaceNames()
- .Where(w => string.IsNullOrWhiteSpace(_userConfigService.UserConfig.Workspaces.First(uw => uw.Name == w)
- .BindToMonitor));
+ var inactiveWorkspaceConfigs = GetInactiveWorkspaceConfigs();
+ var boundWorkspaceConfig = inactiveWorkspaceConfigs
+ .FirstOrDefault(config => config.BindToMonitor == monitor.DeviceName);
+
+ if (boundWorkspaceConfig is not null)
+ return boundWorkspaceConfig;
+
+ var unreservedWorkspaceConfig = inactiveWorkspaceConfigs
+ .FirstOrDefault(config => string.IsNullOrWhiteSpace(config.BindToMonitor));
+
+ if (unreservedWorkspaceConfig is not null)
+ return unreservedWorkspaceConfig;
+
+ return inactiveWorkspaceConfigs.ElementAtOrDefault(0);
}
public static Workspace GetWorkspaceFromChildContainer(Container container)