optimize tree views

This commit is contained in:
pointfeev 2023-03-15 20:50:32 -04:00
parent 134a5da8fb
commit e9bd9c7f21

View file

@ -14,6 +14,16 @@ namespace CreamInstaller.Components;
internal sealed class CustomTreeView : TreeView internal sealed class CustomTreeView : TreeView
{ {
private const string KoaloaderToggleString = "Koaloader"; private const string KoaloaderToggleString = "Koaloader";
private static readonly Color C1 = ColorTranslator.FromHtml("#FFFF99");
private static readonly Color C2 = ColorTranslator.FromHtml("#696900");
private static readonly Color C3 = ColorTranslator.FromHtml("#AAAA69");
private static readonly Color C4 = ColorTranslator.FromHtml("#99FFFF");
private static readonly Color C5 = ColorTranslator.FromHtml("#006969");
private static readonly Color C6 = ColorTranslator.FromHtml("#69AAAA");
private static readonly Color C7 = ColorTranslator.FromHtml("#006900");
private static readonly Color C8 = ColorTranslator.FromHtml("#69AA69");
private readonly Dictionary<ProgramSelection, Rectangle> checkBoxBounds = new(); private readonly Dictionary<ProgramSelection, Rectangle> checkBoxBounds = new();
private readonly Dictionary<ProgramSelection, Rectangle> comboBoxBounds = new(); private readonly Dictionary<ProgramSelection, Rectangle> comboBoxBounds = new();
@ -61,17 +71,8 @@ internal sealed class CustomTreeView : TreeView
backBrush ??= new(BackColor); backBrush ??= new(BackColor);
Font font = node.NodeFont ?? Font; Font font = node.NodeFont ?? Font;
Brush brush = highlighted ? SystemBrushes.Highlight : backBrush; Brush brush = highlighted ? SystemBrushes.Highlight : backBrush;
string text; // = e.Node.Text;
Size size;
Rectangle bounds = node.Bounds; Rectangle bounds = node.Bounds;
Rectangle selectionBounds = bounds; Rectangle selectionBounds = bounds;
Color color; // = highlighted ? SystemColors.HighlightText : (node.ForeColor != Color.Empty) ? node.ForeColor : node.TreeView.ForeColor;
Point point;
/*Size textSize = TextRenderer.MeasureText(text, font);
Point textLoc = new(bounds.X - 1, bounds.Y);
bounds = new Rectangle(textLoc, new Size(textSize.Width, bounds.Height));
graphics.FillRectangle(brush, bounds);
TextRenderer.DrawText(graphics, text, font, bounds, color, TextFormatFlags.Default);*/
Form form = FindForm(); Form form = FindForm();
if (form is not SelectForm and not SelectDialogForm) if (form is not SelectForm and not SelectDialogForm)
return; return;
@ -79,25 +80,25 @@ internal sealed class CustomTreeView : TreeView
Platform platform = (node.Tag as Platform?).GetValueOrDefault(Platform.None); Platform platform = (node.Tag as Platform?).GetValueOrDefault(Platform.None);
if (string.IsNullOrWhiteSpace(platformId) || platform is Platform.None) if (string.IsNullOrWhiteSpace(platformId) || platform is Platform.None)
return; return;
color = highlighted Color color = highlighted
? ColorTranslator.FromHtml("#FFFF99") ? C1
: Enabled : Enabled
? ColorTranslator.FromHtml("#696900") ? C2
: ColorTranslator.FromHtml("#AAAA69"); : C3;
text = platform.ToString(); string text = platform.ToString();
size = TextRenderer.MeasureText(graphics, text, font); Size size = TextRenderer.MeasureText(graphics, text, font);
bounds = bounds with { X = bounds.X + bounds.Width, Width = size.Width }; bounds = bounds with { X = bounds.X + bounds.Width, Width = size.Width };
selectionBounds = new(selectionBounds.Location, selectionBounds.Size + bounds.Size with { Height = 0 }); selectionBounds = new(selectionBounds.Location, selectionBounds.Size + bounds.Size with { Height = 0 });
graphics.FillRectangle(brush, bounds); graphics.FillRectangle(brush, bounds);
point = new(bounds.Location.X - 1, bounds.Location.Y + 1); Point point = new(bounds.Location.X - 1, bounds.Location.Y + 1);
TextRenderer.DrawText(graphics, text, font, point, color, TextFormatFlags.Default); TextRenderer.DrawText(graphics, text, font, point, color, TextFormatFlags.Default);
if (platform is not Platform.Paradox) if (platform is not Platform.Paradox)
{ {
color = highlighted color = highlighted
? ColorTranslator.FromHtml("#99FFFF") ? C4
: Enabled : Enabled
? ColorTranslator.FromHtml("#006969") ? C5
: ColorTranslator.FromHtml("#69AAAA"); : C6;
text = platformId; text = platformId;
size = TextRenderer.MeasureText(graphics, text, font); size = TextRenderer.MeasureText(graphics, text, font);
const int left = -4; const int left = -4;
@ -107,8 +108,6 @@ internal sealed class CustomTreeView : TreeView
point = new(bounds.Location.X - 1, bounds.Location.Y + 1); point = new(bounds.Location.X - 1, bounds.Location.Y + 1);
TextRenderer.DrawText(graphics, text, font, point, color, TextFormatFlags.Default); TextRenderer.DrawText(graphics, text, font, point, color, TextFormatFlags.Default);
} }
/*if (highlighted)
ControlPaint.DrawFocusRectangle(graphics, selectionBounds, color, SystemColors.Highlight);*/
if (form is SelectForm) if (form is SelectForm)
{ {
ProgramSelection selection = ProgramSelection.FromPlatformId(platform, platformId); ProgramSelection selection = ProgramSelection.FromPlatformId(platform, platformId);
@ -140,15 +139,13 @@ internal sealed class CustomTreeView : TreeView
checkBoxBounds = new(checkBoxBounds.Location, checkBoxBounds.Size + bounds.Size with { Height = 0 }); checkBoxBounds = new(checkBoxBounds.Location, checkBoxBounds.Size + bounds.Size with { Height = 0 });
graphics.FillRectangle(backBrush, bounds); graphics.FillRectangle(backBrush, bounds);
point = new(bounds.Location.X - 1 + left, bounds.Location.Y + 1); point = new(bounds.Location.X - 1 + left, bounds.Location.Y + 1);
TextRenderer.DrawText(graphics, text, font, point, Enabled ? ColorTranslator.FromHtml("#006900") : ColorTranslator.FromHtml("#69AA69"), TextRenderer.DrawText(graphics, text, font, point, Enabled ? C7 : C8, TextFormatFlags.Default);
TextFormatFlags.Default);
this.checkBoxBounds[selection] = RectangleToClient(checkBoxBounds); this.checkBoxBounds[selection] = RectangleToClient(checkBoxBounds);
string proxy = selection.KoaloaderProxy ?? ProgramSelection.DefaultKoaloaderProxy;
if (selection.Koaloader) if (selection.Koaloader)
{ {
comboBoxFont ??= new(font.FontFamily, 6, font.Style, font.Unit, font.GdiCharSet, font.GdiVerticalFont); comboBoxFont ??= new(font.FontFamily, 6, font.Style, font.Unit, font.GdiCharSet, font.GdiVerticalFont);
ComboBoxState comboBoxState = Enabled ? ComboBoxState.Normal : ComboBoxState.Disabled; ComboBoxState comboBoxState = Enabled ? ComboBoxState.Normal : ComboBoxState.Disabled;
text = proxy + ".dll"; text = selection.KoaloaderProxy ?? ProgramSelection.DefaultKoaloaderProxy + ".dll";
size = TextRenderer.MeasureText(graphics, text, comboBoxFont) + new Size(6, 0); size = TextRenderer.MeasureText(graphics, text, comboBoxFont) + new Size(6, 0);
const int padding = 2; const int padding = 2;
bounds = new(bounds.X + bounds.Width, bounds.Y + padding / 2, size.Width, bounds.Height - padding); bounds = new(bounds.X + bounds.Width, bounds.Y + padding / 2, size.Width, bounds.Height - padding);