From 9cb3f33ecbd5f94daf4aec590e8f23ace2de6ace Mon Sep 17 00:00:00 2001 From: Peter Fajdiga Date: Sat, 20 Jan 2024 17:53:04 +0100 Subject: [PATCH] Actions: extract function `findNextStep` --- src/Actions.ts | 53 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/Actions.ts b/src/Actions.ts index b233b8d..8734797 100644 --- a/src/Actions.ts +++ b/src/Actions.ts @@ -182,18 +182,19 @@ namespace Actions { const leftSpace = leftVisibleColumn.getLeft() - visibleRange.getLeft(); const rightSpace = visibleRange.getRight() - rightVisibleColumn.getRight(); - const widthSteps = [ - visibleRange.getWidth(), - column.getWidth() + config.manualResizeStep, - column.getWidth() + leftSpace + rightSpace, - ].sort((a, b) => a - b); - - const nextWidthStep = widthSteps.find(width => width > column.getWidth()); - if (nextWidthStep === undefined) { + const newWidth = findNextStep( + [ + visibleRange.getWidth(), + column.getWidth() + config.manualResizeStep, + column.getWidth() + leftSpace + rightSpace, + ], + width => width - column.getWidth(), + ) + if (newWidth === undefined) { return; } - column.setWidth(nextWidthStep, true); + column.setWidth(newWidth, true); desktop.scrollCenterVisible(column); desktop.onLayoutChanged(); desktop.autoAdjustScroll(); @@ -229,19 +230,20 @@ namespace Actions { const leftOffScreen = leftOffScreenColumn === null ? 0 : leftOffScreenColumn.getWidth() + grid.config.gapsInnerHorizontal - unusedWidth; const rightOffScreen = rightOffScreenColumn === null ? 0 : rightOffScreenColumn.getWidth() + grid.config.gapsInnerHorizontal - unusedWidth; - const widthSteps = [ - visibleRange.getWidth(), - column.getWidth() - config.manualResizeStep, - column.getWidth() - leftOffScreen, - column.getWidth() - rightOffScreen, - ].sort((a, b) => b - a); - - const nextWidthStep = widthSteps.find(width => width < column.getWidth()); - if (nextWidthStep === undefined) { + const newWidth = findNextStep( + [ + visibleRange.getWidth(), + column.getWidth() - config.manualResizeStep, + column.getWidth() - leftOffScreen, + column.getWidth() - rightOffScreen, + ], + width => column.getWidth() - width, + ) + if (newWidth === undefined) { return; } - column.setWidth(nextWidthStep, true); + column.setWidth(newWidth, true); desktop.scrollCenterVisible(column); desktop.onLayoutChanged(); desktop.autoAdjustScroll(); @@ -395,6 +397,19 @@ namespace Actions { }); } + function findNextStep(steps: number[], evaluate: (step: number) => number) { + let bestScore = Infinity; + let bestStep = undefined; + for (const step of steps) { + const score = evaluate(step); + if (score > 0 && score < bestScore) { + bestScore = score; + bestStep = step; + } + } + return bestStep; + } + export type Config = { manualScrollStep: number, manualResizeStep: number,