Skip to content

Commit

Permalink
Add default commands for plugins (#161)
Browse files Browse the repository at this point in the history
- `IPlugin` now has `GetCommands`
- Added useful default commands for:
  - `CommandPalettePlugin`
  - `FloatingLayoutPlugin`
  - `GapsPlugin`
- Removed constructor for `GapsConfig`
- Added `WinCtrlShift` keybind to `DefaultCommands`
- Fixed `ConfigLoader` to load the template from the `Whim` assembly
- Removed unnecessary copying of template to `Whim.Runner`
- Updated template to include the default commands
  • Loading branch information
dalyIsaac authored Jul 25, 2022
1 parent e5bd01b commit bee2194
Showing 16 changed files with 156 additions and 76 deletions.
9 changes: 6 additions & 3 deletions omnisharp.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"FileOptions": {
"ExcludeSearchPatterns": [
"src/Whim.Runner/Template/whim.config.csx"
]
"ExcludeSearchPatterns": ["src/Whim.Runner/Template/whim.config.csx"]
},
"RoslynExtensionsOptions": {
"EnableAnalyzersSupport": true
},
"script": {
"enabled": true,
"defaultTargetFramework": "net6.0-windows10.0.19041.0",
"enableScriptNuGetReferences": true
}
}
3 changes: 3 additions & 0 deletions src/Whim.Bar/BarPlugin.cs
Original file line number Diff line number Diff line change
@@ -107,4 +107,7 @@ public void Dispose()
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <inheritdoc />
public (ICommand, IKeybind?)[] GetCommands() => Array.Empty<(ICommand, IKeybind?)>();
}
15 changes: 15 additions & 0 deletions src/Whim.CommandPalette/CommandPalettePlugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Windows.Win32.UI.Input.KeyboardAndMouse;

namespace Whim.CommandPalette;

@@ -94,4 +95,18 @@ public void Dispose()
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <inheritdoc />
public (ICommand, IKeybind?)[] GetCommands() => new (ICommand, IKeybind?)[]
{
// Toggle command palette
(
new Command(
identifier: "command_palette.toggle",
title: "Toggle command palette",
callback: () => Activate()
),
new Keybind(DefaultCommands.WinShift, VIRTUAL_KEY.VK_K)
)
};
}
24 changes: 0 additions & 24 deletions src/Whim.FloatingLayout/FloatingLayoutCommands.cs

This file was deleted.

16 changes: 16 additions & 0 deletions src/Whim.FloatingLayout/FloatingLayoutPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Windows.Win32.UI.Input.KeyboardAndMouse;

namespace Whim.FloatingLayout;

/// <summary>
@@ -84,4 +86,18 @@ public void ToggleWindowFloating(IWindow? window = null)
floatingLayoutEngine.ToggleWindowFloating(window);
_configContext.WorkspaceManager.ActiveWorkspace.DoLayout();
}

/// <inheritdoc />
public (ICommand, IKeybind?)[] GetCommands() => new (ICommand, IKeybind?)[]
{
// Toggle window floating.
(
new Command(
identifier: "floating_layout.toggle_window_floating",
title: "Toggle window floating",
callback: () => ToggleWindowFloating()
),
new Keybind(DefaultCommands.WinShift, VIRTUAL_KEY.VK_F)
)
};
}
3 changes: 3 additions & 0 deletions src/Whim.FocusIndicator/FocusIndicatorPlugin.cs
Original file line number Diff line number Diff line change
@@ -163,4 +163,7 @@ public void Dispose()
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

/// <inheritdoc />
public (ICommand, IKeybind?)[] GetCommands() => Array.Empty<(ICommand, IKeybind?)>();
}
19 changes: 10 additions & 9 deletions src/Whim.Gaps/GapsConfig.cs
Original file line number Diff line number Diff line change
@@ -13,16 +13,17 @@ public class GapsConfig
/// <summary>
/// The gap between windows.
/// </summary>
public int InnerGap { get; set; }
public int InnerGap { get; set; } = 10;

/// <summary>
/// Create a new instance of <see cref="GapsConfig"/>.
/// The default delta used by the commands <c>gaps.outer.increase</c> and
/// <c>gaps.outer.decrease</c>.
/// </summary>
/// <param name="outerGap"></param>
/// <param name="innerGap"></param>
public GapsConfig(int outerGap = 0, int innerGap = 0)
{
OuterGap = outerGap;
InnerGap = innerGap;
}
public int DefaultOuterDelta { get; set; } = 2;

