Skip to content

Commit

Permalink
update control library (#31)
Browse files Browse the repository at this point in the history
* update control library

- added support for StylableTextBox with HintForeColor and TextForeColor
- added support for StylableGroupBox

* switch from BrowsableAttribute to custom InternalUseOnlyAttribute to detect which properties should not be called
  • Loading branch information
wolframhaussig authored Feb 25, 2024
1 parent 86decfc commit 95b7f4e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
50 changes: 36 additions & 14 deletions WinFormsThemes/WinFormsThemes/Themes/AbstractTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Microsoft.Extensions.Logging.Abstractions;
using StylableWinFormsControls;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.ComponentModel;
using WinFormsThemes.Themes.ToolStrip;
using WinFormsThemes.Utilities;

Expand Down Expand Up @@ -143,13 +145,17 @@ public void Apply(Control control, ThemeOptions options)
ArgumentNullException.ThrowIfNull(control);

DarkWindowsTheme.UseDarkThemeVisualStyle(control.Handle, Capabilities.HasFlag(ThemeCapabilities.DarkMode));
ToolStripManager.RenderMode = ToolStripManagerRenderMode.Professional;

control.BackColor = getBackgroundColorForStyle(options);
ToolStripManager.RenderMode = ToolStripManagerRenderMode.Professional;

// some specific controls provide more specific setters (like StylableTextBox with HintForeColor and TextForeColor).
// to not override the color everytime, we ignore marked properties
setIfPublicUsage(control, "BackColor", () => control.BackColor = getBackgroundColorForStyle(options));

// always assume disabled==false here since most controls don't support ForeColor on disabled states
// and have to be set separately
control.ForeColor = getForegroundColorForStyle(options, false);
// and have to be set separately.
// some specific controls provide more specific setters (like StylableTextBox with HintForeColor and TextForeColor).
// to not override the color everytime, we ignore marked setters
setIfPublicUsage(control, "ForeColor", () => control.ForeColor = getForegroundColorForStyle(options, false));

Type t = control.GetType();
ThemePlugins.TryGetValue(t, out IThemePlugin? plugin);
Expand Down Expand Up @@ -208,15 +214,9 @@ public void Apply(Control control, ThemeOptions options)
}

if (control is StylableTextBox stb)
{
//it is okay to run this line multiple times as the eventhandler will detect this and ignore
//subsequent calls
stb.HintActiveChanged += (sender, e) => { if (sender is not null) { Apply((Control)sender); } };
if (stb.IsHintActive && options != ThemeOptions.Hint)
{
Apply(stb, ThemeOptions.Hint);
return;
}
{
stb.HintForeColor = getForegroundColorForStyle(ThemeOptions.Hint, stb.Enabled);
stb.TextForeColor = getForegroundColorForStyle(options, false);
stb.BorderColor = ControlBorderColor;
}

Expand All @@ -232,6 +232,13 @@ public void Apply(Control control, ThemeOptions options)
stl.DisabledForeColor = getForegroundColorForStyle(options, true);
}

if (control is StylableGroupBox sgb)
{
sgb.BorderColor = ControlBorderColor;
sgb.EnabledForeColor = getForegroundColorForStyle(options, false);
sgb.DisabledForeColor = getForegroundColorForStyle(options, true);
}

if (control is StylableListView slv)
{
slv.GroupHeaderForeColor = ListViewHeaderGroupColor;
Expand Down Expand Up @@ -367,5 +374,20 @@ private Color getForegroundColorForStyle(ThemeOptions options, bool disabled)
}
return Color.FromArgb((int)(255 * 0.6), baseColor);
}
/// <summary>
/// some specific controls provide more specific setters (like StylableTextBox with HintForeColor and TextForeColor).
/// to not override the color everytime,this method is used to only set the color if the property is not marked as InternalUseOnly
/// </summary>
/// <param name="control">the control to set the color on</param>
/// <param name="propName">the name of the property</param>
/// <param name="setter">the setter to be executed if the property is browsable</param>
private static void setIfPublicUsage(Control control, string propName, Action setter)
{
InternalUseOnlyAttribute? attr = control.GetType().GetProperty(propName)!.GetCustomAttribute<InternalUseOnlyAttribute>();
if (attr is null)
{
setter();
}
}
}
}
2 changes: 1 addition & 1 deletion WinFormsThemes/WinFormsThemes/WinFormsThemes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AssortedDevelopment.StylableWinFormsControls" Version="0.1.0" />
<PackageReference Include="AssortedDevelopment.StylableWinFormsControls" Version="0.2.0-beta.4" />
<PackageReference Include="JsonSchema.Net" Version="5.2.6" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Roslynator.Analyzers" Version="4.6.1">
Expand Down

0 comments on commit 95b7f4e

Please sign in to comment.