From a3afc382113e82e7bf5a605e79947c4087b8909d Mon Sep 17 00:00:00 2001 From: "13997737+wolframhaussig@users.noreply.github.com" <13997737+wolframhaussig@users.noreply.github.com> Date: Sun, 25 Feb 2024 11:27:44 +0100 Subject: [PATCH] update control library - added support for StylableTextBox with HintForeColor and TextForeColor - added support for StylableGroupBox --- .../WinFormsThemes/Themes/AbstractTheme.cs | 53 +++++++++++++------ .../WinFormsThemes/WinFormsThemes.csproj | 2 +- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/WinFormsThemes/WinFormsThemes/Themes/AbstractTheme.cs b/WinFormsThemes/WinFormsThemes/Themes/AbstractTheme.cs index bc97c40..8075db5 100644 --- a/WinFormsThemes/WinFormsThemes/Themes/AbstractTheme.cs +++ b/WinFormsThemes/WinFormsThemes/Themes/AbstractTheme.cs @@ -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; @@ -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); @@ -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; } @@ -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; @@ -376,5 +384,20 @@ private Color getForegroundColorForStyle(ThemeOptions options, bool disabled) } return Color.FromArgb((int)(255 * 0.6), baseColor); } + /// + /// 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) + /// + /// the control to set the color on + /// the name of the property + /// the setter to be executed if the property is browsable + private static void setIfBrowsable(Control control, string propName, Action setter) + { + BrowsableAttribute? attr = control.GetType().GetProperty(propName)!.GetCustomAttribute(); + if (attr?.Browsable != false) + { + setter(); + } + } } } diff --git a/WinFormsThemes/WinFormsThemes/WinFormsThemes.csproj b/WinFormsThemes/WinFormsThemes/WinFormsThemes.csproj index 325c9fa..792fbf7 100644 --- a/WinFormsThemes/WinFormsThemes/WinFormsThemes.csproj +++ b/WinFormsThemes/WinFormsThemes/WinFormsThemes.csproj @@ -28,7 +28,7 @@ - + all