Skip to content

Commit 33f5905

Browse files
committed
fix tiling algo
1 parent aedebc8 commit 33f5905

File tree

7 files changed

+89
-20
lines changed

7 files changed

+89
-20
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Dragablz.Dockablz;
7+
using Microsoft.CSharp;
8+
using NUnit.Framework;
9+
10+
namespace Dragablz.Test.Dockablz
11+
{
12+
[TestFixture]
13+
public class TilerCalculatorFixture
14+
{
15+
[TestCase(0, new int[0])]
16+
[TestCase(1, new[] { 1 })]
17+
[TestCase(2, new[] { 1, 1 })]
18+
[TestCase(3, new[] { 1, 2 })]
19+
[TestCase(4, new[] { 2, 2 })]
20+
[TestCase(5, new[] { 2, 3 })]
21+
[TestCase(6, new[] { 3, 3 })]
22+
[TestCase(7, new[] { 2, 2, 3 })]
23+
[TestCase(9, new[] { 3, 3, 3 })]
24+
[TestCase(10, new[] { 3, 3, 4 })]
25+
[TestCase(25, new[] { 5, 5, 5, 5, 5 })]
26+
[TestCase(26, new[] { 5, 5, 5, 5, 6 })]
27+
[TestCase(29, new[] { 5, 6, 6, 6, 6 })]
28+
[TestCase(30, new[] { 6, 6, 6, 6, 6 })]
29+
public void WillCalculateCellsPerColumn(int totalCells, int[] expectedCellsPerColumn)
30+
{
31+
var result = TilerCalculator.GetCellCountPerColumn(totalCells);
32+
33+
CollectionAssert.AreEqual(expectedCellsPerColumn, result);
34+
}
35+
}
36+
}

Dragablz.Test/Dragablz.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
</ItemGroup>
4848
<ItemGroup>
4949
<Compile Include="CollectionTeaserFixture.cs" />
50+
<Compile Include="Dockablz\TilerCalculatorFixture.cs" />
5051
<Compile Include="Properties\AssemblyInfo.cs" />
5152
</ItemGroup>
5253
<ItemGroup>

Dragablz/Dockablz/Tiler.cs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,20 @@ public static void Tile(IEnumerable<DragablzItem> dragablzItems, Size bounds)
1717
if (dragablzItems == null) throw new ArgumentNullException("dragablzItems");
1818

1919
var items = new Queue<DragablzItem>(dragablzItems.OrderBy(Panel.GetZIndex));
20-
var sqrt = Math.Sqrt(items.Count());
21-
var lowerRowCount = Math.Floor(sqrt);
22-
var columns = Math.Round(sqrt, MidpointRounding.AwayFromZero);
2320

