Skip to content

Commit

Permalink
Sticky workspace YAML config (#1073)
Browse files Browse the repository at this point in the history
Added YAML config to support sticky workspaces
  • Loading branch information
dalyIsaac authored Nov 10, 2024
1 parent 51eba21 commit d76aef3
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 4 deletions.
11 changes: 9 additions & 2 deletions docs/configure/core/workspaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ workspaces:
## Sticky Workspaces
Sticky workspaces are being worked on in [this GitHub issue](https://github.com/dalyIsaac/Whim/issues/660).
Sticky workspaces can only be displayed on specific monitors. To create a sticky workspace, specify the monitor indices when creating the workspace:
👷🏗️🚧
```yaml
workspaces:
entries:
- name: Browser
monitors: [0, 1]
```
Here, the workspace can only be displayed on the first and second monitors (0-based index). For more on the ordering of monitors monitors, see the [Monitors](monitors.md) page.
2 changes: 1 addition & 1 deletion docs/script/core/workspaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Guid stickyWorkspaceId = context.Store.Dispatch(
);
```

The workspace will only be displayed on the first and second monitors (0-based index). For more on monitors, see the [Monitors](./monitors.md) page.
Here, the workspace can only be displayed on the first and second monitors (0-based index). For more on the ordering of monitors monitors, see the [Monitors](monitors.md) page.

## Example Command

Expand Down
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 d76aef3

Please sign in to comment.