Skip to content

Commit

Permalink
update control library
Browse files Browse the repository at this point in the history
- added support for StylableTextBox with HintForeColor and TextForeColor
- added support for StylableGroupBox
  • Loading branch information
wolframhaussig committed Feb 25, 2024
1 parent 3f842bc commit a3afc38
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
53 changes: 38 additions & 15 deletions WinFormsThemes/WinFormsThemes/Themes/AbstractTheme.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using StylableWinFormsControls;
using StylableWinFormsControls;
using StylableWinFormsControls.Controls;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.ComponentModel;
using WinFormsThemes.Themes.ToolStrip;
using WinFormsThemes.Utilities;

Expand Down Expand Up @@ -152,13 +155,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 non-browsable setters
setIfBrowsable(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 non-browsable setters
setIfBrowsable(control, "ForeColor", () => control.ForeColor = getForegroundColorForStyle(options, false));

Type t = control.GetType();
ThemePlugins.TryGetValue(t, out IThemePlugin? plugin);
Expand Down Expand Up @@ -217,15 +224,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 @@ -241,6 +242,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 @@ -376,5 +384,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 browsable (aka not hidden)
/// </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 setIfBrowsable(Control control, string propName, Action setter)
{
BrowsableAttribute? attr = control.GetType().GetProperty(propName)!.GetCustomAttribute<BrowsableAttribute>();
if (attr?.Browsable != false)
{
setter();
}
}
}
}
2 changes: 1 addition & 1 deletion WinFormsThemes/WinFormsThemes/WinFormsThemes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AssortedDevelopment.StylableWinFormsControls" Version="0.1.0" />
<PackageReference Include="AssortedDevelopment.StylableWinFormsControls" Version="0.2.0-beta.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Roslynator.Analyzers" Version="4.4.0">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit a3afc38

Please sign in to comment.