Skip to content

Commit

Permalink
Sticky workspace YAML config
Browse files Browse the repository at this point in the history
  • Loading branch information
dalyIsaac committed Nov 10, 2024
1 parent 51eba21 commit dccbd70
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
69 changes: 69 additions & 0 deletions src/Whim.Yaml.Tests/YamlLoader/YamlLoader_LoadWorkspacesTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using FluentAssertions;
using NSubstitute;
using Whim.SliceLayout;
using Whim.TestUtils;
using Xunit;
Expand Down Expand Up @@ -149,4 +150,72 @@ public void Load_NoWorkspaces(string config, bool isYaml, IContext ctx)
IWorkspace[] workspaces = YamlLoaderTestUtils.GetWorkspaces(ctx)!;
Assert.Empty(workspaces);
}

public static TheoryData<string, bool> WorkspacesMonitorConfig =>
new()
{
{
"""
workspaces:
entries:
- name: workspace1
monitors: [0, 1]
- name: workspace2
monitors: [2]
- name: workspace3
monitors: []
""",
true
},
{
"""
{
"workspaces": {
"entries": [
{
"name": "workspace1",
"monitors": [0, 1]
},
{
"name": "workspace2",
"monitors": [2]
},
{
"name": "workspace3",
"monitors": []
}
]
}
}
""",
false
},
};

[Theory, MemberAutoSubstituteData<YamlLoaderCustomization>(nameof(WorkspacesMonitorConfig))]
internal void Load_WorkspacesWithMonitors(string config, bool isYaml, IContext ctx)
{
// Given a valid config with workspaces and monitor indices
YamlLoaderTestUtils.SetupFileConfig(ctx, config, isYaml);

// When loading the workspaces
bool result = YamlLoader.Load(ctx, showErrorWindow: false);

// Then the workspaces and monitor indices are loaded correctly
Assert.True(result);

// Verify the expected transforms were executed
var received = ctx.Store.ReceivedCalls().ToArray();
Assert.Equal(3, received.Length);

// Verify the monitor indices.
AddWorkspaceTransform[] transforms = received
.Select(c => c.GetArguments()[0])
.OfType<AddWorkspaceTransform>()
.ToArray();

transforms[0].MonitorIndices.Should().BeEquivalentTo([0, 1]);
transforms[1].MonitorIndices.Should().BeEquivalentTo([2]);
transforms[2].MonitorIndices.Should().BeEmpty();
}
}
13 changes: 12 additions & 1 deletion src/Whim.Yaml/YamlLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,23 @@ private static void UpdateWorkspaces(IContext ctx, Schema schema)
string workspaceName = (string)workspace.Name;

CreateLeafLayoutEngine[]? engines = null;
List<int>? monitorIndices = null;

if (workspace.LayoutEngines.Entries.AsOptional() is Schema.RequiredEntries.RequiredTypeArray definedEngines)
{
engines = YamlLayoutEngineLoader.GetCreateLeafLayoutEngines(ctx, [.. definedEngines]);
}

ctx.Store.Dispatch(new AddWorkspaceTransform(workspaceName, engines));
if (workspace.Monitors.AsOptional() is Schema.RequiredName.MonitorsEntityArray definedMonitorIndices)
{
monitorIndices = [];
foreach (var monitorIndex in definedMonitorIndices)
{
monitorIndices.Add((int)monitorIndex);
}
}

ctx.Store.Dispatch(new AddWorkspaceTransform(workspaceName, engines, MonitorIndices: monitorIndices));
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Whim.Yaml/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@
"layout_engines": {
"$ref": "#/$defs/LayoutEngineList",
"description": "The layout engines to use for the workspace. If not specified, the layout engines in `layout_engines` will be used."
},
"monitors": {
"type": "array",
"items": {
"type": "integer",
"minimum": 0
},
"description": "The monitors which this workspace is restricted to. This is a 0-indexed list of monitor indices - see https://dalyisaac.github.io/Whim/configure/core/monitors.html. If not specified, this workspace will be available on all monitors."
}
}
},
Expand Down

0 comments on commit dccbd70

Please sign in to comment.