move preset width cycling logic into class

This commit is contained in:
Peter Fajdiga
2024-10-04 18:44:19 +02:00
parent 116372c954
commit ec14d5295f
4 changed files with 19 additions and 22 deletions

View File

@@ -5,7 +5,13 @@ class PresetWidths {
this.presets = PresetWidths.parsePresetWidths(presetWidths, spacing);
}
public get(minWidth: number, maxWidth: number) {
public next(currentWidth: number, minWidth: number, maxWidth: number) {
const widths = this.getWidths(minWidth, maxWidth);
const nextIndex = widths.findIndex(width => width > currentWidth);
return nextIndex >= 0 ? widths[nextIndex] : widths[0];
}
private getWidths(minWidth: number, maxWidth: number) {
const widths = this.presets.map(f => clamp(f(maxWidth), minWidth, maxWidth));
widths.sort((a, b) => a - b);
return uniq(widths);

View File

@@ -188,10 +188,7 @@ class Actions {
if (this.config.presetWidths === null) {
return;
}
const widths = this.config.presetWidths.get(column.getMinWidth(), column.getMaxWidth());
const currentWidth = column.getWidth();
const nextIndex = widths.findIndex(width => width > currentWidth);
const nextWidth = nextIndex >= 0 ? widths[nextIndex] : widths[0];
const nextWidth = this.config.presetWidths.next(column.getWidth(), column.getMinWidth(), column.getMaxWidth());
column.setWidth(nextWidth, true);
}

View File

@@ -23,13 +23,21 @@ tests.register("PresetWidths", 1, () => {
{ str: " ", error: true },
];
function assertWidths(presetWidths: PresetWidths, expectedWidths: number[]) {
let currentWidth = 0;
for (const expectedWidth of expectedWidths) {
currentWidth = presetWidths.next(currentWidth, minWidth, maxWidth);
Assert.equal(currentWidth, expectedWidth);
}
const repeatedWidth = presetWidths.next(currentWidth, minWidth, maxWidth);
Assert.equal(repeatedWidth, expectedWidths[0]);
}
for (const testCase of testCases) {
try {
const presetWidths = new PresetWidths(testCase.str, spacing);
Assert.truth(!testCase.error);
const result = presetWidths.get(minWidth, maxWidth);
Assert.equalArrays(result, testCase.result!);
assertWidths(presetWidths, testCase.result!);
} catch (error) {
Assert.truth(testCase.error === true);
}

View File

@@ -68,20 +68,6 @@ namespace Assert {
);
}
export function equalArrays(
actual: any[],
expected: any[],
{ message, skip=0 }: Options = {},
) {
truth(
actual.length === expected.length && actual.every((item, index) => item === expected[index]),
{
message: buildMessage(actual, expected, "Arrays not equal", message),
skip: skip + 1,
},
);
}
export function equalRects(
actual: QmlRect,
expected: QmlRect,