Skip to content

Commit

Permalink
Added AutoWidth feature bounded by MaximumSize.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad45123 committed Feb 18, 2023
1 parent 17c46dd commit b5121d1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
37 changes: 35 additions & 2 deletions AutocompleteMenu/AutocompleteMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ public ImageList ImageList {
[DefaultValue(500)]
public int AppearInterval { get; set; }

/// <summary>
/// If true, the menu's width will be adjusted depending on visible items.
/// </summary>
[Description("If true, the menu's width will be adjusted depending on visible items. Bounded by MaximumSize")]
[DefaultValue(false)]
public bool AutoWidth { get; set; }

[DefaultValue(null)]
public string[] Items
{
Expand Down Expand Up @@ -311,7 +318,10 @@ public IAutocompleteListView ListView
/// </summary>
public void Update()
{
Host.CalcSize();
if (AutoWidth)
UpdateSizeBasedOnVisible();
else
Host.CalcSize();
}

/// <summary>
Expand Down Expand Up @@ -682,7 +692,30 @@ private void BuildAutocompleteList(bool forced)

Host.ListView.HighlightedItemIndex = -1;

Host.CalcSize();
/* Calculate Width Depending on Largest Value */
if (AutoWidth)
{
UpdateSizeBasedOnVisible();
}
else
{
Host.CalcSize();
}
}

private void UpdateSizeBasedOnVisible()
{
int mxWidth = 0;
for (int i = 0; i < VisibleItems.Count; i++)
{
Debug.WriteLine(VisibleItems[i].ToString());
int width = TextRenderer.MeasureText(Host.CreateGraphics(), VisibleItems[i].ToString(), Font).Width;
if (width > mxWidth)
mxWidth = width;
}

mxWidth += LeftPadding;
Host.CalcSize(Math.Min(MaximumSize.Width, mxWidth));
}

internal void OnOpening(CancelEventArgs args)
Expand Down
16 changes: 13 additions & 3 deletions AutocompleteMenu/AutocompleteMenuHost.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

Expand Down Expand Up @@ -63,10 +64,19 @@ protected override void OnPaintBackground(PaintEventArgs e)
e.Graphics.FillRectangle(brush, e.ClipRectangle);
}

internal void CalcSize()
internal void CalcSize(int? customWidth = null)
{
Host.Size = (ListView as Control).Size;
Size = new System.Drawing.Size((ListView as Control).Size.Width + 4, (ListView as Control).Size.Height + 4);
if (customWidth == null)
{
Host.Size = (ListView as Control).Size;
Size = new Size((ListView as Control).Size.Width + 4, (ListView as Control).Size.Height + 4);
}
else
{
Host.Size = new Size((int)customWidth, (ListView as Control).Size.Height);
Size = new Size((int)customWidth + 4, (ListView as Control).Size.Height + 4);
Debug.WriteLine("Setting width to " + customWidth);
}
}

public override RightToLeft RightToLeft
Expand Down
10 changes: 7 additions & 3 deletions Tester/ScintillaSample.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b5121d1

Please sign in to comment.