Skip to content

Commit

Permalink
added AbstractThemePlugin to make plugin implementations more error p…
Browse files Browse the repository at this point in the history
…roof (#29)
  • Loading branch information
wolframhaussig authored Feb 24, 2024
1 parent 314d303 commit 3f842bc
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 21 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,18 @@ After this, you need to register this class in the builder:
As we do not want to force you to use a specific WinForms control library, we currently only support styling of standard controls and controls from our [winforms-stylable-controls](https://github.com/Assorted-Development/winforms-stylable-controls) project.
As we understand you may want to also style other controls, we support adding specialised plugins to handle styling of a specific type of control. To do this, you need to implement ``:
```csharp
internal class MyCustomControlThemePlugin : IThemePlugin
internal class MyCustomControlThemePlugin : AbstractThemePlugin<MyCustomControl>
{
public void Apply(Control control, AbstractTheme theme)
protected override void ApplyPlugin(MyCustomControl mcc, AbstractTheme theme)
{
MyCustomControl mcc = (MyCustomControl)control;
//style control based on the colors available in the Theme
}
}
```
At last, you just need to register it for the correct type:
```csharp
ThemeRegistryHolder.GetBuilder()
.AddThemePlugin<MyCustomControl>(new MyCustomControlThemePlugin())
.AddThemePlugin(new MyCustomControlThemePlugin())
.Build();
```

Expand Down
12 changes: 6 additions & 6 deletions WinFormsThemes/TestProject/AbstractThemeTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Windows.Forms;
using System.Windows.Forms;
using WinFormsThemes;
using WinFormsThemes.Themes;

Expand All @@ -13,7 +13,7 @@ public void PluginShouldBeCalledForExactType()
ThemePlugin plugin = new();
IThemeRegistry registry = ThemeRegistryHolder.GetBuilder()
.SetLoggerFactory(LoggerFactory)
.AddThemePlugin<Button>(plugin)
.AddThemePlugin(plugin)
.Build();
using Button button = new();
registry.GetTheme()?.Apply(button);
Expand All @@ -26,7 +26,7 @@ public void PluginShouldNotBeCalledForDifferentType()
ThemePlugin plugin = new();
IThemeRegistry registry = ThemeRegistryHolder.GetBuilder()
.SetLoggerFactory(LoggerFactory)
.AddThemePlugin<Button>(plugin)
.AddThemePlugin(plugin)
.Build();
using Form form = new();
registry.GetTheme()?.Apply(form);
Expand All @@ -38,7 +38,7 @@ public void PluginShouldNotBeCalledForSubType()
{
ThemePlugin plugin = new();
IThemeRegistry registry = ThemeRegistryHolder.GetBuilder().SetLoggerFactory(LoggerFactory)
.AddThemePlugin<Button>(plugin)
.AddThemePlugin(plugin)
.Build();
using MyCustomButton button = new();
registry.GetTheme()?.Apply(button);
Expand All @@ -48,11 +48,11 @@ public void PluginShouldNotBeCalledForSubType()
private class MyCustomButton : Button
{ }

private class ThemePlugin : IThemePlugin
private class ThemePlugin : AbstractThemePlugin<Button>
{
public bool WasCalled { get; private set; }

public void Apply(Control control, AbstractTheme theme)
protected override void ApplyPlugin(Button button, AbstractTheme theme)
{
WasCalled = true;
}
Expand Down
10 changes: 5 additions & 5 deletions WinFormsThemes/TestProject/ThemeRegistryBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ThemeRegistryBuilderTest : AbstractTestClass
public void AddingThemePluginsShouldWork()
{
IThemeRegistry registry = ThemeRegistryHolder.GetBuilder().SetLoggerFactory(LoggerFactory)
.AddThemePlugin<Button>(new ThemePlugin())
.AddThemePlugin(new ThemePlugin())
.Build();
Assert.AreEqual(1, registry.GetTheme()?.ThemePlugins?.Count);
Assert.AreEqual(typeof(ThemePlugin), registry.GetTheme()?.ThemePlugins?[typeof(Button)]?.GetType());
Expand All @@ -21,8 +21,8 @@ public void AddingThemePluginsShouldWork()
public void AddingThemePluginTwiceShouldThrow()
{
IThemeRegistryBuilder registry = ThemeRegistryHolder.GetBuilder().SetLoggerFactory(LoggerFactory)
.AddThemePlugin<Button>(new ThemePlugin());
InvalidOperationException ex = Assert.ThrowsException<InvalidOperationException>(() => registry.AddThemePlugin<Button>(new ThemePlugin()));
.AddThemePlugin(new ThemePlugin());
InvalidOperationException ex = Assert.ThrowsException<InvalidOperationException>(() => registry.AddThemePlugin(new ThemePlugin()));
Assert.AreEqual("ThemePlugin for Button already added", ex.Message);
}

Expand Down Expand Up @@ -67,9 +67,9 @@ public void DefaultsShouldBeAddedWhenNotSet()
Assert.AreEqual(0, registry.GetTheme(DefaultDarkTheme.THEME_NAME)?.AdvancedCapabilities?.Count);
}

private class ThemePlugin : IThemePlugin
private class ThemePlugin : AbstractThemePlugin<Button>
{
public void Apply(Control control, AbstractTheme theme)
protected override void ApplyPlugin(Button button, AbstractTheme theme)
{ }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ public interface IThemeRegistryBuilder
/// <summary>
/// Add a plugin to handle additional controls for Themes that support it
/// </summary>
/// <typeparam name="T">the Control to handle</typeparam>
/// <param name="plugin">the plugin handling the theming</param>
IThemeRegistryBuilder AddThemePlugin<T>(IThemePlugin plugin) where T : Control;
IThemeRegistryBuilder AddThemePlugin(IThemePlugin plugin);

/// <summary>
/// return the final IThemeRegistry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public ThemeRegistryBuilder()
_logger = new Logger<IThemeRegistryBuilder>(_loggerFactory);
}

public IThemeRegistryBuilder AddThemePlugin<T>(IThemePlugin plugin) where T : Control
public IThemeRegistryBuilder AddThemePlugin(IThemePlugin plugin)
{
Type t = typeof(T);
Type t = plugin.GetSupportedType();
if (_themePlugins.ContainsKey(t))
{
_logger.LogError("ThemePlugin for {ThemeName} already added", t.Name);
Expand Down
27 changes: 27 additions & 0 deletions WinFormsThemes/WinFormsThemes/Themes/AbstractThemePlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

namespace WinFormsThemes.Themes
{
/// <summary>
/// helper implementation to make implementing <see cref="IThemePlugin"/> easier
/// </summary>
/// <typeparam name="T">the control type this plugin supports</typeparam>
public abstract class AbstractThemePlugin<T> : IThemePlugin where T : Control
{
public void Apply(Control control, AbstractTheme theme)
{
ApplyPlugin((T)control, theme);
}

/// <summary>
/// should be implemented by the plugin to style the given control
/// </summary>
/// <param name="control">the control to style</param>
/// <param name="theme">the theme to be applied</param>
protected abstract void ApplyPlugin(T control, AbstractTheme theme);

public Type GetSupportedType()
{
return typeof(T);
}
}
}
8 changes: 6 additions & 2 deletions WinFormsThemes/WinFormsThemes/Themes/IThemePlugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace WinFormsThemes.Themes
namespace WinFormsThemes.Themes
{
/// <summary>
/// Interface for all Theme Plugins
Expand All @@ -11,5 +11,9 @@ public interface IThemePlugin
/// <param name="control"></param>
/// <param name="theme"></param>
void Apply(Control control, AbstractTheme theme);
/// <summary>
/// Returns the Control type that this plugin supports
/// </summary>
Type GetSupportedType();
}
}
}

0 comments on commit 3f842bc

Please sign in to comment.