21+
var cellCountPerColumn = TilerCalculator.GetCellCountPerColumn(items.Count());
2422
var x = 0d;
25-
var cellWidth = bounds.Width/columns;
26-
for (var column = 0; column < columns; column++)
27-
{
28-
var minumumAvailableAfterThisColumn = items.Count - lowerRowCount;
29-
var cellsThisColumn = lowerRowCount;
30-
if (column < columns - 1)
31-
{
32-
var remainingAfterThisColumnOverColumns = minumumAvailableAfterThisColumn/(column + 1);
33-
if (unchecked(remainingAfterThisColumnOverColumns == (int) remainingAfterThisColumnOverColumns))
34-
cellsThisColumn = cellsThisColumn + 1;
35-
}
36-
23+
var cellWidth = bounds.Width / cellCountPerColumn.Length;
24+
foreach (var cellCount in cellCountPerColumn)
25+
{
3726
var y = 0d;
38-
for (var cell = 0; cell < cellsThisColumn; cell++)
27+
var cellHeight = bounds.Height / cellCount;
28+
for (var cell = 0; cell < cellCount; cell++)
3929
{
40-
var cellHeight = bounds.Height/cellsThisColumn;
4130
var item = items.Dequeue();
4231
Layout.SetFloatingItemState(item, WindowState.Normal);
4332
item.SetCurrentValue(DragablzItem.XProperty, x);
44-
item.SetCurrentValue(DragablzItem.YProperty, y);
33+
item.SetCurrentValue(DragablzItem.YProperty, y);
4534
item.SetCurrentValue(FrameworkElement.WidthProperty, cellWidth);
4635
item.SetCurrentValue(FrameworkElement.HeightProperty, cellHeight);
4736

Dragablz/Dockablz/TilerCalculator.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Dragablz.Dockablz
6+
{
7+
internal static class TilerCalculator
8+
{
9+
public static int[] GetCellCountPerColumn(int totalCells)
10+
{
11+
if (totalCells == 2)
12+
return new[] {1, 1};
13+
14+
var sqrt = Math.Sqrt(totalCells);
15+
16+
if (unchecked(sqrt == (int) sqrt))
17+
return Enumerable.Repeat((int) sqrt, (int) sqrt).ToArray();
18+
19+
var columns = (int)Math.Round(sqrt, MidpointRounding.AwayFromZero);
20+
var minimumCellsPerColumns = (int)Math.Floor(sqrt);
21+
var result = Enumerable.Repeat(minimumCellsPerColumns, columns).ToArray();
22+
23+
for (var i = columns - 1; result.Aggregate((current, next) => current + next) < totalCells; i--)
24+
result[i]+=1;
25+
26+
return result;
27+
}
28+
}
29+
}

Dragablz/Dragablz.net40.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<Compile Include="Dockablz\Layout.cs" />
7070
<Compile Include="Dockablz\LocationSnapShot.cs" />
7171
<Compile Include="Dockablz\Tiler.cs" />
72+
<Compile Include="Dockablz\TilerCalculator.cs" />
7273
<Compile Include="DragablzColors.cs" />
7374
<Compile Include="DragablzDragCompletedEventArgs.cs" />
7475
<Compile Include="DragablzDragDeltaEventArgs.cs" />

Dragablz/Dragablz.net45.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<Compile Include="Core\CollectionTeaser.cs" />
7272
<Compile Include="Dockablz\LocationSnapShot.cs" />
7373
<Compile Include="Dockablz\Tiler.cs" />
74+
<Compile Include="Dockablz\TilerCalculator.cs" />
7475
<Compile Include="DragablzColors.cs" />
7576
<Compile Include="DragablzDragCompletedEventArgs.cs" />
7677
<Compile Include="DragablzDragDeltaEventArgs.cs" />

DragablzDemo/MdiExample.xaml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,23 @@
2121
FloatingItemHeaderMemberPath="Name"
2222
FloatingItemDisplayMemberPath="SimpleContent">
2323
<dockablz:Layout.FloatingItems>
24-
<dragablzDemo:SimpleViewModel Name="One" SimpleContent="MDI Child One" />
24+
<dragablzDemo:SimpleViewModel Name="One" SimpleContent="MDI Child One" />
25+
2526
<dragablzDemo:SimpleViewModel Name="Two" SimpleContent="MDI Child Two" />
27+
2628
<dragablzDemo:SimpleViewModel Name="Three" SimpleContent="MDI Child Three" />
29+
2730
<dragablzDemo:SimpleViewModel Name="Four" SimpleContent="MDI Child Four" />
28-
<dragablzDemo:SimpleViewModel Name="Five" SimpleContent="MDI Child Five" />
31+
32+
<dragablzDemo:SimpleViewModel Name="Five" SimpleContent="MDI Child Five" />
33+
<dragablzDemo:SimpleViewModel Name="Six" SimpleContent="MDI Child Six" />
34+
<dragablzDemo:SimpleViewModel Name="Seven" SimpleContent="MDI Child Seven" />
35+
<!--
36+
<dragablzDemo:SimpleViewModel Name="Eight" SimpleContent="MDI Child Eight" />
37+
<dragablzDemo:SimpleViewModel Name="Nine" SimpleContent="MDI Child Nine" />
38+
<dragablzDemo:SimpleViewModel Name="Ten" SimpleContent="MDI Child Ten" />
39+
40+
-->
2941
</dockablz:Layout.FloatingItems>
3042
</dockablz:Layout>
3143
</DockPanel>

0 commit comments

Comments
 (0)