extract Range into Range.ts

This commit is contained in:
Peter Fajdiga
2024-10-27 23:14:13 +01:00
parent 7820c7d00e
commit 55e1037a7b
5 changed files with 55 additions and 54 deletions

View File

@@ -202,7 +202,7 @@ class Actions {
);
visibleColumns.forEach((column, index) => column.setWidth(widths[index], true));
desktop.scrollCenterRange(Desktop.RangeImpl.fromRanges(
desktop.scrollCenterRange(Range.Basic.fromRanges(
visibleColumns[0],
visibleColumns[visibleColumns.length - 1],
));
@@ -279,7 +279,7 @@ class Actions {
const widths = fillSpace(availableSpace - gapsWidth, columnConstraints);
columns.forEach((column, index) => column.setWidth(widths[index], true));
desktop.scrollCenterRange(Desktop.RangeImpl.fromRanges(firstColumn, lastColumn));
desktop.scrollCenterRange(Range.Basic.fromRanges(firstColumn, lastColumn));
return true;
}

View File

@@ -203,7 +203,7 @@ class Column {
window.focus();
}
public arrange(x: number, visibleRange: Desktop.SuperRange, forceOpaque: boolean) {
public arrange(x: number, visibleRange: SuperRange, forceOpaque: boolean) {
if (this.grid.config.offScreenOpacity < 1.0 && !forceOpaque) {
const opacity = visibleRange.contains(this) ? 100 : this.grid.config.offScreenOpacity;
for (const window of this.windows.iterator()) {

View File

@@ -55,7 +55,7 @@ class Desktop {
)
}
public scrollIntoView(range: Desktop.Range) {
public scrollIntoView(range: Range) {
const left = range.getLeft();
const right = range.getRight();
const initialVisibleRange = this.getCurrentVisibleRange();
@@ -72,7 +72,7 @@ class Desktop {
this.setScroll(targetScrollX, false);
}
public scrollCenterRange(range: Desktop.Range) {
public scrollCenterRange(range: Range) {
const windowCenter = range.getLeft() + range.getWidth() / 2;
const screenCenter = this.scrollX + this.tilingArea.width / 2;
this.adjustScroll(Math.round(windowCenter - screenCenter), true);
@@ -101,7 +101,7 @@ class Desktop {
}
private getVisibleRange(scrollX: number) {
return new Desktop.RangeImpl(scrollX, this.tilingArea.width);
return new Range.Basic(scrollX, this.tilingArea.width);
}
public getCurrentVisibleRange() {
@@ -161,49 +161,6 @@ namespace Desktop {
clamper: Desktop.Clamper;
};
export type Range = {
getLeft(): number;
getRight(): number;
getWidth(): number;
};
export type SuperRange = Range & {
contains(child: Range): boolean;
};
export class RangeImpl {
private readonly x: number;
private readonly width: number;
constructor(x: number, width: number) {
this.x = x;
this.width = width;
}
public getLeft() {
return this.x;
}
public getRight() {
return this.x + this.width;
}
public getWidth() {
return this.width;
}
public contains(child: Range) {
return child.getLeft() >= this.getLeft() &&
child.getRight() <= this.getRight();
}
public static fromRanges(leftRange: Range, rightRange: Range) {
const left = leftRange.getLeft();
const right = rightRange.getRight();
return new RangeImpl(left, right - left);
}
}
export class ColumnRange {
private left: Column;
private right: Column;
@@ -215,7 +172,7 @@ namespace Desktop {
this.width = initialColumn.getWidth();
}
public addNeighbors(visibleRange: Desktop.Range, gap: number) {
public addNeighbors(visibleRange: Range, gap: number) {
const grid = this.left.grid;
const columnRange = this;

View File

@@ -104,7 +104,7 @@ class Grid {
this.width = x - this.config.gapsInnerHorizontal;
}
public getLeftmostVisibleColumn(visibleRange: Desktop.SuperRange, fullyVisible: boolean) {
public getLeftmostVisibleColumn(visibleRange: SuperRange, fullyVisible: boolean) {
for (const column of this.columns.iterator()) {
if (visibleRange.contains(column)) {
return column;
@@ -113,7 +113,7 @@ class Grid {
return null;
}
public getRightmostVisibleColumn(visibleRange: Desktop.SuperRange, fullyVisible: boolean) {
public getRightmostVisibleColumn(visibleRange: SuperRange, fullyVisible: boolean) {
let last = null;
for (const column of this.columns.iterator()) {
if (visibleRange.contains(column)) {
@@ -125,7 +125,7 @@ class Grid {
return last;
}
public *getVisibleColumns(visibleRange: Desktop.SuperRange, fullyVisible: boolean) {
public *getVisibleColumns(visibleRange: SuperRange, fullyVisible: boolean) {
for (const column of this.columns.iterator()) {
if (visibleRange.contains(column)) {
yield column;
@@ -133,7 +133,7 @@ class Grid {
}
}
public arrange(x: number, visibleRange: Desktop.SuperRange) {
public arrange(x: number, visibleRange: SuperRange) {
for (const column of this.columns.iterator()) {
column.arrange(x, visibleRange, this.userResize);
x += column.getWidth() + this.config.gapsInnerHorizontal;

44
src/lib/layout/Range.ts Normal file
View File

@@ -0,0 +1,44 @@
type Range = {
getLeft(): number;
getRight(): number;
getWidth(): number;
};
type SuperRange = Range & {
contains(child: Range): boolean;
};
namespace Range {
export class Basic {
private readonly x: number;
private readonly width: number;
constructor(x: number, width: number) {
this.x = x;
this.width = width;
}
public getLeft() {
return this.x;
}
public getRight() {
return this.x + this.width;
}
public getWidth() {
return this.width;
}
public contains(child: Range) {
return child.getLeft() >= this.getLeft() &&
child.getRight() <= this.getRight();
}
public static fromRanges(leftRange: Range, rightRange: Range) {
const left = leftRange.getLeft();
const right = rightRange.getRight();
return new Basic(left, right - left);
}
}
}