Skip to content

Commit

Permalink
Merge pull request #164 from xZise/improve-label
Browse files Browse the repository at this point in the history
Improve custom label format
  • Loading branch information
cyanfish authored Jan 13, 2024
2 parents e7caaaf + 312e2b6 commit 857f506
Showing 1 changed file with 49 additions and 27 deletions.
76 changes: 49 additions & 27 deletions NAPS2.Lib.WinForms/EtoForms/WinForms/WinFormsListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ namespace NAPS2.EtoForms.WinForms;

public class WinFormsListView<T> : IListView<T> where T : notnull
{
private const int TextPadding = 6;
private const int SelectionPadding = 3;
private static readonly Pen DefaultPen = new(Color.Black, 1);
private static readonly Pen SelectionPen = new(Color.FromArgb(0x60, 0xa0, 0xe8), 3);
private static readonly SolidBrush OutlineBrush = new(Color.FromArgb(0x60, 0xa0, 0xe8));
private static readonly SolidBrush SelectionBrush = new(Color.FromArgb(0xcc, 0xe8, 0xff));
private static readonly StringFormat LabelFormat = new() { Alignment = StringAlignment.Center, Trimming = StringTrimming.EllipsisCharacter };

private readonly ListView _view;
private readonly Eto.Forms.Control _viewEtoControl;
Expand Down Expand Up @@ -75,47 +79,65 @@ public WinFormsListView(ListViewBehavior<T> behavior)

private void CustomRenderItem(object? sender, DrawListViewItemEventArgs e)
{
int width, height;
var image = ImageList.Get(e.Item);
if (image.Width > image.Height)
string? label = null;
SizeF textSize = SizeF.Empty;
int textOffset = 0;
if (_behavior.ShowPageNumbers)
{
width = ImageSize;
height = (int) Math.Round(width * (image.Height / (double) image.Width));
label = $"{e.ItemIndex + 1} / {_view.Items.Count}";
textSize = e.Graphics.MeasureString(label, _view.Font);
textOffset = (int)(textSize.Height + TextPadding);
}
else

float scaleHeight = (float)(ImageSize - textOffset) / image.Height;
float scaleWidth = (float)ImageSize / image.Width;

float scale = Math.Min(scaleWidth, scaleHeight);
int height = (int)Math.Round(image.Height * scale);
int width = (int)Math.Round(image.Width * scale);

var x = e.Bounds.Left + (e.Bounds.Width - width) / 2;
var y = e.Bounds.Top + (e.Bounds.Height - height - textOffset) / 2;

if (e.Item.Selected)
{
if (_behavior.ShowPageNumbers)
height = ImageSize - 12;
else
height = ImageSize;
width = (int) Math.Round(height * (image.Width / (double) image.Height));
Size intTextSize = Size.Ceiling(textSize);

int selectionWidth = Math.Max(width, intTextSize.Width);
int selectionHeight = height + TextPadding + intTextSize.Height;

var selectionX = e.Bounds.Left + (e.Bounds.Width - width) / 2;

var selectionRect = new Rectangle(selectionX, y, selectionWidth, selectionHeight);
selectionRect.Inflate(SelectionPadding, SelectionPadding);

var outlineRect = selectionRect;
outlineRect.Inflate(1, 1);
e.Graphics.FillRectangle(OutlineBrush, outlineRect);

e.Graphics.FillRectangle(SelectionBrush, selectionRect);
}
var x = e.Bounds.Left + (e.Bounds.Width - width) / 2;
var y = e.Bounds.Top + (e.Bounds.Height - height) / 2;

e.Graphics.DrawImage(image, new Rectangle(x, y, width, height));
if (_behavior.ShowPageNumbers)
if (!string.IsNullOrEmpty(label))
{
// Draw the text below the image
string drawString = $"{e.ItemIndex + 1} / {_view.Items.Count}";
var drawFont = new Font("Arial", 10);
var drawBrush = new SolidBrush(Color.Black);
var drawBrush = Brushes.Black;

float x1 = x + width / 2;
float y1 = y + height + 6;
float y1 = y + height + TextPadding;

var drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
RectangleF labelRect = new(x1, y1, 0, textSize.Height);
float maxLabelWidth = Math.Min(textSize.Width, e.Bounds.Width - 2 * TextPadding);
labelRect.Inflate(maxLabelWidth / 2, 0);

e.Graphics.DrawString(drawString, drawFont, drawBrush, x1, y1, drawFormat);
e.Graphics.DrawString(label, _view.Font, drawBrush, labelRect, LabelFormat);
}
// Draw border
if (e.Item.Selected)
{
e.Graphics.DrawRectangle(SelectionPen, x - 2, y - 2, width + 3, height + 3);
}
else
if (!e.Item.Selected)
{
e.Graphics.DrawRectangle(DefaultPen, x, y, width, height);
e.Graphics.DrawRectangle(DefaultPen, x - 1, y - 1, width + 1, height + 1);
}
}

Expand Down

0 comments on commit 857f506

Please sign in to comment.