src/vs/base/common/layout.ts
166 LOC · 103 covered · 63 uncovered · 3 ranges · 1 concepts · 1 introducers · 1 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
range.ts ×5
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Range } from './range.js';
export interface IAnchor {
x: number;
y: number;
width?: number;
height?: number;
}
export const enum AnchorAlignment {
LEFT, RIGHT
}
export const enum AnchorPosition {
BELOW, ABOVE
}
export const enum AnchorAxisAlignment {
VERTICAL, HORIZONTAL
}
interface IPosition {
readonly top: number;
readonly left: number;
}
interface ISize {
readonly width: number;
readonly height: number;
}
export interface IRect extends IPosition, ISize { }
export const enum LayoutAnchorPosition {
Before,
After
}
export enum LayoutAnchorMode {
AVOID,
ALIGN
}
export interface ILayoutAnchor {
offset: number;
size: number;
mode?: LayoutAnchorMode; // default: AVOID
position: LayoutAnchorPosition;
}
export interface ILayoutResult {
position: number;
result: 'ok' | 'flipped' | 'overlap';
}
/**
* Lays out a one dimensional view next to an anchor in a viewport.
*
* @returns The view offset within the viewport.
*/
export function layout(viewportSize: number, viewSize: number, anchor: ILayoutAnchor): ILayoutResult {
const layoutAfterAnchorBoundary = anchor.mode === LayoutAnchorMode.ALIGN ? anchor.offset : anchor.offset + anchor.size;
const layoutBeforeAnchorBoundary = anchor.mode === LayoutAnchorMode.ALIGN ? anchor.offset + anchor.size : anchor.offset;
if (anchor.position === LayoutAnchorPosition.Before) {
if (viewSize <= viewportSize - layoutAfterAnchorBoundary) {
return { position: layoutAfterAnchorBoundary, result: 'ok' }; // happy case, lay it out after the anchor
}
if (viewSize <= layoutBeforeAnchorBoundary) {
return { position: layoutBeforeAnchorBoundary - viewSize, result: 'flipped' }; // ok case, lay it out before the anchor
}
return { position: Math.max(viewportSize - viewSize, 0), result: 'overlap' }; // sad case, lay it over the anchor
if (viewSize <= layoutBeforeAnchorBoundary) {
return { position: layoutBeforeAnchorBoundary - viewSize, result: 'ok' }; // happy case, lay it out before the anchor
}
if (viewSize <= viewportSize - layoutAfterAnchorBoundary && layoutBeforeAnchorBoundary < viewSize / 2) {
return { position: layoutAfterAnchorBoundary, result: 'flipped' }; // ok case, lay it out after the anchor
}
return { position: 0, result: 'overlap' }; // sad case, lay it over the anchor
}
interface ILayout2DOptions {
readonly anchorAlignment?: AnchorAlignment; // default: left
readonly anchorPosition?: AnchorPosition; // default: above
readonly anchorAxisAlignment?: AnchorAxisAlignment; // default: vertical
}
export interface ILayout2DResult {
top: number;
left: number;
bottom: number;
right: number;
anchorAlignment: AnchorAlignment;
anchorPosition: AnchorPosition;
}
export function layout2d(viewport: IRect, view: ISize, anchor: IRect, options?: ILayout2DOptions): ILayout2DResult {
let anchorAlignment = options?.anchorAlignment ?? AnchorAlignment.LEFT;
let anchorPosition = options?.anchorPosition ?? AnchorPosition.BELOW;
const anchorAxisAlignment = options?.anchorAxisAlignment ?? AnchorAxisAlignment.VERTICAL;
let top: number;
let left: number;
if (anchorAxisAlignment === AnchorAxisAlignment.VERTICAL) {
const verticalAnchor: ILayoutAnchor = { offset: anchor.top - viewport.top, size: anchor.height, position: anchorPosition === AnchorPosition.BELOW ? LayoutAnchorPosition.Before : LayoutAnchorPosition.After };
const horizontalAnchor: ILayoutAnchor = { offset: anchor.left, size: anchor.width, position: anchorAlignment === AnchorAlignment.LEFT ? LayoutAnchorPosition.Before : LayoutAnchorPosition.After, mode: LayoutAnchorMode.ALIGN };
const verticalLayoutResult = layout(viewport.height, view.height, verticalAnchor);
top = verticalLayoutResult.position + viewport.top;
if (verticalLayoutResult.result === 'flipped') {
anchorPosition = anchorPosition === AnchorPosition.BELOW ? AnchorPosition.ABOVE : AnchorPosition.BELOW;
}
// if view intersects vertically with anchor, we must avoid the anchor
if (Range.intersects({ start: top, end: top + view.height }, { start: verticalAnchor.offset, end: verticalAnchor.offset + verticalAnchor.size })) {
horizontalAnchor.mode = LayoutAnchorMode.AVOID;
}
const horizontalLayoutResult = layout(viewport.width, view.width, horizontalAnchor);
left = horizontalLayoutResult.position;
if (horizontalLayoutResult.result === 'flipped') {
anchorAlignment = anchorAlignment === AnchorAlignment.LEFT ? AnchorAlignment.RIGHT : AnchorAlignment.LEFT;
}
} else {
const horizontalAnchor: ILayoutAnchor = { offset: anchor.left, size: anchor.width, position: anchorAlignment === AnchorAlignment.LEFT ? LayoutAnchorPosition.Before : LayoutAnchorPosition.After };
const verticalAnchor: ILayoutAnchor = { offset: anchor.top, size: anchor.height, position: anchorPosition === AnchorPosition.BELOW ? LayoutAnchorPosition.Before : LayoutAnchorPosition.After, mode: LayoutAnchorMode.ALIGN };
const horizontalLayoutResult = layout(viewport.width, view.width, horizontalAnchor);
left = horizontalLayoutResult.position;
if (horizontalLayoutResult.result === 'flipped') {
anchorAlignment = anchorAlignment === AnchorAlignment.LEFT ? AnchorAlignment.RIGHT : AnchorAlignment.LEFT;
}
// if view intersects horizontally with anchor, we must avoid the anchor
if (Range.intersects({ start: left, end: left + view.width }, { start: horizontalAnchor.offset, end: horizontalAnchor.offset + horizontalAnchor.size })) {
verticalAnchor.mode = LayoutAnchorMode.AVOID;
}
const verticalLayoutResult = layout(viewport.height, view.height, verticalAnchor);
top = verticalLayoutResult.position + viewport.top;
if (verticalLayoutResult.result === 'flipped') {
anchorPosition = anchorPosition === AnchorPosition.BELOW ? AnchorPosition.ABOVE : AnchorPosition.BELOW;
}
}
const right = viewport.width - (left + view.width);
const bottom = viewport.height - (top + view.height);
return { top, left, bottom, right, anchorAlignment, anchorPosition };
}