From 399d0ae88350199db5be28b202560c4d873335b7 Mon Sep 17 00:00:00 2001 From: rounk-ctrl <70931017+rounk-ctrl@users.noreply.github.com> Date: Mon, 18 Sep 2023 22:54:07 +0400 Subject: [PATCH] themes.cs --- .../Controls/CustomProgressBar.cs | 75 -------- Rectify11Installer/Core/Helpers.cs | 8 +- Rectify11Installer/Rectify11Installer.csproj | 3 - Rectify11Installer/Win32/Theme.cs | 167 ++++++++---------- 4 files changed, 82 insertions(+), 171 deletions(-) delete mode 100644 Rectify11Installer/Controls/CustomProgressBar.cs diff --git a/Rectify11Installer/Controls/CustomProgressBar.cs b/Rectify11Installer/Controls/CustomProgressBar.cs deleted file mode 100644 index 56ea4bcbb..000000000 --- a/Rectify11Installer/Controls/CustomProgressBar.cs +++ /dev/null @@ -1,75 +0,0 @@ -using libmsstyle; -using Rectify11Installer.Core; -using System.Drawing; -using System.Windows.Forms; - -namespace Rectify11Installer.Controls -{ - // will be used later - public sealed class CustomProgressBar : ProgressBar - { - private bool _error = false; - public bool Error - { - get => _error; - set - { - _error = value; - Invalidate(); - } - } - public CustomProgressBar() - { - this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true); - - if (Theme.IsUsingDarkMode) - { - BackColor = Color.FromArgb(36, 36, 36); - } - } - - protected override void OnPaint(PaintEventArgs e) - { - var rec = e.ClipRectangle; - - rec.Width = (int)(rec.Width * ((double)Value / Maximum)); - - if (DesignMode) - { - if (ProgressBarRenderer.IsSupported) - ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle); - } - else - { - var currentTheme = Theme.IsUsingDarkMode ? Theme.DarkStyle : Theme.LightStyle; - - var part = Theme.GetProgressbarBg(currentTheme); - var renderer2 = new PartRenderer(currentTheme, part); - var b = renderer2.RenderPreview(ThemeParts.Normal, Width, Height); - - e.Graphics.DrawImage(b, new Point(0, 0)); - } - - if (rec.Width == 0) - return; - - if (DesignMode) - { - e.Graphics.FillRectangle(Brushes.Green, 2, 2, rec.Width, rec.Height); - } - else - { - var currentTheme = Theme.IsUsingDarkMode ? Theme.DarkStyle : Theme.LightStyle; - - var part = Theme.GetProgressbarFill(currentTheme); - - using var renderer2 = new PartRenderer(currentTheme, part); - var tpart = Error ? ThemeParts.Pressed : ThemeParts.Normal; - var b = renderer2.RenderPreview(tpart, rec.Width, rec.Height); - - e.Graphics.DrawImage(b, new Point(0, 0)); - b.Dispose(); - } - } - } -} \ No newline at end of file diff --git a/Rectify11Installer/Core/Helpers.cs b/Rectify11Installer/Core/Helpers.cs index 949beae8b..25f59f89f 100644 --- a/Rectify11Installer/Core/Helpers.cs +++ b/Rectify11Installer/Core/Helpers.cs @@ -110,7 +110,7 @@ public static string InstalledVersion public class NavigationHelper { public static event EventHandler OnNavigate; - public static void InvokeOnNavigate(object sender, EventArgs e) + public static void InvokeOnNavigate(object sender, EventArgs e) => OnNavigate?.Invoke(sender, e); } @@ -195,13 +195,13 @@ public class Logger + "\n═════════════════════════════\n"; #endregion #region Public Methods - public static void WriteLine(string s) + public static void WriteLine(string s) => Text += s + "\n"; - public static void WriteLine(string s, Exception ex) + public static void WriteLine(string s, Exception ex) => Text += s + ". " + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine; - public static void CommitLog() + public static void CommitLog() => File.WriteAllText(System.IO.Path.Combine(Variables.r11Folder, "installer.log"), Text); public static void Warn(string v) diff --git a/Rectify11Installer/Rectify11Installer.csproj b/Rectify11Installer/Rectify11Installer.csproj index 377ff2159..e95e13f79 100644 --- a/Rectify11Installer/Rectify11Installer.csproj +++ b/Rectify11Installer/Rectify11Installer.csproj @@ -106,9 +106,6 @@ - - Component - Component diff --git a/Rectify11Installer/Win32/Theme.cs b/Rectify11Installer/Win32/Theme.cs index 9be74fd6c..a36462e82 100644 --- a/Rectify11Installer/Win32/Theme.cs +++ b/Rectify11Installer/Win32/Theme.cs @@ -7,95 +7,84 @@ namespace Rectify11Installer.Core { - public class Theme - { - #region Variables - public static VisualStyle DarkStyle = new(); - public static VisualStyle LightStyle = new(); - public static bool IsUsingDarkMode { get; private set; } - #endregion - #region Public Methods - public static void InitTheme() - { - var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"); - if (key != null) - { - var registryValueObject = key.GetValue("AppsUseLightTheme"); - if (registryValueObject == null) - { - return; - } + public class Theme + { + #region Variables + public static VisualStyle DarkStyle = new(); + public static VisualStyle LightStyle = new(); + public static bool IsUsingDarkMode { get; private set; } + public static event EventHandler OnThemeChanged; + #endregion + #region Public Methods + public static void InitTheme() + { + IsUsingDarkMode = (int)Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize")?.GetValue("AppsUseLightTheme") <= 0; + SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged; + } - var registryValue = (int)registryValueObject; - IsUsingDarkMode = registryValue <= 0; - } + public static void LoadTheme() + { + DarkStyle.Load(Path.Combine(Path.GetTempPath(), "Dark.msstyles")); + LightStyle.Load(Path.Combine(Path.GetTempPath(), "light.msstyles")); + } - SystemEvents.UserPreferenceChanged += SystemEvents_UserPreferenceChanged; - } - public static event EventHandler OnThemeChanged; - - public static void LoadTheme() - { - DarkStyle.Load(Path.Combine(Path.GetTempPath(), "Dark.msstyles")); - LightStyle.Load(Path.Combine(Path.GetTempPath(), "light.msstyles")); - } - - public static StylePart GetNavArrowPart(VisualStyle v, NavigationButtonType type) - { - return (from classes in v.Classes.Values - where classes.ClassName == "Navigation" - let partStr = type switch - { - NavigationButtonType.Forward => "FORWARDBUTTON", - NavigationButtonType.Backward => "BACKBUTTON", - NavigationButtonType.Menu => "MENUBUTTON", - _ => throw new NotImplementedException() - } - from parts in classes.Parts - where parts.Value.PartName == partStr - select parts.Value).FirstOrDefault(); - } - public static StylePart GetCommandLinkPart(VisualStyle v) - { - return (from classes in v.Classes.Values where classes.ClassName == "Button" from parts in classes.Parts where parts.Value.PartName == "COMMANDLINK" select parts.Value).FirstOrDefault(); - } - public static StylePart GetGroupBox(VisualStyle v) - { - return (from classes in v.Classes.Values where classes.ClassName == "Button" from parts in classes.Parts where parts.Value.PartName == "GROUPBOX" select parts.Value).FirstOrDefault(); - } - public static StylePart GetCommandLinkGlyphPart(VisualStyle v) - { - return (from classes in v.Classes.Values where classes.ClassName == "Button" from parts in classes.Parts where parts.Value.PartName == "COMMANDLINKGLYPH" select parts.Value).FirstOrDefault(); - } -#nullable enable - public static StylePart? GetProgressbarBg(VisualStyle v) - { - return (from classes in v.Classes.Values where classes.ClassName == "Progress" from parts in classes.Parts where parts.Value.PartName == "BAR" select parts.Value).FirstOrDefault(); - } - public static StylePart? GetProgressbarFill(VisualStyle v) - { - return (from classes in v.Classes.Values where classes.ClassName == "Progress" from parts in classes.Parts where parts.Value.PartName == "FILL" select parts.Value).FirstOrDefault(); - } -#nullable disable - public static StylePart GetButtonPart(VisualStyle v) - { - return (from classes in v.Classes.Values where classes.ClassName == "Button" from parts in classes.Parts where parts.Value.PartName == "PUSHBUTTON" select parts.Value).FirstOrDefault(); - } - #endregion - #region Private Methods - private static void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) - { - switch (e.Category) - { - case UserPreferenceCategory.General: - if (OnThemeChanged != null) - { - InitTheme(); - OnThemeChanged.Invoke(sender, e); - } - break; - } - } - #endregion - } + public static StylePart GetNavArrowPart(VisualStyle v, NavigationButtonType type) + { + return (from classes in v.Classes.Values + where classes.ClassName == "Navigation" + let partStr = type switch + { + NavigationButtonType.Forward => "FORWARDBUTTON", + NavigationButtonType.Backward => "BACKBUTTON", + NavigationButtonType.Menu => "MENUBUTTON", + _ => throw new NotImplementedException() + } + from parts in classes.Parts + where parts.Value.PartName == partStr + select parts.Value).FirstOrDefault(); + } + public static StylePart GetCommandLinkPart(VisualStyle v) + { + return (from classes in v.Classes.Values + where classes.ClassName == "Button" + from parts in classes.Parts + where parts.Value.PartName == "COMMANDLINK" + select parts.Value).FirstOrDefault(); + } + public static StylePart GetGroupBox(VisualStyle v) + { + return (from classes in v.Classes.Values + where classes.ClassName == "Button" + from parts in classes.Parts + where parts.Value.PartName == "GROUPBOX" + select parts.Value).FirstOrDefault(); + } + public static StylePart GetCommandLinkGlyphPart(VisualStyle v) + { + return (from classes in v.Classes.Values + where classes.ClassName == "Button" + from parts in classes.Parts + where parts.Value.PartName == "COMMANDLINKGLYPH" + select parts.Value).FirstOrDefault(); + } + public static StylePart GetButtonPart(VisualStyle v) + { + return (from classes in v.Classes.Values + where classes.ClassName == "Button" + from parts in classes.Parts + where parts.Value.PartName == "PUSHBUTTON" + select parts.Value).FirstOrDefault(); + } + #endregion + #region Private Methods + private static void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) + { + if (e.Category == UserPreferenceCategory.General) + { + InitTheme(); + OnThemeChanged?.Invoke(sender, e); + } + } + } + #endregion } \ No newline at end of file