[FZ Editor] Improved a function in GridData.cs (#6565)

* Improved a function in GridData.cs

* Allocate the extra values proportionally

* Refactor a constant
This commit is contained in:
Ivan Stošić 2020-09-14 10:05:28 +02:00 committed by GitHub
parent 5af88de27b
commit 3d5c790fb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,9 @@ namespace FancyZonesEditor
{
public class GridData
{
// The sum of row/column percents should be equal to this number
private const int _multiplier = 10000;
public class ResizeInfo
{
public ResizeInfo()
@ -70,7 +73,7 @@ namespace FancyZonesEditor
total += info[i].Percent;
}
int diff = total - 10000;
int diff = total - _multiplier;
if (diff != 0)
{
TotalPercent -= diff;
@ -1025,26 +1028,13 @@ namespace FancyZonesEditor
total += info[i].Percent;
}
int totalDiff = total - 10000;
if (totalDiff != 0)
int prefixTotal = 0;
for (int i = 0; i < percents.Count; i++)
{
int perLineDiff = totalDiff / percents.Count;
int lastLineDiff = totalDiff - (perLineDiff * (percents.Count - 1));
for (int i = 0; i < percents.Count - 1 && perLineDiff != 0; i++)
{
int percent = percents[i] - perLineDiff;
if (percent < 0)
{
percent = 0;
}
percents[i] = percent;
info[i].Percent = percent;
}
info[percents.Count - 1].Percent -= lastLineDiff;
percents[percents.Count - 1] -= lastLineDiff;
int first = prefixTotal * _multiplier / total;
prefixTotal += info[i].Percent;
int last = prefixTotal * _multiplier / total;
percents[i] = info[i].Percent = last - first;
}
}