Skip to content

Commit

Permalink
Fix KryptonMessageBox imprecise space measuring.
Browse files Browse the repository at this point in the history
- Measure based on the actual font that's used to draw the text.
- Include KTextBox's content padding and container's margin in the calculations.
- Use TextRenderer.MeasureText instead of Graphics.MeasureString for precise measurement.
  • Loading branch information
Ahmed-Abdelhameed committed Jul 8, 2024
1 parent a4b19f8 commit a82ce80
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,13 @@ private Size UpdateMessageSizing(IWin32Window? showOwner)
{
// Find size of the label, with a max of 2/3 screen width
Screen? screen = showOwner != null ? Screen.FromHandle(showOwner.Handle) : Screen.PrimaryScreen;
SizeF scaledMonitorSize = screen.Bounds.Size;
scaledMonitorSize.Width *= 2 / 3.0f;
scaledMonitorSize.Height *= 0.95f;
SizeF messageSize = g.MeasureString(_text, _messageText.Font, scaledMonitorSize);
Size scaledMonitorSize = screen.Bounds.Size;
scaledMonitorSize.Width =(int)(scaledMonitorSize.Width * 2 / 3.0f);
scaledMonitorSize.Height = (int)(scaledMonitorSize.Height * 0.95f);
Font textFont = _messageText.StateCommon.Content.GetContentShortTextFont(PaletteState.Normal);
SizeF messageSize = TextRenderer.MeasureText(_text, textFont, scaledMonitorSize);
// SKC: Don't forget to add the TextExtra into the calculation
SizeF captionSize = g.MeasureString($@"{_caption} {TextExtra}", _messageText.Font, scaledMonitorSize);
SizeF captionSize = TextRenderer.MeasureText($@"{_caption} {TextExtra}", SystemFonts.CaptionFont, scaledMonitorSize);

var messageXSize = Math.Max(messageSize.Width, captionSize.Width);
// Work out DPI adjustment factor
Expand All @@ -592,15 +593,17 @@ private Size UpdateMessageSizing(IWin32Window? showOwner)
messageSize.Width = messageXSize * factorX;
messageSize.Height *= factorY;

// Always add on ad extra 5 pixels as sometimes the measure size does not draw the last
// character it contains, this ensures there is always definitely enough space for it all
messageSize.Width += 5;
textSize = Size.Ceiling(messageSize);
}

return new Size(textSize.Width + _messageIcon.Width + _messageIcon.Margin.Left + _messageIcon.Margin.Right +
_messageText.Margin.Left + _messageText.Margin.Right,
Math.Max(_messageIcon.Height + 10, textSize.Height));

Padding textPadding = _messageText.StateCommon.Content.GetContentPadding(PaletteState.Normal);
Padding textAreaMargin = Padding.Add(textPadding, _panelContentArea.Margin);
Size iconArea = new Size(_messageIcon.Width + _messageIcon.Margin.Left + _messageIcon.Margin.Right,
_messageIcon.Height + _messageIcon.Margin.Top + _messageIcon.Margin.Bottom);
Size textArea = new Size(textSize.Width + textAreaMargin.Left + textAreaMargin.Right,
textSize.Height + textAreaMargin.Top + textAreaMargin.Bottom);
return new Size(textArea.Width + iconArea.Width,
Math.Max(iconArea.Height, textArea.Height));
}

private Size UpdateButtonsSizing()
Expand Down

0 comments on commit a82ce80

Please sign in to comment.