From 5c51b66cffd5e9ef11ed07b8eabf4073b3fa2ab9 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Sun, 10 Nov 2024 12:58:19 -0600 Subject: [PATCH] Make LabeledProgressBar work on Mono --- GUI/Controls/LabeledProgressBar.cs | 51 +++++++++++----------- GUI/Controls/TransparentLabel.cs | 69 ------------------------------ 2 files changed, 25 insertions(+), 95 deletions(-) delete mode 100644 GUI/Controls/TransparentLabel.cs diff --git a/GUI/Controls/LabeledProgressBar.cs b/GUI/Controls/LabeledProgressBar.cs index 48fcae5fb..0d864aafc 100644 --- a/GUI/Controls/LabeledProgressBar.cs +++ b/GUI/Controls/LabeledProgressBar.cs @@ -1,6 +1,7 @@ using System.Drawing; using System.Windows.Forms; using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; #if NET5_0_OR_GREATER using System.Runtime.Versioning; #endif @@ -18,19 +19,9 @@ public class LabeledProgressBar : ProgressBar public LabeledProgressBar() : base() { - SuspendLayout(); - - label = new TransparentLabel() - { - ForeColor = SystemColors.ControlText, - Dock = DockStyle.Fill, - TextAlign = ContentAlignment.MiddleCenter, - Text = "", - }; - Controls.Add(label); - - ResumeLayout(false); - PerformLayout(); + SetStyle(ControlStyles.OptimizedDoubleBuffer, true); + Font = SystemFonts.DefaultFont; + Text = ""; } [Bindable(false)] @@ -39,25 +30,33 @@ public LabeledProgressBar() [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [EditorBrowsable(EditorBrowsableState.Always)] // If we use override instead of new, the nullability never matches (!) - public new string? Text - { - get => label.Text; - set => label.Text = value; + public new string Text { + get => text; + [MemberNotNull(nameof(text), nameof(textSize))] + set + { + text = value; + textSize = TextRenderer.MeasureText(text, Font); + } } + [Bindable(false)] + [Browsable(true)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] + [EditorBrowsable(EditorBrowsableState.Always)] // If we use override instead of new, the nullability never matches (!) - public new Font Font - { - get => label.Font; - set => label.Font = value; - } + public new Font Font { get; set; } - public ContentAlignment TextAlign + protected override void OnPaint(PaintEventArgs e) { - get => label.TextAlign; - set => label.TextAlign = value; + base.OnPaint(e); + TextRenderer.DrawText(e.Graphics, Text, Font, + new Point((Width - textSize.Width) / 2, + (Height - textSize.Height) / 2), + SystemColors.ControlText); } - private readonly TransparentLabel label; + private string text; + private Size textSize; } } diff --git a/GUI/Controls/TransparentLabel.cs b/GUI/Controls/TransparentLabel.cs deleted file mode 100644 index 5bbc63dc8..000000000 --- a/GUI/Controls/TransparentLabel.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -#if NET5_0_OR_GREATER -using System.Runtime.Versioning; -#endif - -namespace CKAN.GUI -{ - #if NET5_0_OR_GREATER - [SupportedOSPlatform("windows")] - #endif - public class TransparentLabel : Label - { - public TransparentLabel() - { - SetStyle(ControlStyles.SupportsTransparentBackColor - | ControlStyles.ResizeRedraw - | ControlStyles.Opaque - | ControlStyles.AllPaintingInWmPaint, - true); - SetStyle(ControlStyles.OptimizedDoubleBuffer, false); - BackColor = Color.Transparent; - } - - // If we use override instead of new, the nullability never matches (!) - public new string? Text - { - get => base.Text; - set - { - base.Text = value; - Parent?.Invalidate(Bounds, false); - } - } - - // If we use override instead of new, the nullability never matches (!) - public new ContentAlignment TextAlign - { - get => base.TextAlign; - set - { - base.TextAlign = value; - Parent?.Invalidate(Bounds, false); - } - } - - protected override CreateParams CreateParams - { - get - { - var cp = base.CreateParams; - cp.ExStyle |= (int)WindowExStyles.WS_EX_TRANSPARENT; - return cp; - } - } - - protected override void OnMove(EventArgs e) - { - base.OnMove(e); - RecreateHandle(); - } - - protected override void OnPaintBackground(PaintEventArgs e) - { - // Do nothing - } - } -}