Skip to content

Commit

Permalink
VerticalGrid
Browse files Browse the repository at this point in the history
  • Loading branch information
miloush committed Feb 23, 2024
1 parent 6232f6c commit fe53834
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
File renamed without changes.
82 changes: 82 additions & 0 deletions DWBox/Controls/VerticalGrid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Windows;
using System.Windows.Controls;

namespace DWBox
{
// basically uniform grid, but adding columns only on full rows
// prefers vertical arrangement, not really uniform
public class VerticalGrid : Panel
{
protected override Size MeasureOverride(Size constraint)
{
UpdateComputedValues();

Size childConstraint = new Size(constraint.Width / _columns, constraint.Height / _rows);
double maxChildDesiredWidth = 0.0;
double maxChildDesiredHeight = 0.0;

for (int i = 0, count = InternalChildren.Count; i < count; ++i)
{
UIElement child = InternalChildren[i];
child.Measure(childConstraint);
Size childDesiredSize = child.DesiredSize;

if (maxChildDesiredWidth < childDesiredSize.Width)
maxChildDesiredWidth = childDesiredSize.Width;

if (maxChildDesiredHeight < childDesiredSize.Height)
maxChildDesiredHeight = childDesiredSize.Height;
}

return new Size((maxChildDesiredWidth * _columns), (maxChildDesiredHeight * _rows));
}

protected override Size ArrangeOverride(Size arrangeSize)
{
Rect childBounds = new Rect(0, 0, arrangeSize.Width / _columns, arrangeSize.Height / _rows);
double xStep = childBounds.Width;
double xBound = arrangeSize.Width - 1.0;

foreach (UIElement child in InternalChildren)
{
child.Arrange(childBounds);

if (child.Visibility != Visibility.Collapsed)
{
childBounds.X += xStep;
if (childBounds.X >= xBound)
{
childBounds.Y += childBounds.Height;
childBounds.X = 0;
}
}
}

return arrangeSize;
}

private void UpdateComputedValues()
{
int nonCollapsedCount = 0;

for (int i = 0, count = InternalChildren.Count; i < count; ++i)
{
UIElement child = InternalChildren[i];
if (child.Visibility != Visibility.Collapsed)
nonCollapsedCount++;
}

if (nonCollapsedCount == 0)
nonCollapsedCount = 1;

_columns = (int)Math.Sqrt(nonCollapsedCount);
_rows = nonCollapsedCount / _columns;
if ((_rows * _columns) < nonCollapsedCount)
_rows++;
}

private int _rows;
private int _columns;
}
}
2 changes: 1 addition & 1 deletion DWBox/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<local:RenderingsLayoutViewModel Name="Uniform grid" Icon="{StaticResource GridPane}">
<local:RenderingsLayoutViewModel.ItemsPanelTemplate>
<ItemsPanelTemplate>
<UniformGrid />
<local:VerticalGrid />
</ItemsPanelTemplate>
</local:RenderingsLayoutViewModel.ItemsPanelTemplate>
</local:RenderingsLayoutViewModel>
Expand Down
2 changes: 1 addition & 1 deletion DWBox/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
[assembly: AssemblyVersion("2.3.0.0")]
[assembly: AssemblyFileVersion("2.3.0.0")]

// 2.3.0.0 layout options, text alignment, paragraph alignment, word wrapping, remember last added font, antialiasing mode, locale list, Alt+X, add one per family font
// 2.3.0.0 layout options, text alignment, paragraph alignment, word wrapping, remember last added font, antialiasing mode, locale list, Alt+X, add one per family font, less uniform grid
// 2.2.0.0 copy image/box/name to clipboard, DirectWriteElement measuring, bitmap AV fix, empty GlyphRun analysis fix, remove all but this font, save settings on drop, \u U+ and acronyms decoding, core switch
// 2.1.0.0 refactor DWrite into namespace & struct with properties, text analysis, remove filter, set size, font face strings, add all progress, remember last input
// 2.0.0.1 instance brush
Expand Down

0 comments on commit fe53834

Please sign in to comment.