Skip to content

Commit

Permalink
LayoutPreviewPlugin YAML config (#1029)
Browse files Browse the repository at this point in the history
Added a declarative config for the LayoutPreviewPlugin.
  • Loading branch information
dalyIsaac authored Sep 17, 2024
1 parent 80e837a commit 1539fc6
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/docs/customize/yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,15 @@ The color can be any valid RGB or RGBA hex color. The following named colors are
| `white_smoke` | `#FFF5F5F5` | <span style="background:#F5F5F5" class="color-block"></span> |
| `yellow` | `#FFFFFF00` | <span style="background:#FFFF00" class="color-block"></span> |
| `yellow_green` | `#FF9ACD32` | <span style="background:#9ACD32" class="color-block"></span> |

### Layout Preview Plugin

The `LayoutPreviewPlugin` adds a layout preview to Whim.

![Layout preview demo](../../images/layout-preview-demo.gif)

```yaml
plugins:
layout_preview:
is_enabled: true
```
2 changes: 1 addition & 1 deletion src/Whim.Yaml.Tests/YamlLoader_LoadFocusIndicatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ IContext ctx
}

public static TheoryData<string, bool> DisabledFocusIndicatorConfig =>
new TheoryData<string, bool>()
new()
{
// YAML, is_enabled set to false
{
Expand Down
114 changes: 114 additions & 0 deletions src/Whim.Yaml.Tests/YamlLoader_LoadLayoutPreviewTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using NSubstitute;
using Whim.LayoutPreview;
using Whim.TestUtils;
using Xunit;

namespace Whim.Yaml.Tests;

public class YamlLoader_LoadLayoutPreviewTests
{
public static TheoryData<string, bool, bool> LayoutPreviewConfig =>
new()
{
// YAML, enabled'
{
"""
plugins:
layout_preview:
is_enabled: true
""",
true,
true
},
// JSON, enabled
{
"""
{
"plugins": {
"layout_preview": {
"is_enabled": true
}
}
}
""",
false,
true
},
// YAML, no values set
{
"""
plugins:
layout_preview: {}
""",
true,
false
},
// JSON, no values set
{
"""
{
"plugins": {
"layout_preview": {}
}
}
""",
false,
false
},
};

[Theory, MemberAutoSubstituteData<YamlLoaderCustomization>(nameof(LayoutPreviewConfig))]
public void LoadLayoutPreviewPlugin(string yaml, bool isYaml, bool isEnabled, IContext ctx)
{
// Given a a YAML config with the layout preview plugin
YamlLoaderTestUtils.SetupFileConfig(ctx, yaml, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);

// Then the result is true, and the layout preview plugin is set
Assert.True(result);
ctx.PluginManager.Received().AddPlugin(Arg.Any<LayoutPreviewPlugin>());
}

public static TheoryData<string, bool> DisabledLayoutPreviewConfig =>
new()
{
// YAML
{
"""
plugins:
layout_preview:
is_enabled: false
""",
true
},
// JSON
{
"""
{
"plugins": {
"layout_preview": {
"is_enabled": false
}
}
}
""",
false
},
};

[Theory, MemberAutoSubstituteData<YamlLoaderCustomization>(nameof(DisabledLayoutPreviewConfig))]
public void LoadLayoutPreviewPlugin_ConfigIsNotEnabled_PluginIsNotLoaded(string schema, bool isYaml, IContext ctx)
{
// Given a valid config with the layout preview plugin not enabled
YamlLoaderTestUtils.SetupFileConfig(ctx, schema, isYaml);

// When loading the config
bool result = YamlLoader.Load(ctx);

// Then the result is true, and the layout preview plugin is not set
Assert.True(result);
ctx.PluginManager.DidNotReceive().AddPlugin(Arg.Any<LayoutPreviewPlugin>());
}
}
1 change: 1 addition & 0 deletions src/Whim.Yaml/Whim.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<ProjectReference Include="..\Whim.CommandPalette\Whim.CommandPalette.csproj" />
<ProjectReference Include="..\Whim.FocusIndicator\Whim.FocusIndicator.csproj" />
<ProjectReference Include="..\Whim.Gaps\Whim.Gaps.csproj" />
<ProjectReference Include="..\Whim.LayoutPreview\Whim.LayoutPreview.csproj" />
<ProjectReference Include="..\Whim\Whim.csproj" />
</ItemGroup>

Expand Down
21 changes: 21 additions & 0 deletions src/Whim.Yaml/YamlPluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Whim.CommandPalette;
using Whim.FocusIndicator;
using Whim.Gaps;
using Whim.LayoutPreview;

namespace Whim.Yaml;

Expand All @@ -22,6 +23,7 @@ public static void LoadPlugins(IContext ctx, Schema schema)
LoadGapsPlugin(ctx, schema);
LoadCommandPalettePlugin(ctx, schema);
LoadFocusIndicatorPlugin(ctx, schema);
LoadLayoutPreviewPlugin(ctx, schema);
}

private static void LoadGapsPlugin(IContext ctx, Schema schema)
Expand Down Expand Up @@ -150,4 +152,23 @@ private static void LoadFocusIndicatorPlugin(IContext ctx, Schema schema)

ctx.PluginManager.AddPlugin(new FocusIndicatorPlugin(ctx, config));
}

private static void LoadLayoutPreviewPlugin(IContext ctx, Schema schema)
{
var layoutPreview = schema.Plugins.LayoutPreview;

if (!layoutPreview.IsValid())
{
Logger.Debug("LayoutPreview plugin is not valid.");
return;
}

if (layoutPreview.IsEnabled.AsOptional() is { } isEnabled && !isEnabled)
{
Logger.Debug("LayoutPreview plugin is not enabled.");
return;
}

ctx.PluginManager.AddPlugin(new LayoutPreviewPlugin(ctx));
}
}
18 changes: 18 additions & 0 deletions src/Whim.Yaml/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
"focus_indicator": {
"$ref": "#/$defs/FocusIndicatorPlugin",
"description": "Plugin to add a focus indicator"
},

"layout_preview": {
"$ref": "#/$defs/LayoutPreviewPlugin",
"description": "Plugin to add a layout preview"
}
}
}
Expand Down Expand Up @@ -306,6 +311,19 @@
}
},

"LayoutPreviewPlugin": {
"type": "object",
"description": "Plugin to add a layout preview. For more, see https://dalyisaac.github.io/Whim/docs/plugins/layout-preview.html",
"additionalProperties": false,
"properties": {
"is_enabled": {
"type": "boolean",
"description": "Whether the plugin is enabled",
"default": true
}
}
},

"Color": {
"anyOf": [
{
Expand Down

0 comments on commit 1539fc6

Please sign in to comment.