/// <summary>
/// The default delta used by the commands <c>gaps.inner.increase</c> and
/// <c>gaps.inner.decrease</c>.
/// </summary>
public int DefaultInnerDelta { get; set; } = 2;
}
46 changes: 46 additions & 0 deletions src/Whim.Gaps/GapsPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Windows.Win32.UI.Input.KeyboardAndMouse;

namespace Whim.Gaps;

/// <summary>
@@ -48,4 +50,48 @@ public void UpdateInnerGap(int delta)
_gapsConfig.InnerGap += delta;
_configContext.WorkspaceManager.LayoutAllActiveWorkspaces();
}

/// <inheritdoc />
public (ICommand, IKeybind?)[] GetCommands() => new (ICommand, IKeybind?)[]
{
// Increase outer gap
(
new Command(
identifier: "gaps.outer.increase",
title: "Increase outer gap",
callback: () => UpdateOuterGap(_gapsConfig.DefaultOuterDelta)
),
new Keybind(DefaultCommands.WinCtrlShift, VIRTUAL_KEY.VK_L)
),

// Decrease outer gap
(
new Command(
identifier: "gaps.outer.decrease",
title: "Decrease outer gap",
callback: () => UpdateOuterGap(-_gapsConfig.DefaultOuterDelta)
),
new Keybind(DefaultCommands.WinCtrlShift, VIRTUAL_KEY.VK_H)
),

// Increase inner gap
(
new Command(
identifier: "gaps.inner.increase",
title: "Increase inner gap",
callback: () => UpdateInnerGap(_gapsConfig.DefaultInnerDelta)
),
new Keybind(DefaultCommands.WinCtrlShift, VIRTUAL_KEY.VK_K)
),

// Decrease inner gap
(
new Command(
identifier: "gaps.inner.decrease",
title: "Decrease inner gap",
callback: () => UpdateInnerGap(-_gapsConfig.DefaultInnerDelta)
),
new Keybind(DefaultCommands.WinCtrlShift, VIRTUAL_KEY.VK_J)
)
};
}
3 changes: 1 addition & 2 deletions src/Whim.Runner/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.UI.Xaml;
using System;
using System.Reflection;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -40,7 +39,7 @@ private void StartWhim()
{
try
{
_configContext = Engine.CreateConfigContext(Assembly.GetAssembly(typeof(Program)));
_configContext = Engine.CreateConfigContext();

_configContext.Exited += ConfigContext_Exited;
_configContext.Initialize();
6 changes: 0 additions & 6 deletions src/Whim.Runner/Whim.Runner.csproj
Original file line number Diff line number Diff line change
@@ -104,12 +104,6 @@
</ItemGroup>

<Target Name="CopyPluginsAfterBuild" AfterTargets="Build;Publish">
<!--Copy the Template folder from Whim-->
<ItemGroup>
<TemplateSourceFile Include="$(SolutionDir)\src\Whim\Template\**\*" />
</ItemGroup>
<Copy SourceFiles="@(TemplateSourceFile)" DestinationFolder="$(TargetDir)Template" />

<!--Whim.Bar-->
<ItemGroup>
<BarSourceFile Include="$(SolutionDir)\src\Whim.Bar\$(OutDir)\Whim.Bar\**\*" />
7 changes: 6 additions & 1 deletion src/Whim/Commands/DefaultCommands.cs
Original file line number Diff line number Diff line change
@@ -24,7 +24,12 @@ public static class DefaultCommands
public const KeyModifiers WinCtrl = KeyModifiers.LWin | KeyModifiers.LControl;

/// <summary>
/// Gets the default commands.
/// The value for the key modifier <c>Win</c> + <c>Ctrl</c> + <c>Shift</c>.
/// </summary>
public const KeyModifiers WinCtrlShift = KeyModifiers.LWin | KeyModifiers.LControl | KeyModifiers.LShift;

/// <summary>
/// Gets the default commands and their associated keybinds.
/// </summary>
public static (ICommand, IKeybind?)[] GetCommands(IConfigContext configContext) => new (ICommand, IKeybind?)[]
{
12 changes: 2 additions & 10 deletions src/Whim/ConfigContext/ConfigContext.cs
Original file line number Diff line number Diff line change
@@ -14,11 +14,6 @@ namespace Whim;
/// </summary>
internal class ConfigContext : IConfigContext
{
/// <summary>
/// The assembly which is running this context.
/// </summary>
private readonly Assembly _assembly;

public Logger Logger { get; private set; }
public IWorkspaceManager WorkspaceManager { get; private set; }
public IWindowManager WindowManager { get; private set; }
@@ -34,11 +29,8 @@ internal class ConfigContext : IConfigContext
/// <summary>
/// Create a new <see cref="IConfigContext"/>.
/// </summary>
/// <param name="assembly">The assembly which is running this context.</param>
public ConfigContext(Assembly assembly)
public ConfigContext()
{
_assembly = assembly;

Logger = new Logger();
RouterManager = new RouterManager(this);
FilterManager = new FilterManager();
@@ -52,7 +44,7 @@ public ConfigContext(Assembly assembly)
public void Initialize()
{
// Load the config context.
DoConfig doConfig = ConfigLoader.LoadConfigContext(_assembly);
DoConfig doConfig = ConfigLoader.LoadConfigContext();
doConfig(this);

// Initialize the managers.
14 changes: 9 additions & 5 deletions src/Whim/ConfigLoader/ConfigLoader.cs
Original file line number Diff line number Diff line change
@@ -75,10 +75,15 @@ private static string ReadTemplateConfigFile(this Assembly assembly)
/// Creates a config based on the Whim template and saves it to the config file.
/// This will throw if any null values are encountered.
/// </summary>
/// <param name="assembly">The assembly from which to load.</param>
/// <exception cref="ConfigLoaderException"></exception>
private static void CreateConfig(Assembly assembly)
private static void CreateConfig()
{
Assembly? assembly = Assembly.GetAssembly(typeof(ConfigLoader));
if (assembly is null)
{
throw new ConfigLoaderException("Could not find assembly for ConfigLoader");
}

string template = assembly.ReadTemplateConfigFile();
string omnisharpJson = assembly.ReadFile("omnisharp.json");

@@ -90,10 +95,9 @@ private static void CreateConfig(Assembly assembly)
/// <summary>
/// Acquires and evaluates the user's <see cref="IConfigContext"/>.
/// </summary>
/// <param name="assembly"></param>
/// <returns>The <see cref="IConfigContext"/>.</returns>
/// <exception cref="ConfigLoaderException"></exception>
internal static DoConfig LoadConfigContext(Assembly assembly)
internal static DoConfig LoadConfigContext()
{
// Ensure the Whim directory exists.
FileHelper.EnsureWhimDirExists();
@@ -102,7 +106,7 @@ internal static DoConfig LoadConfigContext(Assembly assembly)
bool configExists = DoesConfigExist();
if (!configExists)
{
CreateConfig(assembly);
CreateConfig();
}

string rawConfig = LoadRawConfig();
11 changes: 2 additions & 9 deletions src/Whim/Engine.cs
Original file line number Diff line number Diff line change
@@ -12,19 +12,12 @@ public static class Engine
/// <summary>
/// Get the <see cref="IConfigContext"/>.
/// </summary>
/// <param name="assembly">The calling assembly.</param>
/// <returns>The <see cref="IConfigContext"/>.</returns>
/// <exception cref="ConfigLoaderException"></exception>
public static IConfigContext CreateConfigContext(Assembly? assembly)
public static IConfigContext CreateConfigContext()
{
if (_configContext == null)
{
if (assembly == null)
{
throw new ConfigLoaderException("Provided assembly was null.");
}

_configContext = new ConfigContext(assembly);
_configContext = new ConfigContext();
}

return _configContext;
7 changes: 7 additions & 0 deletions src/Whim/Plugin/IPlugin.cs
Original file line number Diff line number Diff line change
@@ -19,4 +19,11 @@ public interface IPlugin
/// Put things which rely on the rest of the config context here.
/// </summary>
public void PostInitialize();

/// <summary>
/// Get the default commands for this plugin, and their associated keybinds.
/// It's up to the user to register the commands themselves using
/// <see cref="ICommandManager.LoadCommands"/>.
/// </summary>
public (ICommand, IKeybind?)[] GetCommands();
}
Loading

0 comments on commit bee2194

Please sign in to comment.