Skip to content

Commit 6389fe0

Browse files
committed
Enable configure Tab height to be fixed
1 parent 2ae9f7a commit 6389fe0

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/Eto/Forms/ThemedControls/ThemedDocumentControlHandler.cs

+23-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class ThemedDocumentControlHandler : ThemedContainerHandler<TableLayout,
1515
float startx;
1616
Size maxImageSize;
1717
PointF? draggingLocation;
18+
bool useFixedTabHeight;
1819

1920
Drawable tabDrawable;
2021
Panel contentPanel;
@@ -223,6 +224,7 @@ public ThemedDocumentControlHandler()
223224
mousePos = new PointF(-1, -1);
224225
selectedIndex = -1;
225226
allowNavigationButtons = true;
227+
useFixedTabHeight = false;
226228
nextPrevWidth = 0;
227229
startx = 0;
228230
font = SystemFonts.Default();
@@ -411,18 +413,29 @@ void CalculateTabHeight(bool force = false)
411413
{
412414
if (!force && !Widget.Loaded)
413415
return;
414-
var scale = Widget.ParentWindow?.Screen?.Scale ?? 1f;
415-
var fontHeight = (int)Math.Ceiling(Font.Ascent * scale);
416+
var fontHeight = CalculateFontHeight();
416417

417-
var height = Math.Max(maxImageSize.Height, fontHeight);
418+
var height = useFixedTabHeight ? fontHeight : Math.Max(maxImageSize.Height, fontHeight);
418419
tabDrawable.Height = height + TabPadding.Vertical; // 2 px padding at top and bottom
419420
}
420421

422+
private int CalculateFontHeight()
423+
{
424+
var scale = Widget.ParentWindow?.Screen?.Scale ?? 1f;
425+
return (int)Math.Ceiling(Font.Ascent * scale);
426+
}
427+
421428
void CalculateImageSizes(bool force = false)
422429
{
423430
if (!force && !Widget.Loaded)
424431
return;
425-
maxImageSize = Size.Empty;
432+
433+
var fontHeight = CalculateFontHeight();
434+
maxImageSize = new Size(fontHeight, fontHeight);
435+
436+
if(UseFixedTabHeight)
437+
return;
438+
426439
for (int i = 0; i < pages.Count; i++)
427440
{
428441
var img = pages[i].Image;
@@ -617,7 +630,7 @@ void CalculateTab(ThemedDocumentPageHandler tab, int i, ref float posx)
617630
var textoffset = 0;
618631
if (tab.Image != null)
619632
{
620-
textoffset = tab.Image.Size.Width + tabPadding.Left;
633+
textoffset = maxImageSize.Width + tabPadding.Left;
621634
size.Width += textoffset;
622635
}
623636

@@ -631,7 +644,7 @@ void CalculateTab(ThemedDocumentPageHandler tab, int i, ref float posx)
631644

632645
tab.Rect = tabRect;
633646

634-
tab.CloseRect = new RectangleF(tabRect.X + tab.Rect.Width - tabDrawable.Height / 4 - closesize, tabDrawable.Height / 4, closesize, closesize);
647+
tab.CloseRect = new RectangleF(tabRect.X + tab.Rect.Width - tabPadding.Right - closesize, tabDrawable.Height / 4, closesize, closesize);
635648
tab.TextRect = new RectangleF(tabRect.X + tabPadding.Left + textoffset, (tabDrawable.Height - size.Height) / 2, textSize.Width, textSize.Height);
636649

637650
posx += tab.Rect.Width;
@@ -671,8 +684,10 @@ void DrawTab(Graphics g, ThemedDocumentPageHandler tab, int i)
671684
g.FillRectangle(backcolor, tabRect);
672685
if (tab.Image != null)
673686
{
674-
var imageSize = tab.Image.Size;
675-
g.DrawImage(tab.Image, tabRect.X + TabPadding.Left + (maxImageSize.Width - imageSize.Width) / 2, (tabDrawable.Height - imageSize.Height) / 2);
687+
g.SaveTransform();
688+
g.ImageInterpolation = ImageInterpolation.High;
689+
g.DrawImage(tab.Image, tabRect.X + TabPadding.Left, (tabDrawable.Height - maxImageSize.Height) / 2, maxImageSize.Width, maxImageSize.Height);
690+
g.RestoreTransform();
676691
}
677692
g.DrawText(Font, textcolor, textRect.Location, tab.Text);
678693

test/Eto.Test/Sections/Controls/DocumentControlSection.cs

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public virtual Control Create()
3636
RemovePage(),
3737
SelectPage(),
3838
tabControl.Handler is ThemedDocumentControlHandler ? HasNavigationButtonsCheckBox() : null,
39+
tabControl.Handler is ThemedDocumentControlHandler ? UseFixedTabHeightCheckBox() : null,
3940
allowReorder,
4041
enabled,
4142
null }
@@ -95,6 +96,14 @@ Control HasNavigationButtonsCheckBox()
9596
return control;
9697
}
9798

99+
Control UseFixedTabHeightCheckBox()
100+
{
101+
var control = new CheckBox { Text = "Use fixed Tab height" };
102+
control.CheckedChanged += (sender, args) => ((ThemedDocumentControlHandler)tabControl.Handler).UseFixedTabHeight =
103+
((CheckBox)sender).Checked ?? false;
104+
return control;
105+
}
106+
98107
DocumentControl DefaultTabs()
99108
{
100109
var control = CreateDocumentControl();

0 commit comments

Comments
 (0)