From 61db5ca69fe045d678c73d01ba150f322b421d61 Mon Sep 17 00:00:00 2001 From: Peter Fajdiga Date: Sat, 16 Dec 2023 09:31:03 +0100 Subject: [PATCH] use different implementations of `clampScrollX` in different scrollers --- src/layout/Desktop.ts | 8 ++------ src/layout/ScrollerCentered.ts | 16 ++++++++++++++++ src/layout/ScrollerGrouped.ts | 4 ++++ src/layout/ScrollerLazy.ts | 9 +++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/layout/Desktop.ts b/src/layout/Desktop.ts index ce0f181..7f1b3ce 100644 --- a/src/layout/Desktop.ts +++ b/src/layout/Desktop.ts @@ -119,12 +119,7 @@ class Desktop { } private clampScrollX(x: number) { - let minScroll = 0; - let maxScroll = this.grid.getWidth() - this.tilingArea.width; - if (maxScroll < 0) { - return Math.round(maxScroll / 2); - } - return clamp(x, minScroll, maxScroll); + return this.config.scroller.clampScrollX(this, x); } public setScroll(x: number, force: boolean) { @@ -233,5 +228,6 @@ namespace Desktop { export type Scroller = { scrollToColumn(desktop: Desktop, column: Column): void; + clampScrollX(desktop: Desktop, x: number): number; } } diff --git a/src/layout/ScrollerCentered.ts b/src/layout/ScrollerCentered.ts index 3be76b2..7e86209 100644 --- a/src/layout/ScrollerCentered.ts +++ b/src/layout/ScrollerCentered.ts @@ -2,4 +2,20 @@ class ScrollerCentered { public scrollToColumn(desktop: Desktop, column: Column) { desktop.scrollCenterRange(column); } + + public clampScrollX(desktop: Desktop, x: number) { + return ScrollerCentered.clampScrollX(desktop, x); + } + + public static clampScrollX(desktop: Desktop, x: number) { + const firstColumn = desktop.grid.getFirstColumn(); + if (firstColumn === null) { + return 0; + } + const lastColumn = desktop.grid.getLastColumn()!; + + let minScroll = Math.round((firstColumn.getWidth() - desktop.tilingArea.width) / 2); + let maxScroll = Math.round(desktop.grid.getWidth() - (desktop.tilingArea.width + lastColumn.getWidth()) / 2); + return clamp(x, minScroll, maxScroll); + } } diff --git a/src/layout/ScrollerGrouped.ts b/src/layout/ScrollerGrouped.ts index af66383..ad876cc 100644 --- a/src/layout/ScrollerGrouped.ts +++ b/src/layout/ScrollerGrouped.ts @@ -12,6 +12,10 @@ class ScrollerGrouped { columnRange.addNeighbors(visibleRange, this.layoutConfig.gapsInnerHorizontal, false); desktop.scrollCenterRange(columnRange); } + + public clampScrollX(desktop: Desktop, x: number) { + return ScrollerCentered.clampScrollX(desktop, x); + } } namespace ScrollerGrouped { diff --git a/src/layout/ScrollerLazy.ts b/src/layout/ScrollerLazy.ts index bc511e0..76fead4 100644 --- a/src/layout/ScrollerLazy.ts +++ b/src/layout/ScrollerLazy.ts @@ -2,4 +2,13 @@ class ScrollerLazy { public scrollToColumn(desktop: Desktop, column: Column) { desktop.scrollToRange(column); } + + public clampScrollX(desktop: Desktop, x: number) { + let minScroll = 0; + let maxScroll = desktop.grid.getWidth() - desktop.tilingArea.width; + if (maxScroll < 0) { + return Math.round(maxScroll / 2); + } + return clamp(x, minScroll, maxScroll); + } }